Run Coder directly in Kubernetes

Online IDEs are becoming mainstream due to their ability to provide true one-click development environments, surpass the capabilities of developer machines and enable a new level of team collaboration. A few examples are Coder, Codeanywhere, Codenvy or AWS Cloud9.

On the other hand, Docker and Kubernetes are the de facto standard to deploy applications. Kubernetes makes easier and faster than ever to run online IDEs in the cloud. At the same time, an online IDE running inside Kubernetes might improve the Kubernetes developer experience, one of the main Kubernetes pain points.

In this blog post, we will cover this scenario using Coder, an online IDE serving Visual Studio Code, and Okteto, a tool that makes it very simple to deploy development environments in Kubernetes.

What is Coder?

Coder is an online IDE serving VS Code, compatible with the VS Code extensions you already know and love. And it is beautifully dockerized. You can try it locally by executing the command below from your project’s source code folder:

1
2
$ docker run -it -p 127.0.0.1:8443:8443 -v “${PWD}:/home/coder/project” codercom/code-server
-allow-http -no-auth

This is an easy way to try Coder, but the power of online IDEs is to have them running in the cloud, in your own Kubernetes cluster. The benefits of this approach are:

  • On-demand development environments that spin up in milliseconds.
  • Code Your development environment can consume services running in your clusters, such as logs and metrics aggregators or a Kafka instance, for example.
  • Superpass the capabilities of your development machine. Network and hardware go at the speed of the cloud.
  • Code in a production-like environment and reduce integration issues to the minimum.
  • Your development environment is in the cloud, available for anyone for fast validation or troubleshooting.

Powerful, isn’t it? Let’s see how we can automate the Coder orchestration in Kubernetes with Okteto.

What is Okteto?

Okteto is an open source tool that lets you develop directly in your Kubernetes cluster. It does the following things:

  • Mount a folder from your local filesystem to a remote container.
  • Forwards ports from the remote container to your local machine.
  • Replace the original remote container image by another one with all your dev tools available.

The source code is available here. Check it out and star it if you like it 🤗!

To install the Okteto CLI in your computer, follow the installation instructions and check that it is properly installed by running:

1
2
$ okteto version
okteto version 1.0.11

The Okteto CLI works in any Kubernetes cluster. However, to make things simpler, we are going to follow this guide using the Okteto Cloud, a free-trial Kubernetes cluster. Initialize your Okteto account by running this command:

1
2
3
4
5
$ okteto login
Authentication will continue in your default browser
Received code=68b6b0520e2bc58e00dc
Getting an access token...
✓ Logged in as pchico83

Okteto Cloud automatically takes care of setting Roles, Network Policies, Pod Security Policies, Quotas, Limit Ranges and all the other tedious work needed to provide controlled access by several developers to the same Kubernetes cluster.

Run the following command to download your Kubernetes credentials:

1
2
$ okteto namespace
✓ Updated context 'cloud_okteto_com' in '/Users/pablo/.kube/config'

You are now ready to deploy our application.

Deploy the Python Sample App

Get a local version of the Python Sample App by executing the following commands in your local terminal:

1
2
$ git clone [https://github.com/okteto/samples](https://github.com/okteto/samples)
$ cd samples/coder

You now have a functioning git repository that contains the Python Sample App, a web application that lets you place votes for your favorite animals (votes are stored in Redis). In the manifest/ directory, you also have raw Kubernetes manifests to deploy the application. To deploy the application execute:

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

If you don’t have kubectl installed, follow this guide.

Wait for a few seconds and the Python Sample App will be ready. Open your browser and go to https://cloud.okteto.com. The application’s endpoint will be displayed on the right side of the screen.

Click on it to see your application running:

Activate your Coder environment

Cool! Now that we have the application running, we are going to use Okteto to replace the python container with our containerized development environment. The development environment will contain a fully-configured Coder instance and, thanks to Okteto, all our source code.

This way your Coder Terminal is running inside of the Kubernetes cluster. Execute the python application from your Coder Terminal, and seamlessly, it will be integrated with the HTTPS endpoint provided by Okteto. Your application can now consume every resource available in your Kubernetes cluster, including the Redis instance we deployed earlier.

And you get all that by simply executing one command:

1
2
3
4
5
6
7
8
9
10
11
12
$ okteto up 
✓ Okteto Environment activated
✓ Files synchronized
✓ Your Okteto Environment is ready
Namespace: pchico83
Name: vote
Forward: 8443 -> 8443
8080 -> 8080
...
INFO code-server development
...
INFO Connected to shared process

Go back to your browser and go to http://localhost:8443 to access your Coder IDE instance through Okteto’s secure tunnel. The IDE is already configured with your source code (freshly synchronized from your local machine by Okteto) and with all the plugins you need to develop the python app.

Now that we have our IDE ready, let’s do some development. Open app.py and modify the getOptions function so that instead of animals, you can vote between Local and Cloud development.

Save your changes, open a terminal directly in Coder and start the application by running python app.py.

Go back to the application endpoint and reload the page. Yes, your changes are instantly applied! Now go ahead and vote a few times for the Cloud option 😉.

Keep editing your files and enjoy all the Coder features and VS Code extensions, but run everything remotely in a production-like environment. No commit, build or push required 😎!

Let’s step back for a second and analyze what happened when executing okteto up. okteto up reads metadata from the okteto.yml file. Let’s have a look at the content of this file:

1
2
3
4
5
6
7
name: vote
image: okteto/coder-python:latest
command: ["dumb-init", "code-server", "--no-auth", "--allow-http"]
workdir: /home/coder/project
forward:
- 8443:8443
- 8080:8080

Based on the field name , okteto up replaces the deployment called vote by a container running my development image , okteto/coder-python:latest. This image is generated by the Dockerfile.coderfile, which installs python and pip dependencies from the base image codercom/code-server. It also installs two VS Code extensions, vscodevim.vim and ms-python.python. Note that all this is available in your development environment without you having anything installed locally, and how everything is configured directly from the repo.

The field workdir is used to indicate the path where your local folder is synchronized in the remote container. The synchronization is based on syncthing and it is bidirectional. Remote changes done in my Coder IDE will be synced back to my local filesystem. This way I can use other tools locally if I need then( e.g. a git visualizer).

Finally, the forward field indicates that the remote ports 8443 and 8080 will be forwarded to your laptop over a secure tunnel. http://localhost:8443 exposes the Coder IDE, and the URL http://localhost:8080 exposes the python application locally.

Cleanup

Cancel the okteto up command by pressing Ctrl + C and run the following commands to remove the resources created by this guide:

1
2
3
4
5
6
7
8
$ okteto down -v
✓ Okteto Environment deactivated

$ kubectl delete -f manifests
service "redis" deleted
statefulset.apps "redis" deleted
deployment.apps "vote" deleted
service "vote" deleted

Conclusions

We have shown the advantages of developing directly in Kubernetes while keeping the same developer experience than working on a local machine. Our development environment was deployed with a single command, and everything came configured directly from the repo, including python dependencies and VS Code extensions. 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.