一、特征

优先级最高,先于其他容器启动,主要做一些初始化配置,如下载配置文件、注册信息、证书等

二、示例

在初始化容器中把init container test写入到/work_dir/index.html下,并把/work_dir挂载到/usr/share/nginx/html,

那么当访问Nginx首页时显示的内容为init container test

K8S初始化容器init container_kubernetes
K8S初始化容器init container_nginx_02

三、yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: init-demo
name: init-demo
spec:
replicas: 1
selector:
matchLabels:
app: init-demo
template:
metadata:
labels:
app: init-demo
spec:
initContainers:
- name: init-container
image: busybox
imagePullPolicy: IfNotPresent
command: ["sh"]
args:
[
"-c",
"echo 'init container test' >/work_dir/index.html",
]
volumeMounts:
- name: workdir
mountPath: "/work_dir"
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: web
ports:
- containerPort: 80
volumeMounts:
- name: workdir
mountPath: /usr/share/nginx/html
volumes:
- name: workdir
emptyDir: {}