Categories
Cloud General Products Sotfware & DevOps Tools & HowTo

Kubernetes nodes: K8s setup and run apps

Setup a kubernetes multi-node cluster from scratch and run a simple app on it.

kubernetes

Kubernetes is a Linux based open source platform that automates or manages applications in containers. With Kubernetes, you can efficiently respond to the requests by consumers by using Kubernetes nodes.

Kubernetes is in a thriving ecosystem. Kubernetes services, support, and tools are widely available– as quoted on the Kubernetes website.

For installation, you must provide an operating system with special specifications. You can also use Kubernetes on-premise, for example, on GCP, AWS, and DigitalOcean.

Installing Kubernetes on Linux

For the installation of Kubernetes, I used CentOS 7 with two nodes connected. In the first step, you will enter your CentOS 7 servers. I used 2 CentOS 7 operating systems for the Master and Kubernetes nodes. But first, let’s change the hostnames of CentOS master and worker node.

[[email protected] ~]# hostnamectl set-hostname k8s-master

Then change the hostname on CentOS 7 worker nodes.

[[email protected] ~]# hostnamectl set-hostname k8s-nodes

After that, we add the hostname and IP address to the master and Kubernetes nodes so they can be connected the /etc/hosts file.

Before doing so, add on the CentOS 7 master node.

[[email protected] ~]# vi /etc/hosts

Then add the hostname and IP address of the master and worker nodes on the CentOS 7 worker node as well with this command:

[[email protected] ~]# vi /etc/hosts
kubernetes nodes

If SELinux on CentOS 7 is still enforcing, then we need to turn off SELinux on CentOS 7 master and worker nodes.

[[email protected] ~]# setenforce 0
[[email protected] ~]# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

Then we configure for networking so that IPTables can forward it. We can run the below command on the CentOS 7 master and worker nodes.

[[email protected] ~]# modprobe br_netfilter
[[email protected] ~]# echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

Another key prerequisite for installation of Kubernetes on CentOS 7 or linux is to turn off swap. Our first step is to disable it with the command below, we will do it first on the centos 7 master node.

[[email protected] ~]# swapoff -a

