How to Develop Go Applications in Kubernetes

Image of How to Develop Go Applications in Kubernetes

Kubernetes is an open-source project for automating deployment, scaling, and management of containers. It has rapidly become the standard to run production workloads and the community around it is just great!

But developing in Kubernetes presents some challenges. The typical development workflow looks like this: write code, build a Docker image, push it to the registry, redeploy, validate your changes and repeat. This flow is not only slow, but it also prevents us from benefitting from standard features of Go tools such as fast incremental builds, hot reloaders or debuggers.

Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for Go developers. You will be able to take full advantage of tools like go build, dependency caching, hot-reloaders (gin or IDE debuggers (VS Code, GoLand, Atom …) while developing your application directly in Kubernetes.

Step 1: Deploy the Go Sample App

Get a local version of the Go Sample App by executing the following commands:

1
2
$ git clone https://github.com/okteto/go-getting-started
$ cd go-getting-started

The k8s.yml file contains the Kubernetes manifests to deploy the Go Sample App. Run the application by executing:

1
$ kubectl apply -f k8s.yml
1
2
deployment.apps "hello-world" created
service "hello-world" created

This is cool! You typed one command and a dev version of your application just runs 😎.

Step 2: Install the Okteto CLI

The Okteto CLI is an open-source project that lets you develop your applications directly in Kubernetes while taking advantage of well-known local tooling. We will use it to speed up our development cycle instead of using the typical development workflow based on building docker images and redeploying containers.

Install the Okteto CLI by running the following command:

MacOS / Linux
1
$ curl https://get.okteto.com -sSfL | sh
Windows
1
$ wget https://downloads.okteto.com/cli/okteto-Windows-x86_64 -OutFile c:\windows\system32\okteto.exe

Step 3: Start your development environment in Kubernetes

With the Go Sample Application deployed, run the following command:

1
$ okteto up
1
2
3
4
5
6
7
8
 ✓  Development environment activated
✓ Files synchronized
Namespace: pchico83
Name: hello-world
Forward: 8080 -> 8080
2345 -> 2345

okteto>

The okteto up command starts a Kubernetes development environment, which means:

  • The Go Sample App container is updated with the docker image okteto/golang:1. This image contains the required dev tools to build, test, debug and run the Go Sample App.
  • A file synchronization service is created to keep your changes up-to-date between your local filesystem and your application pods.
  • A volume is attached to persist the Go cache and packages in your Kubernetes development environment.
  • Container ports 8080 (the application) and 2345 (the debugger) are forwarded to localhost.
  • A remote shell is started in your Kubernetes development environment. Build, test and run your application as if you were in your local machine.

All of this (and more) can be customized via the okteto.yml manifest file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name: hello-world
image: okteto/golang:1
workdir: /okteto
command: ["bash"]
volumes:
- /go/pkg/
- /root/.cache/go-build/
securityContext:
capabilities:
add:
- SYS_PTRACE
forward:
- 8080:8080
- 2345:2345

To run the application, execute in the remote shell:

1
okteto> go run main.go
1
Starting hello-world server...

The first time you run the application, Go will download your dependencies and compile your application. Wait for this process to finish and test your application by running the command below in a local shell:

1
$ curl localhost:8080
1
Hello world!

Step 4: Develop directly in Kubernetes

Open the main.go file in your favorite local IDE and modify the response message on line 17 to be Hello world from the cluster!. Save your changes.

1
2
3
func helloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world from the cluster!")
}

Okteto will synchronize your changes to your development environment in Kubernetes. Cancel the execution of go run main.go from the remote shell by pressing ctrl + c. Rerun your application:

1
okteto> go run main.go
1
Starting hello-world server...

Call your application from a local shell to validate the changes:

1
$ curl localhost:8080
1
Hello world from the cluster!

Cool! Your code changes were instantly applied to Kubernetes. No commit, build or push required 😎!

Step 5: Debug directly in Kubernetes

Okteto enables you to debug your applications directly from your favorite IDE. Let’s take a look at how that works in VS Code, one of the most popular IDEs for Go development.

Cancel the execution of go run main.go from the remote shell by pressing ctrl + c. Rerun your application in debug mode:

1
okteto> dlv debug --headless --listen=:2345 --log --api-version=2
1
2
API server listening at: [::]:2345
2019-10-17T14:39:24Z info layer=debugger launching process with args: [/okteto/__debug_bin]

Open the Debug extension and run the Connect to okteto launch configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"version": "0.2.0",
"configurations": [
{
"name": "Connect to okteto",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "/okteto",
"port": 2345,
"host": "127.0.0.1"
}
]
}

Add a breakpoint on main.go, line 17. Call your application by executing from your local shell:

1
$ curl localhost:8080

The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc…

Conclusions

Kubernetes has the potential to be a great development platform, providing replicable, resource-efficient and production-like development environments. We have shown you how to use Okteto to create a development workflow that also lets you take advantage of features like incremental builds, hot reloaders or debuggers while developing your application directly in Kubernetes.

Accelerate your development and start developing directly in Kubernetes today. Let us know what you think about it on Twitter, or in the Okteto channel in the Kubernetes slack.