Single node Kubernetes on CentOS
Are you looking for setting up a quick Kubernetes cluster for testing some App without paying for a hosted Kubernetes like IKS, AKS, GKE, EKS ?
Then you can read on here… This post will help you setup a Kubernetes in a few mins.
Important Notes:
- The steps described here are only for sample testing/demo purpose.
- Do not use this configuration for a Dev/Production environment.
- I am using
root
user (Not recommended).
Prerequisites:
- You need a virtual machine with internet connectivity and more than one CPU. This can be either on a Laptop (Like VirtualBOX, KVM), Your Datacenter, VMware, or on any Cloud (IBM,Azure, GCP, EC2,…)
Step 1: System Pre-requisites
I am disabling Firewall and SELINUX so that it wont interfere with the Kubernetes. If you want firewall to be on, make sure that the necessary ports are added to firewall configuration.
service firewalld stop
chkconfig firewalld off
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
Disabling Swap. If swap is not disabled, kubelet service will not start on the masters and nodes
swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Step 2: Install Docker
Reference: https://docs.docker.com/engine/install/centos.
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io -y
service docker start
chkconfig docker on
Step 3: Install kubeadm
Reference: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm
Letting iptables see bridged traffic,
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
Adding the kubernetes repo,
cat <<EOF > /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
exclude=kubelet kubeadm kubectl
EOF
Option 1 : Install kubelet, kubectl and kubeadm, (Latest Version)
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
Option 2 : Install kubelet, kubectl and kubeadm, (Specific Version). In this case v1.21.1
yum install -y kubelet-1.21.1 kubeadm-1.21.1 kubectl 1.21.1 --disableexcludes=kubernetes
systemctl enable --now kubelet
Update: With the latest Kubernetes version (v1.24) to work, you need to run the following command. Thanks Paulo Cabrita for pointing this out.
containerd config default > /etc/containerd/config.toml
systemctl restart containerd
Step 4: Start the cluster
Start Kubeadm with the following parameters. We are using Calico as the network here.
kubeadm init --pod-network-cidr=192.168.0.0/16
This will take a few minutes to pull the images and start the cluster.
Step 5: Post install configurations
Setup the kubectl
command,
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Install Calico network,
kubectl apply -f https://docs.projectcalico.org/v3.11/manifests/calico.yaml
By default, you wont be able to run PoDs in the master nodes. Since we have a single node cluster, we should enable this,
kubectl taint nodes --all node-role.kubernetes.io/master-
Step 6: Install the kubernetes Dashboard
Reference : https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
By default the service will be running on ClusterIP. Change it to NodePort by following command,
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
This will open up a vi
screen. Search for the entry ClusterIP
and replace it with NodePort
(Case sensitive)
Run the following command to find out the port on which it will listen,
kubectl get svc -n kubernetes-dashboard|grep NodePort|awk '{print $5}'|cut -f 2 -d :|cut -f 1 -d /
This will return a port number . For example, in my case it returned 31967
.
Get the token for login by the following command,
kubectl create serviceaccount cluster-admin-dashboard-sakubectl create clusterrolebinding cluster-admin-dashboard-sa \
--clusterrole=cluster-admin \
--serviceaccount=default:cluster-admin-dashboard-sakubectl describe secret $(kubectl get secret | awk '/^cluster-admin-dashboard-sa-/{print $1}') | awk '$1=="token:"{print $2}'
Now you can access the Kubernetes dashboard on https://<vm_ipaddress>:31967
. Use the token got in the previous command.
Enjoy using your kubernetes environment.