上一节讲解了K8s包管理工具Helm、使用Helm部署mongodb集群(主从数据库集群),这里来看看K8s使用Helm部署mysql集群(主从数据库集群)
一.Helm 搭建mysql集群
1.安装mysql不使用persistence(无本地存储)
无本地存储:当重启的时候,数据库消失
(1).打开官网的应用中心
打开应用中心,搜索mysql
(2).安装
1).添加repo仓库
#添加repo仓库源
# mysql-repo 是自定义的
helm repo add mysql-repo https://charts.bitnami.com/bitnami
2).安装
#安装: mysql-repo和上面保存一致
#mongo:是自定义的
#--set 可以跟参数
#persistence.enabled 配置应用的持久化
helm install mysqltest mysql-repo/mysql --set persistence.enabled=false,auth.rootPassword="mysqlpass"
具体命令如下:
[root@VM-0-12-tencentos ~]# helm install mysqltest mysql-repo/mysql --set primary.persistence.size=10Gi,auth.rootPassword=mysqlpass
NAME: mysqltest
LAST DEPLOYED: Wed Jan 10 20:30:35 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mysql
CHART VERSION: 9.16.1
APP VERSION: 8.0.35** Please be patient while the chart is being deployed **Tip:Watch the deployment status using the command: kubectl get pods -w --namespace defaultServices:echo Primary: mysqltest.default.svc.cluster.local:3306Execute the following to get the administrator credentials:echo Username: rootMYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysqltest -o jsonpath="{.data.mysql-root-password}" | base64 -d)To connect to your database:1. Run a pod that you can use as a client:kubectl run mysqltest-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.35-debian-11-r2 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash2. To connect to primary service (read/write):mysql -h mysqltest.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
3).自动生成secret,PVC,磁盘等
可以通过面板进行查看
(3).设置环境变量
使用安装完成的提示命令设置环境变量
[root@VM-0-3-tencentos ~]#MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)
[root@VM-0-3-tencentos ~]# echo $MYSQL_ROOT_PASSWORD
mysqlpass
(4).开启一个客户端进行测试
参考上面安装完成的代码
1).开启临时客户端
kubectl run mysqltest-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.35-debian-11-r2 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash
具体如下:
2).连接数据库
[root@VM-0-12-tencentos ~]# kubectl run testmysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.35-debian-11-r2 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash
If you don't see a command prompt, try pressing enter.
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$
I have no name!@testmysql-client:/$ mysql -h testmysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.35 Source distributionCopyright (c) 2000, 2023, 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>
(5).查看 pod
(6).外部访问
这里需要进行一下端口转发,命令如下:
kubectl port-forward --address 0.0.0.0 service/testmysql 3306:3306
在服务端开启一个临时mysql客户端,看看有上面添加的数据不?
[root@VM-0-12-tencentos ~]# kubectl run testmysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.35-debian-11-r2 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash
If you don't see a command prompt, try pressing enter.
I have no name!@testmysql-client:/$ mysql -h testmysql.default.svc.