Kubernetes: A Beginner's Guide | Mohammad Mahdi Afshar | Senior Software Developer
Back to blogArticle
Kubernetes: A Beginner's Guide A simple guide to install Kubernetes on your VM. Learn how to set up a complete Kubernetes cluster from scratch with step-by-step instructions.
Kubernetes Installation Guide for beginners this guide is for begginers. like me.
it's the simplest way to install Kubernetes on your VM, that i've used to install my Cluster. it took me 8 hours to research and install it.
Prerequisites Creating a Cluster ready machine. Step 1: Make sure you have a freshly installed VM. if you dont, you might face some errors. i had those issues and i decided to do it on a fresh VM.
Step 2: Update your repositories and packages sudo apt update && sudo apt upgrade -y
bash Step 3: Disable swap and enable some kernel configurations
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
bash these kernel configurations are required for Kubernetes to work properly.
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo /etc/sysctl.d/kubernetes.conf <<
sysctl --system
bash tee
EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.ipv4.ip_unprivileged_port_start=0
EOF
sudo
net.ipv4.ip_unprivileged_port_start=0 seems to be important to use traefik. otherwise you'll get bind to:80/443 error.
when i was trying to configure my Cluster, i faced this issue. that traefik was failing to bind to :80/443.
i had to set net.ipv4.ip_unprivileged_port_start=0 and then, everything worked fine.
Step 4: Add the required repositories always update your repositories and install the required packages.
then install curl, software-properties-common, apt-transport-https, and ca-certificates.
sudo apt install -y curl software-properties-common apt-transport-https ca-certificates
bash now add the kubernetes, docker, and containerd reposetories.
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
bash
sudo apt install -y containerd.io
bash then write the configuration to /etc/containerd/config.toml and set SystemdCgroup = true
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
bash sudo systemctl restart containerd
sudo systemctl enable containerd
bash
Step 6: Install kubernetes packages sudo apt install -y kubelet kubeadm kubectl
bash use apt-mark to keep them from being updated, deleted or changed.
sudo apt-mark hold kubelet kubeadm kubectl
bash
Creating a Cluster
Step 1: Initialize the node first get the machine ip address.
export MACHINE_IP=$(hostname -I | awk '{print $1}' )
bash then initialize the node. (you could just initialize it with sudo kubeadm init but i added some flags to it to make it easier to debug)
sudo kubeadm init --pod-network-cidr=10.96.0.0/12 --control-plane-endpoint=${MACHINE_IP} --v=5
bash Don't worry about the join token, we can create it later again using :)
sudo kubeadm token create --print-join-command
bash
Step 2: Create the kubeconfig file mkdir -p $HOME /.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME /.kube/config
sudo chown $(id -u):$(id -g) $HOME /.kube/config
bash
Step 3: Apply the network configuration i'd like to use calico for the network configuration. you can use any other network plugin if you want. like flannel, cilium, etc.
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
bash
Step 4: Wait for the nodes to be ready (you have to wait for network pods to be ready) watch kubectl get nodes -A
bash Congratulations! you've created your first Kubernetes cluster.
if you want to add more nodes (it should always be an odd number), you have to repeat the initialization process on the new node. and then use the join command that we created earlier. to join the new node to the cluster.
Step 1: Install helm i'd like to install helm using apt. you can do it how ever you want.
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
bash then update the package manager
sudo apt install helm
bash
Step 2: Install ingress i'd like to use traefik for the ingress. you can use any other ingress controller if you want. like nginx, etc.
helm install traefik traefik/traefik --namespace traefik --create-namespace -f traefik-values.yaml
bash i'd like to use my own values file for the traefik. you can use the default values file if you want.
ports:
web:
port: 80
websecure:
port: 443
service:
type: LoadBalancer
additionalArguments:
- "--api.dashboard=true"
yaml
Step 3: Install longhorn to manage your storage helm install longhorn longhorn-system/longhorn-manager --namespace longhorn-system --create-namespace
bash
Step 4: Install Rancher you can use rancher how ever you want. but this is how i do it:
first, add the rancher repo
helm repo add rancher-stable https://releases.rancher.com/server-charts/stable
bash kubectl create namespace cattle-system
bash then we may need a cert-manager if we don't have one already, we can apply the following command to install it
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.3/cert-manager.yaml
bash helm repo add jetstack https://charts.jetstack.io
bash helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace
bash helm install rancher rancher-stable/rancher \
--namespace cattle-system --create-namespace \
--set hostname=${RANCHER_DOMAIN} \
--set ingress.tls.source=secret \
--set ingress.tls.secretName=rancher-cert-tls \
--set bootstrapPassword=admin
bash replace ${RANCHER_DOMAIN} with your domain name.
and then apply a certificate
kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: rancher-cert
namespace: cattle-system
spec:
secretName: rancher-cert-tls
issuerRef:
name: letsencrypt-prod
kind: ClusterIssuer
dnsNames:
- ${RANCHER_DOMAIN}
EOF
bash wait for rancher to be ready, then goto https://${RANCHER_DOMAIN}/dashboard?setup=admin and login with admin and admin.
then you'll see a page that says You have access to the dashboard.
click on it and you'll see a page that says You have access to the dashboard.
UnTainted your node if you're using a Single node cluster set the node to untainted
export NODE_NAME=$(hostname)
bash kubectl taint nodes ${NODE_NAME} node-role.kubernetes.io/control-plane-
bash if you ever want to taint the node again, you can use the following command:
kubectl taint nodes ${NODE_NAME} node-role.kubernetes.io/control-plane:NoSchedule
bash
should you really use Kubernetes? Kubernetes is a complex system that requires a lot of resources to maintain.
if you have multiple vms and you do need to manage them and have a lot of services, then yes, you should use Kubernetes.
but if you have a single vm and you do need to manage a few services, then you don't need to use Kubernetes.
and remember, it's always easier to pay a DevOps Engineer to initiate your Cluster. then all you have to do is to manage it :)
Share this post X LinkedIn Facebook Copy link