Volume is the shared directory accessible to a plurality of containers in kubernetes Pod
Kubernetes offers numerous volume types, including: emptyDir, hostPath, NFS, GlusterFS, configMap, Cephfs
. 1, emptyDir
emptyDir type vloume in Pod assigned to be created, Kubernetes automatically assigns a directory on the node when the node, it is not necessary to specify the corresponding on the host node directory file, the directory initialization is empty, when the Pod from the node on being removed when the data in emptyDir will be permanently deleted
1. Edit the emptyDir.yaml
file
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web-demo-empty
spec:
replicas: 2
selector:
matchLabels:
app: app-demo-empty
template:
metadata:
labels:
app: app-demo-empty
spec:
containers:
- name: tomcat-demo
image: tomcat
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
volumeMounts:
# 将/mydata-data目录挂载到共享仓库
- mountPath: /mydata-data
name: datavol
- name: nginx-demo
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
# 将/mydata-data目录挂载到共享仓库
- mountPath: /mydata-data
name: datavol
# emptyDir是一个共享仓库(中央仓库)
volumes:
- name: datavol
emptyDir: {}
2. Performkubectl create
$ kubectl create -f emptyDir.yaml
deployment.extensions/web-demo-empty created
3. The first into tomcat-demo
the container to create a file
$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c tomcat-demo bash
$ touch /mydata-data/data.txt
$ ls /mydata-data/
data.txt
4. Go to the nginx-demo
vessel to see if there is data.txt
a file
$ kubectl exec -it web-demo-empty-9b5644c45-7bpn5 -c nginx-demo bash
$ ls /mydata-data/
data.txt
After tests proved empty emptyDir is a shared directory that allows multiple containers Pod is shared between the directory
2, hostPath
hostPath type Pod vloume is mounted in the directory or a file on the host, the host so that the container can be used for storage of the file system, but in the kubernetes, Pod are based schedule
to a different node in the dynamic scheduling node assembly on, when a Pod is up and on the current node by node hostPath
to store the file locally, the next time a scheduled start on another node, you can not use the files previously stored on the node.
1. Edit the hostPath.yaml
file
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web-demo-hostpath
spec:
replicas: 2
selector:
matchLabels:
app: app-demo-hostpath
template:
metadata:
labels:
app: app-demo-hostpath
spec:
containers:
containers:
- name: nginx-demo
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- mountPath: /mydata-data
name: datavol
volumes:
- name: datavol
hostPath:
path: "/data"
2. Performkubectl create
$ kubectl create -f hostPath.yaml
deployment.extensions/web-demo-hostpath created
3. The need to create at each node node /data
directory, and create a test file
$ mkdir /data
$ touch /data/test.txt
4. Check whether the container into the /mydata-data
mount to the host
$ kubectl exec -it web-demo-hostpath-7866c644c4-7f8fk bash
$ ls /mydata-data/
test.txt