查看k8s支持的存储类
kubectl explain pods.spec.volumes
1.emptydir
kubectl explain pods.spec.volumes.emptyDir
kubectl explain pods.spec.containers 容器中设置
kubectl explain pods.spec.containers.volumeMounts
实例:
cd manifests/
mkdir volumes
cd volumes/
cp ../pod-demo.yaml ./
mv pod-demo.yaml pod-vol-demo.yaml
vim pod-vol-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
volumeMounts: 容器中挂载
- name: html 关联pod存储卷名
mountPath: /data/web/html 容器中挂载的路径
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
volumeMounts:
- name: html
mountPath: /data
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
volumes: pod存储卷
- name: html pod存储卷名
emptyDir: {} pod存储卷类型
kubectl apply -f pod-vol-demo.yaml
kubectl exec -it pod-demo -c busybox -- /bin/sh
-c 指定busybox容器
df -hT 查看挂载
cd /data
date >> index.html
kubectl exec -it pod-demo -c myapp -- /bin/sh
连入myapp容器
df -hT
cd /data/web/html
cat index.html 可以判定两个容器共享一个目录 emptyDir
实例:
kubectl delete -f pod-vol-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
volumeMounts:
- name: html
mountPath: /data
command:
- "/bin/sh"
- "-c"
- "while true; do echo $(date) >> /data/index.html;sleep 2;done" 命令不会终止,终止容器就会退出
volumes:
- name: html
emptyDir: {}
kubectl apply -f pod-vol-demo.yaml
curl 10.244.1.120 访问验证数据
1.1 gitrRepo
kubectl explain pod.spec.volumes.gitRepo
2.hostPath 宿主机路径 挂载宿主机真实目录,pod消失,数据还在宿主机目录上
节点级持久
kubectl explain pod.spec.volumes.hostPath
vim pod-hostpath-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
hostPath: pod存储卷类型
path: /data/pod/volume1 指定的hostPath存储路径,即本地目录路径
type: DirectoryOrCreate 类型 目录不存在则创建
在两个节点执行,用于验证
mkdir -p /data/pod/volume1
echo "node1.baidu.com" > /data/pod/volume1/index.html
创建
kubectl apply -f pod-hostpath-vol.yaml
kubectl get pods -o wide
验证
curl 10.244.1.122
3.网络共享存储 nfs 具有持久性
实例
node2作为 nfs共享存储
yum install nfs-utils -y
mkdir /data/volumes
vim /etc/exports
/data/volumes 192.168.81.0/24(rw,no_root_squash) 压缩root权限
systemctl start nfs nfs工作在2049端口
去node1节点测试
mount -t nfs node2:/data/volumes /mnt
不支持挂载,安装nfs套件
yum install nfs-utils -y
mount -t nfs node2:/data/volumes /mnt 测试能挂载上才能做网络共享存储
然后卸载 umount /mnt/
cp pod-hostpath-vol.yaml pod-nfs-vol.yaml
kubectl explain pods.spec.volumes.nfs
vim pod-nfs-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-nfs
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
nfs: 指定pod存储卷类型为nfs网络共享存储
path: /data/volumes nfs的共享目录
server: node2 nfs服务器地址
创建
kubectl apply -f pod-nfs-vol.yaml
验证
kubectl get pods -o wide
去nfs共享目录创建一个网页
echo "<h1>nfs store</h1>" > /data/volumes/index.html
curl 10.244.2.96
4.persistentVolumeClaim PVC 持久存储申请
kubectl explain pod.spec.volumes.persistentVolumeClaim
kubectl explain pvc
pvc 与 pv一一对应
pvc可以给多个pod访问
创建pv
以前面nfs为存储
cd /data/volumes
mkdir v{1,2,3,4,5}
vim /etc/exports
/data/volumes/v1 192.168.81.0/24(rw,no_root_squash)
/data/volumes/v2 192.168.81.0/24(rw,no_root_squash)
/data/volumes/v3 192.168.81.0/24(rw,no_root_squash)
/data/volumes/v4 192.168.81.0/24(rw,no_root_squash)
/data/volumes/v5 192.168.81.0/24(rw,no_root_squash)
systemctl restart nfs
exportfs -arv
在node1上 showmount -e node2
在master上k8s把5个存储卷定义成pv
kubectl explain pv
kubectl explain pv.spec
kubectl explain pv.spec.nfs
vim pv-demo.yaml
apiVersion: v1
kind: PersistentVolume 资源类型 pv
metadata:
name: pv001 pv名
labels: pv标签
name: pv001
polity: fast
spec: pv规格
nfs: pv的存储类型nfs
path: /data/volumes/v1 nfs共享的目录
server: node2 nfs服务器地址
accessModes: ["ReadWriteMany","ReadWriteOnce"] pv读写模式 多路读写 单路读写
capacity: pv最大容量
storage: 1Gi 最大容量1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
name: pv002
polity: fast
spec:
nfs:
path: /data/volumes/v2
server: node2
accessModes: ["ReadWriteOnce"]
capacity:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv003
labels:
name: pv003
polity: fast
spec:
nfs:
path: /data/volumes/v3
server: node2
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 20Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv004
labels:
name: pv004
polity: fast
spec:
nfs:
path: /data/volumes/v4
server: node2
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 10Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv005
labels:
name: pv005
polity: fast
spec:
nfs:
path: /data/volumes/v5
server: node2
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 10Gi
创建
kubectl apply -f pv-demo.yaml
kubectl get pv
注:RECLAIM POLICY 回收策略 pvc绑定解除之后数据处理问题
创建pvc
vim pod-vol-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim 资源类型 pvc
metadata:
name: mypvc pvc名
namespace: default pvc名称空间要与使用该pvc的pod名称空间一致
spec: 规格
accessModes: ["ReadWriteMany"] 指定使用pv的方式 多路读写
resources: 要求的资源大小
requests: 要求
storage: 6Gi 大小6G存储
---
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-pvc
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes: 指定pod存储类型
- name: html pod存储名
persistentVolumeClaim: pod存储类型为pvc
claimName: mypvc 申请名,就是pvc名
创建pvc和pod
kubectl apply -f pod-vol-pvc.yaml
验证
kubectl get pv 可以看到一个pv被绑定了
kubectl get pvc 查看pvc
kubectl get pods -o wide
kubectl describe pods pod-vol-pvc
测试:
到nfs的 v5目录
echo test node v5 > index.html
然后在任一节点
curl 10.244.2.49
kubectl delete pods pod-vol-pvc 删除pod数据依然在