To enable permanent, we must put a hash (#) for swap in the /etc/fstab file. We will do this first on the CentOS 7 master node.

[[email protected] ~]# vi /etc/fstab

Next we will disable swap on the CentOS 7 worker node using the command below.

[[email protected] ~]# swapoff -a

We will disable permanent swap in the /etc/fstab file, just like on the CentOS 7 master node on the worker node.

kubernetes nodes

Now, we will reboot both the master and worker nodes using the commands below.

You landed the Cloud Storage of the future internet. Cloud Storage Services Sesame Disk by NiHao Cloud

Use 10GB for free NOW and forever!

Support the growth of a Team File sharing system that works in China, USA, Europe, APAC and everywhere else.
[[email protected] ~]# reboot

Kubernetes Container: Dependencies

Before we install Docker and the Kubernetes tools, we have to install some packages on the master and worker nodes. In the following part of this post, we will install the package on the CentOS 7 master and worker nodes.

[[email protected] ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y

After the package is installed on the master and worker, we will add the docker repository on the CentOS 7 master and worker nodes

[[email protected] ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Then we will install docker-ce on the master and worker nodes, for the first we will install on the CentOS 7 master node and worker nodes.

[[email protected] ~]# yum install docker-ce -y

Wait for until docker-ce has been successfully installed on both master and worker nodes. Enable and start docker-ce so that docker can continue when you reboot the server.

[[email protected] ~]# systemctl enable docker && systemctl start docker

Kubernetes Nodes: Installation of Kubernetes Tools

To be able to use kubernetes on CentOS 7, the first step we have to add the kubernetes repository on CentOS 7. For that, add the Kubernetes repository on the CentOS 7 master and worker nodes.

[[email protected] ~]# 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

You have successfully added the Kubernetes repository to the master and worker nodes now.

After that, you will have to install Kubernetes and its tools on the CentOS 7 master and worker nodes.

[[email protected] ~]# yum install kubelet kubeadm kubectl -y

After the installation processes on the master and worker nodes complete, activate and enable Kubernetes so that it can run properly. But first, activate it on the CentOS 7 master node.

[[email protected] ~]# systemctl enable kubelet && systemctl start kubelet

Then, enable and activate Kubernetes on the CentOS 7 worker node.

[[email protected] ~]# systemctl enable kubelet && systemctl start kubelet

In the next step, equate the cgroup driver of docker and Kubernetes. The first step you must check is the cgroup driver on docker using this command on the master node CentOS 7.

[[email protected] ~]# docker info | grep -i 'cgroup driver'

Then, add the cgroup driver that was found into the /etc/sysconfig/kubelet file on the CentOS 7 master and worker nodes.

[[email protected] ~]# vi /etc/sysconfig/kubelet

Setup Kubernetes Nodes

If you have set up the cgroup, you will create an init network with kubeadm on the CentOS 7 master node.

[[email protected] ~]# kubeadm init --pod-network-cidr=10.244.0.0/16

If the process goes smoothly, it will display this:

kubernetes kubernetes nodes

Run this command so that that Kubernetes runs properly.

[[email protected] ~]# mkdir -p $HOME/.kube
[[email protected] ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[[email protected] ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

Then you may run the command below to see if the node is running properly or not.

[[email protected] ~]# kubectl get nodes

After that, check the pods in kubernetes.

kubernetes kubernetes nodes

To make nodes ready, you have to add add pods i.e. weave-net. Add it with this command:

[[email protected] ~]# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')" 

Check again the Kubernetes node; maybe you need to wait a while for the node to be ready.

[[email protected] ~]# kubectl get nodes

When the node is ready, combine the master and worker nodes with the tokens created from the previous kubeadm init.

[[email protected] ~]# kubeadm join 192.168.8.124:6443 --token opd50x.ohi8hq7jsmu76r3o \
	--discovery-token-ca-cert-hash sha256:9ee1755bb2fb50db2d4a5c6f18f480360682de1e2405b2d5a3ef1a3078a6cede
running nodes on kubernete kubernetes nodes

Once the master node is joined to the worker node, you need to describe the worker node so it can be ready on the master node.

master and worker

After that, you can use this command to see if both nodes are ready to use.

[[email protected] ~]# kubectl get nodes

We have seen that the node is ready, so the next step will be to create a label for the worker node role with the command below.

[[email protected] ~]# kubectl label node k8s-nodes node-role.kubernetes.io/worker=worker

Then, check the nodes again. You will see that the roles on the worker node have changed.

deploying apps

Build Nginx Contaienr Apps

In this part, you will build an Nginx container in Kubernetes. Nginx functions as a very powerful web server for our needs. You can create an nginx deployment with the following command:

[[email protected] ~]# kubectl create deployment nginx --image=nginx

If the nginx deployment has been created, then check the deployment with this command to see a list:

[[email protected] ~]# kubectl get deployments

To describe deployment of nginx, use the following command.

[[email protected] ~]# kubectl describe deployment nginx

By describing the nginx deployment you will be able to find out complete information on the nginx deployment.

master and worker

Then we can create an nginx service with this command.

[[email protected] ~]# kubectl create service nodeport nginx --tcp=80:80

After that, use this command below to check the service port on nginx.

[[email protected] ~]# kubectl get svc

To check the nginx service, you can use curl with this command “curl -v ip_private:port or hostname:port “.

[[email protected] ~]# curl -v k8s-nodes:31173

To remove deployment on nginx using kubectl, use this command.

kubernetes

Conclusion

Kubernetes is invaluable for us to use as a cloud-native because considering that it is open-source, it has many features for scaling and managing containers in multiple nodes. You can read How to Use MySQL with Haproxy Load Balance if you previously used Docker. With Kubernetes, your managed container will be even more powerful.

I hope you found this post to be interesting. What I would love is that you were to follow along this tutorial and see if you face any problems. You can comment these problems below and I will help you out. Don’t forget to subscribe to our blog!

Edited By: Syed Umar Bukhari

Hits: 267

By Budi Santoso

Budi Santoso is a Development Operations and Technical Writer with experience in cloud, server operating systems, and CICD. Enthusiastic to continue to develop learning technology topics and have a passion for information technology.

Leave a Reply

Your email address will not be published.

Start Sharing and Storing Files for Free

You can also get your own Unlimited Cloud Storage on our pay as you go product.
Other cool features include: up to 100GB size for each file.
Speed all over the world. Reliability with 3 copies of every file you upload. Snapshot for point in time recovery.
Collaborate with web office and send files to colleagues everywhere; in China & APAC, USA, Europe...
Tear prices for costs saving and more much more...
Create a Free Account Products Pricing Page
LinkedIn
LinkedIn
Share
WeChat
WhatsApp