初始化容器(Init Container)是用来做初始化工作的,可以是一个或者多个,如果存在多个InitC,这些InitC会按照定义的顺序依次执行,只有所有InitC执行完毕后,主容器才会被启动。在一个Pod中,所有的容器都是共享数据卷和网络命名空间的,因此我们可以把一些初始化的操作交给InitC来完成。

示例

创建一个busybox的Pod

apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
initContainers:
- name: init-demo
image: busybox
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- wget -O /workdir/index.html http://www.baidu.com
volumeMounts:
- mountPath: /workdir
name: workdir
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
volumeMounts:
- mountPath: /workdir
name: workdir
volumes:
- name: workdir
emptyDir: {}

查看 Events

kubectl describe pod busybox

查看初始化操作的结果

kubectl exec -it busybox -- cat /workdir/index.html