Hello Website on GKE Cluster

Let’s explore, how deploy web application “Hello Website” written in go language at Google Kubernetes Engine Cluster.

Source code for “Hello Website” in the file helloweb.go

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello website! You've requested: %s\n", r.URL.Path)
    })

    http.ListenAndServe(":80", nil)
}

Source code for dockerfile

# image environment definition
# 
FROM golang:1.14

WORKDIR /go/src/app
#copy current dir on local host to current dir on image, in this case workdir
COPY . .

#get all packages in current dir (.) and all subdirs (...), just download (-d)
RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]

Steps:

  1. At https://console.cloud.google.com/apis/library search for Kubernetes (k8s) and then enable k8s Engine API
  2. Create Docker image
    At authenticated terminal or cloud shell go to the directory with helloweb source files and do:
    docker build -t helloweb-container-image .
    docker images
    gcloud auth configure-docker
    docker tag helloweb-container-image gcr.io/<MY_PROJECT_ID>/helloweb-container-image:v1
    docker push gcr.io/<MY_PROJECT_ID>/helloweb-container-image:v1
    gcloud container images list
    gcloud container images list –repository=gcr.io/<MY_PROJECT_ID>
  3. Create Kubernetes Engine cluster:
    gcloud config set compute/zone us-central1-a
    gcloud container clusters create helloweb-cluster1 –num-nodes=4
    gcloud container clusters get-credentials helloweb-cluster1
    kubectl create deployment hello-website –image=gcr.io/<MY_PROJECT_ID>/helloweb-container-image:v1
  4. Expose deployed workload
    kubectl expose deployment hello-website –type=LoadBalancer –name=hello-website-service –port=80 –target-port=80
    kubectl get services
  5. With your web browser, go to the EXTERNAL-IP to see Hello Website!

											
This entry was posted in golang, workday and tagged . Bookmark the permalink.

Leave a Reply