How to change image of a pod in a running kubernetes cluster (pod without deployment)

8/19/2021

I have a running kubernetes cluster for magento2. It has a pod for elasticsearch (you can see code here ).

I need to change the image from elasticsearch:7.9.0 to a custom image.

Since using kustomize, I tried using a patch:

deploy/es-swap:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
  name: es_swap

bases:
  - ../step-4

patchesJson6902:
- path: patch/es.json
  target:
    group: elasticsearch.k8s.elastic.co
    version: v1
    kind: Elasticsearch
    name: elasticsearch

deploy/es-swap/es.json:

[
    {
      "op": "replace",
      "path": "/spec/nodeSets/0/podTemplate/spec/containers/0/name",
      "value": "mydockerhubuser/elastic:v7.9.0_custom"
    }
  ]
  

I deployed to patch like this:

$ kustomize build deploy/es_swap | kubectl apply -f -
serviceaccount/ingress-nginx unchanged
serviceaccount/ingress-nginx-admission unchanged
:
elasticsearch.elasticsearch.k8s.elastic.co/elasticsearch configured

but it didn't actually work since it is still using previous image:

$ k describe pod elasticsearch-es-elasticsearch-0 | grep image
  Normal   Pulled                  25m   kubelet                  Container image "docker.elastic.co/elasticsearch/elasticsearch:7.9.0" already present on machine

When checking output of kustomize build deploy/es_swap I see that mydockerhubuser/elastic:v7.9.0_custom is image of the elasticsearch container. So I don't understand why it is not swapped.

Another option that I was thinking about is that instead of changing the image, I can add initContainers, as explained here. I am not entirely sure how to do that.

I tried checking rollout and kubernetes patch, but I only see examples that apply them to deployments.

What is the way to change the image of this running pod?

-- justadev
elasticsearch
kubernetes

1 Answer

8/20/2021

kubernetes does not allow changing image in non-managed pods. that's just it.

if you need to rely on simple pods,you need to delete and recreate the resource with your changes.

however, you can create a deployment matching the current labels of your pod, which will make that pod managed by a deployment. then you are able to change the image in the deployment, leading to a pod with the updated image being created

-- meaningqo
Source: StackOverflow