DEV Community

Nitin Bansal
Nitin Bansal

Posted on • Edited on

Multi-node local kubernetes cluster with loadbalancer and private registry

I’ll keep this one short and to the point. Mostly copy-paste commands.

  1. 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.
  2. For k8s cluster, we’ll use k3d, an excellent tool to setup local kubernetes cluster.
  3. Loadbalancer component will allow us to expose our k8s services externally.
  4. 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

Let’s begin

  1. Clone the repo: https://github.com/freakynit/go-k8s
  2. cd into it: cd go-k8s
  3. Install k3d by following their official guide
  4. 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"
    ]
    
  5. Start local registry server in docker container:

    k3d registry create registry.localhost --port 5000
    
  6. Create multi-node kubernetes cluster with name: mycluster and 2 worker nodes. Notice that we are also specifying loadbalancer and 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
    
  7. Build docker image for application:

    docker build -t freakynit/go-k8s:1.0.0 .
    
  8. Tag this image with local registry host:

    docker tag freakynit/go-k8s:1.0.0 k3d-registry.localhost:5000/go-k8s:1.0.0
    
  9. Add following entries to /etc/hosts so that you can access your k8s services on loadbalancer host and local registry:

    127.0.0.1 loadbalancer
    127.0.0.1 k3d-registry.localhost
    ::1       k3d-registry.localhost
    
  10. Push your app image to local registry:

    docker push k3d-registry.localhost:5000/go-k8s:1.0.0
    
  11. Apply kubernetes config file:

    kubectl apply -f k8s-deployment.yaml
    
  12. Check if pods are running.. keep trying every few seconds until all pods show status as running:

    kubectl get pod
    
  13. 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.

Top comments (0)