Kubernetes JSON部署补丁定义

时间:2019-06-01 07:12:01

标签: kubernetes

我正在尝试使用以下命令修补部署。我试图通过传递Yaml的元数据标签定义中的现有值来在pod中设置环境变量。

修补命令

kubectl patch deployment deploytest3 --type=json -p='[{"op": "add", "path": "/spec/template/spec/containers/1/env/2","value": {"name": "CUSTOMERID","valueFrom": {"fieldRef": {"fieldPath": "metadata.labels['customerid']"}}}}]'

下面是我的Yaml供参考。(这是供参考,而不是原始内容)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "10"
  creationTimestamp: 2019-01-22T11:24:17Z
  generation: 10
  labels:
    app: my-cloud
  name: deploytest3
  namespace: default
  resourceVersion: "60076984"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/deploytest3
  uid: 41950c24-1e38-11e9-ba0c-42010a8000d6
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: my-cloud
      customerid: "gx0388d"
      customername: deploytest3
      environment: dev
      tier: backend
      version: 11.0-latest
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-cloud
        customerid: "gx0388d"
        customername: deploytest3
        environment: dev
        tier: backend
        version: 11.0-latest
    spec:
      containers:
      - args:
        - -c
        - /bin/aSoftMS --simple-media-server;/bin/core_find.sh
        command:
        - /bin/bash
        image: us.gcr.io/data/myms:11.0-latest
        imagePullPolicy: IfNotPresent
        name: ms
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /opt/latest/system_rw/
          name: deploytest3
      - args:
        - -c
        command:
        - /bin/bash
        image: us.gcr.io/latest/core:11.0-latest
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/CoreDumpGen
        livenessProbe:
          failureThreshold: 3
          httpGet:
            httpHeaders:
            - name: User-Agent
              value: DeploymentAdmin
            path: /livenessstatus
            port: 8443
            scheme: HTTPS
          initialDelaySeconds: 60
          periodSeconds: 15
          successThreshold: 1
          timeoutSeconds: 15
        env:
        - name: latest_CUSTOMER
          value: abc-test
        - name: latest
          value: abc
        image: us.gcr.io/latest/latest/core2:11.0-latest
        imagePullPolicy: IfNotPresent
        name: core2
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostname: deploytest3
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 60
      volumes:
      - name: bucket-auth
        secret:
          defaultMode: 256
          secretName: bucket-auth
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: 2019-01-22T11:24:17Z
    lastUpdateTime: 2019-01-22T11:24:17Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: 2019-01-22T11:24:17Z
    lastUpdateTime: 2019-05-31T13:49:03Z
    message: ReplicaSet "deploytest3-7ddbc9b9c4" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 10
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

下面是错误,我正在执行上面的共享命令

The Deployment "deploytest3" is invalid: spec.template.spec.containers[2].env[2].valueFrom.fieldRef.fieldPath: Invalid value: "metadata.labels[customerid]": error converting fieldPath:
 field label not supported: metadata.labels[customerid]

我正在环境变量中手动添加它。 我认为我没有使用json格式设置正确,有人可以帮我吗。

1 个答案:

答案 0 :(得分:0)

"fieldPath": "metadata.labels['customerid']"-不正确

显然,它应该是"fieldPath": "spec.template.metadata.labels['customerid']"

这不完全是kubectl patch,但是这也会从部署标签定义中设置环境变量

kubectl set env deployment/deploytest3 CUSTOMERID=$(kubectl get deployments deploytest3 -o jsonpath='{$.spec.template.metadata.labels.customerid}')