一. 本地卷
• Kubernetes中的Volume提供了在容器中挂载外部存储的能力。
• Pod设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume。
**卷来源: **
① 本地卷: emptyDir、hostpath
② 网络卷:NFS、cephfs ....
1. emptyDir:
创建一个空卷,挂载到Pod中的容器。Pod删除该卷也会被删除。应用场景:Pod中容器之间数据共享
# cat emptyDir-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: emptydir-pod
spec:
containers:
- name: write
image: centos
command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
volumeMounts:
- name: data
mountPath: /data
- name: read
image: centos
command: ["bash","-c","tail -f /data/hello"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
# kubectl create -f emptyDir-pod.yaml
# kubectl log emptydir-pod -c read
2. hostpath
挂载Node文件系统上文件或者目录到Pod中的容器。应用场景:Pod中容器需要访问宿主机文件
# cat hostpath-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: hostpath-pod
spec:
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep 36000
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
hostPath:
path: /tmp
type: Directory
# kubectl create -f hostpath-pod.yaml
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
hostpath-pod 1/1 Running 0 2m49s 172.17.31.6 10.40.6.210 <none>
###到10.40.6.210 Node创建一个/tmp/test.txt
# echo 'test' > /tmp/test.txt
# kubectl exec -it hostpath-pod sh
/ # cat /data/test.txt
test
二. 网络卷
1. NFS 网络存储
要挂载NFS共享目录的Node节点必须安装nfs客户端: yum install nfs-utils -y
NFS服务端 10.40.6.214 安装:yum install nfs-utils -y
服务端配置并启动守护进程:
# cat /etc/exports
/data/nfs *(rw,no_root_squash)
# mkdir /data/nfs -p
# systemctl start nfs
yaml配置文件:
# cat nfs-pod.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
volumes:
- name: www
nfs:
server: 10.40.6.214
path: /data/nfs
# kubectl create -f nfs-pod.yaml
deployment.apps/nginx-deployment created
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-759cdcdc87-sz88v 1/1 Running 0 74s
nginx-deployment-759cdcdc87-w8f6l 1/1 Running 0 74s
###到 10.40.6.214 /data/nfs 目录下创建index.html
# cat index.html
<h1>hello world!</h1>
###进入容器查看index.html
# kubectl exec -it nginx-deployment-759cdcdc87-sz88v bash
[root@nginx-deployment-759cdcdc87-sz88v nginx]# cat /usr/share/nginx/html/index.html
<h1>hello world!</h1>
网友评论