[kubernetes] Persistent Volume

wordpress mysql Persistent Volume 배포

kubernetes 에서 Persistent Volume 을 이용하여 wordpress , mysql 을 Persistent Volume 에 배포 합니다.

참고페이지: https://cloud.google.com/kubernetes-engine/docs/tutorials/persistent-disk?hl=ko

https://kubernetes.io/ko/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/#mysql과-wordpress에-필요한-리소스-구성-추가하기

 

 

1. kubernetes Service 확인

[root@k8s-master ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-node-fcbq8 2/2 Running 2 81d
kube-system calico-node-kzqlv 2/2 Running 2 81d
kube-system calico-node-r8ggc 2/2 Running 2 81d
kube-system coredns-fb8b8dccf-4zq8l 1/1 Running 1 81d
kube-system coredns-fb8b8dccf-fg9l7 1/1 Running 1 81d
kube-system etcd-k8s-master 1/1 Running 1 81d
kube-system kube-apiserver-k8s-master 1/1 Running 1 81d
kube-system kube-controller-manager-k8s-master 1/1 Running 1 81d
kube-system kube-proxy-ph9np 1/1 Running 1 81d
kube-system kube-proxy-x28cx 1/1 Running 1 81d
kube-system kube-proxy-z252g 1/1 Running 1 81d
kube-system kube-scheduler-k8s-master 1/1 Running 1 81d
kube-system kubernetes-dashboard-5f7b999d65-qmjfx 1/1 Running 1 81d
[root@k8s-master ~]#

 

 

2. Persistent Volume 생성

wordpress 와 mysql 에서 사용할 Persistent Volume 을 지정 합니다.

[root@k8s-master wordpress]# vi web01.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: web01
  labels:
    type: local
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:

    path: "/data/web01"



[root@k8s-master wordpress]# vi db01.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: db01
  labels:
    type: local
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/db01"

[root@k8s-master wordpress]# kubectl create -f web01.yaml
persistentvolume/web01 created
[root@k8s-master wordpress]# kubectl create -f db01.yaml
persistentvolume/db01 created
[root@k8s-master wordpress]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
db01    5Gi        RWO            Retain           Available                                   3s
web01   5Gi        RWO            Retain           Available                                   8s
[root@k8s-master wordpress]#

 

 

3. Persistent Volume Claim 생성

[root@k8s-master wordpress]# vi wordpress-vol.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: wordpress-volumeclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:

      storage: 5Gi
[root@k8s-master wordpress]# vi mysql-vol.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-volumeclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:

      storage: 5Gi

[root@k8s-master wordpress]# kubectl create -f wordpress-vol.yaml
persistentvolumeclaim/wordpress-volumeclaim created
[root@k8s-master wordpress]# kubectl create -f mysql-vol.yaml
persistentvolumeclaim/mysql-volumeclaim created
[root@k8s-master wordpress]# kubectl get pvc
NAME                    STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-volumeclaim       Bound    db01     5Gi        RWO                           4s
wordpress-volumeclaim   Bound    web01    5Gi        RWO                           11s
[root@k8s-master wordpress]#

 

 

 

 

4. mysql root password 생성

secret 삭제시 kubectl delete secret mysql-password

[root@k8s-master wordpress]# kubectl create secret generic mysql-password --from-literal=password=mysqlpassword
secret/mysql-password created
[root@k8s-master wordpress]# kubectl describe secret mysql-password
Name:         mysql-password
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  13 bytes
[root@k8s-master wordpress]#

 

 

5. mysql pod 생성

wordpress 에서 사용할 DB 와 DB 사용자 / Password 를 지정 합니다.

[root@k8s-master wordpress]# vi mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - image: mysql:5.6
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-password
                  key: password
            - name: MYSQL_DATABASE 
              value: wordpress         # WP 에서 사용할 DB명
            - name: MYSQL_USER
              value: wordpress         # WP 에서 사용할 USER명
            - name: MYSQL_ROOT_HOST
              value: '%'
            - name: MYSQL_PASSWORD 
              value: wordpress         # WP 데이터베이스 Password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-volumeclaim


[root@k8s-master wordpress]# kubectl create -f mysql.yaml
deployment.apps/mysql created
[root@k8s-master wordpress]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
mysql-6845698854-46pmx   1/1     Running   0          4s    20.20.2.10   k8s-node02   <none>           <none>
[root@k8s-master wordpress]#

 

 

 

6. mysq-service 생성

[root@k8s-master wordpress]# vi mysql-service.yaml
apiVersion: v1

kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  type: ClusterIP
  ports:
    - port: 3306
  selector:
    app: mysql
[root@k8s-master wordpress]# kubectl create -f mysql-service.yaml
service/mysql created
[root@k8s-master wordpress]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    10d
mysql        ClusterIP   10.109.232.234   <none>        3306/TCP   3s
[root@k8s-master wordpress]#

 

 

7. wordpress pod 생성

[root@k8s-master wordpress]# vi wordpress.yaml
apiVersion: apps/v1

kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
        - image: wordpress
          name: wordpress
          env:
          - name: WORDPRESS_DB_HOST
            value: mysql:3306
          - name: WORDPRESS_DB_NAME
            value: wordpress
          - name: WORDPRESS_DB_USER
            value: wordpress
          - name: WORDPRESS_DB_PASSWORD
            value: wordpress
          ports:
            - containerPort: 80
              name: wordpress
          volumeMounts:
            - name: wordpress-persistent-storage
              mountPath: /var/www/html
      volumes:
        - name: wordpress-persistent-storage
          persistentVolumeClaim:
            claimName: wordpress-volumeclaim


[root@k8s-master wordpress]# kubectl create -f wordpress.yaml
deployment.apps/wordpress created
[root@k8s-master wordpress]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
NAME                         READY   STATUS    RESTARTS   AGE
mysql-6845698854-46pmx       1/1     Running   0          2m12s
wordpress-74747f4dbf-fbbnh   1/1     Running   0          12s

[root@k8s-master wordpress]#

 

 

8. wordpress-service 생성

[root@k8s-master wordpress]# vi wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: wordpress
[root@k8s-master wordpress]# kubectl create -f wordpress-service.yaml
service/wordpress created
[root@k8s-master wordpress]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        10d     <none>
mysql        ClusterIP   10.111.162.117   <none>        3306/TCP       5m56s   app=mysql
wordpress    NodePort    10.107.48.226    <none>        80:30950/TCP   5m24s   app=wordpress
[root@k8s-master wordpress]#

 

 

 

k8s-master:30950 로 접속 하여 확인 가능 합니다.

k9s-master 는 192.168.0.10입니다.

 

 

댓글 남기기