개발강의정리/DevOps

[데브옵스를 위한 쿠버네티스 마스터] 쿠버네티스 환경에서 예제를 활용한 애플리케이션 개발

nineDeveloper 2021. 1. 22.
728x90

쿠버네티스 환경에서 WordPress + Mysql 프로젝트 개발


Kubernetes Examples Git

https://github.com/kubernetes/examples

https://github.com/kubernetes/examples/tree/master/mysql-wordpress-pd


시스템 구축 방법을 YAML로 정리하고 배포하시오


Wordpress MySql 쿠버네티스 배포 실습

GCP 에서 실습

kustomization.yaml 생성하기

https://kubernetes.io/ko/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/#kustomization-yaml-%EC%83%9D%EC%84%B1%ED%95%98%EA%B8%B0

~/yaml/wordpress-project 경로에서
다음 명령어로 kustomization.yaml 내에 시크릿 제네레이터를 추가
YOUR_PASSWORD는 사용하기 원하는 암호로 변경

cat <<EOF >./kustomization.yaml
secretGenerator:
- name: mysql-pass
  literals:
  - password=YOUR_PASSWORD
EOF

MySQL과 WordPress에 필요한 리소스 구성 추가하기

https://kubernetes.io/ko/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/#mysql%EA%B3%BC-wordpress%EC%97%90-%ED%95%84%EC%9A%94%ED%95%9C-%EB%A6%AC%EC%86%8C%EC%8A%A4-%EA%B5%AC%EC%84%B1-%EC%B6%94%EA%B0%80%ED%95%98%EA%B8%B0

mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

MySQL 디플로이먼트 구성 파일을 다운로드

$ curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml

wordpress-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

WordPress 구성 파일을 다운로드

$ curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml

두 파일을 kustomization.yaml에 추가

cat <<EOF >>./kustomization.yaml
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
EOF

적용하고 확인하기

https://kubernetes.io/ko/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/#%EC%A0%81%EC%9A%A9%ED%95%98%EA%B3%A0-%ED%99%95%EC%9D%B8%ED%95%98%EA%B8%B0

kubectl apply -k ./
secret/mysql-pass-dkg6cf67fb created
service/wordpress-mysql created
service/wordpress created
deployment.apps/wordpress-mysql created
deployment.apps/wordpress created
persistentvolumeclaim/mysql-pv-claim created
persistentvolumeclaim/wp-pv-claim created

$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-7f7cb4d86d-2jn8l        1/1     Running   0          8m42s
wordpress-mysql-cbff5766d-flcbk   1/1     Running   0          8m42s

서비스 및 수신으로 이동해서 wordpress 엔드포인트로 접속

wordpress scale 증가 후 확인

$ kubectl scale deployment wordpress --replicas=3
deployment.apps/wordpress scaled

$ kubectl get pod
NAME                              READY   STATUS              RESTARTS   AGE
wordpress-7f7cb4d86d-2jn8l        1/1     Running             0          11m
wordpress-7f7cb4d86d-9qmhb        0/1     ContainerCreating   0          50s
wordpress-7f7cb4d86d-hsnms        0/1     ContainerCreating   0          50s
wordpress-mysql-cbff5766d-flcbk   1/1     Running             0          11m

$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP          NODE                                       NOMINATED NODE   READINESS GATES
wordpress-7f7cb4d86d-2jn8l        1/1     Running   0          22m   10.32.2.3   gke-cluster-1-default-pool-84a8d654-m1zt   <none>           <none>
wordpress-mysql-cbff5766d-flcbk   1/1     Running   0          22m   10.32.0.6   gke-cluster-1-default-pool-84a8d654-4nwl   <none>           <none>

현재 readWriteMany 를 GCP 에서 지원하지 않아서 더이상 스케일이 안됨


워드프레스와 MySQL 스케일링 가능하도록 수정

초기화

