Skip to content

Instantly share code, notes, and snippets.

@coresolve
Created October 18, 2017 00:16
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save coresolve/364c80b817eb8d84bfb1c6e2c94d2886 to your computer and use it in GitHub Desktop.
Save coresolve/364c80b817eb8d84bfb1c6e2c94d2886 to your computer and use it in GitHub Desktop.
An example of using kubectl patch

Start with a simple deployment:

kubectl run simple --image=quay.io/dcooley/simple-app:plain --replicas=3 --port=80 --labels=app=simple

show the yaml for this deployment:

$ kubectl get deployment simple -o yaml --export
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: null
  generation: 1
  labels:
    app: simple
  name: simple
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/simple
spec:
  replicas: 3
  selector:
    matchLabels:
      app: simple
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: simple
    spec:
      containers:
      - image: quay.io/dcooley/simple-app:plain
        imagePullPolicy: IfNotPresent
        name: simple
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status: {}

Show the pods with the labels:

$ kubectl get pods -l app=simple --show-labels
NAME                      READY     STATUS    RESTARTS   AGE       LABELS
simple-1632802685-b3fff   1/1       Running   0          1m        app=simple,pod-template-hash=1632802685
simple-1632802685-cdns1   1/1       Running   0          1m        app=simple,pod-template-hash=1632802685
simple-1632802685-p2h3q   1/1       Running   0          1m        app=simple,pod-template-hash=1632802685

Make the patch:

$ kubectl patch deployment simple --type=json -p='[{"op": "add", "path": "/spec/template/metadata/labels/this", "value": "that"}]'
deployment "simple" patched

Show that the pods have the new label:

$ kubectl get pods -l app=simple --show-labels
NAME                      READY     STATUS    RESTARTS   AGE       LABELS
simple-2157335326-85pzk   1/1       Running   0          25s       app=simple,pod-template-hash=2157335326,this=that
simple-2157335326-g7jwv   1/1       Running   0          27s       app=simple,pod-template-hash=2157335326,this=that
simple-2157335326-qmcjk   1/1       Running   0          27s       app=simple,pod-template-hash=2157335326,this=that

Show the new deployment:

$ kubectl get deployment simple -o yaml --export
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "2"
  creationTimestamp: null
  generation: 1
  labels:
    app: simple
  name: simple
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/simple
spec:
  replicas: 3
  selector:
    matchLabels:
      app: simple
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: simple
        this: that
    spec:
      containers:
      - image: quay.io/dcooley/simple-app:plain
        imagePullPolicy: IfNotPresent
        name: simple
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status: {}
@cricketfan5
Copy link

Hi, Is there a way to patch the daemonset by updating the created timestamp with todays data?

@RezhaBlue
Copy link

For cricketfan5 and anyone else who visits this in the future:

kubectl patch deployment web -p
"{"spec":{"template":{"metadata":{"annotations":{"date":"date +'%s'"}}}}}"

or

kubectl patch deployment mydeployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"mycontainer","env":[{"name":"LAST_MANUAL_RESTART","value":"'$(date +%s)'"}]}]}}}}'

@grdnrio
Copy link

grdnrio commented May 13, 2019

Is there a way to add an additional container arg to a running daemonset. For example:

containers:
        - name: example
          image: example/monitor:2.1.1
          args:
            ["-a", "somevalue", "-b", "somevalue", "-c", "somevalue",]

kubectl patch daemonset example -p '"spec": {"template": {"spec": {"containers": [{"name": "example","args": ["-d","somevalue"],}],}}}}'

@marcellodesales
Copy link

marcellodesales commented Jan 17, 2020

@grdnrio, you can add an element at a given position on the args by updating the deployment... For instance, adding an argument at the first container of a given deployment by adding a new argument at the 3rd position (0-indexed)...

kubectl patch deployment external-dns --type='json' \
        -p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/2", "value": "--source=istio-gateway" }]

@scheung38
Copy link

What is the difference between kubectl patch vs helm upgrade

@cslinuxfr
Copy link

cslinuxfr commented Jul 27, 2021

@marcellodesales
Thx mate for the tips, I've handle to patch a container command like this:

kubectl patch deployment -n kube-system cluster-autoscaler --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/command/1", "value": "--balance-similar-node-groups"}]'

kubectl patch deployment -n kube-system cluster-autoscaler --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/command/1", "value": "--skip-nodes-with-system-pods=false"}]'

here the result:

k get deployments.apps -n kube-system cluster-autoscaler -o yaml| egrep "^command|balance|system-pods"
- --skip-nodes-with-system-pods=false
- --balance-similar-node-groups

@ilyesAj
Copy link

ilyesAj commented Jan 26, 2022

add a new port named metrics to an existing deployment :
kubectl patch deployment internal-nginx-ingress-controller -n kube-system --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value": {"containerPort": 10254,"name": "metrics"}}]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment