There are a lot of posts about deploying Kubernetes, loads of which are awesome, but long. So, here’s a simple method to deploy, the commands can even be consolidated into a single script for easy running.
For a lab, you should probably also deploy DNS and DHCP Services, these commands should be run to build a small K8S Cluster. It is based on Ubuntu 16.04, but should also work on 18.04 and most debian based distro’s.
First, install Linux, and add OpenSSH Server during the installation, if you miss it during the installation use
sudo apt-get install openssh-server
Then generate a new certificate for the user you are going to be logged on as, this will help with ssh’ing to other servers in the cluster
ssh-keygen -t rsa
(press enter to accept the defaults)
Then copy the cert to the target server to allow password-less authentication
ssh-copy-id <target server username>@<hostname or IP>
- Install Docker
sudo apt-get update
The below works for Ubuntu 18
sudo apt-get install docker.io -y
The below works for Ubuntu 20.04
sudo apt install docker-compose -y
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/docker/daemon.json <<EOF { “exec-opts”: [“native.cgroupdriver=systemd”], “log-driver”: “json-file”, “log-opts”: { “max-size”: “100m” }, “storage-driver”: “overlay2” } EOF
# Start and enable Services
sudo systemctl daemon-reload
- Enable docker (automatically start up after reboot)
sudo systemctl enable docker
sudo systemctl status docker
(optional) if the docker process isnt running then start docker with sudo systemctl start docker
(optional) repeat this on all the nodes which will be part of the cluster
Add the current user to the docker usergroup
sudo groupadd docker
sudo usermod -aG docker $USER
This will only be in effect after a logoff / logon as the host evaluates the
- We are going to be pulling Kubernetes from a non-standard repository, so we need to add the key for that to APT.
(optional) if curl is not install, install it with sudo apt-get curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
Add the kubernetes repository
sudo apt-add-repository “deb http://apt.kubernetes.io/ kubernetes-xenial main”
sudo apt-get update -y
- install the Kubernetes deployment binaries and services Kubeadm, Kubectl, and Kubelet
sudo apt-get install kubeadm kubelet kubectl -y
sudo apt-get mark kubeadm kubelet kubectl
Make sure it works (and check the version with kubeadm version
(optional) repeat this for every server in the cluster)
- Kubelet will not install (or RUN) with a swapfile, you can set it to ignore this but this would leave the host with an unsupported config use the following command
sudo swapoff -a
sudo sed -i.bak -r ‘s/(.+ swap .+)/#\1/’ /etc/fstab
***IMPORTANT*** It’s vital that SWAP is disabled, and commented out in FSTAB or KUBELET will NOT start and random errors will occur. If you struggle with the next step, make sure you have rebooted and that SWAP is commented/removed from the FSTAB (reboot again if necessary)
Use this command to help identify problems starting kubelet
Systemctl status kubelet
journalctl -xeu kubelet
- Initialise the cluster on the master node.
sudo kubeadm init –pod-network-cidr=10.244.0.0/16
upon finishing you will be given the join command, make a note of it.. An example is below
kubeadm join 10.1.1.55:6443 –token tih6ql.usjapbtcyeo4a06c \
–discovery-token-ca-cert-hash sha256:a6c6276e6c348e530c7b165a72ba2de224dd02060e06a1b19d67f6cb8b0296e5
- Setup directories for the cluster
sudo mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- Deploy the pod network (this example is flannel)
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- Verify
Run a few kubectl commands to check stuff..
kubectl get nodes
kubectl get pods –all-namespaces
kubectl get pods
docker ps
Additionally – if you need to add a node within 24 hour of bootstrapping the master server, use the command you coped in step 6. if it is at a later date then you can create another bootstrap token and command, the following below will provide a new node bootstrap command which doesn’t expire ( –ttl=0 ), or set the ttl for example 1m, 5m, 30m , default (removing –ttl) is 24 hours.
sudo kubeadm token create –ttl=0 –print-join-command
Leave a comment