Complete Guide to Kubernetes: Container Orchestration for Scalable Applications

Kubernetes has become the leading platform for container orchestration and management in the world of application infrastructure. Through this blog, I will explain a tour of the most used Kubernetes concepts, from the most basic to the most complex use cases with examples and key commands to use.

Introducing Kubernetes

Kubernetes, also known as K8s, is an open source platform that automates the deployment, scaling, and operation of containerized applications. Containers are units that encapsulate an application and all its dependencies. Kubernetes provides tools for managing containers efficiently, ensuring high availability and scalability of applications.

Installation and Configuration

The installation of Kubernetes may vary depending on the platform, but there are some tools such as 'Kubeadm', 'minikube' or cloud solutions such as:

  • Google Kubernetes Engine (GKE) ⇨ Google Cloud Platform (GCP) cloud.
  • Elastic Kubernetes Services (EKS) ⇨ Amazon Web Service (AWS) Cloud.
  • Azure Kubernetes Services (AKS) ⇨ Azure Cloud (Azure).

Once installed, you can configure your command line tools to access the cluster.

Example of installation using the 'minikube'


# Instalación de minikube (Linux)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube-linux-amd64
sudo mv minikube-linux-amd64 /usr/local/bin/minikube

# Iniciar clúster de minikube
minikube start


💡 Note: Before starting and seeing what are the different components used in Kubernetes, you have to be clear about several things, first the indentation in the Yaml files, without them it will generate an error, and second ALWAYS, but ALWAYS a Yaml file is built of four (4) levels essential for the creation of a Yaml file, and in this order respectively, which are:

  • ApiVersion
  • Kind
  • Metadata
  • Spec

Where:

Having explained the above, we can see each of the components contained in this orchestration tool called kubernetes.

Pods and Containers

A Pod is the most basic unit of deployment in Kubernetes. It can contain one or more containers that share resources and network.

Let's see an example of how to create a Pod using a Yml file.


# pod-example.yaml

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
        image: nginx:latest



💡 Note: In the example above we see a Yaml file that is called 'pod-example.yaml', the first thing would be to create the pod, secondly it would be to list the pods to see if our pod was created in a satisfactory way and if we want to learn more about our Pod we can use the reserved word 'describe', the commands would be in this order and like this:


# Crear el Pod
kubectl apply -f pod-example.yaml

# Listar Pods
kubectl get pods

# Obtener información detallada de un Pod
kubectl describe pod my-pod



Replication and Scaling

Kubernetes allows you to easily replicate and scale applications using a Yaml file or in a declarative (terminal) form, to improve availability and performance.

Let's see an example of how to create a ReplicaSet by means of a Yml file.


# replicaset-example.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: nginx:latest



💡 Note: In the example above we see a Yaml file that is called 'replicaset-example.yaml', the first thing would be to create the replicaset, secondly it would be to list the replicasets to see if our replica was successfully created and if we want to scale our replica if we had to enter our Yml file they would be in this order and in this way:


# Crear ReplicaSet
kubectl apply -f replicaset-example.yaml

# Listar ReplicaSets
kubectl get replicasets
# Escalar ReplicaSet
kubectl scale replicaset my-replicaset --replicas=5



Services and Networking

The Services allow communication between different sets of Containers in Kubernetes. A Service can be exposed internally or externally.

Let's see an example of how to create a Service by means of a Yml file.


# service-example.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP


💡 Note: In the example above we see a Yaml file that is called 'service-example.yaml', the first thing would be to create the service, secondly it would be to list the services to see if it was created in a satisfactory way', the commands would be in this order and like this:


# Crear Service
kubectl apply -f service-example.yaml

# Listar Services
kubectl get services



Configuration and Secrets

Secrets in Kubernetes allow you to store sensitive information, such as passwords, in a secure manner.

Let's see an example of how to create a Secret by means of a Yml file.


# secret-example.yaml

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=  # Base64-encoded "admin"
  password: cGFzc3dvcmQ=  # Base64-encoded "password"

💡 Note: In the example above we see a Yaml file that is called 'secret-example.yaml', the first thing would be to create the secret, secondly it would be to list the secrets to see if it was created in a satisfactory way', the commands would be in this order and like this:


# Crear Secret
kubectl apply -f secret-example.yaml

# Listar Secretos
kubectl get secrets




Monitoring

Observability is critical in Kubernetes, as it helps teams identify problems and proactively manage Kubernetes clusters that. Tools such as Prometheus can be used.

Example of installation using the 'Prometheus'


# Instalar Prometheus usando Helm
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/prometheus

# Acceder a la interfaz web de Prometheus
kubectl port-forward svc/prometheus-server 9090:80


Deployments

A Deployment is an object that can represent an application in the cluster. When the Deployment is created, it can be specified in the Spec of the Deployment how many replicas of the application you want to run.

Let's see an example of how to create a Deployment by means of a Yml file.


# deployment-example.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels: 
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: my-app
        type: front-end
    spec:
      containers:
        - name: my-container
          image: nginx:latest
  replicas: 3
  selector:
    matchlabels:
      type: front-end


💡 Note: In the example above we see a Yaml file that is called 'deployment-example.yaml', the first thing would be to create the deployment, secondly it would be to list the secrets to see if it was created in a satisfactory way', the commands would be in this order and like this:


# Crear Deployment
kubectl create -f deployment-example.yaml

# Listar Deployments
kubectl get deployments

#Listar replicasets
kubectl get replicaset

#Listar Pods
kubectl get pods



Updates and Rollbacks

Los Rolling Updates allow you to update applications gradually to avoid interruptions.

Example of upgrading a 'Deployment'



# Actualizar la imagen del contenedor
kubectl set image deployment/my-deployment my-container=nginx:1.21.1

# Realizar un rollback a la versión anterior
kubectl rollout undo deployment/my-deployment



Advanced Use Case

Kubernetes can be used in a variety of use cases, such as multicluster deployments, persistent storage, batch execution, and more.

Let's see an example of how to create a TareaBatch by means of a Yml file.



# job-example.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  template:
    spec:
      containers:
        - name: my-container
          image: busybox:1.33.1
          command: ["echo", "Hello from the job!"]
      restartPolicy: Never


💡 Note: In the example above we see a Yaml file that is called 'job-example.yaml', the first thing would be to create the job, secondly it would be to list the jobs to see if it was created in a satisfactory way', the commands would be in this order and like this:


# Crear Job
kubectl apply -f job-example.yaml

# Listar Jobs
kubectl get jobs



Conclusion

With this, we have explored a wide range of concepts that are used in Kubernetes, from the most basic concepts such as creating a Pod to the most advanced. This is a very powerful tool that allows you to orchestrate applications in an efficient and scalable way in container environments.

Ready to optimize the management of your applications with Kubernetes?

At Kranio, we have experts in container orchestration who will help you implement efficient solutions using Kubernetes, ensuring the scalability and performance of your applications. Contact us and discover how we can promote the digital transformation of your company.

Roberto Palacios

September 16, 2024