Difficulty: beginner
Estimated Time: 20-30 minutes

In this scenario you'll run your Go application in a Kubernetes cluster.

These exercises have been tests in the single node Kubernetes cluster that is set up for you here in Katacoda, but you should also be able to run the same exercises using the Kubernetes support in Docker for Mac/Windows on your laptop if you prefer.

Congratulations, you have run Go code under Kubernetes!

Kubernetes basics

Step 1 of 6

Write a simple Go app

In this scenario you will write a very simple Go application, build it into a container, and run it under Kubernetes.

package main

import (
  "fmt"
  "os"
  "time"
)

func main() {
  hostname, _ := os.Hostname()
    for {
        fmt.Printf("hello, my hostname is %v\n", hostname)
        time.Sleep(5*time.Second)
    }
}

Build that code into a container with this Dockerfile:

# First stage: start with a Golang base image
FROM golang:1.12-alpine3.10

# Move to the directory where the source code will live
WORKDIR /go/src/hello

# Copy the source code into the current directory
COPY . .

# Get any dependencies, and compile the code
RUN CGO_ENABLED=0 go get -v ./...

# Second stage: start from an empty base image
FROM scratch

# Copy the binary from the first stage
COPY --from=0 /go/bin/hello /

# Tell Docker what executable to run by default when starting this container
ENTRYPOINT ["/hello"]

docker build -t hello .

You now have a container and in the next step you will run it using Kubernetes. But before we move on to that, let's describe what Kubernetes is.

Container orchestration and more

If you have run scenarios like Container basics you will have seen how you can run an individual container on a particular host by issuing docker run commands on that (virtual) machine. The basic function of a container orchestrator like Kubernetes is to run containers on your behalf. It chooses where to run each container across a number of nodes (known as a cluster).

In order to use containers effectively they may need to be connected together, and they may need to have access to the internet. Kubernetes can perform these functions too, along with managing permissions, secrets, connecting to storage, and many other capabilities that are needed to run a full application in the cloud.

Reconciliation

Kubernetes uses declarative configuration - you tell it what state you want it to achieve, and it constantly checks whether the current state matches that config, making changes as necessary to bring reality in line with that desired state. This is called reconciliation.

Kubernetes pods

One of the concepts you'll need to get to grips with when you start using Kubernetes is the pod. A pod consists of one or more containers that are always scheduled (run) together on a node. When you want to run a container, you define a pod specifying that container.

Next step and further reading

In the next step you will define and run a Kubernetes pod to run your new image.

If you want to learn more about Kubernetes concepts before you move on, you can read this article or dive into the Kubernetes documentation. You can also find some great material introducing Kubernetes in Jerôme Petazzoni's Container Training.

hello.go
Dockerfile
pod.yaml
deployment.yaml
service.yaml