K8s数据持久化
数据持久化 Volume介绍
Volume介绍:Volume是Pad中能够被多个容器访问的共享目录Kubernetes中的Volume不Pad生命周期相同,但不容器的生命周期丌相关Kubernetes支持多种类型的Volume,并且一个Pod可以同时使用任意多个VolumeVolume类型包括:- EmptyDir:Pod分配时创建, K8S自动分配,当Pod被移除数据被清空。用于临时空间等。- hostPath:为Pod上挂载宿主机目录。用于持久化数据。- nfs:挂载相应磁盘资源。
1.EmptyDir实验
EmptyDir实验cat >emptyDir.yaml <> /data/busybox/index.html;sleep 3;done"] volumes: - name: cache-volume emptyDir: {}EOF
2.hostPath实验
hostPath实验1.发现的问题:- 目录必须存在才能创建- POD不固定会创建在哪个Node上,数据不统一2.type类型说明https://kubernetes.io/docs/concepts/storage/volumes/#hostpathDirectoryOrCreate 目录不存在就自动创建Directory 目录必须存在FileOrCreate 文件不存在则创建File 文件必须存在3.根据Node标签选择POD创建在指定的Node上方法1: 直接选择Node节点名称apiVersion: v1kind: Podmetadata: name: busybox-nodenamespec: nodeName: node2 containers: - name: busybox-pod image: busybox volumeMounts: - mountPath: /data/pod/ name: hostpath-volume command: ["/bin/sh","-c","while true;do echo $(date) >> /data/pod/index.html;sleep 3;done"] volumes: - name: hostpath-volume hostPath: path: /data/node/ type: DirectoryOrCreate 方法2: 根据Node标签选择Node节点kubectl label nodes node3 disktype=SSDapiVersion: v1kind: Podmetadata: name: busybox-nodenamespec: nodeSelector: disktype: SSD containers: - name: busybox-pod image: busybox volumeMounts: - mountPath: /data/pod/ name: hostpath-volume command: ["/bin/sh","-c","while true;do echo $(date) >> /data/pod/index.html;sleep 3;done"] volumes: - name: hostpath-volume hostPath: path: /data/node/ type: DirectoryOrCreate 4.实验-编写mysql的持久化deploymentapiVersion: apps/v1kind: Deploymentmetadata: name: mysql-dp namespace: defaultspec: selector: matchLabels: app: mysql replicas: 1 template: metadata: name: mysql-pod namespace: default labels: app: mysql spec: containers: - name: mysql-pod image: mysql:5.7 ports: - name: mysql-port containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "123456" volumeMounts: - mountPath: /var/lib/mysql name: mysql-volume volumes: - name: mysql-volume hostPath: path: /data/mysql type: DirectoryOrCreate nodeSelector: disktype: SSD
3.PV和PVC
PV和PVC1.master节点安装nfsyum install nfs-utils -ymkdir /data/nfs-volume -pvim /etc/exports/data/nfs-volume 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)systemctl start rpcbindsystemctl start nfsshowmount -e 127.0.0.12.所有node节点安装nfsyum install nfs-utils.x86_64 -yshowmount -e 10.0.0.113.编写并创建nfs-pv资源cat >nfs-pv.yaml <mysql-pvc.yaml <mysql-dp.yaml <