天天看點

k8s如何修改annotate注釋

作者:MacLex

雖然“kubectl annotate”将直接在對象上設定注釋,但它不會在部署或Daemonset的更深嵌套的pod模闆上設定注釋。如果您想在不修改完整清單的情況下在pod模闆(.spec.template.metadata.annotations)上快速設定注釋,您可以使用“更新檔”指令。舉個簡短的例子:

或者有時候你改了deployment又發現Pod的annotate始終沒有變化,那麼這個文檔可能會幫到你

首先拉去鏡像,建立Deployment在特定的namespace裡面

如下

  • 拉取鏡像
  • 建立namespace
  • 應用deployment
docker pull nginx:latest 
# create deployment
kubectl create ns lex
namespace/lex created
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  name: nginx-app
  namespace: lex
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: nginx-app
        ports:
        - containerPort: 80
          protocol: TCP
      restartPolicy: Always
```
kubectl apply -f nginx.yaml
deployment.apps/nginx-app created           

現在檢視下狀态及對應的annotations

檢視目前的 annotation

kubectl get deployment nginx-app -o=jsonpath="{.metadata.annotations}" -n lex|jq

➜  ~ kubectl get deployment nginx-app -o=jsonpath="{.metadata.annotations}" -n lex|jq
{
  "deployment.kubernetes.io/revision": "1",
  "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{\"deployment.kubernetes.io/revision\":\"1\"},\"name\":\"nginx-app\",\"namespace\":\"lex\"},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"app\":\"nginx-app\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"nginx-app\"}},\"spec\":{\"containers\":[{\"image\":\"nginx:latest\",\"imagePullPolicy\":\"Always\",\"name\":\"nginx-app\",\"ports\":[{\"containerPort\":80,\"protocol\":\"TCP\"}]}],\"res           

Pod模闆的注釋為空

~ kubectl get deployment nginx-app -o=jsonpath="{.spec.template.metadata.annotations}" -n lex           

現在,讓我們在部署中使用“注釋”指令。請注意,部署本身的注釋被修改了,但pod模闆注釋沒有修改。

kubectl annotate deployment nginx-app "splunk_id:lextest" -n lex
deployment.apps/nginx-app annotated           

檢視下結果

➜  ~ kubectl get deployment nginx-app -o=jsonpath="{.metadata.annotations}" -n lex |jq
{
  "deployment.kubernetes.io/revision": "1",
  "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{\"deployment.kubernetes.io/revision\":\"1\"},\"name\":\"nginx-app\",\"namespace\":\"lex\"},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"app\":\"nginx-app\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"nginx-app\"}},\"spec\":{\"containers\":[{\"image\":\"nginx:latest\",\"imagePullPolicy\":\"Always\",\"name\":\"nginx-app\",\"ports\":[{\"containerPort\":80,\"protocol\":\"TCP\"}]}],\"restartPolicy\":\"Always\"}}}}\n",
  "splunk_id": "lextest"
}           

但pod模闆的注釋仍然為空

~ kubectl get deployment nginx-app -o=jsonpath="{.spec.template.metadata.annotations}" -n lex           

要修改更深的pod模闆注釋,請使用“更新檔”

這将在pod模闆上設定深度注釋,根本上是應用字段的問題

kubectl patch deployment nginx-app -p '{"spec": {"template":{"metadata":{"annotations":{"sidecar.istio.io/inject":"false"}}}} }' -n lex           

現在pod模闆成功顯示注釋!

➜  ~ kubectl get deployment nginx-app -o=jsonpath="{.spec.template.metadata.annotations}" -n lex|jq
{
  "sidecar.istio.io/inject": "false"
}
也可以直接檢視這個Pod的yaml
➜  ~ kubectl get pod nginx-app-c9cffdc55-dmvlg -n lex -o yaml|more
apiVersion: v1
kind: Pod
metadata:
  annotations:
    sidecar.istio.io/inject: "false"           

是以結論就是可以通過

Path方式來給deployment 對應的spec.template 應用annotation

如果您想删除pod模闆注釋,則不能像以前那樣使用更新檔,因為它使用的合并政策隻能附加。但是,如果您使用jsonpatch規範,那麼您可以指定“删除”操作。

➜ ~ kubectl patch deployment nginx-app --type=json -p='[{"op":"remove","path":"/spec/template/metadata/annotations/sidecar.istio.io~1inject"}]' -n lex
deployment.apps/nginx-app patched           

在此檢視下Pod模闆注釋現在又為空了

➜ ~ kubectl get deployment nginx-app -o=jsonpath="{.spec.template.metadata.annotations}" -n lex|jq           

要删除部署,請執行以下操作:我這個比較暴力直接删除namespace

➜ ~ kubectl delete ns lex
namespace "lex" deleted           

# Other

如果你調整了對應的deployment的字段,還是不能生效

嘗試檢視下replicas

kubectl get replicaset -n lex

可能能幫到你

kubectl scale deployment nginx-app --replicas=1 -n lex

控制Pod的replicaset

https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/replicaset/

k8s如何修改annotate注釋

繼續閱讀