Skip to main content

Setup Kubernetes

·3 mins

This guide provides a general overview of setting up Kubernetes, with specific steps for different operating systems, including Amazon Linux 2.

Prerequisites #

  • A compatible operating system (Amazon Linux 2, Ubuntu, etc.)
  • Administrator access on the system

Step 1: Install Docker #

On Amazon Linux 2 #

sudo yum -y install docker
sudo systemctl start docker

On Ubuntu #

sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker

Configuring Docker for Non-Root User (Common for All OS) #

  1. Create the Docker group:
    sudo groupadd docker
    
  2. Add your user to the Docker group:
    sudo usermod -aG docker $USER
    
  3. Apply the group changes:
    newgrp docker
    

Step 2: Install Kubernetes Tools #

You will need kubectl, kubelet, and kubeadm.

On Amazon Linux 2 #

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo systemctl enable --now kubelet

On Ubuntu #

sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Step 3: Preparing the Environment (Common for All OS) #

  1. Disable SELinux (if applicable):

    sudo setenforce 0
    sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    
  2. Disable Swap:

    sudo swapoff -a
    sudo sed -i '/swap/d' /etc/fstab
    
  3. Configure Sysctl for Network Settings:

    cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sudo sysctl --system
    

Step 4: Initializing the Kubernetes Cluster (Common for All OS) #

sudo kubeadm init

Follow the on-screen instructions to configure kubectl for cluster management.

Step 5: Configuring Kubectl (Common for All OS) #

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 6: Selecting and Installing a Pod Network #

Choosing a Pod Network #

When setting up your Kubernetes cluster, it’s essential to select a pod network that fits your specific requirements. Kubernetes supports a variety of network options, which you can explore in detail in the Kubernetes Networking and Network Policy documentation.

Installing Calico as the Pod Network #

For the purposes of this guide, we’ll be using Calico as our pod network. Calico is a popular choice for Kubernetes networking, providing high performance and easy scalability.

To install Calico, run the following command in your terminal:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

This command will apply the Calico manifest from its official documentation, setting up Calico as the networking solution for your Kubernetes cluster.

Step 7: Adding Worker Nodes to the Cluster (Optional) #

This step is optional and applicable to all operating systems. It involves adding worker nodes to your Kubernetes cluster.

Joining Worker Nodes to the Cluster #

  1. Initial Setup on Worker Nodes: If you have one or more worker nodes, you need to follow steps 1 to 3 as previously outlined in this guide for each worker node.
  2. Running the Join Command: On each worker node, execute the join command provided by the output of kubeadm init command you ran on the master node. This command will securely join the worker nodes to your Kubernetes cluster.

Verifying the Worker Nodes #

After joining the worker nodes to the cluster, it’s important to verify if they have been successfully added. Run the following command on the master node to list all the nodes in your cluster, including the master and worker nodes:

kubectl get nodes

This command will display the status of each node, ensuring that your worker nodes are properly connected and functioning within the cluster.