-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44e8157
commit a19b439
Showing
2 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Kubernetes Setup Script | ||
|
||
This script automates the setup and configuration of a Kubernetes environment on a Linux system. It handles kernel module loading, system configuration, and container runtime adjustments. | ||
|
||
## Overview | ||
|
||
This script performs the following tasks: | ||
1. **Installs the kernel-devel package**: Ensures compatibility with the current kernel version. | ||
2. **Loads necessary kernel modules**: Modules required for Kubernetes networking and container management. | ||
3. **Creates configuration files**: Sets up Kubernetes-specific configurations in `/etc/modules-load.d` and `/etc/sysctl.d`. | ||
4. **Disables swap**: Updates `/etc/fstab` to prevent swap usage, which is necessary for Kubernetes. | ||
5. **Updates containerd configuration**: Ensures `SystemdCgroup` is set to `true` for containerd. | ||
6. **Reboots the system**: Applies changes by rebooting the machine. | ||
|
||
## Prerequisites | ||
|
||
- A Linux-based operating system (e.g., RHEL, CentOS). | ||
- `dnf` package manager (for RHEL/CentOS 8 and later). | ||
- Root or sudo access to execute system commands. | ||
|
||
## Usage | ||
|
||
1. **Clone the Repository**: If you haven't already, clone the repository containing this script. | ||
|
||
```bash | ||
git clone <repository-url> | ||
cd <repository-directory> | ||
``` | ||
|
||
2. **Make the Script Executable**: | ||
|
||
```bash | ||
chmod +x setup-kubernetes.sh | ||
``` | ||
|
||
3. **Run the Script**: | ||
|
||
```bash | ||
sudo ./setup-kubernetes.sh | ||
``` | ||
|
||
The script will prompt you with progress messages and verify each step's success. | ||
## Script Details | ||
### Install kernel-devel Package | ||
Installs the kernel development package matching the current kernel version to ensure compatibility. | ||
### Load Kernel Modules | ||
Loads necessary kernel modules required for Kubernetes. These include: | ||
- `br_netfilter` | ||
- `ip_vs` | ||
- `ip_vs_rr` | ||
- `ip_vs_wrr` | ||
- `ip_vs_sh` | ||
- `overlay` | ||
### Create Configuration Files | ||
- **/etc/modules-load.d/kubernetes.conf**: Specifies kernel modules to load at boot. | ||
- **/etc/sysctl.d/kubernetes.conf**: Configures sysctl settings for Kubernetes networking. | ||
### Disable Swap | ||
Disables swap immediately and comments out swap entries in `/etc/fstab` to prevent swap usage, which is required for Kubernetes. | ||
### Update Containerd Configuration | ||
Updates the containerd configuration to set `SystemdCgroup` to `true`, ensuring proper cgroup management for containers. | ||
### Reboot System | ||
Reboots the system to apply changes and ensure all configurations are active. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/bin/bash | ||
|
||
# Exit on error | ||
set -e | ||
|
||
# Retry function with verification and output capture | ||
retry_command() { | ||
local cmd="$1" | ||
local verification_cmd="$2" | ||
local verification_msg="$3" | ||
local retries=2 | ||
local count=0 | ||
local success=0 | ||
|
||
while [ $count -lt $retries ]; do | ||
echo "Attempt $((count + 1)) for command: $cmd" | ||
if eval "$cmd"; then | ||
echo "Verification: $verification_cmd" | ||
local verification_output | ||
verification_output=$(eval "$verification_cmd" 2>&1) | ||
if [ $? -eq 0 ]; then | ||
echo "$verification_msg" | ||
success=1 | ||
break | ||
else | ||
echo "Verification failed. Output:" | ||
echo "$verification_output" | ||
echo "Retrying..." | ||
fi | ||
else | ||
echo "Command failed. Retrying..." | ||
fi | ||
count=$((count + 1)) | ||
sleep 2 | ||
done | ||
|
||
if [ $success -ne 1 ]; then | ||
echo "Command failed after $retries attempts. Please try this manually: $cmd" | ||
exit 1 | ||
fi | ||
} | ||
|
||
# Install kernel-devel package for the current kernel version | ||
echo "====================" | ||
echo "Installing kernel-devel package..." | ||
echo "====================" | ||
retry_command "sudo dnf install -y kernel-devel-$(uname -r)" "rpm -q kernel-devel-$(uname -r)" "Kernel-devel package installed successfully." | ||
echo "--------------------" | ||
|
||
# Load necessary kernel modules | ||
echo "====================" | ||
echo "Loading kernel modules..." | ||
echo "====================" | ||
retry_command "sudo modprobe br_netfilter" "lsmod | grep br_netfilter" "Module br_netfilter loaded successfully." | ||
retry_command "sudo modprobe ip_vs" "lsmod | grep ip_vs" "Module ip_vs loaded successfully." | ||
retry_command "sudo modprobe ip_vs_rr" "lsmod | grep ip_vs_rr" "Module ip_vs_rr loaded successfully." | ||
retry_command "sudo modprobe ip_vs_wrr" "lsmod | grep ip_vs_wrr" "Module ip_vs_wrr loaded successfully." | ||
retry_command "sudo modprobe ip_vs_sh" "lsmod | grep ip_vs_sh" "Module ip_vs_sh loaded successfully." | ||
retry_command "sudo modprobe overlay" "lsmod | grep overlay" "Module overlay loaded successfully." | ||
echo "--------------------" | ||
|
||
# Create configuration files for Kubernetes | ||
echo "====================" | ||
echo "Creating /etc/modules-load.d/kubernetes.conf..." | ||
echo "====================" | ||
retry_command "cat > /etc/modules-load.d/kubernetes.conf << EOF | ||
br_netfilter | ||
ip_vs | ||
ip_vs_rr | ||
ip_vs_wrr | ||
ip_vs_sh | ||
overlay | ||
EOF" "grep 'br_netfilter' /etc/modules-load.d/kubernetes.conf && grep 'ip_vs' /etc/modules-load.d/kubernetes.conf && grep 'ip_vs_rr' /etc/modules-load.d/kubernetes.conf && grep 'ip_vs_wrr' /etc/modules-load.d/kubernetes.conf && grep 'ip_vs_sh' /etc/modules-load.d/kubernetes.conf && grep 'overlay' /etc/modules-load.d/kubernetes.conf" "Configuration file /etc/modules-load.d/kubernetes.conf created successfully." | ||
echo "--------------------" | ||
|
||
echo "====================" | ||
echo "Creating /etc/sysctl.d/kubernetes.conf..." | ||
echo "====================" | ||
retry_command "cat > /etc/sysctl.d/kubernetes.conf << EOF | ||
net.ipv4.ip_forward = 1 | ||
net.bridge.bridge-nf-call-ip6tables = 1 | ||
net.bridge.bridge-nf-call-iptables = 1 | ||
EOF" "grep 'net.ipv4.ip_forward = 1' /etc/sysctl.d/kubernetes.conf && grep 'net.bridge.bridge-nf-call-ip6tables = 1' /etc/sysctl.d/kubernetes.conf && grep 'net.bridge.bridge-nf-call-iptables = 1' /etc/sysctl.d/kubernetes.conf" "Configuration file /etc/sysctl.d/kubernetes.conf created successfully." | ||
echo "--------------------" | ||
|
||
# Disable swap by commenting out the swap line in /etc/fstab | ||
echo "====================" | ||
echo "Disabling swap..." | ||
echo "====================" | ||
# Disable swap immediately | ||
sudo swapoff -a | ||
echo "Swap disabled successfully." | ||
# Comment out swap entries in /etc/fstab | ||
#retry_command "sudo sed -i '/swap/s/^/#/' /etc/fstab" "grep '^#' /etc/fstab" "Swap disabled successfully." | ||
echo "--------------------" | ||
|
||
# Update containerd config | ||
echo "====================" | ||
echo "Updating containerd configuration..." | ||
echo "====================" | ||
# Remove existing systemd_cgroup setting if present | ||
retry_command "sudo sed -i '/systemd_cgroup/d' /etc/containerd/config.toml" "grep 'systemd_cgroup' /etc/containerd/config.toml || true" "Existing systemd_cgroup setting removed successfully." | ||
# Set SystemdCgroup = true | ||
retry_command "sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml" "grep 'SystemdCgroup = true' /etc/containerd/config.toml" "Containerd configuration updated successfully." | ||
echo "--------------------" | ||
|
||
# Reboot the system | ||
echo "====================" | ||
echo "Rebooting the system..." | ||
echo "====================" | ||
retry_command "sudo systemctl reboot" "systemctl is-system-running --quiet" "System reboot initiated successfully." |