一、emptyDir
emptyDir
emptyDir类型的volume在pod分配到node上时被创建,kubernetes会在node上自动分配 一个目录,因此无需指定宿主机node上对应的目录文件。这个目录的初始内容为空,当Pod从node上移除时,emptyDir中的数据会被永久删除。
emptyDir Volume主要用于某些应用程序无需永久保存的临时目录,多个容器的共享目录等。
apiVersion: v1
kind: Service
metadata:
name: redisinsight-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 8001
nodePort: 31888
selector:
app: redisinsight
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redisinsight
labels:
app: redisinsight
spec:
replicas: 1
selector:
matchLabels:
app: redisinsight
template:
metadata:
labels:
app: redisinsight
spec:
containers:
- name: redisinsight
image: redislabs/redisinsight:1.7.0
imagePullPolicy: IfNotPresent
volumeMounts:
- name: db
mountPath: /db
ports:
- containerPort: 8001
protocol: TCP
volumes:
- name: db
emptyDir: {}
二、hostPath
hostPath Volume为pod挂载宿主机上的目录或文件,
使得容器可以使用宿主机的高速文件系统进行存储。缺点是,在k8s中,
pod都是动态在各node节点上调度。
当一个pod在当前node节点上启动并通过hostPath存储了文件到本地以后,
下次调度到另一个节点上启动时,
就无法使用在之前节点上存储的文件。
所以需要指定pod创建在哪一个node上面,
使用nodeName可以指定这个pod在k8s-master节点上创建
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
nodeName: k8s-master
restartPolicy: OnFailure
containers:
- image: nginx
name: nginx-example
volumeMounts:
- mountPath: /test-nginx
name: test-volume
ports:
- containerPort: 80
hostPort: 31123
name: http
volumes:
- name: test-volume
hostPath:
path: /www/k8s
type: Directory