By initializing the container, you can also easily do something similar to poststart
However, it should be clearly distinguished that the target of initializing the container is pod itself, while the target of poststart is container
In most cases, the two methods can be interchanged
apiVersion: v1
kind: Pod
metadata:
name: init-demo
labels:
app: init
spec:
initContainers:
- name: init
image: busybox
command:
- wget
- "-O"
- "/tmp/index.html"
- https://studyk8s.com
volumeMounts:
- name: initdir
mountPath: "/tmp"
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: initdir
mountPath: /usr/share/nginx/html
volumes:
- name: initdir
emptyDir: {}
The init container is very similar to an ordinary container, except for the following two points:
- Init container always runs to completion (for example, execute WGet, curl, Ping and other commands)
- If there are multiple init containers, the operation must be ended before the next startup
If the init container in the pod fails to start, kubernetes will continue to restart the pod until the init container succeeds. If the restart policy value corresponding to the pod is never (no restart as long as you exit), it will not restart.