1、init Container
通常我们希望在真正的应用启动之前去创建一些应用需要的文件、目录。在k8s资源中我们可以通过init container来实现。配置如下:
使用init container判断volume是否挂在成功,并切判断里卖弄是否写入文件
# cat << EOF > init-container.yaml
apiVersion: v1
kind: Pod
metadata:
name: init-pod
namespace: andriy-dang
labels:
app: myapp
spec:
volumes:
- name: emptydir
emptyDir: {}
containers:
- name: myapp-container
image: busybox
command: ["sh", "-c", "if [ -f /test/file ]; then sleep 10000; else exit 1;fi"]
volumeMounts:
- mountPath: /test
name: emptydir
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'echo "hello" > /test/file']
volumeMounts:
- mountPath: /test
name: emptydir
EOF
# 创建Pod之后Pod的状态会发生如下变化:
kubectl get pod -n andriy-dang --watch
NAME READY STATUS RESTARTS AGE
init-pod 0/1