動かざることバグの如し

近づきたいよ 君の理想に

MySQL公式のKubernetesパッケージ「mysql-operator」をインストール

環境

mysql-operatorとは

Kubernetes上でデータベースを動かすのは結構難しいらしく、独自のCRD実装したオペレーターでインストールすることが一般的っぽい。

そこでMySQL公式がMySQL Operatorを出しているのだが、2022年5月についにGAになったとのことで試して見たメモ

mysql/mysql-operator: MySQL Operator for Kubernetes

https://blogs.oracle.com/mysql/post/mysql-operator-for-kubernetes-reaches-general-availabilityblogs.oracle.com

アーキテクチャ

Medium.jpeg

セットアップ

基本的には公式のREADME.mdの通りにやっていけばおk

CRDのデプロイ

kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-crds.yaml

MySQLをデプロイ

kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-operator.yaml

するとmysql-operatorってのがデプロイされる

kubectl get deployment -n mysql-operator mysql-operator
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mysql-operator   1/1     1            1           28s

クラスタ

secret.ymlを作成して以下

apiVersion: v1
kind: Secret
metadata:
  name: mypwds
stringData:
  rootUser: root
  rootHost: '%'
  rootPassword: xxxxxxxxxxxxx
kubectl apply -f secret.yml

mycluster.yamlを作成して以下

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: mycluster
spec:
  secretName: mypwds
  tlsUseSelfSigned: true
  instances: 3
  router:
    instances: 1
  mycnf: |
    [mysqld]
    innodb_buffer_pool_size=4G
    innodb_log_file_size=2G

反映

kubectl apply -f mycluster.yaml

するとCRDである「innodbcluster」ってのが準備される。結構時間かかるがしばらくするとONLINEになるはず

kubectl get innodbcluster --watch
NAME          STATUS    ONLINE   INSTANCES   ROUTERS   AGE
mycluster     PENDING   0        3           1         2m6s
...
mycluster     ONLINE    3        3           1         10s

接続

サービスがあるのを確認

kubectl get service mycluster
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                  AGE
mycluster   ClusterIP   10.105.135.227   <none>        3306/TCP,33060/TCP,6446/TCP,6448/TCP,6447/TCP,6449/TCP   40m

port-forwardで接続する myclusterってのはサービス名

kubectl port-forward service/mycluster 3306

でローカルのクライアントから接続してみる

mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 71823
Server version: 8.0.29 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>

よさそう

ただ記事が少ないのでこれ使ってる人の数が未知数なんだよな。。。。

参考リンク