Azure Kubernetes Service - How to attach a Kubernetes Pod using kubectl and Visual Studio Code Kubernetes Tool

azure cloud kubernetes pod container workload kubectl vscode kubernetes-tool devops

The intention of this post is to explain how a Kubernetes Pod, running at an Azure Kubernetes Service, can be attached using kubectl and the Visual Studio Code Kubernetes Tool

cover

1. Introduction

There are many reasons why a deployed Kubernetes pod needs to be attached at runtime. Therefore I’d like to explain, how this can be achieved by applying dedicated kubectl commands and by using the proper Kubernetes extension in Visual Studio Code.

2. Attaching the Kubernetes Pod using kubectl

I’m going to start with kubectl, which is capable of conducting several commands for your Kubernetes cluster. For more details with regard to kubectl, see following link below:

kubernetes.io - Tools

Ensure that your kubeconfig file is up to date. For instance: if your Kubernetes cluster is newly provisioned - use following command for providing the credentials to your kubeconfig file:

az aks get-credentials --name <K8s Cluster> --resource-group <Resource Group>

Switching to my Kubernetes cluster (named “patricks-aks”) in the Azure Portal, reveals that there a no running jobs at the moment:

01_azure_k8s_cluster_no_jobs

This will change after conducting the first kubectl command. I’d like to deploy a workload from type “job” (see kubernetes.io - Workloads - Job). The workload is defined in a YAML file, which sets the deployment rules. Among others, the YAML file refers to a Container Image, which will run within the Kubernetes Pod. I’ve created a quite simple Dockerfile, which corresponding Container Image will be used for that example. It’s a simple Linux based Container, which just prints “Hi, I’m your Linux Container!”. I’m opening the YAML file with Visual Studio Code and would like to deploy the workload. For that, following command is applied, which is typed in the Terminal of Visual Studio Code:

kubectl apply -f deployMyLinuxContainer.yml
02_job_created

After conducting the command, the job should be created. I’m going to prove that by switching back to the Azure Portal and observing again the “Job” section of the Kubernetes cluster.

As expected, the workload named “mylinuxcontainer” is running.

03_aks_workload_running

By clicking at “mylinuxcontainer” I’ll be linked to the containing Kubernetes Pod, which name is extened by an additional string, the full name is “mylinuxcontainer–1-824ph”.

03_aks_k8s_pod_running

By conducting following command, the running Pods should be listed in the Terminal:

kubectl get pods

This reveals the same running Kubernetes Pod (among others), with the name “mylinuxcontainer–1-824ph”.

03_vs_code_kubectl_get_pods

I’m copying the name of the Pod and enter following command for attaching it:

kubectl exec -it mylinuxcontainer--1-824ph -- bash

Afterwards, you’re attached to the Pod and you can start with exploring the content of the file system.

04_vs_code_exec

In my case, I’m going to execute the “start.sh” script, which prints “Hi, I’m your Linux Container!”.

05_vs_code_file_system_attached_print

So, that would be a common approach for attaching a running Kubernetes Pod using kubectl. In the next section, I’d like to show how to attach in a different way - by using an extension of Visual Studio Code.

3. Attaching the Kubernetes Pod by using Visual Studio Code

This section is about using the Kubernetes extension of Visual Studio Code for attaching to the running Pod. For that, I’m again deploying a workload (because the previous one already completed) by conducting the kubectl command again (ensure that the previous workload with the same name is deleted or to use a different name for the workload):

01_deploy_workload_second

In contrast to using kubectl, I’m starting with selecting the Kubernetes icon on the left - for installing the extension see: https://code.visualstudio.com/docs/azure/kubernetes, respectively marketplace.visualstudio.com - vscode-kubernetes-tools

02_choose_k8s_extension

This provides a list of all Kubernetes cluster, which are defined in you kubeconfig file. The current cluster in that case is “patricks-aks”:

03_select_cluster

After expanding it, I’m going to select “Job” and “mylinuxcontainer” for getting to the Pod:

04_expand_jobs

Now, I’ll perform a right click at the Kubernetes Pod and I’ll choose “Terminal”:

05_select_terminal

This results to the same result comparing with using kubectl: the file system of the Container can be accessed:

06_run_start_bash

Again I can switch to the “scripts” directory and execute the same “start.sh” file, which again prints “Hi, I’m your Linux Container!”.

4. Video Demo: kubectl and Visual Studio Code

Attaching the Kubernetes Pod by using kubectl and the Visual Studio Code extension, can be seen in following video demo (does not contain an audio track):

5. Conclusion

This post showed two fast ways how a Kubernetes Pod can be attached. You can use kubectl if you feel more comfortable by using the command line. Visual Studio Code provides a great extension for managing your Kubernetes cluster(s) and by simply selecting your Pod by expanding the proper workload type section.

6. References

kubernetes.io - Tools

kubernetes.io - organize-cluster-access-kubeconfig

https://kubernetes.io - cheatsheet - kubectl-apply

https://kubernetes.io - cheatsheet - interacting-with-running-pods

code.visualstudio.com - azure - kubernetes

marketplace.visualstudio.com - vscode-kubernetes-tools