### 전반적인 FLOW
1. Persistant Volume 생성 필요 → PersistantVolume, PersistantVolumeClaim
2. DB User, Password와 같은 정보 Secret 관리 → Secret
3. MariaDB 서비스 노출 하기 → Service, Deployment
(해보시다가 안되시면 댓글남겨주세요!)
1. Persistent Volume 생성
vi mariaDB-pv.yaml
--- apiVersion: v1 kind: PersistentVolume metadata: name: mariadb-pv-volume labels: type: local spec: storageClassName: mariadb-storage-class capacity: storage: 20Gi # Volume of Storage accessModes: - ReadWriteOnce # Can access One pod, ReadWriteMany : Can access Multi pod hostPath: path: "/var/lib/mysql" # Directory to save |
kubectl create -f mariaDB-pv.yaml
kubectl describe pv mariadb-pv-volume
2. Persistent Volume Claim 생성
vi mariaDB-pvc.yaml
--- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mariadb-pv-claim spec: storageClassName: mariadb-storage-class accessModes: - ReadWriteOnce resources: requests: storage: 20Gi selector: matchLabels: name: mariadb-pv-volume |
kubectl create -f mariaDB-pvc.yaml
kubectl describe pvc mariadb-pv-claim
3. DB 정보 k8s Secret 생성
echo -n 'MariaDbPwd' | base64
TWFyaWFEYlB3ZA==
vi mariaDB-secret.yaml
--- apiVersion: v1 kind: Secret metadata: name: mariadb-secret data: password: TWFyaWFEYlB3ZA== |
kubectl create -f mariaDB-secret.yaml
kubectl get secret mariadb-secret
kubectl describe secret mariadb-secret
4. MariaDB k8s Service 생성
vi mariaDB-svc.yaml
--- apiVersion: v1 kind: Service metadata: name: mariadb spec: ports: - port: 2230 selector: app: mariadb |
kubectl create -f mariaDB-svc.yaml
5. MariaDB k8s Deployment 생성
vi mariaDB-deployment.yaml
--- apiVersion: apps/v1 kind: Deployment metadata: name: mariadb spec: selector: matchLabels: app: mariadb strategy: type: Recreate template: metadata: labels: app: mariadb spec: containers: - image: mariadb:latest imagePullPolicy: “Always” name: mariadb ports: - containerPort: 2230 # Container port name: mariadb env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mariadb-secret # Secret name key: password # Secret data key:value volumeMounts: - name: mariadb-data mountPath: /var/lib/mysql volumes: - name: mariadb-data persistentVolumeClaim: claimName: mariadb-pv-claim |
kubectl create -f mariaDB-deployment.yaml
kubectl describe deployment mariadb
kubectl get pods -A -o wide -l app=mariadb
kubectl describe pvc mariadb-pv-claim
6. MariaDB 접속하기
kubectl exec -it mariadb-${pod hash 값} -- bash
# mysql -u root -p
# create user aa identified by ‘aa’;
# flush privileges
7. MariaDB data 유지 확인
kubectl delete pod mariadb
kubectl exec -it mariadb-7d6c97d79d-sgvn7 -- bash
# mysql -u root -p
# select user_name from mysql.user;
CF)
* 다른 Pod에서 MySQL 접속하기
kubectl run -it --rm --image=mysql:5.6 --restart=Never mariadb-client -- mysql -h ${IP} -p'${DB_Password}'
* scaling
kubectl scale deployment/mariadb --replicas=2
'Developet > Kubernetes' 카테고리의 다른 글
How to Install a Kubernetes Cluster on CentOS 8(쿠버네티스 설치) (0) | 2021.12.01 |
---|---|
kubectl for docker user (0) | 2021.11.07 |
kubectl use rule (0) | 2021.11.07 |
kubectl command (0) | 2021.11.07 |
kubectl Cheat Sheet (0) | 2021.11.06 |