[英]Update deployment labels using “kubectl patch” does not work
I am trying to update a label using kubectl. 我正在尝试使用kubectl更新标签。
When I use apply
it works but it doesn't when doing a patch
. 当我使用
apply
它可以工作,但打patch
时却不能。
I tried kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)"
; 我尝试了
kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)"
; it returns back no change
where I would expect to get back a label change. 它不返回
no change
,而我希望该更改可以返回标签更改。
These are the only changes on my yaml. 这些是对我的Yaml的唯一更改。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: testLab
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: helloWorld
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 80
Is there a restriction on what patch updates or it am I doing something wrong? 补丁更新有限制吗,或者我做错了什么?
I also tried specifying --type strategic
and other types but none seem to work. 我也尝试指定
--type strategic
和其他类型,但似乎都没有用。
After executing command kubectl patch on your second file (where you changed label) you should see following error: 在第二个文件(更改了标签的位置)上执行命令kubectl patch后 ,您应该看到以下错误:
Error from server: cannot restore map from string
After executing command kubectl apply on this file you should get following error : 在命令kubectl应用于此文件后,您将得到以下错误:
error: error validating "nginx.yaml": error validating data: ValidationError(Deployment.metadata): unknown field "label" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta; if you choose to ignore these errors, turn validation off with --validate=false
Your deployment file should looks like this: 您的部署文件应如下所示:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: helloWorld
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 8
You missed to add space after app label. 您错过了在应用标签后添加空格的功能。
Add space and then execute command kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)"
once again. 添加空间,然后再次执行命令
kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)"
。
Here are useful documentations: labels-selectors , kubernetes-deployments , kubernetes-patch . 这里是有用的文档: 标签选择器 , kubernetes-deployments , kubernetes-patch 。
You should be having something like this in your metadata: 您应该在元数据中包含以下内容:
metadata:
name: nginx-deployment
labels:
label: testLabel2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.