k8s部署mysql

    后端存储利用nfs来进行存储数据,nfs安装不阐述,需要注意注意的是在创建mysql 的共享目录的时候参数设定/data/mysql *(rw,sync,no_root_squash,no_subtree_check)

1
2
$ sudo systemctl  restart nfs.service
$ sudo exportfs -arv

1、创建mysql存储

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
# cat mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pvc001
namespace: kube-ops
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Delete
nfs:
server: 172.21.16.240
path: /data/mysql

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
namespace: kube-ops
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi

2、部署mysql

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
# cat mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
namespace: kube-ops
spec:
replicas: 1
selector:
matchLabels:
name: mysql-ops
template:
metadata:
labels:
name: mysql-ops
spec:
containers:
- name: mysql
image: mysql:8.0.12
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: "noc-mysql"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: "/var/lib/mysql"
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc

3、设置端口映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# cat mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
namespace: kube-ops
labels:
name: mysql-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 30003
selector:
name: mysql-ops

4、查看pod部署

1
# kubectl get pod -n kube-ops

img

5、查看mysql部署哪个node节点

1
# kubectl get pod -o wide -n kube-ops

img

6、进入mysql容器

1
2
3
$ sudo docker ps -a
$ sudo docker exec -it ded7f2990db5 /bin/bash
root@mysql-deploy-6dc5d9786b-lgkxk:/# mysql -h127.0.0.1 -uroot -pnoc-mysql

6.1、设置mysql

1
2
3
mysql> alter user 'root'@'%' identified with mysql_native_password by'root';
mysql> alter user 'root'@'%' identified by 'mysql';
mysql> alter user 'root'@'%' identified by 'noc-mysql';

    退出mysql和容器,执行quit;退出mysql,按ctrl+p+q从容器中返回node主机。利用navicat 通过node主机的ip地址和端口30003连接mysql数据库
img

坚持原创技术分享,您的支持将鼓励我继续创作!
0%