通过k8s安装mysql

mysql是程序员一定会打交道的数据库,在学习和练习阶段,都需要准备环境。这里介绍通过deploysvcscpvc部署单机版mysql,通过helm`部署集群版。生产环境推荐使用集群模式,支持高可用。

单机版

单机版通过手动部署一个一个服务即可,单机版本可用于学习部署方案,单机部署方案理解之后,也可以引申到集群方案部署。单机部署的要点是将单机中的资源通过yaml声明,包括持久化存储pv,卷供应pvc,资源管理deployment,服务发现svc

pv.yaml:使用本地存储作为后端存储

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
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 8Gi
local:
path: /data/nfs-pv
volumeMode: Filesystem
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
claimRef:
name: mysql-pv-claim
namespace: mysql
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux

pvc.yaml:指定pv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
volumeName: pv-nfs
storageClassName: ""
resources:
requests:
storage: 1Gi
claimRef:

deployment.yaml

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
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql-con
imagePullPolicy: Never
env:
- name: MYSQL_ROOT_PASSWORD
value: root // root密码
- name: MYSQL_USER
value: dbuser // 用户
- name: MYSQL_PASSWORD
value: dbpaassword // 密码
args: ["--default-authentication-plugin=mysql_native_password"]
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

svc.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Service
metadata:
name: mysql-service
labels:
app: mysql
spec:
type: NodePort
selector:
app: mysql
ports:
- protocol : TCP
port: 3306
targetPort: 3306

部署起来

1
2
3
4
# kubectl create -f pv.yaml
# kubectl create -f pvc.yaml
# kubectl create -f deployment.yaml
# kubectl create -f svc.yaml

查看端口

1
2
3
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-service NodePort 10.1.214.59 <none> 3306:30749/TCP 8m55s

通过IP地址、端口、用户名、密码连接即可。

如果pod状态卡在ContainerCreating,并且日志显示 Warning FailedMount 95s (x2 over 3m52s) kubelet Unable to attach or mount volumes: unmounted volumes=[mysql-persistent-storage], unattached volumes=[mysql-persistent-storage kube-api-access-h4tln]: timed out waiting for the condition,则需要在对应节点上手动创建目录。

集群版

集群版和单机版比较大的区别是实用动态卷供应,可以响应多个副本pod,学习阶段可以通过nfs作为sc,最为简单方便。集群版通过bitnamechart包实现。

添加helm

1
# helm repo add bitnami https://charts.bitnami.com/bitnami

查询mysql

1
2
3
4
5
6
# helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/mysql 9.2.0 8.0.29 MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin 10.1.12 5.2.0 phpMyAdmin is a free software tool written in P...
bitnami/mariadb 11.0.14 10.6.8 MariaDB is an open source, community-developed ...
bitnami/mariadb-galera 7.3.6 10.6.8 MariaDB Galera is a multi-primary database clus...

使用mysql,拉到本地

1
# helm pull bitnami/mysql

修改values.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
global:
storageClass: "nfs-client"
architecture: replication // 副本模式
primary:
service:
type: NodePort
secondary:
service:
type: NodePort
auth:
rootPassword: "password"
createDatabase: true
database: "my_database"
username: "user"
password: "password"

部署

1
# helm install mysql -f values.yaml ./ -n mysql

用户密码

1
2
echo Username: root
MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace mysql mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)

端口

1
2
3
4
5
6
# kubectl get svc -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-primary NodePort 10.1.45.111 <none> 3306:31398/TCP 35s
mysql-primary-headless ClusterIP None <none> 3306/TCP 35s
mysql-secondary NodePort 10.1.171.237 <none> 3306:30464/TCP 35s
mysql-secondary-headless ClusterIP None <none> 3306/TCP 35s

踩坑

集群下安装的mysql版本比较高,是8.x版本,需要手动分配权限。

1
2
3
4
5
# kubectl exec -it mysql-primary-0 -n mysql bash
mysql> SELECT host FROM mysql.user WHERE User = 'root';
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
mysql> FLUSH PRIVILEGES;