Build apps locally and Deploy on MicroK8s
--
This article explains different ways to build container locally and deploy it on a MicroK8s cluster in a development environment.
MicroK8s can not directly access the local docker images repository, so it needs few additional steps to get an image build by docker locally to deploy on the MicroK8s cluster.
One option is to export the docker image into a tar file, and import it into MicroK8s local registry. A second option, for better productivity, is to use an image registry service provided by the microk8s registry add-on and directly push images in it using docker. Both the approach is explained in details below.
Instructions to setup MicroK8s cluster on a local workstation can be found here — MicroK8s for workstations.
Working with local docker registry
A simple web app written in golang is used here as an example.
Create a file ‘main.go’ with the following contents, in the working directory.
# main.go
package mainimport (
"fmt"
"net/http"
)func main() {
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request{
fmt.Fprint(w, "hello from web page")
})
http.ListenAndServe(":8080", nil)
}
A docker definition is required to build the docker container image. Create a file named ‘Dockerfile’ with the following contents in the working directory.
# DockerfileFROM golang:latest
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o hello-web .
CMD ["/app/hello-web"
Run the following command to build the image.
$ sudo docker build . -t hello-web
Verify the image and tag using the following command.
$ sudo docker images
Run the image locally to see the web app in action using docker run
$ sudo docker run hello-web -p 8080:8080
The app should be available at http://localhost:8080/.
Now, deploying this to the MicroK8s cluster needs few additional steps.
Import the image into MicroK8s registry
The MicroK8s registry will not be enabled by default, so needs run the following to enable it.
# Enable registry
$ sudo microk8s.enable registry
Save docker image to a tar file.
$ docker save hello-web > hello-web.tar
Import image into K8s registry. k8s.io is the default namespace, so use that while importing image.
$ sudo microk8s.ctr -n k8s.io image import hello-web.tar
Verify the import by listing images as follows.
$ sudo microk8s.ctr -n k8s.io images ls --name~=hello-web
Working with the MicroK8s registry add-on
MicroK8s is shipped with a registry add-on, when it is enabled, a registry service will be available on port 32000 of the localhost. It is possible to directly build and push the using docker and consume from MicroK8s for deployment. This will help to significantly improve the productivity.
Run the following docker command to tag the image in ‘path/image-name:v' format, this is required to be able to push images to external registries.
$ sudo docker build . -t localhost:32000/hello-web:v3
This image can be pushed to the k8s registry as follows
$ sudo docker push localhost:32000/hello-web
This image is now ready to be used in a deployment.
Create a Deployment
Now that we have the images built and available in local k8s registry or through the add-on service, we are ready to create a deployment to test the web app.
Create a hello-web-deployment.yaml with the content as follows.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hellow-deployment
labels:
app: hellow
spec:
selector:
matchLabels:
app: hellow
template:
metadata:
labels:
app: hellow
spec:
containers:
- name: hello-web
image: hello-app:latest
# Use the localhost:32000 path to use image from registry service
# e.g. image: localhost:32000/hello-app:v3
ports:
- containerPort: 8080
Run the following command to create a deployment
$ sudo microk8s.kubectl apply -f hello-web-deployment.yaml
Verify the deployment and the pods
$ sudo microk8s.kubectl get deployments
$ sudo microk8s.kubectl get pods
Now, expose the web service running at port 8080 through a NodePort.
$ sudo microk8s.kubectl expose deployment hellow-deployment --type=NodePort --name=hello-service
List the services to get the local port mapped to the hello-service.
$ sudo microk8s.kubectl get svc
Notice the port 31819 above, the web service will be accessible through the localhost:31819.
$ curl http://localhost:31819Response:
hello from web page