GoLang Tutorial - Building Docker image and deploying it to a Kubernetes cluster (minikube)
In this post, we'll build a Docker image from the simple application we created earlier (Web Application Part 0 (Introduction)).
Here is the code:
This code is available : web-app-0.go
The application looks like this:
Here is our Dockerfile:
FROM golang:alpine RUN mkdir /app COPY . /app WORKDIR /app RUN go build -o main . CMD ["/app/main"]
Let's build the image:
$ docker build -t go-app-img . Sending build context to Docker daemon 7.304MB Step 1/6 : FROM golang:alpine ---> b97a72b8e97d Step 2/6 : RUN mkdir /app ---> Using cache ---> 493029660590 Step 3/6 : COPY . /app ---> e907c54b6b18 Step 4/6 : WORKDIR /app ---> Running in 130a4c80c611 Removing intermediate container 130a4c80c611 ---> d7bb7a9a6db8 Step 5/6 : RUN go build -o main . ---> Running in 518fd62e940b Removing intermediate container 518fd62e940b ---> 5ca1a1ef0e58 Step 6/6 : CMD ["/app/main"] ---> Running in 675128cd77bf Removing intermediate container 675128cd77bf ---> 12af20a5db3f Successfully built 12af20a5db3f Successfully tagged go-app-img:latest
Run it:
$ docker run -d -p 3333:3000 --name go-app-container go-app-img 57a96e20d1766978d25685dd86384d696ba8d64433ef70ca9f41d64dcaefa1d9
Here we have indicated Docker to run a new container from our image (go-app-img) binding host port 3333 to container's internal port 3000 (-p 3333:3001), running the container in detached mode (-d) and giving a custom name to this container (-name go-app-container).
Go to the browser and navigate to localhost:3333:
Let's deploy our app to Kubernetes cluster, well, to minikube.
Minikube
First, we need to start the minikube:
$ minikube start ๐ minikube v1.0.0 on darwin (amd64) ๐คน Downloading Kubernetes v1.14.0 images in the background ... ๐ก Tip: Use 'minikube start -p' to create a new cluster, or 'minikube delete' to delete this one. ๐ Restarting existing virtualbox VM for "minikube" ... โ Waiting for SSH access ... ๐ถ "minikube" IP address is 192.168.99.100 ๐ณ Configuring Docker as the container runtime ... ๐ณ Version of container runtime is 18.06.2-ce โ Waiting for image downloads to complete ... โจ Preparing Kubernetes environment ... ๐ Pulling images required by Kubernetes v1.14.0 ... ๐ Relaunching Kubernetes v1.14.0 using kubeadm ... โ Waiting for pods: apiserver proxy etcd scheduler controller dns ๐ฏ Updating kube-proxy configuration ... ๐ค Verifying component health ...... ๐ kubectl is now configured to use "minikube" ๐ Done! Thank you for using minikube!
Check the current status of the cluster:
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d $ kubectl get pods No resources found. $ kubectl get deployments No resources found.
Push the image to DockerHub
Let's push the image to DockerHub. To do that we may want to build the image specifying the repository:
$ docker build -t dockerbogo/go-app . ... Successfully tagged dockerbogo/go-app:latest $ docker push dockerbogo/go-app:latest The push refers to repository [docker.io/dockerbogo/go-app] ...
Deploy to minikube cluster
Here is our deploy manifest file (deployment.yaml):
apiVersion: apps/v1 kind: Deployment metadata: name: my-go-app spec: replicas: 1 selector: matchLabels: app: go-app template: metadata: labels: app: go-app spec: containers: - name: go-app-container image: dockerbogo/go-app resources: limits: memory: "128Mi" cpu: "500m" ports: - containerPort: 3000
Deploy:
$ kubectl create -f deployment.yaml deployment.apps/my-go-app created $ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE my-go-app 1/1 1 1 24s $ kubectl get pods NAME READY STATUS RESTARTS AGE my-go-app-5bb8767f6d-2pdtk 1/1 Running 0 43s
Exposing the go-app pod
Let's create a Service object that exposes the deployment:
$ kubectl expose deployment my-go-app --type=NodePort --name=go-app-svc --target-port=3000 service/go-app-svc exposed $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE go-app-svc NodePort 10.111.127.140 <none> 3000:30652/TCP 81s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d $ minikube ip 192.168.99.100
Testing - access the app
Go to browser with minikube-ip:nodePort which is 192.168.99.100:30652:
Great! Now we've just deployed our go application to Kubernetes cluster.
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization