Getting Started on Okteto Cloud with Golang

Okteto Cloud gives you free access to secure Kubernetes namespaces, compatible with any Kubernetes tool. Login with Github, download your Kubernetes credentials and get full access to your Kubernetes namespace. It’s that simple.

This tutorial will show you how to create an account in Okteto Cloud and how to deploy and develop a Golang sample application.

Step 1: Install the Okteto CLI

The Okteto CLI lets you develop your applications directly in Kubernetes while taking advantage of well-known tooling like go build, go get, hot reloads or IDE debuggers. Code locally. Update your application in Kubernetes in seconds. No commit, build or push required.

The Okteto CLI is open source, and the code is available at Github. Check it out and star it to show your support 🤗!

If you haven’t done it yet, install the Okteto CLI running the following command in your local shell:

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 2: Configure access to your Kubernetes namespace

To authenticate the Okteto CLI against Okteto Cloud, run the following command:

1
$ okteto login

To configure your Kubernetes credentials, run the following command :

1
$ okteto namespace

The okteto namespace command downloads your Kubernetes credentials from Okteto Cloud, adds them to your kubeconfig file, and sets it as the current context. Once you do this, you will have full access to your Kubernetes namespace with any Kubernetes tool.

Okteto Cloud namespaces are configured automatically to include network policies, quotas, pod security policies, admission webhooks, roles, role bindings, and limit ranges (more information here). This way, your applications are isolated from other developer namespaces.

To see that the new configuration is working, enter this command:

1
2
$ kubectl get all
No resources found.

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

Step 3: Deploy the Golang Sample App

Get a local version of the Golang 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 of the Golang Sample App. Deploy a dev version of the application by executing:

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

Open your browser and go to the URL of the application. You can get the URL by logging into Okteto Cloud and clicking on the application’s endpoint:

Did you notice that you are accessing your application through an HTTPs endpoint? This is because Okteto Cloud will automatically create them for you when you deploy your application. Cool no 😎?

Congratulations, you just deployed your first application in Okteto Cloud 🚀.

Step 4: Start your development environment in Kubernetes

You can also use Okteto Cloud to develop your applications directly in Kubernetes, avoiding dependencies on local installations and developing in a much more production-like environment. We are going to use the dev version of the Golang Sample Application we just deployed as our development environment. To do that, run the following command:

1
$ okteto up
1
2
3
4
5
6
7
8
 ✓  Development environment activated
✓ Files synchronized
Namespace: cindy
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 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.
  • Attach a volume to persist the Golang cache in your Kubernetes development environment.
  • Container ports 8080 (the application) and 2345 (the debugger) are forwarded to localhost.
  • You have a remote shell in the development environment. Build, test and run your application as if you were in your local machine.

To run the application, execute in the remote shell:

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

Go back to the browser, and reload the page to test that your application is running.

Step 5: Develop directly in Kubernetes

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

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

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

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

Go back to the browser, and reload the page. Your code changes were instantly applied. No commit, build or push required 😎!

Step 6: 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 Golang development.

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

1
2
3
okteto> dlv debug --headless --listen=:2345 --log --api-version=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. Go back to the browser, and reload the page. The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc…

Next steps

Congratulations, you just developed your first application in Okteto Cloud 🚀.

Okteto helps you move your entire development workflow to Kubernetes. This way you can:

  • Eliminate integration issues by developing the same way your application runs in production.
  • Uninstall Docker or Minikube. No more cpu cycles wasted in your machine.
  • Hardware and network just limited by the power of the cloud!
  • Your development endpoints are always available. No need to expose your local machine to the internet through remote tunnels.

Now it is time to confidently commit your code changes, send a PR, and use your CI/CD system to deploy to production 💪.

You can have several namespaces in Okteto Cloud, one to develop your applications, and another one to run your applications.

Okteto uses the okteto.yml file to determine the name of your development environment, the docker image to use and where to upload your code. Check the Okteto manifest docs to customize your development environments with your own dev tools, images, and dependencies to adapt Okteto to your own application.

Got questions? Join the conversation in our Kubernetes Slack channel! If you don’t have a Kubernetes slack account yet, sign up here. We’d love to hear from you.