K8S Installation Script
- RNREDDY

- Sep 1
- 2 min read

#!/bin/bash
set -euo pipefail
# Define network and host details
NIC="ens160"
IP="192.168.1.24/24"
GW="192.168.1.1"
DNS="192.168.1.1"
# Configure static IP
nmcli connection modify $NIC ipv4.addresses "$IP"
nmcli connection modify $NIC ipv4.gateway "$GW"
nmcli connection modify $NIC ipv4.dns "$DNS"
nmcli connection modify $NIC ipv4.method manual
nmcli connection down $NIC || true
nmcli connection up $NIC
# Verify
nmcli device show $NIC
# Update /etc/hosts safely
declare -A HOST_ENTRIES=(
["192.168.1.18"]="k8s-master"
["192.168.1.15"]="k8s-node1"
["192.168.1.19"]="k8s-node2"
["192.168.1.24"]="mini"
)
for ip in "${!HOST_ENTRIES[@]}"; do
host="${HOST_ENTRIES[$ip]}"
grep -q "$ip" /etc/hosts || echo "$ip $host" | sudo tee -a /etc/hosts
done
# System update
dnf -y update
# Disable firewall and swap
systemctl disable --now firewalld
swapoff -a
sed -i '/swap/d' /etc/fstab
# Disable SELinux
setenforce 0 || true
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# Enable kernel modules
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
# Set sysctl params
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
# Verify modules
lsmod | grep overlay || echo "overlay not loaded"
lsmod | grep br_netfilter || echo "br_netfilter not loaded"
# Install containerd dependencies
dnf install -y yum-utils device-mapper-persistent-data lvm2
# Add Docker repo for containerd
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Install containerd
dnf install -y containerd.io
# Configure containerd
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml > /dev/null
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
# Enable containerd
systemctl daemon-reexec
systemctl enable --now containerd
containerd --version
systemctl status containerd --no-pager
# Add Kubernetes YUM repo
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
enabled=1
gpgcheck=1
exclude=kubelet kubeadm kubectl
EOF
# Update and install Kubernetes tools
dnf update -y
dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# Enable kubelet
systemctl enable --now kubelet
systemctl status kubelet --no-pager
k8s installation script.txt
Displaying k8s installation script.txt.



Comments