Using kubectl set image to update image of initContainer - kubernetes

Currently, to update a k8s deployment image, we use the kubectl set image command like this:
kubectl set image deployment/deployment_name container=url_to_container
While this command updates the URL used for the main container in the deployment, it does not update the URL for the initContainer also set within the deployment.
Is there a similar kubectl command I can use to update the initContainer to the same URL?

Since the accepted answer, the team developed the ability to set image for Kubernetes init containers. Using the same command, simply use the init container name for the container part of the command you supplied.
kubectl set image deployment/deployment_name myInitContainer=url_to_container
In case you want to update both container images in a single command, use:
kubectl set image deployment/deployment_name myInitContainer=url_to_container container=url_to_container

The documentation seems to suggests that only containers are concerned.
Maybe you could switch to kubectl patch ?
(I know it's more tedious...)
kubectl patch deployment/deployment_name --patch "{\"spec\": {\"template\": {\"spec\": {\"initContainers\": [{\"name\": \"container_name\",\"image\": \"url_to_container\"}]}}}}"

The snippet above is based on solution provided by #Hiruma. Therefore, here I removed the empty spaces on the json argument and added a namespace example.
I did that because I faced issues when I was writing a pipeline for drone.io.
kubectl patch deployment deployment_name -n namespace_name -p "{\"spec\":{\"template\":{\"spec\":{\"initContainers\":[{\"name\":\"deployment_name\",\"image\":\"url_to_container"}]}}}}"

Related

How to set/change container image of the kubernetes deployment using a single command?

Or in simple words, what is syntax of the kubectl set image?
Note:
kubectl provides set image command but it often confuses developers. I hope this question helps =)
We can use this format to easily remember how this command works:
kubectl set image deployment <deployment-name> <container-name>=<image-name>:<image-version>
For example:
kubectl set image deployment frontend simple-webapp=kodekloud/webapp-color:v1
Here in the above example, frontend is the deployment name, simple-webapp is the name of the container, kodekloud/webapp-color is the name of the image and finally, v1 is the image version.
P.S.
If this helps, do upvote!
Wanna connect, feel free to share an invite at Linkedin

How to create deployment without images and later on, add new images?

I'm new to k8s and I'm trying to learn how to setup deployments.
I'm searching for a way to create a new deployment without any images. Over time, I will add new (0 or more) images (and specify thier desired state). As I don't know what images the deployment will contain in advance, I can't use any existing configuration files.
Is it possible? If yes, how?
If it's possible, a command-line solution will be great.
If you want to start a single instance of nginx, you can do
$ kubectl run nginx --image=nginx
But it is not possible to create any deployments without image.
$ kubectl run demo --image=""
error: --image is required
If you want to edit your existing deployment, then you can run
$ kubectl edit deployments <deployment-name> -n <namespace>
You can also patch container with new image to existing deployments by running following command
$ kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"myapp","image":"newimage"}]}}}}'
To replace image of a containers in deployment, run
$ kubectl set image deployment/<deployment-name> <container-name>=<image>

kubectl set image error: arguments in resource/name form may not have more than one slash (kubernetes)

I want to deploy my project to the Kubernetes cluster. I want to deploy it by using command:
- kubectl set image deployment/$CLUSTER_NAME gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest
But here I get error :
It's a misleading error message.
Essentially instead of abcxyz/abcxyz:example you also need to specify the container name that the image should be assigned to so for example example=abcxyz/abcxyz:example.
It's quite complicated and misleading, I got to say. Public docs don't help much but the kubectl set image --help does.
The problem is that you might have MULTIPLE instances in the deployment. If you have only one you can do something like this (note that this works but it's not AS SPECIFIC as you might want):
# The part before = is the spec.template.spec.containers.name which is image's brother
kubectl set image deployments goliardiait-staging=gcr.io/goliardia-prod/goliardia-it-matrioska:2.12 --all
I'll update when I find what nails it. In your case:
kubectl set image deployment $CLUSTER_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest --all
- kubectl set image deployment/$CLUSTER_NAME $INSTANSE_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest
It is working with using command like this

Automated alternative for initiating a rolling update for a deployment

So in order to update the images running on a pod, I have to modify the deployment config (yaml file), and run something like kubectl apply -f deploy.yaml.
This means, if I'm not editing the yaml file manually I'll have to use some template / search and replace functionality. Which isn't really ideal.
Are there any better approaches?
It seems there is a kubectl rolling-update command, but I'm not sure if this works for 'deployments'.
For example running the following: kubectl rolling-update wordpress --image=eu.gcr.io/abcxyz/wordpress:deploy-1502443760
Produces an error of:
error: couldn't find a replication controller with source id == default/wordpress
I am using this for changing images in Deployments:
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
If you view the yaml files as source of truth then use a tag like stable in the yaml and only issue kubectl set image commands when the tag is moved (use the sha256 image id to actually trigger a rollout; the image names are matched like a string so updating from :stable to :stable is a noop even if the tag now points to a different image).
See updating a deployment for more details.
The above requires the deployment replica count to be set more then 1, which is explained here: https://stackoverflow.com/a/45649024/1663462.

Kubernetes rolling update for same image

Document of kubernetes says to do rolling update for a updated docker image. In my case I need to do rolling update for my pods using the same image. Is it possible to do rolling update of a replication controller for a same docker image?
In my experience, you cannot. If you try to (e.g., using the method George describes), you get the following error:
error: must specify a matching key with non-equal value in Selector for api
see 'kubectl rolling-update -h' for help.
The above with kubernetes v1.1.
Sure you can, Try this command:
$ kubectl rolling-update <rc name> --image=<image-name>:<tag>
If your image:tag has been used before, you may like to do following to make sure you get the latest image on kubernetes.
$ docker pull <image-name>:<tag>