实验目的:
将MySQL5.7使用nfs持久化存储部署到Kubernetes集群中
nfs存储地址:
nfs.myit.icu
nfs存储配置:
临时测试---100G
安装nfs
yum install -y nfs-utils rpcbind
创建nfs存储目录
[root@nfs ~]#mkdir /data/nfsData -p
格式化磁盘
[root@nfs ~]#mkfs.ext4 /dev/vdb
挂载磁盘到nfs存储目录
[root@nfs ~]#mount /dev/vdb /data/nfsData/
设置硬盘开机自动挂载
[root@nfs ~]#vim /etc/fstab
/dev/vdb /data/nfsData ext4 defaults
设置nfs存储目录权限
[root@nfs ~]#chmod -R 777 /data/nfsData/
进入到存储目录
[root@nfs ~]#cd /data/nfsData/
创建数据库存储目录
[root@nfs /data/nfsData]#mkdir Bystart_MySQL_5_7
设置nfs配置---生产最好设置一个B或者C或者单个IP,不要用*
[root@nfs ~]#vim /etc/exports
/data/nfsData *(rw,no_root_squash,no_all_squash,sync)
/data/nfsData/Bystart_MySQL_5_7 *(rw,no_root_squash,no_all_squash,sync)
设置开机自启并启动
[root@nfs ~]#systemctl enable rpcbind --now
[root@nfs ~]#systemctl enable nfs --now
注意:
K8s各节点请安装以下软件,来确保集群支持NFS
yum install -y nfs-utils rpcbind
创建yaml存放目录并进入
mkdir Bystart_MySQL_5_7 && cd Bystart_MySQL_5_7
创建MySQL PV资源清单
vim mysql57_pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: bystart-mysql-5-7-data-pv
spec:
capacity:
storage: 15Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /data/nfsData/Bystart_MySQL_5_7 #nfs共享目录
server: nfs.myit.icu #nfs服务器地址
创建MySQL PVC资源清单
vim mysql57_pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: bystart-mysql-5-7-data-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
创建MySQL Deployment资源清单---我们需要的镜像为:MySQL:5.7,为了部署顺利,可以提前在各节点下载该镜像
vim mysql57_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: bystart-mysql-5-
spec:
replicas:
template:
metadata:
labels:
name: bystart-mysql
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- name: db
containerPort:
env:
- name: MYSQL_ROOT_PASSWORD
value: "taozi.cn" #数据库密码
volumeMounts:
- mountPath: /var/lib/mysql
name: bystart-mysql-data
volumes:
- name: bystart-mysql-data
persistentVolumeClaim:
claimName: bystart-mysql-5-7-data-pvc
selector:
matchLabels:
name: bystart-mysql
创建MySQL SVC资源清单
vim mysql57_svc.yaml
apiVersion: v1
kind: Service
metadata:
name: bystart-mysql-5-
spec:
type: NodePort
ports:
- port:
targetPort:
nodePort:
selector:
name: bystart-mysql
开始部署---这个文档没部署到指定命名空间下,如果生产环境,先创建命名空间,将资源部署到指定命名空间下即可!
[root@master1 ~/Bystart_MySQL_5_7]#kubectl create -f mysql57_pv.yaml
persistentvolume/bystart-mysql-5-7-data-pv created
[root@master1 ~/Bystart_MySQL_5_7]#kubectl create -f mysql57_pvc.yaml
persistentvolumeclaim/bystart-mysql-5-7-data-pvc created
[root@master1 ~/Bystart_MySQL_5_7]#kubectl apply -f mysql57_deployment.yaml
deployment.apps/bystart-mysql-5-7 created
[root@master1 ~/Bystart_MySQL_5_7]#kubectl create -f mysql57_svc.yaml
service/bystart-mysql-5-7 created
查看PV信息
root@master1 ~/Bystart_MySQL_5_7]#kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
bystart-mysql-5-7-data-pv 15Gi RWX Retain Available 4s
查看PVC信息
[root@master1 ~/Bystart_MySQL_5_7]#kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
bystart-mysql-5-7-data-pvc Bound bystart-mysql-5-7-data-pv 15Gi RWX 7s
查看Pod信息
[root@master1 ~/Bystart_MySQL_5_7]#kubectl get pods
NAME READY STATUS RESTARTS AGE
bystart-mysql-5-7-7556448d47-jj7dw 1/1 Running 0 43m
myapp-deploy-64d6d979b7-kpm7v 1/1 Running 0 30d
myapp-deploy-64d6d979b7-pblx8 1/1 Running 0 30d
myapp-deploy-64d6d979b7-tzlgt 1/1 Running 0 30d
nginx-dm-6c75d65c4b-g954p 1/1 Running 0 28d
nginx-dm-6c75d65c4b-m4hrz 1/1 Running 0 28d
查看svc信息
[root@master1 ~/Bystart_MySQL_5_7]#kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
bystart-mysql-5-7 NodePort 10.105.79.87 <none> 3306:32306/TCP 43m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 79d
myapp ClusterIP 10.96.44.160 <none> 80/TCP 29d
myapp-externalname ExternalName <none> hub.myit.icu <none> 29d
myapp-headless ClusterIP None <none> 80/TCP 29d
myapp-nodeport NodePort 10.97.28.150 <none> 80:30842/TCP 29d
nginx-svc ClusterIP 10.97.94.6 <none> 80/TCP 28d
traefik-ingress-service NodePort 10.107.183.78 <none> 80:31044/TCP,8080:30316/TCP,443:30362/TCP 79d
结果:
通过Master的IP加上端口,账户密码即可连接到数据库
在nfs存储服务器上查看数据
[root@nfs /data/nfsData/Bystart_MySQL_5_7]#ll
total 188476
-rw-r----- 1 polkitd input 56 Jan 2 16:20 auto.cnf
-rw------- 1 polkitd input 1676 Jan 2 16:20 ca-key.pem
-rw-r--r-- 1 polkitd input 1112 Jan 2 16:20 ca.pem
-rw-r--r-- 1 polkitd input 1112 Jan 2 16:20 client-cert.pem
-rw------- 1 polkitd input 1680 Jan 2 16:20 client-key.pem
-rw-r----- 1 polkitd input 1353 Jan 2 16:21 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Jan 2 16:21 ibdata1
-rw-r----- 1 polkitd input 50331648 Jan 2 16:21 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Jan 2 16:20 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Jan 2 16:21 ibtmp1
drwxr-x--- 2 polkitd input 4096 Jan 2 16:20 mysql
drwxr-x--- 2 polkitd input 4096 Jan 2 16:20 performance_schema
-rw------- 1 polkitd input 1680 Jan 2 16:20 private_key.pem
-rw-r--r-- 1 polkitd input 452 Jan 2 16:20 public_key.pem
-rw-r--r-- 1 polkitd input 1112 Jan 2 16:20 server-cert.pem
-rw------- 1 polkitd input 1676 Jan 2 16:20 server-key.pem
drwxr-x--- 2 polkitd input 12288 Jan 2 16:20 sys
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
标签:Kubernetes,data,MySQL5.7,MySQL,---,nfs,mysql,bystart,root
From: https://www.cnblogs.com/gaoyanbing/p/17705747.html