项目环境搭建【Docker+k8s】七 kubernetes使用数据卷

项目环境搭建【Docker+k8s】七 kubernetes使用数据卷

下面介绍如何在k8s中部署mysq使用网络存储NFS挂载数据卷。

mysql数据卷使用

创建持久化存储卷,设置容量为10G,nfs-pv-mysql.yml的配置文件如下

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv-mysql
spec:
  #设置容量
  capacity:
    storage: 10Gi
  #访问模式
  accessModes:
    - ReadWriteMany #该卷能够以读写模式被多个节点同时加载
  #回收策略 这里是基础排除 rm -rf /thevolume/*
  persistentVolumeReclaimPolicy: Recycle
  nfs: 
    #NFS服务端配置的路径
    path: "/usr/local/kubernetes/volumes" 
	#NFS服务器地址
	server: 192.168.100.81
	readOnly: false

部署存储卷服务

[root@yn100-22 volumes]# kubectl create -f nfs-pv-mysql.yml
persistentvolume/nfs-pv-mysql created
[root@yn100-22 volumes]# kubectl get pv
NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
nfs-pv-mysql   5Gi        RWX            Recycle          Available                                   4m47s

查看存储卷,服务为Available激活状态,现在数据卷已经创建好了,但是没有挂载,所以为Available

定义存储卷消费者PVC,创建一个名为nfs-pvc-mysql-open.yml的配置文件

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc-mysql-open
spec:
  accessModes:
    - ReadWriteMany
  #按需分配资源
  resources:
    requests:
	  storage: 5Gi
#部署
kubectl create -f nfs-pvc-mysql-open.yml
#删除
kubectl delete -f nfs-pvc-mysql-open.yml
#查看
kubectl get pvc

查看存储卷消费者,此时绑定到了nfs-pv上面了,状态由之前的Available变为了Bound

[root@yn100-22 volumes]# kubectl get pvc
NAME                 STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nfs-pvc-mysql-open   Bound    nfs-pv-mysql   10Gi       RWX                           47s
查看状态,由之前的Available变为了Bound
[root@yn100-22 volumes]# kubectl get pv
NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                        STORAGECLASS   REASON   AGE
nfs-pv-mysql   10Gi       RWX            Recycle          Bound    default/nfs-pvc-mysql-open                           10m
[root@yn100-22 volumes]# 

部署mysql

注意:确保每台node上都安装NFS客户端

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql-open
spec:
  replicas: 1
  template:
    metadata:
      labels: 
        name: mysql-open
    spec:
      containers: 
        - name: mysql-open
          image: mysql:8.0.18
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "root"
		  #容器中的挂载目录
          volumeMounts:
            - name: nfs-vol-open
              mountPath: /var/lib/mysql
      volumes: 
	   #挂载到数据卷
        - name: nfs-vol-open
          persistentVolumeClaim:
            claimName: nfs-pvc-mysql-open
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-open
spec: 
  ports:
    - port: 3306
	  targetPort: 3306
  type: LoadBalancer
  selector:
    name: mysql-open

创建服务,查看启动状态为Ready

[root@yn100-22 service]# kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          3d23h
mysql-open   LoadBalancer   10.107.29.201   <pending>     3306:30201/TCP   6m38s
[root@yn100-22 service]# kubectl get deployment
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
mysql-open   1/1     1            1           6m55s
[root@yn100-22 service]# kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
mysql-open-68dbbdfcff-49cgz   1/1     Running   0          7m3s

查看NFS服务器,volumes目录已经有mysql数据文件了

关注微信公众号,免费获取文档及资源

项目环境搭建【Docker+k8s】七  kubernetes使用数据卷

学习交流QQ群【883210148】

项目环境搭建【Docker+k8s】七  kubernetes使用数据卷

上一篇:k8s采用storageclass方式挂载nfs


下一篇:CentOS下LVM分区简介