k8s 集群引入外部mysql数据库
如:用户名密码(内网/外网)端口。
·
k8s 集群引入外部mysql数据库 并进行测试
引用k8s 外部的mysql
如: mysql:oot Password 10.4.10.14 3306 用户名 密码 (内网/外网) 端口
创建 service 和 endpoint
apiVersion: v1 kind: Service metadata: name: mysql namespace: default spec: #clusterIP: None type: ClusterIP ports: - name: mysql port: 3306 protocol: TCP targetPort: 3306 --- apiVersion: v1 kind: Endpoints metadata: name: mysql namespace: default subsets: - addresses: - ip: 10.4.10.14 ports: - name: mysql port: 3306 protocol: TCP 释义: # Service的name名称: mysql 必须和Endpoints的name名称: mysql一致 ,命名空间一样的 #注意也可以设置 cluster为 None 的建 kubectl apply -f mysql_svc_endpoint.yaml kubectl get svc -n 命名空间 (默认不加) NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP 10.97.230.222 <none> 3306/TCP 95m #查看 clusterIP设置为 None kubectl get svc -n 命名空间 (默认不加) NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP None <none> 3306/TCP 4m25s kubectl get endpoints -n 命名空间 (默认不加) NAME ENDPOINTS AGE mysql 101.42.101.141:3306 95m
导入数据——防止中文乱码创建mysql configmap 配置文件
vi mysql-configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: mysql-config data: mysql.cnf: | [mysqld] symbolic-links=0 max_connections=3000 max_user_connections=500 wait_timeout=200 character-set-server=utf8 collation-server=utf8_general_ci [mysql] default-character-set=utf8 [client] default-character-set=utf8 释义: ConfigMap 的name名称: mysql-config 映射到容器的文件名是 mysql.cnf mysql.cnf 相当于 key |下面相当于 values 值 | 竖线 保留原格式, #创建 configmap kubectl apply -f mysql-configmap.yaml # 查看 [root@node1 test]# kubectl get configmap -n 命名空间 (默认不加) NAME DATA AGE mysql-config 1 21m
创建 k8s 客户端 mysql
apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: default spec: selector: matchLabels: app: mysql-v1 template: metadata: labels: app: mysql-v1 spec: containers: - name: mysql image: mysql:8.0 #command: ["/bin/sh","-c","sleep 1d"] env: - name: MYSQL_ROOT_PASSWORD value: "123456" # 设置root用户的密码 volumeMounts: - name: config-volume mountPath: /etc/mysql/conf.d volumes: - name: config-volume configMap: name: mysql-config
#创建 deployment kubectl apply -f mysql-deployment.yam #查看deployment [root@node1 test]# kubectl get deployment -n 命名空间 (默认不加) NAME READY UP-TO-DATE AVAILABLE AGE mysql 1/1 1 1 35m #查看 pod [root@node1 test]# kubectl get po -n 命名空间 (默认不加) NAME READY STATUS RESTARTS AGE mysql-5d57f47d56-smrjh 1/1 Running 0 38m
测试mysql 连接
#查看 k8s创建的service 服务的名字;如果不在默认空间 kubectl get svc -n 命名空间 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP 10.97.230.222 <none> 3306/TCP 114m #查看pod kubectl get po -n 命名空间 NAME READY STATUS RESTARTS AGE mysql-5d57f47d56-smrjh 1/1 Running 0 38m #进入mysql的pod kubectl exec -it mysql-5d57f47d56-smrjh -- /bin/bash #查看 mysql 的配置 root@mysql-5d57f47d56-smrjh:/# cat /etc/mysql/conf.d/mysql.cnf [mysqld] symbolic-links=0 max_connections=3000 max_user_connections=500 wait_timeout=200 character-set-server=utf8 collation-server=utf8_general_ci [mysql] default-character-set=utf8 [client] default-character-set=utf8 #释义 mysql.cnf 就是 configmap 创建的key # 连接k8s创建的service 服务的名字 root@mysql-5d57f47d56-smrjh:/# mysql -uroot -p'Passw0rd' -h mysql 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.28 MySQL Community Server - GPL Copyright (c) 2000, 2021, 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> exit Bye root@mysql-5d57f47d56-smrjh:/# #这样连接也是可以远程数据库 root@mysql-5d57f47d56-smrjh:/# mysql -uroot -p'Passw0rd' -h mysql.default.svc.cluster.local 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 25 Server version: 8.0.28 MySQL Community Server - GPL Copyright (c) 2000, 2021, 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> #释义 mysql.default.svc.cluster.local mysql 是 service的名字 default 是命名空间 svc.cluster.local 是固定格式 mysql>
补充:
报错:Host XXX is not allowed to connect to this MySQL server。
# 在安装Mysql数据库的主机上登录root用户: mysql -u root -p # 切换数据库 use mysql; select host from user where user='root'; # 可以看到当前主机配置信息为localhost. update user set host = '%' where user ='root' # 将Host设置为通配符% flush privileges # 使权限配置生效
更多推荐
已为社区贡献1条内容
所有评论(0)