Kubernetes – Install and Test on CentOS8

This is how to install Kubernetes on the Linux machine (CentOS 8) – Master Node + Worker Node

Prerequisite – Install Docker

Docker – Install on CentOS 8

docker version

Check the OS

cat /etc/redhat-release

Switch User to root

sudo su

Update the System

dnf check-update

dnf -y upgrade

Disable SELinux

  • SELinux (Security Enhanced Linux) is a Linux kernel security module
    • Enforcing: SELinux allows access based on SELinux policy rules. (default)
    • Permissive: SELinux only logs actions that would have been denied if running in enforcing mode.
    • Disabled: No SELinux policy is loaded.
  • The containers need to access the host file system
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

Enable the br_netfilter module for cluster communication

  • This ensures that packets are properly processed by IP tables during filtering and port forwarding.
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
  • Enable IP masquerade at the firewall
  • Enable ports that are used by Kubernetes at the firewall
firewall-cmd --add-masquerade --permanent
firewall-cmd --zone=public --permanent --add-port={6443,2379,2380,10250,10251,10252}/tcp
firewall-cmd --reload

Disable swap to prevent memory allocation issues

swapoff -a

vim /etc/fstab
  • Comment out the swap line only if it exists

Add the Kubernetes repo

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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

Install Kubernetes

dnf install -y kubelet kubeadm kubectl

Check the docker

systemctl enable docker
systemctl start docker

docker image ls

Enable Kubernetes

  • The kubelet service will not start until you run kubeadm init.
systemctl enable kubelet
systemctl start kubelet

Fix Configuration

rm /etc/containerd/config.toml
systemctl restart containerd

Initialize the cluster using the IP range for Flannel – Master Only

  • This is only for the master node. Do not run this on a worker node.
kubeadm init --pod-network-cidr=10.244.0.0/16
  • Copies the last command “kubeadmin join”

Exit sudo and Copy the config – Manage Cluster as Regular User – Master Only

exit

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

Check the Installation – Master

kubectl get nodes
  • There is 1 node and its status is “NotReady”

Deploy Flannel – Master Only

  • A Pod Network allows nodes within the cluster to communicate. The flannel virtual network add-on is used for this purpose.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl get nodes
  • Now the node is ready
    • If not, wait a little bit and run “kubectl get nodes” command again

Join worker nodes to master – Worker

  • This is only for the worker node.
  • In the worker node, run the “kubeadm join” command copied before
exit

sudo kubeadm join 172.31.115.9:6443 --token lzbl14.prno5dl0kxm73jee --discovery-token-ca-cert-hash sha256:1f73ceb22b2af3bec315d35abd313f19576d96bfd0cee771df2f9763f8bb00f7

Check the cluster State – Master

kubectl get nodes

kubectl get pods --all-namespaces

kubectl cluster-info
  • You can see all nodes (master + workers)

Testing

Create a simple pod running an nginx container

cat << EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80 
EOF

Get a list of pods and verify that your new nginx pod is in the Running state

kubectl get pods

kubectl describe pod nginx

Get the IP addresses of your pods

kubectl get pods -o wide
  • Get the ip address of the pod
  • Log in to the corresponding worker node and curl to the ip == > you can get the nginx welcome page
    • Or in the master node, you can use the kubectl exec command.
kubectl exec nginx -- curl 10.244.1.2


Delete the pod

kubectl delete pod nginx


Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s