Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Last active June 17, 2024 18:50
Show Gist options
  • Save Cdaprod/97d23164ed664ebb41a03d0d82bc7950 to your computer and use it in GitHub Desktop.
Save Cdaprod/97d23164ed664ebb41a03d0d82bc7950 to your computer and use it in GitHub Desktop.
Install Kubernetes on Ubuntu

Based on the information gathered from multiple sources, it appears that using a /24 CIDR for the Weave Net pod network should work, but there are a few considerations to ensure proper configuration.

Key Steps to Configure Weave Net with a /24 CIDR

  1. Remove Incorrect Repository and Add Correct One: Ensure you remove any existing incorrect Kubernetes repositories and add the correct one for your version of Ubuntu.

  2. Disable Swap: Disabling swap is necessary for Kubernetes to function correctly.

  3. Initialize Kubernetes Cluster with /24 CIDR: Use the appropriate pod network CIDR during initialization.

  4. Configure Weave Net: Ensure Weave Net is configured with the same pod network CIDR.

Revised Script

Here is the revised script to set up Kubernetes with a /24 pod network CIDR and install Weave Net accordingly:

#!/bin/bash

# Ensure the script is run as root
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 
    exit 1
fi

# Update and upgrade the system
apt update && apt upgrade -y

# Install Docker
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update
apt install -y docker-ce
systemctl enable docker
systemctl start docker

# Remove the incorrect Kubernetes repository if it exists
if [ -f /etc/apt/sources.list.d/kubernetes.list ]; then
    rm /etc/apt/sources.list.d/kubernetes.list
fi

# Add the correct Kubernetes repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF'

# Update package list
apt update

# Install Kubernetes components
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

# Disable swap
swapoff -a
sed -i '/ swap / s/^/#/' /etc/fstab

# Initialize Kubernetes master with /24 pod network CIDR
kubeadm init --pod-network-cidr=192.168.0.0/24

# Setup kubeconfig for the ubuntu user
mkdir -p /home/ubuntu/.kube
cp -i /etc/kubernetes/admin.conf /home/ubuntu/.kube/config
chown $(id -u ubuntu):$(id -g ubuntu) /home/ubuntu/.kube/config

# Install Weave Net network plugin with /24 network range
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=192.168.0.0/24"

# Output join command
kubeadm token create --print-join-command > /joincluster.sh

echo "Kubernetes master setup is complete."
echo "Run the following command on worker nodes to join the cluster:"
cat /joincluster.sh

Notes

  • Ensure IP Forwarding is Enabled: IP forwarding must be enabled on all nodes for Weave Net to function correctly.

    sudo sysctl net.ipv4.conf.all.forwarding=1
    echo "net.ipv4.conf.all.forwarding=1" | sudo tee -a /etc/sysctl.conf
  • Verify Weave Net Pods: After applying the Weave Net configuration, verify that the Weave Net pods are running.

    kubectl get pods -n kube-system

These steps and script adjustments should help you set up your Kubernetes cluster with a /24 CIDR and properly configure Weave Net oai_citation:1,Weave Net for NetworkPolicy | Kubernetes oai_citation:2,Setting Up Kubernetes Networking with Weave Net oai_citation:3,Integrating Kubernetes via the Addon | Weave Net oai_citation:4,Pod-network-cidr and ip range allocation in CNI plugin - General Discussions - Discuss Kubernetes oai_citation:5,Weave Net for NetworkPolicy | Kubernetes.

#!/bin/bash
# Ensure the script is run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
# Update and upgrade the system
apt update && apt upgrade -y
# Install Docker
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update
apt install -y docker-ce
systemctl enable docker
systemctl start docker
# Remove the incorrect Kubernetes repository if it exists
if [ -f /etc/apt/sources.list.d/kubernetes.list ]; then
rm /etc/apt/sources.list.d/kubernetes.list
fi
# Ensure correct permissions for sources.list.d directory
chmod 755 /etc/apt/sources.list.d/
# Add the correct Kubernetes repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF'
# Update package list
apt update
# Install Kubernetes components
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
# Disable swap
swapoff -a
sed -i '/ swap / s/^/#/' /etc/fstab
# Initialize Kubernetes master with /24 pod network CIDR
kubeadm init --pod-network-cidr=192.168.0.0/24
# Setup kubeconfig for the ubuntu user
mkdir -p /home/ubuntu/.kube
cp -i /etc/kubernetes/admin.conf /home/ubuntu/.kube/config
chown $(id -u ubuntu):$(id -g ubuntu) /home/ubuntu/.kube/config
# Install Weave Net network plugin with /24 network range
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=192.168.0.0/24"
# Output join command
kubeadm token create --print-join-command > /joincluster.sh
echo "Kubernetes master setup is complete."
echo "Run the following command on worker nodes to join the cluster:"
cat /joincluster.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment