How to patch Kubernetes Daemonset

4/22/2021

I have an ongoing requirement to patch my nginx-ingress daemonset each time I wish to expose new TCP ports. I have reviewed the documentation and I cannot understand the correct kubectl patch syntax to perform the patch. An excerpt from the yaml follows:

spec:
    revisionHistoryLimit: 10
    selector:
      matchLabels:
        name: nginx-ingress-microk8s
    template:
      metadata:
        creationTimestamp: null
        labels:
          name: nginx-ingress-microk8s
      spec:
        containers:
        - args:
          - /nginx-ingress-controller
          - --configmap=$(POD_NAMESPACE)/nginx-load-balancer-microk8s-conf
          - --default-backend-service=ingress/custom-default-backend
          - --tcp-services-configmap=$(POD_NAMESPACE)/nginx-ingress-tcp-microk8s-conf
          - --udp-services-configmap=$(POD_NAMESPACE)/nginx-ingress-udp-microk8s-conf
          - --ingress-class=public
          - ' '
          - --publish-status-address=127.0.0.1
          env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
          image: k8s.gcr.io/ingress-nginx/controller:v0.44.0
          imagePullPolicy: IfNotPresent
          lifecycle:
            preStop:
              exec:
                command:
                - /wait-shutdown
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          name: nginx-ingress-microk8s
          ports:
          - containerPort: 80
            hostPort: 80
            name: http
            protocol: TCP
          - containerPort: 443
            hostPort: 443
            name: https
            protocol: TCP
          - containerPort: 10254
            hostPort: 10254
            name: health
            protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          resources: {}

I want to use kubectl patch to append another port definition under ports i.e.

          - containerPort: 1234
            hostPort: 1234
            name: my-port-1234
            protocol: TCP

Patching a config map was simple using:

kubectl patch configmap nginx-ingress-tcp-microk8s-conf -n ingress --type merge -p '{"data":{"1234":"namespace1/api-connect:1234"}}'

but I cannot understand how to amend the command to cope with the more complex update required for the Daemonset.

Any assistance gratefully received. Thanks

-- royneedshelp
kubectl
kubernetes
nginx-ingress

2 Answers

4/22/2021

Try this command:

kubectl patch ds/name --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports/1", "value":{"containerPort":8888,"name":"http"}}]'
-- XciD
Source: StackOverflow

4/23/2021

As already mentioned by David in the comment it is better to keep every change under version control.

But if you really need to do this, here is the command:

kubectl patch ds -n ingress nginx-ingress-microk8s-controller --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value":{"containerPort":1234,"name":"my-port-1234","hostPort":1234,"protocol":"TCP"}}]'

patch command is explained in k8s docs: update-api-object-kubectl-patch, and the json type patch details are explained in rfc6902.

-- Matt
Source: StackOverflow