1. 引用k8s 外部的mysql
如:
mysql root mysql Passw0rd mysql 101.42.101.141 mysql 3306
mysql 用户名 mysql 密码 mysql ip mysql 端口
2. 创建 service 和 endpoint
vi mysql_svc_endpoint.yaml
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: 101.42.101.141
ports:
- name: mysql
port: 3306
protocol: TCP
释义:
# Service 的 name名称: mysql 必须 和 Endpoints name名称: mysql 一致 ,命名空间一样,
# Endpoints 的 - ip: 101.42.101.141 是外部mysql的 ip 端口也是外部的port: 3306
#注意 也可以设置 clusterIP 为 None
创建
kubectl apply -f mysql_svc_endpoint.yaml
[root@node1 test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.97.230.222 <none> 3306/TCP 95m
#查看 clusterIP 设置 为 None
[root@node1 test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP None <none> 3306/TCP 4m25s
[root@node1 test]# kubectl get endpoints
NAME ENDPOINTS AGE
mysql 101.42.101.141:3306 95m
3. 导入数据,防止中文乱码创建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
NAME DATA AGE
mysql-config 1 21m
#查看详细
[root@node1 test]# kubectl describe configmap mysql-config
Name: mysql-config
Namespace: default
Labels: <none>
Annotations: <none>
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
BinaryData
====
Events: <none>
4. 创建 k8s 客户端 mysql
vi mysql-deployment.yaml
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:5.7
#command: ["/bin/sh","-c","sleep 1d"]
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
volumeMounts:
- name: config-volume
mountPath: /etc/mysql/conf.d
volumes:
- name: config-volume
configMap:
name: mysql-config
#创建 deployment
kubectl apply -f mysql-deployment.yaml
#查看deployment
[root@node1 test]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
mysql 1/1 1 1 35m
#查看 pod
[root@node1 test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-5d57f47d56-smrjh 1/1 Running 0 38m
5. 测试mysql 连接 k8s创建的service 服务,服务名字 mysql , 测试字符集utf-8
#查看 k8s创建的service 服务的名字
[root@node1 test]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.97.230.222 <none> 3306/TCP 114m
#查看 pod
[root@node1 test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-5d57f47d56-smrjh 1/1 Running 0 38m
#进入mysql 的pod
[root@node1 test]# 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 是固定格式
# 查看字符集 是 utf8
mysql> show variables like '%character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8mb3 |
| character_set_connection | utf8mb3 |
| character_set_database | utf8mb3 |
| character_set_filesystem | binary |
| character_set_results | utf8mb3 |
| character_set_server | utf8mb3 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql>