In Part 1 we laid the groundwork for setting up our cluster. Now we will get into actually create the control plane and worker(s). At this stage you should consider what CNI you will be using. For our example we will be using Calico with a CIDR range of 192.187.0.0/16.
TL;DR
1. Initialize your CP. Be sure to copy the join command returned in the output.
$ sudo kubeadm init --pod-network-cidr=192.187.0.0/16
2. Make sure kubectl can work with a non-root user
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. Confirm that your kubectl commands work with the below. You should see a list of pods returned.
$ kubectl get pods -n kube-system
4. Install your CNI. The default CIDR range is 192.168.0.0/16 which is likely to cause a conflict. To avoid this, we will set the range to 192.187.0.0/16 (an unallocated subnet in my environment). First, download the calico yaml and uncomment and edit the value for CALICO_IPV4POOL_CIDR
# The default IPv4 pool to create on startup if none exists. Pod IPs will be
# chosen from this range. Changing this value after installation will have
# no effect. This should fall within `--cluster-cidr`.
- name: CALICO_IPV4POOL_CIDR
value: "192.187.0.0/16"
5. Create the CNI
$ kubectl apply -f calico.yaml
6. Get the join command (Optional if you have retained this from the output of step 1). The join command includes the authentication token and the cert hash
$ kubeadm token list
$ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
7. Join the worker to the cluster
$ sudo kubeadm join --token zsq1vb.q8l3pfv12pokniz0 kube-master:6443 --discovery-token-ca-cert-hash sha256:87e181b4e678a5de7c19cd859447678e2bb40df3b1ead6cfd3dc1689f064b8cb
8. Confirm the node was added by connecting to the control plane and listing the nodes
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
kube-worker1 NotReady <none> 9s v1.22.1
kube-master NotReady control-plane,master 36m v1.22.1