This script will install Kubernetes on Ubuntu 22.
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
set -e
set -u
set -o pipefail
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 "----------------------------------------------------------------"
}
# --------------------------------------------------------------------------------------------------
if [ ! -f "STEP1" ]; then
banner1 "Initial steps..."
FILE=/etc/needrestart/needrestart.conf
if [ -f "$FILE" ]; then
sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' $FILE
fi
# 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 "qlen " | grep -v "lo" | cut -d ":" -f 2 | cut -d " " -f 2)
FILE="00-networks.yaml"
# Display settings
echo "Hostname: $HOSTNAME"
echo "IP: $IP_ADDRESS"
echo "Gateway: $GATEWAY"
echo "Interface name: $INT_NAME"
echo "Nameservers: $NAMESERVERS"
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
ls -1 /etc/netplan/*.yaml | xargs -I{} sudo mv {} {}.ORG
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
sudo swapoff -a
sudo sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab
touch STEP3
fi
# --------------------------------------------------------------------------------------------------
if [ ! -f "STEP4" ]; then
# Set hostname
banner1 "Setting hostname..."
sudo hostnamectl set-hostname $1
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
touch STEP4
fi
# --------------------------------------------------------------------------------------------------
if [ ! -f "STEP5" ]; then
banner1 "Installing..."
sudo apt-get -y install curl gnupg2 software-properties-common apt-transport-https ca-certificates
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get -y autoremove
sudo apt-get -y install containerd.io
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
touch STEP5
fi
# --------------------------------------------------------------------------------------------------
if [ ! -f "STEP6" ]; then
sudo wget -q -O /etc/apt/trusted.gpg.d/apt-key.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
#echo " " > /etc/apt/sources.list
sudo apt-add-repository -y -S "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
#sudo kubeadm config images pull
#sudo kubeadm init --control-plane-endpoint=192.168.1.40
touch STEP6
fi
# --------------------------------------------------------------------------------------------------
if [ ! -f "STEP7" ]; then
sudo apt autoremove
sudo apt clean
touch STEP7
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
If you are not using TLS certs, you will need to download this 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
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.
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