Kubernetes cluster in this example I use a lightweight version of Kubernetes with K3s
Simple http application with GO
A simple illustrative example with http server on port 80 with basic handle respond on a main url.
If you already have any application you can go to the next step.
You can run the application to ensure works correctly:
If the application its works then we can prepare docker image to “Dokcerize” the application.
Build docker image from scrath
Firstly, we create a file named infrastucture/docker/Dockerfile to contain all config. We use the alpine image as a builder to compile de Go binary and then use image from scratch to copy binary with SSL certificates and define de binary as entrypoint.
This way we get a minimal docker image that contain your binary application than only around 5 MB!
And we can make the build of the image:
-t: Name and optionally a tag in the name:tag format
-f: Name of the Dockerfile (Default is PATH/Dockerfile)
For Github Actions can access to your k3s cluster you need import de kubeconfig into secrets actions.
In k3s you can found the kubeconfig on /etc/rancher/k3s/k3s.yaml. You kubeconfig will like somehow like this:
Then you go to your project from Github and add the secret named KUBE_CONFIG with the k3s.yaml content.
Project ⮕ Settings ⮕ Secret and Variables ⮕ Actions
Github action for Continuous Delivery (CD)
Finally we come to the long awaited section. We will see how to create a flow with two steps.
Create a file into root of main project: .github/workflow/cd.yaml where we create a workflow triggered on a push/merge on your principal branch, in this case main branch.
First step
In the first step build the docker image and publish on the container registry, in this case use the ghcr (GitHub Container Registry).
Publish your docker image from registry to k8s cluster.
First of all need a create a token on github with read:package scope and create secret in json format encoded as base64 that you publish on your k8s secrets.
Let’s Base64 encode it first:
Change username for your GitHub username and your_token with the previous generated token
Create a json and encode to Base64 again:
Create a manifest to deploy your application on kuberntes deployment, service, ingress
Finally, once we have the manifests and secrets, we can create the last step of the workflow.
And just commit in your project the complete CD workflow config .github/workflow/cd.yaml.
Then you can go to Actions section in your repository and show all workflows and you can see the summary.