Can I switch k8s context during running helm upgrade command?

8/26/2021

I have 6 shell aliases for switching context between k8s namespaces different k8s clusters. The contexts are set in kube config file.<br><br> An example of alias is like this:

alias kc11='kubectl config use-context cluster1-namespace-1'

And My question is:<br><br> If I run helm upgrade command that takes about 30 mins, can I switch k8s context during the upgrade process to don't impact running operation?<br><br> How helm works with k8s context? Does it load into runtime at the beginning or before every simple operation in helm upgrade?

Helm upgrade command looks like this:

helm upgrade my-app . --reuse-values --values values.yaml --timeout 1200s

As you see, I haven't to add -n argument to specify k8s namespace.

-- Jan Matějka
helm3
kubernetes
kubernetes-helm

1 Answer

8/26/2021

TL;DR: Yes, you can.


When you issue helm upgrade command, all relevant manifests are sent to the control plane. This happens almost instantaneously, and after all is sent, all the work on your side is done. You may notice, that even if you interrupt helm while running, eventually all the relevant pods, services etc. will be created.

You can also freely switch current context, and continue working while the resources are getting installed.


To prove my point:
I have two clusters configured:

$ kubectl config get-contexts 
CURRENT   NAME
*         cluster-1
          istio

Ass an example I'm going to install superset, as I know it will take some time to complete.

$ helm install superset superset/superset &
[1] 14291

<sup>& sends it to background, so I can issue more commands while helm does it's thing</sup>
Now, switching to another context

$ kubectl config use-context istio
Switched to context "istio".

helm is still working in the background

$ jobs
[1]+  Running                 helm install superset superset/superset &

After some time, helm finishes it's job, and throws info about sucessfull deployment

NAME: superset
LAST DEPLOYED: Thu Aug 26 13:55:03 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1. Get the application URL by running these commands:
  echo "Visit http://127.0.0.1:8088 to use your application"
  kubectl port-forward service/superset 8088:8088 --namespace default

[1]+  Done                    helm install superset superset/superset

Switching back to original context, and checking if all pods were created

$ kubectl config use-context cluster-1 
Switched to context "cluster-1".
$ kubectl get pods
NAME                               READY   STATUS      RESTARTS   AGE
superset-9cb794755-jwjls           1/1     Running     0          90s
superset-init-db-ndhlt             0/1     Completed   0          89s
superset-postgresql-0              1/1     Running     0          89s
superset-redis-master-0            1/1     Running     0          89s
superset-worker-66bb97f7ff-4ptq5   1/1     Running     0          90s

As you can see, all resources were deployed correctly, in the cluster-1context, in default namespace (as I didn't specified differently).

-- p10l
Source: StackOverflow