Golang Sample with Okteto

Okteto Cloud gives you free access to our multi-tenant Kubernetes cluster. 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 sample application using Kubernetes manifests. Okteto Cloud is compatible with any Kubernetes tool.

Step 1: Create your Okteto Cloud account

Login with Github into Okteto Cloud. Okteto Cloud gives you access to your own namespace in our multi-tenant Kubernetes cluster. This namespace is configured automatically to include network policies, quotas, pod security policies, admission webhooks, roles, role bindings, and limit ranges. This way, your applications are isolated respect to other developer namespaces.

Step 2: Download your Kubernetes credentials

Download your Kubernetes credentials from Okteto Cloud and save them in a well-known location. You should find the Credentials button at the top menu of the main screen:

Point your KUBECONFIG environment variable to the credentials file you just downloaded:

MacOS/Linux:

1
$ export KUBECONFIG=$HOME/Downloads/okteto-kube.config:${KUBECONFIG:-$HOME/.kube/config}

Windows:

1
> $Env:KUBECONFIG=("$HOME\Downloads\okteto-kube.config;$Env:KUBECONFIG;$HOME\.kube\config")

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 Sample Application

To get a local version of the sample application that you can then deploy to Okteto Cloud, execute the following commands in your local terminal:

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

You now have a functioning git repository that contains a sample golang application. In the manifest/ directory you also have the Kubernetes manifests needed to deploy the application.

Deploy the application by executing the command below:

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

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 on the application’s endpoint.

Did you notice that you are accessing your application through an SSL 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: Install the Okteto CLI

The Okteto CLI keeps your code synchronized between your local machine and the containers running your application. See your changes live in Kubernetes without having to rebuild images and redeploy containers!

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

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

Step 5: Create your Development Environment

Open a terminal, and navigate to the folder where you cloned the sample application’s code. Once you’re there, run the okteto up command to create your development environment:

1
$ okteto up
1
2
3
4
5
6
7
8
 ✓  Persistent volume provisioned
✓ Files synchronized
✓ Okteto Environment activated
Namespace: cindy
Name: hello-world
Forward: 8080 -> 8080

okteto>

Your development environment is now ready. The okteto up command executes the following tasks:

  • Update your application container with the docker image okteto/golang:1. This image contains the required dev tools to build, test and run the sample application. Enjoy the advantages of having development environments as code.
  • Create a bidirectional file synchronization service to keep your changes up-to-date between your local filesystem and your remote containers. Moving diff of code through the internet is way faster than moving Docker layers.
  • Start a terminal into the remote container. Build, test and run your application as if you were in your local machine.

Now run your application by executing the following command:

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

Open your browser and go to the URL of the application to test it.

Step 6: Develop your application in Kubernetes

Edit the file main.go and replace the word cluster with Okteto Cloud on line 23. Save your changes.

1
2
3
4
5
...
} else {
fmt.Fprint(w, fmt.Sprintf("Hello from the cluster namespace '%s'", namespace))
}
...

Cancel the execution of go run main.go 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. Notice how your changes are instantly applied. No commit, build or push required 😎.

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.

Step 7: Cleanup

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

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

Next steps

Your next step is to deploy and develop your own application in Okteto Cloud.

Notice the okteto.yml file in the sample folder? Okteto uses that manifest 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.

Visit our samples section to learn more about developing and deploying applications with Okteto Cloud.

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 😄.