Golang Sample with Okteto

Okteto gives you instant cloud environments to code and collaborate in your Kubernetes cluster. Write code using your favorite IDE, and see the results directly in the cloud. No commit 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

You’ll use the CLI to create an account in Okteto, download your Kubernetes credentials, activate your namespaces and manage your Okteto Environments.

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

Using an existing Kubernetes cluster?
It is possible to use Okteto with an existing cluster. If you’re interested in that scenario, jump to step 4 of the guide.

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 account in Okteto, a service account, namespace, and credentials will be automatically created for you in 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 to give you the best possible development experience.

Once the login process is completed, open your browser and go to https://cloud.okteto.com. you should now see your empty personal namespace, ready to create new resources.

Step 3: Switch Kubernetes namespaces and context

Run the following command to activate your new personal namespace:

1
$ okteto namespace

The okteto namespace command lets you easily change your Kubernetes context. If no namespace is provided, the command will switch to your Okteto personal namespace.

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. Okteto 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 "math" created
service "math" 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.

Start your Okteto Environment by running the following command in your local terminal:

1
$ okteto up
1
2
3
4
5
6
7
Deployment 'math' doesn't exist. Do you want to create a new one? [y/n]: y
✓ Okteto Environment activated
✓ Files synchronized
✓ Your Okteto Environment is ready
Name: math

okteto>

The okteto up command will automatically start an Okteto Environment. It will also start a file synchronization service 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

You can now access the Math app at http://localhost/mult/3/4.

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 switch the word mult by times at line 43. Save your changes. Cancel the execution of go run main.go from your Okteto Terminal by pressing ctrl + c. Run your tests to check that everything is fine with your changes:

1
okteto> go test ./...

And finally rerun your application:

1
okteto> go run main.go

Go back to the browser, and go to the new endpoint http://localhost:8080/times/3/4. 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 "math" deleted
service "math" 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!