当前位置:   article > 正文

[Kubernetes]8. K8s使用Helm部署mysql集群(主从数据库集群)

helm部署mysql集群

上一节讲解了K8s包管理工具Helm、使用Helm部署mongodb集群(主从数据库集群),这里来看看K8s使用Helm部署mysql集群(主从数据库集群)

一.Helm 搭建mysql集群 

1.安装mysql不使用persistence(无本地存储)

无本地存储:当重启的时候,数据库消失

(1).打开官网的应用中心

打开应用中心,搜索mysql

(2).安装 

1).添加repo仓库
  1. #添加repo仓库源
  2. # mysql-repo 是自定义的
  3. helm repo add mysql-repo https://charts.bitnami.com/bitnami
2).安装
  1. #安装: mysql-repo和上面保存一致
  2. #mongo:是自定义的
  3. #--set 可以跟参数
  4. #persistence.enabled 配置应用的持久化
  5. helm install mysqltest mysql-repo/mysql --set persistence.enabled=false,auth.rootPassword="mysqlpass"

具体命令如下: 

  1. [root@VM-0-12-tencentos ~]# helm install mysqltest mysql-repo/mysql --set primary.persistence.size=10Gi,auth.rootPassword=mysqlpass
  2. NAME: mysqltest
  3. LAST DEPLOYED: Wed Jan 10 20:30:35 2024
  4. NAMESPACE: default
  5. STATUS: deployed
  6. REVISION: 1
  7. TEST SUITE: None
  8. NOTES:
  9. CHART NAME: mysql
  10. CHART VERSION: 9.16.1
  11. APP VERSION: 8.0.35
  12. ** Please be patient while the chart is being deployed **
  13. Tip:
  14. Watch the deployment status using the command: kubectl get pods -w --namespace default
  15. Services:
  16. echo Primary: mysqltest.default.svc.cluster.local:3306
  17. Execute the following to get the administrator credentials:
  18. echo Username: root
  19. MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysqltest -o jsonpath="{.data.mysql-root-password}" | base64 -d)
  20. To connect to your database:
  21. 1. Run a pod that you can use as a client:
  22. 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
  23. 2. To connect to primary service (read/write):
  24. mysql -h mysqltest.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
3).自动生成secret,PVC,磁盘等 

可以通过面板进行查看

(3).设置环境变量 

使用安装完成的提示命令设置环境变量

  1. [root@VM-0-3-tencentos ~]#MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)
  2. [root@VM-0-3-tencentos ~]# echo $MYSQL_ROOT_PASSWORD
  3. 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).连接数据库
  1. [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
  2. If you don't see a command prompt, try pressing enter.
  3. I have no name!@testmysql-client:/$
  4. I have no name!@testmysql-client:/$
  5. I have no name!@testmysql-client:/$
  6. I have no name!@testmysql-client:/$
  7. I have no name!@testmysql-client:/$
  8. I have no name!@testmysql-client:/$
  9. I have no name!@testmysql-client:/$
  10. I have no name!@testmysql-client:/$ mysql -h testmysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
  11. mysql: [Warning] Using a password on the command line interface can be insecure.
  12. Welcome to the MySQL monitor. Commands end with ; or \g.
  13. Your MySQL connection id is 22
  14. Server version: 8.0.35 Source distribution
  15. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  16. Oracle is a registered trademark of Oracle Corporation and/or its
  17. affiliates. Other names may be trademarks of their respective
  18. owners.
  19. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  20. mysql>

(5).查看 pod 

(6).外部访问

这里需要进行一下端口转发,命令如下:

kubectl port-forward --address 0.0.0.0 service/testmysql 3306:3306

在服务端开启一个临时mysql客户端,看看有上面添加的数据不? 

  1. [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
  2. If you don't see a command prompt, try pressing enter.
  3. I have no name!@testmysql-client:/$ mysql -h testmysql.default.svc.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/523622
推荐阅读
相关标签
  

闽ICP备14008679号