Photo by Dustin Lee on Unsplash

Get Kubernetes source code up and running using VS Code and a remote VM!

A walk-through on how to use cloud VMs and the Visual Studio Code Remote extension to setup a Kubernetes development environment from scratch.

--

You may be a new contributor, just want a quick and isolated environment, or simply don't want to have loads of things installed in your machine that you only need whilst coding. In any of those scenarios, you could benefit by running your entire development environment on a cloud virtual machine and use the VS Code Remote extension to make it as you were running it all locally.

Here's what the end state will look like:

mac icon made by Freepik from www.flaticon.com

Note that the local machine can be MacOs, Linux or Windows. The development machine on this case will be Linux Ubuntu.

0. Create a Virtual Machine

I am not going to spend much time on this as the three main cloud providers have loads of instructions on how to create linux VMs in azure, gcp and aws.

The steps below assumes you have a VM, you know its public IP and have your SSH keys setup so you can get into it.

For the purposes of this post the virtual machine is in Google Cloud. However, nothing prohibits you from using VMs on other cloud providers or even containers hosted somewhere else.

1. Setup the local machine

Visual Studio Code

Install Visual Studio Code in your actual machine. We will use this IDE as the main bridge between your machine and the actual development machine.

Install Go Extension

Install the ms-vscode.go extension. You can use the VS Code Quick Open shortcut (Ctrl+P), paste the following command, and press enter.

ext install ms-vscode.go

Don't forget to click install:

Install VS Code Remote Extension Pack

Install the vs-code-remote-extension pack. Same here, Ctrl+P:

ext install ms-vscode-remote.vscode-remote-extensionpack

Note that there are a few variations of this extension. I prefer using the pack as it includes in a single extension the ability to remote into: Containers, WSL and SSH Server (what we will be using).

Connect to the Remote VM

Once again use Ctrl+P and type the command:

> Remote-ssh Add New SSH Host

Then fill up the username and IP/FQDN for the virtual machine:

In the format below:

ssh myusername@IP

Select your local ssh configuration option, if in doubt, it is probably the first one:

To open the Remote Explorer, click on the new icon that appeared on the left bar when you installed the Remote Development extension. Alternatively, use Ctrl+P:

> View: Show Remote Explorer

The remote explorer should display have the virtual machine IP inside the SSH Targets section. Right-click on the host and select Connect to Host in Current Window.

Confirm the fingerprint of the virtual machine:

Once connected, you will start having access to the remote machine. When you try to open a folder (Ctrl+K Ctrl+O) by default it will show the folders from the VM:

The same will happen when opening the terminal (Ctrl+`):

2. Setup the Virtual Machine

Now that we have the remote machine connected, we can start setting up the Kubernetes environment.

I have created the script below that should install all the requirements inside the virtual machine. This script is for Ubuntu, so if you are using a different distro you may need to make a few changes to it.

Note that the script defines the GO version and repo/fork location, so adjust them accordingly:

To simply download and execute, use the VS Code Terminal (Ctrl+`) to execute the commands below:

wget https://gist.githubusercontent.com/pjbgf/a8e02c81a9af62af3ca9b06196d84c70/raw/cbfb440e5298c99907c3891b1cfd733e6d7fb510/setup-kubernetes-contributors-developmroment.sh -O setup.shchmod +x setup.sh
./setup.sh
# to also install crio add a --crio at the end of the line above

Without crio, this generally takes around 3 minutes on a n1-standard-4 (4 vCPUs, 15 GB memory) in a Google Cloud VM

Building the source code

All dependencies and source code should be properly installed in the virtual machine. Now you can simply reload the profile, this will load the changes on PATH and the new GOPATH environment variables.

Enter the project folder and compile it:

source ~/.profile
cd $GOPATH/src/k8s.io/kubernetes
make

This should take about 8 minutes on the same spec as mentioned above.

3. Optimising Visual Studio Code

To get the most out of VS Code, we need to make a few changes to its default settings.

Go: Install/Update Tools

There are several go tools that need to be installed in order to get a IDE rich experience. They will provide things such as auto complete, formatting and etc. To install them use Ctrl+P and type Go: Install/Update Tools :

You can pick all of them and click OK. Alternatively, you can install them manually.

VS Code Settings

Big projects and Files Changes

VS Code will automatically monitor all the files within your project for changes. If you have a file opened and it detects external changes to it, it will refresh them for you. That is a great feature, however, it can get VS Code to run slow when the amount of files being monitored is too great.

When opening the project by Opening Folder (Ctrl+K Ctrl+O) and defining the kubernetes folder:

Most certainly you will give you this message on the bottom right side:

Do not follow the Instructions to increase the amount of file handles at OS level. This project has way too many files and monitoring all of them would be a waste of resources. To have an idea, below is the number of files per folder after the project was compiled:

Instead, we will exclude a few of them from the file watcher by opening the Settings (Ctrl+,) and changing the Files: Watcher Exclude setting. For easy access, write "watch" in the filter text box:

Adding those 6 kubernetes related exceptions should get you running a lot lighter. Just remember that external changes to those files won't be automatically detected.

Go Configuration

Change the settings filter to "go" and enable both “Use Language Server” and “Autocomplete Unimported Packages”.

Change the "Go: Format tool" to "goimports" as currently the default option (goreturns) does not support go modules.

Et voilà

That's pretty much it for a setup. Now you should be able to go through the source code with features such as "Go to" or "Peek" Definition, "Find All References" and etc. You should also be able to compile and run a single node cluster to test your changes.

To run a single node cluster based off the source code run:

API_HOST=0.0.0.0 hack/local-up-cluster.sh

This may take a few minutes. Once it finishes loading, open another remote terminal to check that the kube-dns is up and running:

If you are keen on port forwarding your remote cluster locally, the Code Remote extension also supports that. Just make sure that your kubeconfig is properly configured at your local machine.

From here, where?

Well, now you have a Kubernetes environment up and running, you can pretty much do anything. :)

If you are just starting as a contributor, I would certainly recommend that you read the Contributor's guide and then try to pick up one of those "Good first issue" items from github. Also, make sure that you join the Kubernetes Slack channels.

--

--

Paulo Gomes
Analytics Vidhya

Software craftsman on the eternal learning path towards (hopefully) mastery. Security enthusiast keen on SecDevOps. My opinions are my own.