
Kubernetes basics – creating first load balancer

Kubernetes is a portable Open Source platform for managing, automating and scaling container applications. Provides an environment for running highly reliable distributed systems. In this article, we will create our own, self-scalable Kubernetes cluster on a local machine (at no cost). That’s all in the desktop version of Enterprise Linux 8. The article contains three […]
Kubernetes is a portable Open Source platform for managing, automating and scaling container applications. Provides an environment for running highly reliable distributed systems. In this article, we will create our own, self-scalable Kubernetes cluster on a local machine (at no cost). That’s all in the desktop version of Enterprise Linux 8.
The article contains three object definitions in YAML files. To familiarize yourself with their structure and capabilities, please refer to the Kubernetes documentation. Because the topic is much more extensive, we limit ourselves only to general information allowing, however, to launch an autoscal cluster.
Installation
The first step will be to install the virtualization kit KVM / QEMU / libvirt according to ours previous article “Virtualization without compromise, or a few words about KVM, QEMU, libvirt and virt-manager”. Minikube (a tool that allows you to run the Kubernetes cluster on a local machine) needs a hypervisor, and the above set is the most efficient virtualization solution in the GNU / Linux system.
After installing the kit, we must add your user to the libvirt group:
sudo usermod -aG libvirt user_name
Then we download the latest version of miniikube – RPM file for x86_64 architecture from the site: https://github.com/kubernetes/minikube/releases and install in the system.
After installing miniikube, the following command should return the program version:
minikube version
We start the machine with the virtual command:
minikube start
Minikube will download the iso image from which it will build and launch the cluster using libvirt.
The next step is to install the latest version of kubectl software, i.e. the CLI program for managing Kubernetes clusters. The program can be downloaded from: https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ and install with the command:
sudo install -o root -g root -m 0755 kubectl / usr / local / bin / kubectl
The correct display of the kubectl version means that we can start using minikube.
kubectl version
Command:
minikube dashboard
will open a web page with dashboard miniikube in a new browser window. By using dashboard we can preview and make changes in our cluster.
We can also access our virtual minikube machine via SSH with the command:
minikube ssh
Deployment
Let’s now create a new application using the web interface. In the browser we choose the plus sign Create new resourcethen bookmark Create from form and enter:
App Name: cntident
Container image: eurolinuxid / cntident
Number of subs: 1
Then press the button Deploy.
Our EuroLinux test container should be taken from the Docker image repository and start working under Kubernetes’ control as „under ”, i.e. the smallest Kubernetes computing unit.
Let’s display our name from the command line:
kubectl get pod
After copying the displayed name, let’s redirect port 8080 of our local machine to port 80 will provide:
kubectl port-forward pod/nazwa_poda 8080:80
and then enter http://127.0.0.1:8080 in the browser address field
A simple web page will appear with our ID and internal IP address on which it is running.
ReplicaSet
The ReplicaSet facility in Kubernetes is responsible for scaling, i.e. for controlling the number of run horses depending on the specific conditions.
Let’s create a definition of the ReplicaSet object, still using our test application:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: ident-rs
labels:
app: cntident
spec:
replicas: 3
selector:
matchLabels:
app: cntident
template:
metadata:
name: cntident
labels:
app: cntident
spec:
containers:
- name: cntident
image: eurolinuxid/cntident
ports:
- containerPort: 80
Let’s enter this yaml file into the minicube using the command:
kubectl apply -f ident-rs.yaml
In the left menu of the dashboard interface, after clicking the ReplicaSet section, we should see the default ReplicaSet of our previous feed and the object that we just created using the YAML file. Let’s go inside this object by clicking the name link ident-rs.
In group Pods three – pods will be displayed, removing one of them will immediately create a new feed with a new, unique identifier.
We can scale the number of horses up to four with the command:
kubectl scale rs ident-rs --replicas = 4
or by selecting the YAML file edition in the web interface (option: Edit resurce) and inserting the number 4 in the replicas position.
The change should be visible in the interface within a few seconds.
We can also set the autoscaling of our cluster depending on the level of processor consumption. Let’s activate the metric server, which is necessary for proper auto-scaling operation:
minikube addons enable metrics-server
The following command will cause that as the load increases, it will run smoothly up to 10 yards with our application. All this within the previously defined ReplicaSet object ident-rs:
kubectl autoscale rs ident-rs --min = 3 --max = 10 --cpu-percent = 80
Load Balancing using the Ingress appendix
The Nginx Ingress controller is a proxy server based on the Nginx server. It uses the service data ident-sv (which we will create later in the article) and then prox our pods on one IP address.
First of all, we need to enable the Ingress – add-on to check if it is enabled by default:
minikube addons list
If it is turned off, we enable it with the command:
minikube addons enable ingress
Then we need to attach a service that exposes traffic to 8070 listening containers on port 80:
apiVersion: v1
kind: Service
metadata:
name: ident-sv
spec:
ports:
- port: 8070
protocol: TCP
targetPort: 80
selector:
app: cntident
type: ClusterIP
Activation of the service:
kubectl apply -f ident-sv.yaml
As there are currently small conflicts in the API versions used by Ingress, a temporary workaround should be started before activating the Ingress definition:
kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission
Now we can create and activate the definition of our load balanceer:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ident-ing
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "false"
spec:
rules:
- http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: ident-sv
port:
number: 80
We activate with the standard command:
kubectl apply -f ident-ing.yaml
In dashboard on the menu Service – > Ingresses in a group ident-ing click the link with the IP address in the Endpoints field. Note: The IP address is only displayed after the service has been fully activated, which may take over a minute. Refreshing the page, we get the result of the application from various fields defined in our ReplicaSet object, in other words: properly functioning load balancing.
Summary
In the article I used the EuroLinux 8 system, but you can successfully use these solutions in other desktop versions of the Enterprise class Linux system, including Red Hat® Enterprise Linux®, CentOS or Oracle® Linux. Minikube is a light implementation of Kubernetes – implements a simple cluster containing only one node. However, thanks to it we can get acquainted with Kubernetes service at a cost-free comfort at home, and use the acquired experience in commercial cloud solutions.