通过官方提供的MySQL Operator for Kubernetes来部署mysql InnoDB Cluster。
安装operator
安装operator有两种方式,kubectl或者helm,任选其一。
kubectl 安装
1 2 3 4 5
| # 安装crd kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-crds.yaml
# 安装operator kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-operator.yaml
|
前面两部执行成功后应该会在mysql-operator命名空间创建deployment,验证一下,如果这里pod没跑起来,后面安装InnoDB Cluster不会进行下去。
1
| kubectl get deployment -n mysql-operator mysql-operator
|
helm 安装
1 2 3
| helm repo add mysql-operator https://mysql.github.io/mysql-operator/ helm repo update helm install mysql-operator mysql-operator/mysql-operator --namespace mysql-operator --create-namespace
|
安装InnoDB Cluster
使用Kubectl
需要先手动创建一个secret
1 2 3 4
| kubectl create secret generic mypwds \ --from-literal=rootUser=root \ --from-literal=rootHost=% \ --from-literal=rootPassword="sakila"
|
创建一个集群声明文件,这里用了刚才创建的mypwds秘钥
1 2 3 4 5 6 7 8 9 10
| apiVersion: mysql.oracle.com/v2 kind: InnoDBCluster metadata: name: mycluster spec: secretName: mypwds tlsUseSelfSigned: true instances: 3 router: instances: 1
|
假设上一步创建的声明文件保存为mycluster.yaml
1
| kubectl apply -f mycluster.yaml
|
一切正常集群就应该跑起来了
1
| kubectl get innodbcluster
|
使用helm
1 2 3 4 5 6 7
| # 先拉helm下来 helm pull mysql-operator/mysql-innodbcluster # 解压 tar -xvzf mysql-innodbcluster-2.0.9.tgz # 进去编辑values文件 cd mysql-innodbcluster vim values.yml
|
这里我修改了密码,改了密码之后这里必须注意useSelfSigned: true,不然安装的时候就会报错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| image: pullPolicy: IfNotPresent pullSecrets: enabled: false secretName:
credentials: root: user: root password: ZAQ!2wsx host: "%"
tls: useSelfSigned: true
serverInstances: 3 routerInstances: 1 baseServerId: 1000
|
安装集群
1
| helm install mycluster -f values.yaml --namespace mycluster .
|
连接集群
这里我用helm 安装完成后,创建的服务是headless,helm文件中也没找到配置的地方,就手动创建服务。
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
| --- apiVersion: v1 kind: Service metadata: annotations: service.alpha.kubernetes.io/tolerate-unready-endpoints: 'true' labels: app.kubernetes.io/managed-by: Helm mysql.oracle.com/cluster: mycluster tier: mysql name: mycluster-nodeport namespace: mycluster spec: externalTrafficPolicy: Cluster ports: - name: mysql nodePort: port: 3306 protocol: TCP targetPort: 3306 - name: mysqlx nodePort: port: 33060 protocol: TCP targetPort: 33060 - name: gr-xcom nodePort: port: 33061 protocol: TCP targetPort: 33061 publishNotReadyAddresses: true selector: component: mysqld mysql.oracle.com/cluster: mycluster tier: mysql sessionAffinity: None type: NodePort status: loadBalancer: {}
|
这里还需要注意,直接连可能会报错Authentication plugin ‘caching_sha2_password’ cannot be loaded,因为mysql8之后密码插件的问题,需要更新密码为native,可以直接进去mysql容器中使用mysql命令连接数据库执行下面sql,如果提示只读,那就换一个pod进去。
1
| ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'ZAQ!2wsx';
|
参考:
mysql/mysql-operator: MySQL Operator for Kubernetes (github.com)
MySQL :: MySQL Operator for Kubernetes 手册 :: 1 简介