标签:支持 mount pat 运行 共享 usr shell 工作 远程服务
Init Container的应用场景当我们在运用一个服务之前,通常会做一些初始化的工作,而这些工作一般只需要运行一次,成功后就不再运行。为此kubernetes 引入了Init Container,用于在启动应用容器之前启动一个或多个“初始化”容器,完成应用容器的所需的预制条件。
Init Containers与常规的容器非常类似,但是它一些独有的特征:
Init Container 通常有如下应用方式:
具体的应用场景示例:
这里定义一个nginx,在nginx容器启动前更改默认起始页面内容:
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
initContainers:
- name: init-myservice
image: busybox
command: [‘sh‘, ‘-c‘, ‘echo "this init-container test page" > /html/index.html‘]
volumeMounts:
- name: index-dir
mountPath: "/html"
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: index-dir
mountPath: /usr/share/nginx/html
volumes:
- name: index-dir
emptyDir: {}
创建容器后,可以看到,先执行初始化操作:
# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/1 0 12s
# 显示正在初始化:
# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 PodInitializing 0 22s
# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myapp-pod 1/1 Running 0 38s 10.2.74.5 10.0.0.3
#日志信息:
# kubectl describe pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 29s default-scheduler Successfully assigned myapp-pod to 10.0.0.3
Normal SuccessfulMountVolume 29s kubelet, 10.0.0.3 MountVolume.SetUp succeeded for volume "index-dir"
Normal SuccessfulMountVolume 29s kubelet, 10.0.0.3 MountVolume.SetUp succeeded for volume "default-token-hmvnc"
Normal Pulling 28s kubelet, 10.0.0.3 pulling image "busybox"
Normal Pulled 16s kubelet, 10.0.0.3 Successfully pulled image "busybox"
Normal Created 16s kubelet, 10.0.0.3 Created container
Normal Started 16s kubelet, 10.0.0.3 Started container
Normal Pulling 15s kubelet, 10.0.0.3 pulling image "nginx"
Normal Pulled 1s kubelet, 10.0.0.3 Successfully pulled image "nginx"
Normal Created 1s kubelet, 10.0.0.3 Created container
Normal Started 1s kubelet, 10.0.0.3 Started container
Pod中的每个应用程序和Init Container的名称必须是唯一的; 任何Container与另一个Container共享一个名称都会引发验证错误。
在Pod重新启动时, init Container 将会重新运行,那么所执行的初始化操作也会再次执行,这就要求Init Container的操作是可以重复执行的。例如上面的示例中,在对挂载目录中文件的添加前,可以先判断文件是否已经存在的处理,来防止出错。
常见的Pod重启场景如下:
标签:支持 mount pat 运行 共享 usr shell 工作 远程服务
原文地址:http://blog.51cto.com/tryingstuff/2130997