Golang Sample with Okteto

Okteto keeps your local development workflow untouched, but magically integrated in your Kubernetes cluster. Write code using your favorite IDE, and see the results directly in the cloud. No commit, build or push required.

This example shows how to leverage Okteto to develop a Go application directly in a Kubernetes cluster. It will be deployed using raw Kubernetes manifests.

Step 1: Install the Okteto CLI

The Okteto CLI transforms your remote Kubernetes pods into development environments with all your dev tools available to build and test your applications. It also starts a bidirectional synchronization service between your local filesystem and your remote containers to avoid the build/push/redeploy cycle. See your changes live in the cloud in seconds!

The Okteto CLI is open source, and the code is available here. Feel free to check it out, contribute, and please, star it 🤗!

Install the Okteto CLI by running the following command in your local terminal:

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

The Okteto CLI works in any Kubernetes cluster. If you are interested in using your existing Kubernetes cluster, jump to step 4 of the guide. However, the Okteto CLI is deeper integrated with Okteto Cloud, the hosted version of Okteto Enterprise. Check all the features provided by Okteto Cloud here.

Step 2: Login from the Okteto CLI

Run to okteto login command to create and initialize your Okteto account.

1
$ okteto login

When you create an Okteto Cloud account, a service account, namespace, and credentials will be automatically created for you in the Okteto’s multi-tenant kubernetes cluster. These resources are configured automatically to include network policies, quotas, pod security policies, admission webhooks, roles, role bindings and limit ranges. This way, every developer has access to the Kubernetes cluster without interfering with other developer namespaces.

Step 3: Switch Kubernetes namespaces and context

Run the following command to activate your personal namespace:

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.

Step 4: Deploy the Golang Sample App

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

1
2
$ git clone https://github.com/okteto/samples
$ cd samples/golang

In the manifest/ directory you also have raw Kubernetes manifests that we will use in this guide to deploy the application in the cluster. The Okteto CLI works however independently of your common deployment practices or tools.

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

Run the Golang Sample App by executing:

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

Step 5: Create your Okteto Environment

You now have a functioning git repository that contains a simple Go application and your application deployed in the cluster. Notice that since you’ll be working directly on a remote environment, you don’t need to have Go, nor Docker in your machine to run your application.

Transform your application pods into a development environment by running the following command:

1
$ okteto up
1
2
3
4
5
6
 ✓  Okteto Environment activated
✓ Files synchronized
✓ Your Okteto Environment is ready
Name: hello-world

okteto>

The okteto up command will automatically start an Okteto Environment, which means:

  • The Golang Sample App container is updated with the docker image okteto/hello-world:golang-dev. This image contains the required dev tools to build, test and run the Golang Sample App.
  • A bidirectional file synchronization service is started to keep your changes up to date between your local filesystem and your Okteto Environment.

Once the Okteto Environment is ready, start your application by executing the following command in your Okteto Terminal:

1
okteto> go run main.go

Your application is now ready. Open your browser and go the URL of the application. You can get the URL by logging into Okteto Cloud and clicking in the application’s endpoint.

Congratulations, you just launched an Okteto Environment 🚀!

Step 6: Develop directly in the cloud

Now things get more exciting. Edit the file main.go and replace the word cluster with Okteto Cloud on line 23. Save your changes.

Cancel the execution of go run main.go from your Okteto Terminal by pressing ctrl + c. Now rerun your application:

1
okteto> go run main.go

Go back to the browser and reload the page. Notice how your changes are instantly applied. No commit, build or push required 😎!

Step 7: Cleanup

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

1
$ okteto down -v
1
✓  Okteto Environment deactivated
1
$ kubectl delete -f manifests
1
2
deployment.apps "hello-world" deleted
service "hello-world" deleted

Next steps

You now know how easy you can do efficient development in the cloud.

Notice the okteto.yml file in the sample folder. Okteto uses that manifest to determine the name of your Okteto Environment, the docker image to use and where to upload your code. Check the Okteto manifest docs to customize your Okteto Environments with your own dev tools, images and dependencies.

Improve your team’s Kubernetes and Docker development workflows with Okteto Enterprise. Schedule your demo today!