Zero to Kubernetes in 5 Mins
By: Ahmed Nader
Tags: Golang, Cloud Native, Kubernetes, Tutorial, Docker

K8s in practice

Now that we have a good understanding of what Kubernetes is, let’s see it in practice.

In this tutorial, we will:

Create a simple http server with golang

Deploy a k8s cluster, we will use K3s (a lightweight Kubernetes distribution)

Deploy our golang server on top of K8s

Expose our server to the outer world to be able to access it over the internet.

Prerequisites:

Linux based machine (Ubuntu or Centos)- For Windows you can use MiniKube or Kind instead of K3s and almost everything else should remain the same.

Docker

Golang (this tutorial assumes version 1.15.3 but older versions should work as well)

A docker registry account (you can create an account for free on dockerhub)

Now let’s get our hands dirty!

Install golang following the steps mentioned here. Then create a folder in any location of your choice. In this tutorial I will be using the following path as the root folder to my project /root/tutorial/ Create a simple http go server. Use the following file and save it in /root/tutorial under any name. I will refer to it as server.go

3. Make sure everything is setup correctly and the files runs correctly by running the following command $ go run server.go You should be able to see “Starting server at port 8080”

4. Next step is to build our go project into an executable. Run the following command at the root directory of the project(where server.go occurs)

$ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build . You should be able to see a newly created file with no extension.

5. To complete the next steps we’ll need to create a dockerhub account. You can signup on it for free. We will need an account on dockerhub on any registry to push the docker image that we will create.

6. Then we need to containerize our server to run it inside a container. First we will need to install docker. After installing docker successfully, we can then build a docker image for our server. To do that, copy the following gist into a file named Dockerfile inside /root/tutorial

Afterwards run the following command

$ docker build . -t <dockerhub-username>/tutorial where you should replace <dockerhub-username> with your username on dockerhub.

7. Login using docker cli command $ docker login -u <dockerhub-username> Then push the created image to dockerhub $ docker push <dockerhub-username>/tutorial

8. Now that our docker image is ready, we can use K8s to deploy and manage for us container(s) from that image. To do that, let’s install K3s first. I am using K3s for this tutorial as I find it a great reliable and lightweight distribution for K8s, perfect for development and edge (environments with limited resources) deployments. To install it you just need to run $ curl -sfL https://get.k3s.io | sh - That’s it! That is all what you need to do in order to have a functional Kubernetes cluster.

9. Next we want to create a k8s deployment file for our image. Copy and paste the following yaml deployment file on your machine.

10. Now we want to instruct k8s to create that deployment for us. To do that we need to run $ kubectl create -f tutorial-deployment.yaml To validate the deployment was created successfully, we should run $ kubectl get pods and we should be able to see something similar to

NAME READY STATUS RESTARTS AGE

go-server-deployment-xxxx 1/1 Running 0 65m

11. We are almost there, we have everything set by now and K8s is currently managing our container for us. The only missing step is to expose our pod to the outer world in order to be able to access it over the internet. To achieve that we just need to run the following command

$ kubectl expose deployment go-server-deployment --type=NodePort

This command will expose our deployment to the outer world on a random port. To identify that random port, we need to run $ kubectl get svc we should be able to see a service that got created for us

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP x.x.x.x <none> 443/TCP 87m

go-xxx NodePort x.x.x.x <none> 8080:31628/TCP 37m