Install K3S Agent

With K3S installed on the Master Node, let’s install the K3S on the agent nodes.

NOTE: You will need to do the following for all of your Worker Nodes

  1. Set the Environment variables

    Similar to the Master Node, we’ll need to set a couple of environment variables.

    1. The first line allows us to run kubectl as a non-root user
    2. The second line provides the endpoint for the K3S server running on the Master Node. Be sure to change the IP Address to the IP Address of your Master Node
    3. The third line is the Node Token we got from the Master Node by running $ sudo cat /var/lib/rancher/k3s/server/node-token
    $ export K3S_KUBECONFIG_MODE="644"                                                                                                  
    $ export K3S_URL="https://192.168.122.146:6443"                                                                                     
    $ export K3S_TOKEN="K109cdd14ff6974270f501e2660958f45dfff65a2311408e4e2ece7960b0dbef0ab::server:e0db12c2f18dfe7720874bb0485724a0"   
    
  2. Install the K3S Agent

    Now that we have the environment variables out of the way, let’s install the K3S agent on the Worker Nodes:

    basedmeezus@K3SWorker:~$ curl -sfL https://get.k3s.io | sh -
    [sudo] password for basedmeezus: 
    [INFO]  Finding release for channel stable
    [INFO]  Using v1.22.7+k3s1 as release
    [INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.22.7+k3s1/sha256sum-amd64.txt
    [INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.22.7+k3s1/k3s
    [INFO]  Verifying binary download
    [INFO]  Installing k3s to /usr/local/bin/k3s
    [INFO]  Skipping installation of SELinux RPM
    [INFO]  Creating /usr/local/bin/kubectl symlink to k3s
    [INFO]  Creating /usr/local/bin/crictl symlink to k3s
    [INFO]  Creating /usr/local/bin/ctr symlink to k3s
    [INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
    [INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
    [INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
    [INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
    [INFO]  systemd: Enabling k3s unit
    Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
    [INFO]  systemd: Starting k3s
    
  3. Confirmation Checks Let’s confirm the K3S Agent system services is installed correctly

    basedmeezus@K3SWorker:~/.kube$ sudo systemctl status k3s-agent.service 
    ● k3s-agent.service - Lightweight Kubernetes
        Loaded: loaded (/etc/systemd/system/k3s-agent.service; enabled; vendor preset: enabled)
        Active: active (running) since Wed 2022-03-09 21:52:12 UTC; 2min 57s ago
    

    Now if you want to be able to run kubectl commands from your worker nodes, you’ll need to do the following, though this isn’t necessary(considering you’re able to connect to the Master Node). These steps can also be used for connecting remotely(though only if within the same network. If connecting from outside the network, you’ll need to do some networking port forwarding/nat voodoo magic)

    1. Set the environment variable for your Master Node:

      $ export MASTERNODE=192.168.122.146               #The IP Address of your master node
      
    2. Create a .kube directory in the home directory and import the kubectl config file

      $ mkdir ~/.kube/
      $ scp basedmeezus@$MASTERNODE:/etc/rancher/k3s/k3s.yaml ~/.kube/config && sed -i 's/127.0.0.1/'"$MASTERNODE"'/g' ~/.kube/config
      
    3. Confirm kubectl is working as expected

      basedmeezus@K3SWorker:~/.kube$ kubectl get all -A
      NAMESPACE     NAME                                          READY   STATUS    RESTARTS   AGE
      kube-system   pod/local-path-provisioner-84bb864455-4r75p   1/1     Running   0          74m
      kube-system   pod/coredns-96cc4f57d-fj4q2                   1/1     Running   0          74m
      kube-system   pod/metrics-server-ff9dbcb6c-xkvng            1/1     Running   0          74m
      
      NAMESPACE     NAME                     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
      default       service/kubernetes       ClusterIP   10.43.0.1      <none>        443/TCP                  75m
      kube-system   service/kube-dns         ClusterIP   10.43.0.10     <none>        53/UDP,53/TCP,9153/TCP   75m
      kube-system   service/metrics-server   ClusterIP   10.43.40.210   <none>        443/TCP                  75m
      
      NAMESPACE     NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
      kube-system   deployment.apps/local-path-provisioner   1/1     1            1           75m
      kube-system   deployment.apps/coredns                  1/1     1            1           75m
      kube-system   deployment.apps/metrics-server           1/1     1            1           75m
      
      NAMESPACE     NAME                                                DESIRED   CURRENT   READY   AGE
      kube-system   replicaset.apps/local-path-provisioner-84bb864455   1         1         1       74m
      kube-system   replicaset.apps/coredns-96cc4f57d                   1         1         1       74m
      kube-system   replicaset.apps/metrics-server-ff9dbcb6c            1         1         1       74m
      
  4. As a final check, let’s go back to the Master Node, and confirm all worker nodes have joined the cluster successfully:

    basedmeezus@K3SMaster:~$ kubectl get nodes -o wide
    NAME         STATUS   ROLES                  AGE   VERSION        INTERNAL-IP       EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
    k3sworker    Ready    <none>                 26m   v1.22.7+k3s1   192.168.122.73    <none>        Ubuntu 20.04.3 LTS   5.4.0-104-generic   containerd://1.5.9-k3s1
    k3smaster    Ready    control-plane,master   79m   v1.22.7+k3s1   192.168.122.146   <none>        Ubuntu 20.04.3 LTS   5.4.0-104-generic   containerd://1.5.9-k3s1
    k3sworker2   Ready    <none>                 61s   v1.22.7+k3s1   192.168.122.117   <none>        Ubuntu 20.04.3 LTS   5.4.0-104-generic   containerd://1.5.9-k3s1