Skip to content

Instantly share code, notes, and snippets.

@bergpb
Last active December 14, 2022 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bergpb/51b3b69f62c02e23bf6c618b29d3bf09 to your computer and use it in GitHub Desktop.
Save bergpb/51b3b69f62c02e23bf6c618b29d3bf09 to your computer and use it in GitHub Desktop.
k3s with MetalLB - Sketch

K3S + MetalLB

Install k3s + MetalLB in a cluster with RaspberryPi.

Instructions:

  1. Edit /boot/cmdline.txt file and the configuration at the end of line: cgroup_memory=1 cgroup_enable=memory

  2. On control node, generate a secret and install k3s:

    TOKEN=`python3 -c "import secrets; print(secrets.token_hex(32))"`

    curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644 --disable servicelb --token $TOKEN --bind-address <control_node_ip>

  3. Run the followed command in worker nodes:

    curl -sfL https://get.k3s.io | K3S_URL=https://<control_node_ip>:6443 K3S_TOKEN=<token> sh -

  4. When the installation is completed, let's relabel the worker nodes:

    kubectl label nodes <node_name> kubernetes.io/role=worker

  5. Now, connect throught ssh with control node and install MetalLB:

    kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml

  6. The followed commands need to be executed in Control Node.

  7. Define the IP addresses that will be used by MetalLB, we need to create a config.yml file and apply with kubectl:

    touch config.yml

    apiVersion: metallb.io/v1beta1
    kind: IPAddressPool
    metadata:
      name: config
      namespace: metallb-system
    spec:
      addresses:
      - <first_node_ip_in_cluster>-<last_node_ip_in_cluster>
    

    kubectl apply -f config.yml

  8. To testing these configurations, let's deploy the a Nginx in the cluster:

    touch deployment.yml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:alpine
            ports:
            - containerPort: 80

    touch service.yml

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      selector:
        app: nginx
      ports:
        - port: 80
          targetPort: 80
      type: LoadBalancer

    kubectl apply -f deployment.yml
    kubectl apply -f service.yml

  9. To check if the the deployment and services are running as expected, run the followed commands:

    kubectl get pods
    kubectl get services

  10. The output will be similar to this one:

    pi@raspberrypi:~/k8s-nginx $ kubectl get pods
    NAME                    READY   STATUS    RESTARTS   AGE
    nginx-965685897-bddf7   1/1     Running   0          19h
    nginx-965685897-cfx4z   1/1     Running   0          19h
    nginx-965685897-n274m   1/1     Running   0          19h
    nginx-965685897-wxblb   1/1     Running   0          19h
    nginx-965685897-t2zhr   1/1     Running   0          19h
    pi@raspberrypi:~/k8s-nginx $ kubectl get services
    NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
    kubernetes   ClusterIP      10.43.0.1       <none>        443/TCP        19h
    nginx        LoadBalancer   10.43.134.193   10.0.0.10     80:30341/TCP   19h
  11. The nginx service will be avaliable on External IP 10.0.0.10, to access in your browser.

  12. If you need to remove the k3s installation just run the followed commands:

    1. Control Plane:

      /usr/local/bin/k3s-uninstall.sh

    2. Workers:

      /usr/local/bin/k3s-agent-uninstall.sh

Useful links related to MetalLB configurations:

Reload IP configuration
IP Range change workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment