How to patch statefulset on kubernetes cluster and set imagePullPolicy

4/11/2019

Need to understand exactly how patch works. How could I patch "imagePullPolicy" for instance. Could someone explain in simple details how patch works.

kubectl patch statefulset my-set -p '{"spec":{"containers":{"imagePullPolicy":"IfNotPresent"}}}'

This is not working what is wrong with it?

-- Janis Karklins
kubernetes
patch

2 Answers

4/11/2019

In addition to @Colwins answer, you should also add mandatory key name into container spec, otherwise you'll get does not contain declared merge key: name

So, you kubectl command should look like:

kubectl patch statefulset my-set -p '{"spec": {"template": {"spec":{"containers":[{"name":"nginx","imagePullPolicy":"Never"}]}}}}'
-- A_Suh
Source: StackOverflow

4/11/2019

I think you are missing the template key in your command

kubectl patch statefulset my-set -p '{"spec": {"template": {"spec":{"containers":[{"name": "xxxxxxx", "imagePullPolicy":"IfNotPresent"}]}}}}'

The stateful set yaml looks something like this

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

So the path to the containers field is

spec >> template >> spec >> containers

-- Colwin
Source: StackOverflow