Why you should consider VS Code for your Kubernetes/Docker work

Developer Support

Premier Developer consultant Julien Oudot spotlights VS Code for Kubernetes and Docker workloads.


Visual Studio Code (VS Code) is sometimes considered as a slimmer and minimalist version of Visual Studio. However, depending on the technology stack used, VS Code can really be the platform of choice to benefit from the best features. Furthermore, its cross platform support allow users to have the same customer experience on multiple platforms (Windows, Linux and Mac OS).

With its modular architecture based on the concept of extensions, a lot can be done with Docker, Kubernetes or Helm just after only few extension installations. From simple YAML editor functionalities that save a lot of headaches dealing with indentation, to more advanced scenarios, like opening an interactive session inside a Docker Container running in a remote Kubernetes cluster.

The below tutorial will walk you through some of the convenient features provided by the following VS Code extensions:

On a Linux based OS, you need to be able to run docker commands from VS Code. To that aim, we need to have the current user added to the docker group. Run the two following commands (restart might be needed):

sudo groupadd docker
sudo usermod -aG docker $USER

1. Connect VS Code to Docker Hub account and Kubernetes cluster

To connect your Visual Studio Code to a Docker hub account, open Visual Studio Code and click on Settings at the bottom left.

kb1

Search for vsdocker and override the two settings by entering the two key-value pairs on the right-hand side:

"vsdocker.imageUser": "docker.io/<dockerHubUserName>",

“docker.defaultRegistryPath”:”<dockerHubUserName>”

kb2

Then, click on the Docker extension and click on Docker Hub to authenticate using the Docker ID and password set up in the previous task

kb3

Finally, you also need to authenticate from the terminal running inside VS Code. Click at the bottom of the window, then Terminal and enter the command that will prompt you for your docker credentials.

docker login

kb4

Before we can deploy a basic application to a Kubernetes cluster, we just need to make sure that we are connected to the right cluster. Type the following command

kubectl config current-context

kb5

If you are not connected to any Kubernetes cluster and have an AKS cluster running in Azure, you will need to run the following command.

az aks get-credentials --resource-group <AKS-RESOURCE-GROUP> --name <AKS-CLUSTER-NAME>

2. Deploy a NodeJS application to Kubernetes from VS Code

Download a basic Node JS application from https://github.com/joudot/nodejs.

From VS Code, click on Explorer and then Open Folder.

kb6

Select the folder nodejs that was just downloaded and click OK. You can see the basic Node JS application provided, as well as a Dockerfile to build its image.

kb7

Open command palette by clicking on the Settings icon and then Command Palette.

kb8

Type Kubernetes Run and select it. It will build the Node JS image, push it to Docker Hub and deploy the application into your AKS cluster.

kb9

You can follow these steps at the bottom of the IDE.

Once complete, click on the Kubernetes extension, then expand the cluster and click Workloads – Deployments – nodejs. You will see what is deployed in your cluster. The view will show you the YAML file that you can interpret, understand and reuse for other Kubernetes API objects to be deployed. More generally, VS Code is a user-friendly solution to work with YAML files since it helps with indentation, auto complete and coloration. There is also the kubectl explain integrated tool to annotate Kubernetes API objects and dynamically see documentation when hovering over YAML fields.

kb10

If you look under Services, you will see that the Node JS application is deployed but is not exposed through a service. To do this, open the VS Code Terminal and type

kubectl expose deployments/nodejs --port=80 --target-port=8080 --type=LoadBalancer

kb11

Note the target port that is 8080 because, as specified on the Dockerfile, we expect traffic to come through the port 8080 in the container.

Just like we did from the Linux terminal, we can follow the service creation from the VS Code terminal with the command

kubectl get svc

kb12

Once the service has been set up and the IP address has been created in Azure, you can refresh the cluster view and call the IP address from any browser.

kb13

kb14

3. Interact with deployed Pods and Containers from VS Code

Another convenient feature we can access from the Kubernetes clusters view, is to interact with the pods and containers. Right-click on the nodejs pod and Show Logs. It will show you the container logs. In our case, the Node JS application started successfully and is waiting for traffic on port 8080. Everything looks good!

kb15

kb16

You can also click Describe, which is equivalent to the kubectl describe pod command and will give you information on the pod status.

kb17

kb18

Finally, what we can do is open an interactive session from within the container for troubleshooting purpose. Right-click on the nodejs pod and click Terminal.

kb19

You can type the ls or cat server.js commands to see what is inside the container file system.

kb20

Open the Command Palette one last time and type Create. You will see that VS Code can help you to create Azure Container Registries, Helm Chart or even Kubernetes clusters. All of it without leaving the IDE.

kb21

0 comments

Discussion is closed.

Feedback usabilla icon