Install and Set Up Kubernetes on Linux
Tags: kubernetes
Published: 2024-06-29 13:58:01

This article is about setting up kubernetes use kubeadm.

### Prerequisites

Prepare two or more servers running Linux in the same network.

#### Set Hostname

```bash
sudo hostnamectl set-hostname k8-smaster
sudo hostnamectl set-hostname k8-node1
sudo hostnamectl set-hostname k8-node2
```

`vim /etc/hosts`

```bash
192.168.x.x k8-smaster
192.168.x.x k8-node1
192.168.x.x k8-node2
```

#### Disable Selinux

Check

```bash
[root@dev-server ~]# getenforce
Disabled
[root@dev-server ~]# /usr/sbin/sestatus -v
SELinux status:                 disabled
```

Temporarily Closed

```bash
##Set SELinux to permissive mode
##setenforce 1 sets SELinux to enforcing mode
setenforce 0
```

Permanently Disable

```bash
vi /etc/selinux/config
```

Change SELINUX=enforcing to SELINUX=disabled. The setting will take effect after a restart.

#### Swap Off

Check

```bash
free -m
```

Temporarily Closed

```bash
swapoff -a
```

Permanently Disable

```bash
sed -ri 's/.*swap.*/#&/' /etc/fstab
```

#### Disable Firewall

```bash
systemctl status firewalld
systemctl stop firewalld
systemctl disable firewalld
chkconfig iptables off
sudo iptables -F
sudo iptables -X
sudo iptables -L
```

#### Enable IPv4 packet forwarding

https://kubernetes.io/docs/setup/production-environment/container-runtimes/

```bash
# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system
```

#### Install Docker

https://docs.docker.com/engine/install/centos/


#### Install CRI

```bash
wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.14/cri-dockerd-0.3.14.amd64.tgz
tar xvf cri-dockerd-0.3.14.amd64.tgz
mv ./cri-dockerd/* /usr/local/bin/

wget https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.service
wget https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.socket
sudo mv cri-docker.socket cri-docker.service /etc/systemd/system/
sudo sed -i -e 's,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,' /etc/systemd/system/cri-docker.service

systemctl daemon-reload
systemctl enable cri-docker.service
systemctl enable --now cri-docker.socket

systemctl status cri-docker.socket
```

#### Installing kubeadm, kubelet and kubectl

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phc7qawkx5olbifivkfd.png)

```bash
kubeadm config images pull --cri-socket unix:///var/run/cri-dockerd.sock
kubeadm version
```

### Init Master Node

```bash
kubeadm init \
--apiserver-advertise-address=192.168.116.133 \
--control-plane-endpoint=k8smaster \
--kubernetes-version=v1.30.2 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16 \
--cri-socket=unix:///var/run/cri-dockerd.sock
```

### Join Node into Master

```bash
kubeadm join k8smaster:6443 --token cgdl45.0c8wq1z9pxez59jr \
        --discovery-token-ca-cert-hash sha256:2fabc33e8c57c01f3214cf69dff97acc54ad6dd727f1255143bf310c4b40e2cb --cri-socket=unix:///var/run/cri-dockerd.sock
```

Next step, install canal and dashboard.