Kubernetes Installation in RedHat/CentOS
In this article, take a look at a tutorial on Kubernetes installation in RedHat/CentOS.
Join the DZone community and get the full member experience.
Join For FreeWelcome! In this article, we are going to look at how to configure Kubernetes cluster for container orchestration.
Before reading this article, you should know basic core concepts of kubernetes components and basic administration in Redhat OS or CentOS
Prerequisites:
- Hosts - 2 or 3 Machines (node) requiredRAM - 4 GB
- Storage - 50 GB
- CPU - 2 CPU
Let’s start how to install and configure Kubernetes in Redhat Enterprise Linux.
Here we are going to configure Kubernetes cluster using 4 VM’s.
- One VM – Master Node
- Other three VM’s – Worker Nodes
Steps for Kubernetes Cluster Configuration
Step1: Set Hostname with its IP address
Add the Hostname of all Hosts with those IP address (Consider all Hosts or VM as Nodes)
Run the below command to go Hosts file location to change host name with its IP
xxxxxxxxxx
nano /etc/hosts
“Hostname with its IP (We need separate IP for each host or node)”
We just gave the below names
- Master node name – k8master
- Worker nodes name – knode1, knode2, knode3
Step 2: Update OS
Keep the OS Up to date
Run the below Command to update OS
xxxxxxxxxx
yum update -y
Step 3: Disable SElinux
By disabling the SElinux, all containers can easily access the host filesystem.
We can Disable SElinux by two methods
- Run the below command
xxxxxxxxxx
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
- Go to SELinux configuration file and disable it
Run the below command
xxxxxxxxxx
nano /etc/sysconfig/selinux and type SELINUX=disabled
Step 4: Disable or Off the SWAP
By disabling the SWAP, kubelet will work perfectly.
Run the below command to disable SWAP:
xxxxxxxxxx
swapoff -a && sed -i '/swap/d' /etc/fstab
Step 5: To Allow Ports in Firewall or Disable Firewall
By allowing the below ports or disabling firewall, all containers, network drivers, and pods are communicating across the kubernetes cluster properly
Run the following command to allow ports in firewall:
xxxxxxxxxx
firewall-cmd --permanent --add-port=6443/tcp
firewall-cmd --permanent --add-port=2379-2380/tcp
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=10251/tcp
firewall-cmd --permanent --add-port=10252/tcp
firewall-cmd --permanent --add-port=10255/tcp
firewall-cmd –-reload
Run the below command to disable firewall (This step is not recommended for production environment, but in this article, we are going to do disable firewall)
xxxxxxxxxx
systemctl stop firewalld
systemctl disable firewalld
Step 6: To update the IP Tables run the following command
By updating IP Tables, Port forwarding and Filtering process will work perfectly
Run the below command to update the IP tables:
xxxxxxxxxx
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
Step 7: To Install Docker and Kubernetes in nodes, need to configure docker and Kubernetes repositories
Kubernetes: - Run the below command to add Kubernetes repo
xxxxxxxxxx
nano /etc/yum.repos.d/kubernetes.repo
Paste the below details in nano editor
xxxxxxxxxx
[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
You can see below screenshot I have added the above details in all four VM’s and save the file
Docker:- Run the below command to add docker repo
xxxxxxxxxx
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
To download external packages:- Run the below command to get all docker and Kubernetes packages from docker and google repos without any issues
xxxxxxxxxx
subscription-manager repos --enable=rhel-7-server-extras-rpms
Up to this, you can see all the steps in below snap.
Step 8: To install the docker and Kubernetes components
Run the following command to install the Kubenetes/Docker (kublet kubeadm kubectl docker)
xxxxxxxxxx
yum install kubelet kubeadm kubectl docker -y
Step 9: To start and enable Kubernetes and docker services
Run the below commands to start:
xxxxxxxxxx
systemctl start docker && systemctl enable docker
xxxxxxxxxx
systemctl start kubelet && systemctl enable kubelet
Step 10: To run cluster configuration in Master node, this step should follow only in master node
Run the below command to start cluster configuration in master node
xxxxxxxxxx
kubeadm init --apiserver-advertise-address=10.1.5.46 --ignore-preflight-errors all --pod-network-cidr=10.244.0.0/16 --token-ttl 0
apiserver address must be masternode IP (10.1.5.46) address
You can see the below output
After the successful start of kubadm master, we need to run the above-shown command from the non-root or root user then only a user can control the kubectl commands.
Run the command
xxxxxxxxxx
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
Run the command
xxxxxxxxxx
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 11: To check the all pods are running successful in cluster
Run the command you can see all pods in namespaces
xxxxxxxxxx
kubectl get pods –all-namespaces
You can see the coredns service not yet started, still in pending, So that we need to install flannel network plugin to run coredns to start pod network communication.
Step 12: To Install Flannel Pod network driver
Run the below command to install POD network
xxxxxxxxxx
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Now you can see coredns and all pods in namespaces are in ready and running successfully.
Step 13: To Taint master node as a Master
Run the below command to taint the master node and make as a master.
xxxxxxxxxx
kubectl taint nodes --all node-role.kubernetes.io/master-
Step 14: Join the Worker Nodes to Master Node.
Run the token which produced by master node in other nodes to join to the cluster.
Generated Token:-
xxxxxxxxxx
kubeadm join 10.1.5.46:6443 –token lixbn2.aea4n63ypd42578
Run the command to check all the nodes are connected to cluster or not
xxxxxxxxxx
Kubectl get nodes
Step 15: To Install and configure Kubernetes Dashboard
Run the Below Command to install the dashboards
xxxxxxxxxx
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
Create a service account dashboard(username) for dashboard to access it
xxxxxxxxxx
kubectl create serviceaccount dashboard -n default
Run the Below command to give admin access to user(dashboard) to bind with cluster for accessing dashboard.
xxxxxxxxxx
kubectl create clusterrolebinding dashboard-admin -n default --clusterrole=cluster-admin --
serviceaccount=default:dashboard
Run the below command to generate the secret key for user(dashboard), to access Kubernetes dashboard
xxxxxxxxxx
kubectl get secret $(kubectl get serviceaccount dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode
You can see the generated key below, copy and save it.
To start the dashboard service, Run the below command.
xxxxxxxxxx
kubectl proxy
Paste and Go to the below URL in Master Node and click the token radio button and then paste the generated access token.
After signing in you can see the Kubernetes dashboard
In the end, we completed our Kubernetes cluster configuration setup successfully.
I hope you love this article; please share and like it.
Cheers,
Gokulakrishna
Opinions expressed by DZone contributors are their own.
Comments