VS Code Remote Development in Kubernetes

VS Code recently announced VS Code Remote Development, a powerful VS Code extension that allows you to take advantage of VS Code’s full feature set in the following scenarios:

  • Develop a local folder in a local container using volume mounts.
  • Develop a remote folder from a remote machine using SSH.

Development environments are getting more complex, in great part due to the broader variety of technologies being used today (e.g. polyglot apps, micro-service or third-party APIs). Instead of having to spend hours setting everything, VS Code Remote Development simplifies it by letting you use a pre-configured container as your development environment.

As teams have become more geographically distributed, a need for new collaboration models has arisen. In the middle of the Cloud Native revolution, we still develop locally. VS Code Remote Development, like Okteto, is helping us evolve towards a Cloud Native development workflow.

In this blog post, I’ll explain the advantages of developing directly in a remote container running in Kubernetes, and how to achieve the best developer experience with the combined powers of VS Code Remote Development and Okteto.

Why should you develop directly in your cluster?

There are three important benefits of developing directly in your cluster:

  • You are developing in a container: Containers give you replicable and isolated environments, without affecting your local machine.
  • You are developing remotely: Remote execution gives you access to the same OS, network speed and hardware than in production. It also enables a new level of collaboration by making your development environment available to the rest of your team for troubleshooting or early validation.
  • You are developing in Kubernetes: Develop with the same Kubernetes Manifests that in production, using the same ingress rules, secrets, config maps, volumes, service meshes or Admission Webhooks. Kubernetes will launch your development environments in seconds, scaling them vertically or horizontally to make the best usage of your resources.

Powerful, isn’t it? But what about my local development experience? As a developer, I love my debuggers, my VS Code extensions… I want to be fast, and not have to build a Docker image or call kubectl every time I want to validate my changes.

Let me show you how to keep this amazing development experience while working remotely through a sample application, the Voting App.

Deploy the Voting App to Kubernetes

Get a local version of the Voting App, by executing the following commands from your local terminal:

1
2
$ git clone [email protected]:okteto/samples.git
$ cd samples/vscode

You now have a functioning local git repository that contains:

  • A simple python 3 application. The Voting App consists of a flask app that allows you to vote for your favorite animals.
  • A multi-stage Dockerfile to generate the Voting App Docker Images.
  • A manifests folder with the YAML files to deploy the Voting App.
  • A okteto.yml to configure the behavior of Okteto.

In order to deploy the Voting App, you need to have access to a Kubernetes cluster. The easiest way to follow this demo is to deploy the Voting App using Okteto Cloud, a free multi-tenant Kubernetes service provided by Okteto.

Okteto Cloud gives you an isolated Kubernetes namespace to develop and host your applications. Okteto automatically takes care of setting Roles, Network Policies, Pod Security Policies, Quotas, Limit Range and all the other tedious work needed to provide controlled access by several developers to the same Kubernetes cluster. All you need to do is to login with your Github account and start developing.

Install the Okteto CLI to get your kubectl credentials by running the command below:

MacOS/Linux

1
$ curl https://get.okteto.com -sSfL | sh

Windows

1
$ wget https://downloads.okteto.com/cloud/cli/okteto-Windows-x86_64 -OutFile c:\windows\system32\okteto.exe

Once the CLI is installed, run the okteto login and okteto namespace commands to obtain your kubectl credentials.

1
2
$ okteto login
$ okteto namespace

VS Code uses key-based authentication to secure the communication between your local machine and your remote environment. The first thing you need to do is to upload your public SSH key to your Okteto-hosted namespace. Do it by running the command below (if you need to install kubectl, follow this link).:

1
2
$ kubectl create secret generic ssh-public --from-file=authorized_keys=$HOME/.ssh/id_rsa.pub
secret "ssh-public" created

We are almost ready. Deploy the Voting App by executing the command below:

1
2
3
$ kubectl apply -f manifests
deployment.apps "vote" created
service "vote" created

Wait for a few seconds until the application is running. Once ready, go to https://cloud.okteto.com and the Voting App will be accessible at https://vote-[githubid].cloud.okteto.net.

Develop the Voting App directly in Kubernetes

Let’s prepare your remote development environment to develop as if you were in your local machine. Start your Okteto environment by executing the following command:

1
2
3
4
5
6
$ okteto up
✓ Okteto Environment activated
✓ Files synchronized
✓ Your Okteto Environment is ready

Name: vote

The okteto up command automatically does the following tasks:

  • Replace the original Voting App image with the dev image indicated in the okteto.yml file. The dev image includes an SSH server to integrate with VS Code Remote SSH Development.
  • Locally expose the remote ports indicated by the okteto.yml file. In this case, it exposes the container port 22 to localhost:22000 to integrate with VS Code Remote SSH Development, using the public key you updated for authentication.
  • Initiate a synchronization loop to move your local files to the remote container (it uses syncthing under the hood). The synchronization is bidirectional, so every change done by VS Code in the remote container will be synced back to your local machine, and vice-versa, keeping you git flow untouched.

Now, everything is ready to set up your VS Code Remote SSH environment.

Run Remote-SSH: Connect to Host... from the Command Palette (F1) and enter -p 22000 [email protected] in the input box as follows:

If you’re not familiar with the VS Code Remote SSH extension, go here to learn more

After a moment, VS Code will connect to the SSH server and set itself up. Once you are connected, you’ll be in an empty window. Open the /src folder on the remote machine using File > Open... or File > Open Workspace....

Now that we are connected to our remote development environment, any terminal we open from VS Code will automatically run on the remote host rather than locally. Open a terminal with the Terminal > New Terminal and execute the following command:

1
2
3
4
5
6
7
8
$ python app.py
* Serving Flask app "app" (lazy loading)
* Debug mode: on
* Running on http://0.0.0.0:8080/
(Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 117-959-944

This command will start the python service on your remote development environment. You can verify that everything is up and running by going to your service’s endpoint at https://vote-[githubid].cloud.okteto.net.

Now let’s make a code change. Open app.py in VS Code and modify the getOptions function with the code below:

1
2
3
4
def getOptions():
optionA = 'Otters'
optionB = 'Dogs'
return optionA, optionB

Go to the browser again and reload the page. Your changes were applied instantly. No commit, build or push required 😎! And what is even more awesome, enjoy all the VS Code features and extensions, but run everything remotely in a production-like environment!

We recommend to keep Git extensions locally. This way they use your local keys and you don´t need to install them remotely.

Conclusions

We explained the advantages of developing directly in Kubernetes while keeping the same developer experience than working on a local machine. Working on the Cloud is always better. You don’t work on your spreadsheets and listen to media files locally, do you? Stop dealing with local environments and become a Cloud Native Developer today 😎!

Interested in improving your Kubernetes and Docker development workflows? Contact Okteto to see how to install our platform in your own infrastructure.