Home Installing Kubernetes on Ubuntu 20
Post
Cancel

Installing Kubernetes on Ubuntu 20

This script assumes you are starting with a brand new Ubuntu Server v20 install.

I would not suggest Ubuntu 22 quite yet.

Run this script on your master and worker nodes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
function usage {
  echo "Please supply the following settings in this order:"
  echo "  hostname"
  echo "  ip address"
  echo "  gateway"
  echo "  nameserver"
}

function banner1 {
  echo "----------------------------------------------------------------"
  echo "$1"
  echo "----------------------------------------------------------------"
}

# export DEBIAN_FRONTEND=noninteractive

FILE=/etc/needrestart/needrestart.conf
if [ -f "$FILE" ]; then
  sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' $FILE
fi

# ====================================================================================================================
if [ ! -f "STEP1" ]; then
  # Stop on error
  set -e

  # Stop on uninitialized variables
  set -u

  # Stop on failed pipes
  set -o pipefail

  # parm check
  if [[ $# -ne 4 ]];
    then
      usage
      exit 1
  fi

  # Just to make sure the password is loaded
  sudo echo "Starting..."

  sudo apt-get update && sudo apt upgrade -y
  sudo apt autoremove -y
  sudo apt clean -y
  touch STEP1
fi

# ====================================================================================================================
if [ ! -f "STEP2" ]; then
  banner1 "Setting up the network..."
  HOSTNAME=$1
  IP_ADDRESS=$2
  GATEWAY=$3
  NAMESERVERS=$4

  INT_NAME=$(ip a | grep "inet " | grep "brd" | rev | cut -d" " -f1 | rev)
  FILE="00-networks.yaml"

  # Display settings
  echo "Hostname: $HOSTNAME"
  echo "IP: $IP_ADDRESS"
  echo "Gateway: $GATEWAY"
  echo "Interface name: $INT_NAME"
  echo "Nameservers: $NAMESERVERS"

  ls -1 /etc/netplan/*.yaml | xargs -I{} sudo mv {} {}.ORG

  echo "Setting static IP address..."
  echo "network:" > $FILE
  echo "  renderer: networkd" >> $FILE
  echo "  version: 2" >> $FILE
  echo "  ethernets:" >> $FILE
  echo "    $INT_NAME:" >> $FILE
  echo "      dhcp4: no" >> $FILE
  echo "      addresses:" >> $FILE
  echo "      - $IP_ADDRESS/24" >> $FILE
  echo "      routes:" >> $FILE
  echo "      - to: default" >> $FILE
  echo "        via: $GATEWAY" >> $FILE
  echo "      nameservers:" >> $FILE
  echo "        addresses:" >> $FILE
  echo "        - $NAMESERVERS" >> $FILE

  sudo mv $FILE /etc/netplan/$FILE

  # Disable IPv6
  echo "net.ipv6.conf.all.disable_ipv6 = 1" > test.dat
  echo "net.ipv6.conf.default.disable_ipv6 = 1" >> test.dat
  cat test.dat | sudo tee –a /etc/sysctl.d/15-disable-ivp6.conf
  rm test.dat
  touch STEP2
fi

# --------------------------------------------------------------------------------------------------------------------
if [ ! -f "STEP3" ]; then
  # Disable swap in fstab
  banner1 "No swap..."
  sed "s/\/swap.img/#\/swap.img/" /etc/fstab > fstab.txt
  sudo cp fstab.txt /etc/fstab
  rm fstab.txt
  sudo swapoff -a

  # Delete the existing installation netplan file if it exists
  if [ -f "/etc/netplan/00-installer-config.yaml" ]; then
    sudo rm /etc/netplan/00-installer-config.yaml
  fi
  touch STEP3
fi

# ====================================================================================================================
if [ ! -f "STEP4" ]; then
# Set hostname
banner1 "Setting hostname..."
sudo hostnamectl set-hostname $1
touch STEP4
fi

# ====================================================================================================================
if [ ! -f "STEP5" ]; then
  banner1 "Installing docker..."
  sudo apt-get update && sudo apt upgrade -y
  sudo apt install docker.io -y
  sudo systemctl enable docker
  sudo systemctl start docker
  if command -v ufw &> /dev/null
  then
    sudo ufw disable
  fi
  touch STEP5
fi

# ====================================================================================================================
if [ ! -f "STEP6" ]; then
  FILE=daemon.json
  sudo echo '{ "exec-opts": ["native.cgroupdriver=systemd"],' > $FILE
  sudo echo '"log-driver": "json-file",' >> $FILE
  sudo echo '"log-opts":' >> $FILE
  sudo echo '{ "max-size": "100m" },' >> $FILE
  sudo echo '"storage-driver": "overlay2"' >> $FILE
  sudo echo '}' >> $FILE
  sudo cp $FILE /etc/docker/$FILE
  rm $FILE
  touch STEP6
fi

# ====================================================================================================================
if [ ! -f "STEP7" ]; then
  banner1 "More installing..."
  sudo apt-get install -y apt-transport-https net-tools nfs-common
  touch STEP7
fi

# ====================================================================================================================
if [ ! -f "STEP8" ]; then
  banner1 "Installing Kubernetes..."
  curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
  sudo bash -c 'echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list'
  sudo apt update && sudo apt-get install -y kubelet kubeadm kubectl
  sudo systemctl enable kubelet
  sudo systemctl start kubelet
  touch STEP8
fi

# ====================================================================================================================
if [ ! -f "STEP9" ]; then
  sudo apt autoremove
  sudo apt clean
  touch STEP9
fi

banner1 "Completed!"
rm STEP?
banner1 "Rebooting..."
sudo reboot
# --------------------------------------------------------------------------------------------------------------------

Run this on the master nodes only

1
2
3
4
5
6
7
8
9
10
11
12
13
# Run this on the master node only:
#!/bin/bash
# Stop on error
set -e
# Stop on uninitialized variables
set -u
# Stop on failed pipes
set -o pipefail

echo Master only!!
sudo kubeadm config images pull

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

After this runs, you will see something like this displayed. Copy this command to a text file as you will need it later.

1
2
kubeadm join 192.168.1.60:6443 --token a_token \
        --discovery-token-ca-cert-hash sha256:a_really_long_key

Setting kubectl to run as a non-root user

1
2
3
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Install networking

1
2
3
4
# get nodes will show NotReady until networking applied and everything if setup in the background.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

watch kubectl get pods --all-namespaces

Do not proceed to the worker nodes until all of the statuses say ‘Running’

Install Metrics Server

This allows the collection of performance data of nodes and pods. Source: https://blog.devgenius.io/how-to-install-metrics-server-on-kubernetes-cluster-60dd754873c2

1
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Be aware that this pod starts very slowly and once started takes 5-10 minutes to start to collect information.

If you are not using TLS certs, you will need to download the above components.yaml file and add the last two lines to it.

1
2
3
4
5
6
7
8
9
10
    spec:
      containers:
      - args:
        - --cert-dir=/tmp
        - --secure-port=4443
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        - --kubelet-use-node-status-port
        - --metric-resolution=15s
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP

Joining the worker nodes to the master node

Run the command you saved off earlier. It will look something like this.

1
2
sudo kubeadm join 192.168.1.60:6443 --token a_token \
        --discovery-token-ca-cert-hash sha256:a_really_long_key

Joining new worker nodes to the master node

The above ‘join’ command is only good got a few hours. After that point, you will need to generate a new ‘join’ link.

1
sudo kubeadm token create --print-join-command
This post is licensed under CC BY 4.0 by the author.