Home [K8S] Database
Post
Cancel

[K8S] Database

PV, PVC 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
iMacPro:~$ vi domain-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: domain-dev
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Secret(MySQL Root Password) 생성

1
iMacPro:~$ k -n domain-dev create secret generic mysql-root-password --from-literal='password=root'

MySQL Service 및 Deployment 생성

pv에서 마운트한 /mnt/data 에 데이터가 있을 경우 환경변수 값이 적용되지 않음. 새로 만들 경우 마운트 경로의 데이터를 모두 지우고 진행해야 함

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
iMacPro:~$ vi domain-mysql.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: domain-dev
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: domain-dev
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-password
              key: password
        - name: MYSQL_DATABASE
          value: domain
        - name: MYSQL_USER
          value: domainops
        - name: MYSQL_PASSWORD
          value: tlsxhqnfdl#
        - name: MYSQL_ROOT_HOST
          value: '%'
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

클러스터 내 데이터베이스 접속 확인

1
2
3
4
5
6
7
8
9
10
11
12
13
iMacPro:~$ k -n domain-dev run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ptlsxhqnfdl# -udomainops
If you don't see a command prompt, try pressing enter.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| domain              |
+--------------------+
2 rows in set (0.00 sec)

mysql>

외부 접속을 위한 서비스 생성 (Optional)

외부에서 접속하기 위한 서비스는 아래와 같이 생성. 이후 ingress와 연결해야하기 때문에 clusterip로 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
iMacPro:~$ cat ./domain-mysql.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: domain-dev
spec:
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql
  type: NodePort
status:
  loadBalancer: {}
ubuntu@master:~$ k -n domain-dev get svc
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
demo-web-svc   ClusterIP   10.101.167.253   <none>        80/TCP           9d
mysql          NodePort    10.105.168.240   <none>        3306:32334/TCP   12m
This post is licensed under CC BY 4.0 by the author.