I’ll keep this one short and to the point. Mostly copy-paste commands.
- We’ll use sample go-lang helloworld application from this link: https://github.com/freakynit/go-k8s. Don’t worry if you don’t know go-lang. This tutorial has nothing to do with the programming language.
- For k8s cluster, we’ll use k3d, an excellent tool to setup local kubernetes cluster.
- Loadbalancer component will allow us to expose our k8s services externally.
- Private registry(docker registry) will allow us to speed-up our code-build-push-pull iterations as we’ll not need to push new image every time we make changes to docker hub or any other public registry
- Clone the repo: https://github.com/freakynit/go-k8s
- cd into it:
- Install k3d by following their official guide
Edit your docker config file (
~/.docker/daemon.json) and add following (enables insecure registries, our private one basically):
"insecure-registries": [ "registry.localhost:5000" ], "registry-mirrors": [ "https://registry-1.docker.io" ]
Start local registry server in docker container:
k3d registry create registry.localhost --port 5000
Create multi-node kubernetes cluster with name:
2 worker nodes. Notice that we are also specifying
loadbalancerand our local registry config file path(k3d-registries.yaml):
k3d cluster create --api-port 0.0.0.0:6550 -p "8081:80@loadbalancer" --registry-use k3d-registry.localhost:5000 --registry-config k3d-registries.yaml --agents 2 mycluster
Build docker image for application:
docker build -t freakynit/go-k8s:1.0.0 .
Tag this image with local registry host:
docker tag freakynit/go-k8s:1.0.0 k3d-registry.localhost:5000/go-k8s:1.0.0
Add following entries to
/etc/hostsso that you can access your k8s services on
loadbalancerhost and local registry:
127.0.0.1 loadbalancer 127.0.0.1 k3d-registry.localhost ::1 k3d-registry.localhost
Push your app image to local registry:
docker push k3d-registry.localhost:5000/go-k8s:1.0.0
Apply kubernetes config file:
kubectl apply -f k8s-deployment.yaml
Check if pods are running.. keep trying every few seconds until all pods show status as running:
kubectl get pod
Access your application:
curl localhost:8081/ # OR curl loadbalancer:8081/
Once done, you can delete the cluster using:
k3d cluster delete mycluster
k3d has a lot more options that allow much configuration and customization. Do explore these.
That’s all folks. Let me know if you face any issues in this.