05A. MySQL ์คํ ์ดํธํ์ ์์ฑํ๊ธฐ
05A. MySQL ์คํ ์ดํธํ์ ์์ฑํ๊ธฐ ๊ด๋ จ
๋จผ์ MySQL ์คํ ์ดํธํ์ ์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฐ๋ฐ ์คํ ์ดํธํ์ ๊ณผ ์ง๊ธ๊น์ง ์ฌ์ฉํ๋ ๋ํ๋ก์ด๋จผํธ๋ ์ด๋ค ์ฐจ์ด์ ์ด ์์๊น์? ์ด๋ฆ ๊ทธ๋๋ก ์คํ ์ดํธํ์ ์ ์ํ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ๋ํ๋ก์ด๋จผํธ๋ ์ํ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ต๋๋ค(stateless). ๋ฐ๋ผ์ ์คํ ์ดํธํ์ ์ ํผ์์คํดํธ ๋ณผ๋ฅจ ํด๋ ์(pvc)์ ํตํด ํผ์์คํดํธ ๋ณผ๋ฅจ(pv)์ ์์ฑํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์ ์งํฉ๋๋ค.
๋ค์ ๋ด์ฉ์ mysql-statefulset.yaml
ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
mysql-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mysql
image: mysql:5.7
ports:
- protocol: TCP
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: examplepassword
- name: MYSQL_DATABASE
value: example
volumeMounts:
- name: data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: data
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
๋ค์ ๋ช
๋ น์ ์คํํ์ฌ mysql-statefulset.yaml
ํ์ผ๋ก ์คํ
์ดํธํ์
์ ์์ฑํฉ๋๋ค.
kubectl create -f mysql-statefulset.yaml
#
# statefulset.apps/mysql created
kubectl get all
๋ช
๋ น์ผ๋ก ์ค๋ธ์ ํธ ๋ชฉ๋ก์ ์ถ๋ ฅํด๋ด
๋๋ค.kubectl get all
#
# NAME READY STATUS RESTARTS AGE
# pod/mysql-0 1/1 Running 0 11s
#
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d20h
#
# NAME READY AGE
# statefulset.apps/mysql 1/1 12s
statefulset.apps/mysql
์ด ์์ฑ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ํนํ ์คํ
์ดํธํ์
์ ๋ ํ๋ฆฌ์นด์
์ ์์ฑํ์ง ์์ผ๋ฉฐ, ํ๋์ ์ด๋ฆ์ด ๋๋คํ์ง ์๊ณ mysql-0์ฒ๋ผ 0๋ถํฐ ์์ฐจ์ ์ผ๋ก ์ฆ๊ฐํฉ๋๋ค.
์ด์ ์๋น์ค๋ฅผ ์์ฑํด๋ณด๊ฒ ์ต๋๋ค. ๋ค์ ๋ด์ฉ์ mysql-service.yaml
ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
clusterIP: None
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
๋ค์ ๋ช
๋ น์ ์คํํ์ฌ mysql-service.yaml
ํ์ผ๋ก ์๋น์ค๋ฅผ ์์ฑํฉ๋๋ค.
kubectl create -f mysql-service.yaml
#
# service/mysql created
kubectl get all
๋ช
๋ น์ผ๋ก ์ค๋ธ์ ํธ ๋ชฉ๋ก์ ์ถ๋ ฅํด๋ด
๋๋ค.kubectl get all
#
# NAME READY STATUS RESTARTS AGE
# pod/mysql-0 1/1 Running 0 21m
#
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d20h
# service/mysql ClusterIP None <none> 3306/TCP 97s
#
# NAME READY AGE
# statefulset.apps/mysql 1/1 21m
๊ทธ๋ผ MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋์ง ์ฒดํฌํด๋ณด๊ฒ ์ต๋๋ค.
.kubectl run -it --rm mysql-client --image=mysql:5.7 --restart=Never -- sh
๋ก mysql ํด๋ผ์ด์ธํธ ํ๋๋ฅผ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ mysql -h mysql -u root -p
๋ฅผ ์คํํ ๋ค Enter password: ๊ฐ ๋์ค๋ฉด examplepassword
๋ฅผ ์
๋ ฅํ์ฌ MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ก๊ทธ์ธํฉ๋๋ค. show databases;
๋ฅผ ์
๋ ฅํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ชฉ๋ก์ ์ถ๋ ฅํด๋ด
๋๋ค. ์ด์์ด ์์ผ๋ฉด exit
, exit
๋ฅผ ์
๋ ฅํ์ฌ ๋น ์ ธ๋์ต๋๋ค.
kubectl run -it --rm mysql-client --image=mysql:5.7 --restart=Never -- sh
#
# If you don't see a command prompt, try pressing enter.
# sh-4.2#
mysql -h mysql -u root -p
#
# Enter password: examplepassword
# Welcome to the MySQL monitor. Commands end with ; or \g.
# Your MySQL connection id is 2
# Server version: 5.7.40 MySQL Community Server (GPL)
#
# Copyright (c) 2000, 2022, Oracle and/or its affiliates.
#
# Oracle is a registered trademark of Oracle Corporation and/or its
# affiliates. Other names may be trademarks of their respective
# owners.
#
# Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#
# mysql>
show databases;
#
# +--------------------+
# | Database |
# +--------------------+
# | information_schema |
# | example |
# | mysql |
# | performance_schema |
# | sys |
# +--------------------+
# 6 rows in set (0.01 sec)
exit
#
# Bye
# sh-4.2#
exit
#
# exit
# pod "mysql-client" deleted