$ kubectl delete all --all
pod "wordpress-7f7cb4d86d-2jn8l" deleted
pod "wordpress-mysql-cbff5766d-flcbk" deleted
service "kubernetes" deleted
service "wordpress" deleted
service "wordpress-mysql" deleted
deployment.apps "wordpress" deleted
deployment.apps "wordpress-mysql" deleted
replicaset.apps "wordpress-7f7cb4d86d" deleted

$ kubectl delete pvc mysql-pv-claim
persistentvolumeclaim "mysql-pv-claim" deleted

$ kubectl delete pvc wp-pv-claim
persistentvolumeclaim "wp-pv-claim" deleted

https://kubernetes.io/ko/docs/concepts/scheduling-eviction/assign-pod-node/

mysql-deployment.yaml 파일 수정

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  #strategy: # Recreate 전략 제거
  #  type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      nodeSelector: # nodeSelect 추가
        app: mysql # app 이 mysql 일때
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

wordpress-deployment.yaml 파일 수정

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress
  namespace: wordpress # wordpress 네임스페이스 추가
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  #strategy: # Recreate 전략 제거
  #  type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      nodeSelector: # nodeSelect 추가
        app: wordpress # app 이 wordpress 일때
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

kustomization.yaml 파일 수정

secretGenerator:
- name: mysql-pass
  namespace: wordpress # wordpress 네임스페이스 추가
  literals:
  - password=1879
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml

wordpress 네임스패이스 생성

$ kubectl create ns wordpress
namespace/wordpress created

어떤 노드에 workpress 와 mysql 을 각각 배치할지 확인 후 설정

$ kubectl get node
NAME                                       STATUS   ROLES    AGE   VERSION
gke-cluster-1-default-pool-84a8d654-4nwl   Ready    <none>   76m   v1.16.15-gke.6000
gke-cluster-1-default-pool-84a8d654-j276   Ready    <none>   76m   v1.16.15-gke.6000
gke-cluster-1-default-pool-84a8d654-m1zt   Ready    <none>   76m   v1.16.15-gke.6000

$ kubectl label node gke-cluster-1-default-pool-84a8d654-4nwl app=wordpress
node/gke-cluster-1-default-pool-84a8d654-4nwl labeled

$ kubectl label node gke-cluster-1-default-pool-84a8d654-j276 app=mysql
node/gke-cluster-1-default-pool-84a8d654-j276 labeled

수정된 내용 적용 및 확인

$ kubectl apply -k ./
secret/mysql-pass-dkg6cf67fb created
service/wordpress-mysql created
service/wordpress created
deployment.apps/wordpress-mysql created
deployment.apps/wordpress created
persistentvolumeclaim/mysql-pv-claim created
persistentvolumeclaim/wp-pv-claim created

$ kubectl get pod -n wordpress
NAME                               READY   STATUS    RESTARTS   AGE
wordpress-586d9d9765-mh5mk         1/1     Running   0          40s
wordpress-mysql-7544ff6996-tj7hx   1/1     Running   0          40s

서비스 및 수신으로 이동해서 wordpress 엔드포인트로 접속

mysql 과 wordpress scale 증가 후 확인

$ kubectl scale -n wordpress deployment wordpress --replicas=3
deployment.apps/wordpress scaled

$ kubectl scale -n wordpress deployment wordpress-mysql --replicas=3
deployment.apps/wordpress-mysql scaled

scale 증가가 잘되는 것을 확인

$ kubectl get pod -w -n wordpress
NAME                               READY   STATUS    RESTARTS   AGE
wordpress-586d9d9765-j7ndg         1/1     Running   0          49s
wordpress-586d9d9765-mh5mk         1/1     Running   0          7m2s
wordpress-586d9d9765-mkkrr         1/1     Running   0          49s
wordpress-mysql-7544ff6996-s44bs   1/1     Running   0          31s
wordpress-mysql-7544ff6996-tj7hx   1/1     Running   0          7m2s
wordpress-mysql-7544ff6996-x2pzs   1/1     Running   0          31s

