我一直在使用init-containers,因为它们变得可用并且发现它们非常有用。我的核心映像(下面是web-dev)没有太大变化,但我的初始容器映像(下面是web-data-dev)确实经常发生变化。
init-container使用带有版本号的容器映像。我将此版本号更改为最新值,然后执行 kubectl apply -f deployment.yaml
例如,我将 eu.gcr.io/project/web-data-dev:187 更改为 eu.gcr.io/project/web-data-dev:188 < / strong>在运行kubectl之前。
但是,当我这样做时,不会发生部署,如果我对init-container使用的映像进行任何更改,部署仍然不会发生。我认为这是因为没有检测到init-container更改。
然后我尝试在图像字段中添加一些垃圾,如下所示:&#34;图像&#34;:&#34; thisIsNotAnImage&#34; 并再次运行kubectl apply -f ,但仍未应用更新。
我的问题是 - 如何让kubectl应用-f检测init-container中的图像标签更改?我做错了什么,这是一个错误,还是因为初始容器是Alpha?
,这还没有实现完整部署YAML如下。
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
app: web
tier: frontend
annotations:
pod.alpha.kubernetes.io/init-containers: '[
{
"name": "initialiser1",
"image": "eu.gcr.io/project/web-data-dev:187",
"command": ["cp", "-r", "/data-in/", "/opt/"],
"volumeMounts": [
{
"name": "file-share",
"mountPath": "/opt/"
}
]
}
]'
spec:
containers:
- image: eu.gcr.io/project/web-dev:20
name: web
resources:
requests:
cpu: 10m
memory: 40Mi
ports:
- containerPort: 80
name: http
- containerPort: 443
name: https
volumeMounts:
- name: file-share
mountPath: /opt/
volumes:
- name: file-share
emptyDir: {}
答案 0 :(得分:6)
如果您使用的是Kubernetes 1.4,请尝试将pod.alpha.kubernetes.io/init-containers
更改为pod.beta.kubernetes.io/init-containers
。
我无法在GitHub上找到正确的问题,但这两个注释的行为是不同的。我可以使用第二个进行kubectl apply -f
,部署将会更新。
您可以使用以下示例进行测试:
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nginx
spec:
template:
metadata:
labels:
app: nginx
annotations:
pod.beta.kubernetes.io/init-containers: '[
{
"name": "install",
"image": "busybox",
"command": ["/bin/sh", "-c", "echo foo > /work-dir/index.html"],
"volumeMounts": [
{
"name": "workdir",
"mountPath": "/work-dir"
}
]
}
]'
spec:
volumes:
- name: workdir
emptyDir: {}
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: workdir
mountPath: /usr/share/nginx/html
尝试将foo
更改为bar
并查看结果:
$ cat nginx.yaml | kubectl apply -f -
deployment "nginx" created
$ curl $(minikube service nginx --url)
Waiting, endpoint for service is not ready yet...
foo
$ cat nginx.yaml | sed -e 's/foo/bar/g' | kubectl apply -f -
deployment "nginx" configured
$ curl $(minikube service nginx --url)
Waiting, endpoint for service is not ready yet...
bar
使用pod.alpha.kubernetes.io/init-containers
同样的事情:
$ curl $(minikube service nginx --url)
Waiting, endpoint for service is not ready yet...
foo
$ cat nginx.yaml | sed -e 's/foo/bar/g' | kubectl apply -f -
deployment "nginx" configured
$ curl $(minikube service nginx --url)
foo