環境
- Kubernetes v1.24
やること
MySQLサーバーをKubernetesで構築しようと思ったとき、練習用なら自分でYAML書いてデプロイ出来るが、永続化とかレプリケーションまで考えるとちょっと面倒すぎる。
そもそもKubernetesでMySQLのデプロイが向いているのかはさておき、とりあえずbitnami製のhelmレポを使うと簡単にMySQLをデプロイ出来るのでメモ
とりあえず立ててみる
まずはスレーブとか無しの単体運用のデプロイ
レポジトリの追加
helm repo add bitnami https://charts.bitnami.com/bitnami
以下のようなYAML作成 storageClassは適宜変更して
architecture: standalone storageClass: nfs-test auth: rootPassword: password image: debug: true primary: service: type: LoadBalancer extraEnvVars: - name: TZ value: "Asia/Tokyo" persistence: storageClass: nfs-test
いざインストール --create-namespaceつけると自動でNamespaceも作成してくれる「dev-mysql」は名前
helm install dev-mysql bitnami/mariadb --namespace mysql --create-namespace -f bitnami.yaml
設定したパスワードを再確認する方法
kubectl get secret --namespace mysql dev-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d
MySQLサーバーへログイン 一発
mysql -h dev-mysql.mysql.svc.cluster.local -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 840 Server version: 10.6.8-MariaDB-log Source distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
設定変更した場合
helm upgrade dev-mysql bitnami/mysql --namespace mysql -f bitnami.yaml
削除
helm delete dev-mysql --namespace mysql kubectl delete ns mysql
マスタースレーブ構成を試す
architecture: replication にするとマスタースレーブ構成を作成できる
architecture: replication storageClass: nfs-test image: debug: true primary: persistence: storageClass: nfs-test secondary: replicaCount: 2 persistence: storageClass: nfs-test
合計3台のMariaDBのサーバーが作成される。
$ kubectl get -n mysql pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES oyakodon-mariadb-primary-0 1/1 Running 0 74m 10.189.79.21 ubuntu02 <none> <none> oyakodon-mariadb-secondary-0 1/1 Running 0 74m 10.189.79.22 ubuntu02 <none> <none> oyakodon-mariadb-secondary-1 1/1 Running 0 71m 10.189.3.203 ubuntu01 <none> <none>
スレーブの設定も自動でやってくれる 神
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: oyakodon-mariadb-primary
Master_User: replicator
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 471
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 770
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 471
Relay_Log_Space: 2885
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 848
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 8
Slave_Non_Transactional_Groups: 2
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
細かいパラメータは 公式ドキュメント 参照