하나의 pd 에 node 들이 연결되도록 하는 것이 GCP의 규칙이라서 이렇게 설정해야됨
다른 노드랑 연결하는 것에는 제한이 있음


워드프레스에 자동스케일링 기능 추가 실습

초기화

$ kubectl delete all --all -n wordpress
pod "wordpress-586d9d9765-j7ndg" deleted
pod "wordpress-586d9d9765-mh5mk" deleted
pod "wordpress-586d9d9765-mkkrr" deleted
pod "wordpress-mysql-7544ff6996-s44bs" deleted
pod "wordpress-mysql-7544ff6996-tj7hx" deleted
pod "wordpress-mysql-7544ff6996-x2pzs" deleted
service "wordpress" deleted
service "wordpress-mysql" deleted
deployment.apps "wordpress" deleted
deployment.apps "wordpress-mysql" deleted
replicaset.apps "wordpress-586d9d9765" deleted
replicaset.apps "wordpress-mysql-7544ff6996" deleted

$ kubectl delete pvc -n wordpress wp-pv-claim
persistentvolumeclaim "wp-pv-claim" deleted

$ kubectl delete pvc -n wordpress mysql-pv-claim
persistentvolumeclaim "mysql-pv-claim" deleted

리소스 요청 및 제한 설정 추가

https://kubernetes.io/ko/docs/concepts/configuration/manage-resources-containers/#%EB%A9%94%EB%AA%A8%EB%A6%AC%EC%9D%98-%EC%9D%98%EB%AF%B8

리소스 요청 및 제한 설정을 해주지 않으면 HPA에서 기준이 없으므로 성능을 체크할 수 없음

mysql-deployment.yaml 파일에 리소스 요청 및 제한 설정 추가

...
    spec:
      nodeSelector: # nodeSelect 추가
        app: mysql # app 이 mysql 일때
      containers:
      - resources:
          requests:
            memory: "256Mi"
            cpu: "50m"
          limits:
            memory: "512Mi"
            cpu: "100m"

wordpress-deployment.yaml 파일에 리소스 요청 및 제한 설정 추가

...
    spec:
      nodeSelector: # nodeSelect 추가
        app: wordpress # app 이 wordpress 일때
      containers:
      - resources:
          requests:
            memory: "500Mi"
            cpu: "100m"
          limits:
            memory: "1024Mi"
            cpu: "300m"

수정된 내용 적용 및 확인

$ kubectl apply -k ./
secret/mysql-pass-dkg6cf67fb unchanged
service/wordpress-mysql created
service/wordpress created
deployment.apps/wordpress-mysql created
deployment.apps/wordpress created
persistentvolumeclaim/mysql-pv-claim created
persistentvolumeclaim/wp-pv-claim created

$ kubectl get pod -w -n wordpress
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-6dcc9d8cfc-tcjbt        1/1     Running   0          29s
wordpress-mysql-98b8d4b78-7fhkr   1/1     Running   0          29s

HPA 생성 및 적용

https://kubernetes.io/ko/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#horizontal-pod-autoscaler-%EC%83%9D%EC%84%B1

mysql 과 wordpress hpa 생성 및 적용

$ kubectl autoscale deployment wordpress --cpu-percent=50 --min=1 --max=10 -n wordpress
horizontalpodautoscaler.autoscaling/wordpress autoscaled

$ kubectl autoscale deployment wordpress-mysql --cpu-percent=50 --min=1 --max=10 -n wordpress
horizontalpodautoscaler.autoscaling/wordpress-mysql autoscaled

성능에 따른 변화 관찰
성능이 부족하다고 판단되면 오토스케일링을 수행함

$ kubectl get hpa -w -n wordpress
NAME              REFERENCE                    TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
wordpress         Deployment/wordpress         1%/50%    1         10        1          29s
wordpress-mysql   Deployment/wordpress-mysql   2%/50%    1         10        1          18s

일부러 부하를 줘서 성능의 변화를 관찰

$ while true; curl 127.0.0.1; done;
728x90

댓글

💲 추천 글