Using sample http app in go that responds with "Hello, World" to request received on port 8080:
Using your favorite editor create a file named main.go with the following code:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer, "Hello, World")
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Compile the application:
go run main.go
Test the app works as expected by navigating to http://localhost:8080/ in your browser where you should see 'Hello, World' displayed.
Using your favorite editor create a multi-stage Dockerfile named Dockerfile with the following code:
FROM golang:alpine as builder
RUN mkdir /build
ADD . /build/
WORKDIR /build
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o main .
FROM scratch
COPY --from=builder /build/main /app/
WORKDIR /app
ENV PORT 8080
EXPOSE 8080
ENTRYPOINT ["./main"]
Build a minimal docker image:
docker build -t docker-test .
docker image ls | grep docker-test
Test the image using docker:
docker run -d -p 8080:8080 docker-test
docker ps | grep docker-test
curl http://localhost:8080
docker container stop <CONTAINER-ID>
Tag the image and push to Docker Hub:
docker tag docker-test <DOCKERHUB-ACCOUNT>/docker-test
docker push <DOCKERHUB-ACCOUNT>/docker-test
Using your favorite editor create a file named app.tf with the following code:
provider "kubernetes" {}
resource "kubernetes_namespace" "docker-test" {
metadata {
name = "test"
}
}
resource "kubernetes_replication_controller" "docker-test" {
metadata {
name = "docker-test"
namespace = "test"
labels {
App = "DockerTest"
}
}
spec {
replicas = 2
selector {
App = "DockerTest"
}
template {
container {
image = "<DOCKERHUB-ACCOUNT>/docker-test"
name = "docker-test"
port {
container_port = 8080
}
resources {
limits {
cpu = "0.5"
memory = "512Mi"
}
requests {
cpu = "250m"
memory = "50Mi"
}
}
}
}
}
}
resource "kubernetes_service" "docker-test" {
metadata {
name = "docker-test"
namespace = "test"
}
spec {
selector {
App = "${kubernetes_replication_controller.docker-test.metadata.0.labels.App}"
}
port {
port = 8000
target_port = 8080
}
type = "LoadBalancer"
}
}
Initialize Terraform providers:
terraform init
Use terraform to deploy our service to Kubernetes:
terraform apply
Type 'yes' to confirm.
Use kubectl to check deployment:
kubectl get pods --namespace test
Deploy the Kubernetes dashboard using the following command:
kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
Use the kubectl command line proxy to access the dashboard:
kubectl proxy
The Kubernetes dashboard should now be available at: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
Test the deployed service using curl:
curl http://localhost:8000
Use Terraform to teardown the environment:
terraform destroy
Type 'yes' to confirm.