1. Introduction
Kubernetes is an open source platform developed by Google that automates the deployment, scaling, and management of containerized applications. It provides a powerful and flexible environment for managing and orchestrating applications deployed in containers. Kubernetes makes it easier to deploy, manage, and scale applications through management concepts and resources.
In this article, we will learn about the basic resources of Kubernetes, including Pod, Deployment, Service, and ReplicaSet. We’ll explore how they interact with each other, and how to create and manage them to build a powerful and flexible distributed application.
Let’s start with the concept of Pod – the most basic unit of Kubernetes.
2. Pod
2.1. Pod concept
The Pod is the smallest and most basic unit in Kubernetes. A Pod contains one or more containers, usually a Docker container, that share the same network namespace and can access the same filesystem.
Pods provide a single running environment for the containers in it, which simplifies application management and deployment on Kubernetes. Containers in a Pod share the same network environment, files and drives on the host, as well as other resources such as memory and CPU.
Pods are designed to support container applications interacting with each other in the same environment, as well as providing monitoring and resource management capabilities. Pods can also be run on different Nodes in the cluster, allowing containers to move between Nodes dynamically.
Pods help increase stability and flexibility for containerized applications and provide a single running environment for the containers in them, minimizing the dispersion of containers and improving system availability and reliability. .
In addition, Pods also allow containers to share resources such as memory, CPU, and data, which minimizes resource usage and optimizes system performance.
2.2. Create and manage Pods
To create a Pod, you need to create a YAML or JSON file describing the Pod and use kubectl
to create a Pod from that file:
1 2 3 4 5 6 7 8 9 10 11 | <span class="token key atrule">apiVersion</span> <span class="token punctuation">:</span> v1 <span class="token key atrule">kind</span> <span class="token punctuation">:</span> Pod <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> pod <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">containers</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> container <span class="token key atrule">image</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> image <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span> <span class="token punctuation">:</span> <span class="token number">80</span> |
To create a Pod, run the following command:
1 2 | kubectl create -f my-pod.yaml |
To list all running Pods, use the following command:
1 2 | kubectl get pods |
To delete a Pod, use the following command:
1 2 | kubectl delete pod my-pod |
2.3. Multi-container Pod
Multi-container Pod is a Pod that holds more than one container. In a Multi-container Pod, containers share the same network namespace, files, and drives on the host, as well as other resources such as memory and CPU. Using Multi-container Pods can reduce the number of Pods required for an application and help increase flexibility in application deployment on Kubernetes.
Containers in a Multi-container Pod can be used to perform different functions within an application. For example, one container can be used to run a web application, while another can be used to perform data processing functions.
An important point to keep in mind when using a Multi-container Pod is that the containers in the Pod should have some degree of interconnectedness. For example, containers in a Multi-container Pod can use file sharing to transfer data or use network connections to interact with each other. Managing and monitoring Multi-container Pods can also be more complex than Pods containing a single container.
However, the Multi-container Pod is still a useful tool in deploying complex applications that have a wide variety of functions.
2.4. Sharing resources between containers in Pod
In a Pod, containers can share resources with each other to increase system availability and performance. These resources include memory, CPU, disk, and network.
To share memory between containers, you can use techniques like shared memory or use a shared memory manager like Redis. For CPU sharing, Kubernetes can use techniques such as CPU sharing or CPU quota to allocate CPU to containers in the Pod.
To share disk resources, you can use a container that shares data with other containers in the Pod through a shared folder on the host, or use data storage services like NFS or Ceph.
For network sharing, containers in a Pod can share the same network namespace to access other services in the same cluster or share a network connection to interact with each other.
However, resource sharing can lead to security and performance issues, so it should be set up and managed carefully to ensure the safety and stability of the system.
2.5. Advantages and disadvantages
The advantages of Pods include:
- Portability and Independence: Pods are resource independent and can be moved to any node in the cluster.
- Flexibility: Pods can be deployed and configured flexibly to meet application requirements.
- Multitasking: Each Pod can contain multiple containers and containers can interact with each other within the same Pod.
- Portability: Pods can be easily moved from one node to another to ensure distribution and reliability.
- Independence: Each Pod can be managed and configured independently, making it easier to deploy and manage applications.
- Stability: Each Pod can be configured to restart automatically when something goes wrong.
- Isolation: Each Pod is isolated from the other Pods in the cluster, making resource management easier.
- Distributed: Pods can be deployed on many different nodes, which increases the distribution and reliability of the application.
The disadvantages of Pods include:
- Failed to self-heal: If the Pod fails, it will not be able to recover on its own and will need to be deleted and redeployed.
- No load balancing: Pods cannot automatically distribute traffic to instances of an application as a load balancing solution.
- Cannot share resources: A Pod cannot share resources with other Pods, resulting in wasted resources.
- Inability to decentralize access: Pods are not capable of decentralizing access and will not be suitable for complex applications with many different users and levels of access.
- Difficulty in managing single Pods: When there are many single Pods, managing them becomes difficult and complicated.
- No auto-restart: The Pod does not have an auto-restart feature when it fails or is turned off.
- Difficulty in resource management: Pod does not support flexible and efficient resource management, resulting in wasted resources.
3. Deployment
3.1. Deployment concept
Deployment is one of the important resources in the Kubernetes system. It is used to manage the deployment and update of Pods in a complex application. Deployment makes it simpler and easier to deploy and update Pods, helping to ensure system availability and reliability. Deployment provides rollback capability in case of failure and allows users to perform operations such as scale up or scale down the number of Pods dynamically.
3.2. Create and manage Deployment
To create a Deployment, you need to create a YAML or JSON file describing the Deployment and use kubectl
to create the Deployment from that file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <span class="token key atrule">apiVersion</span> <span class="token punctuation">:</span> apps/v1 <span class="token key atrule">kind</span> <span class="token punctuation">:</span> Deployment <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> deployment <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">replicas</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token key atrule">selector</span> <span class="token punctuation">:</span> <span class="token key atrule">matchLabels</span> <span class="token punctuation">:</span> <span class="token key atrule">app</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> app <span class="token key atrule">template</span> <span class="token punctuation">:</span> <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">labels</span> <span class="token punctuation">:</span> <span class="token key atrule">app</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> app <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">containers</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> container <span class="token key atrule">image</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> image <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span> <span class="token punctuation">:</span> <span class="token number">80</span> |
To create a Deployment, run the following command:
1 2 | kubectl create -f my-deployment.yaml |
To list all running Deployments, use the following command:
1 2 | kubectl get deployments |
To update a Deployment, edit the YAML file and run the following command:
1 2 | kubectl apply -f my-deployment.yaml |
To delete a Deployment, use the following command:
1 2 | kubectl delete deployment my-deployment |
3.3. Update and rollback the app version
To update an application in Kubernetes, you can perform the following steps:
- Create a new instance of the application, such as a new Docker image.
- Update the Deployment configuration file, specifying the new version of the application. By changing the
spec.template.spec.containers.image
field in the Deployment configuration file. - Use the
kubectl apply
command to apply the changes in the Deployment configuration file. - Kubernetes will automatically update the Pods running on the cluster with the new version of the application.
However, before updating to a new version of the application, one should do some testing and make sure that the new version works as expected. If necessary, you can use Kubernetes’ rolling update feature to minimize the impact on the system when updating to a new version of the application.
If an app update crashes or doesn’t work as expected, you can rollback to an earlier version of your app using the Kubernetes rollback feature. This feature allows you to roll back to an earlier version of Deployment and update the Pods running on the cluster with the previous version.
To perform the update and rollback of the application version, we can use management tools such as kubectl or graphical interfaces such as Kubernetes Dashboard to perform these operations.
3.4. Advantages and disadvantages
The advantages of deployment:
- Ensure application continuity and reliability: Deployment ensures that the application is deployed continuously and reliably by automatically updating Pods and checking for errors to ensure that the application is always running stable.
- Easy to deploy and update: Deployment enables easy and automated deployment and updating of applications, minimizing the time and effort required to deploy complex applications.
- Application Versions Management: Deployment allows to manage application versions and update Pods to respond to different application requirements.
- Automatic rollback: Deployment provides automatic rollback when errors occur during deployment, helping to ensure application continuity and reliability.
- Resource management: Deployment helps to manage system resources by minimizing unnecessary resource usage and responding to different application requirements.
- Scalability: Deployment allows Pods to be scaled up to accommodate higher loads, ensuring that the application runs consistently and reliably.
- Support for different strategies: Deployment supports different strategies for application deployment, including RollingUpdate, Recreate, etc. to help optimize the deployment and update process of applications.
Disadvantages of deployment:
- Pods cannot be managed directly: Since Deployment only manages Pods through ReplicaSet, Pods cannot be managed directly.
- Does not support advanced features: Deployment has basic features for managing Pods, but does not support advanced features like canary deployment or blue-green deployment.
- Does not support version management: Deployment does not support version management of the application. This means that if you want to update your application, you must create a new instance of Deployment.
- Difficulty managing multiple versions: If you have multiple instances of your application – running on Kubernetes, managing them using Deployment can become a hassle. Service testing is not supported: Deployment does not provide automated service testing, so you must automate testing of your services.
4. Service
4.1. Service concept
Service is an important resource in Kubernetes, used to manage Pods and provide distributed functionality in the system. It helps to create a stable IP address and load balance between Pods providing the same functionality, allowing applications to communicate with each other easily and continuously without interruption.
The service provides the ability to route requests to the appropriate Pods based on information such as port and label. This makes Pod management more efficient by routing requests to the respective Pods. In addition, the Service provides load balancing, allowing requests to be distributed to multiple Pods in response to application load.
In a nutshell, Service helps to create an abstraction layer on top of Pods, making it possible for applications to communicate with each other easily and seamlessly. The service also provides management and distribution of Pods in the system, and provides load balancing and request routing to the appropriate Pods.
4.2. Create and manage Services
To create a Service, you need to create a YAML or JSON file describing the Service and use kubectl
to create the Service from that file:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token key atrule">apiVersion</span> <span class="token punctuation">:</span> v1 <span class="token key atrule">kind</span> <span class="token punctuation">:</span> Service <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> service <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">selector</span> <span class="token punctuation">:</span> <span class="token key atrule">app</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> app <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">protocol</span> <span class="token punctuation">:</span> TCP <span class="token key atrule">port</span> <span class="token punctuation">:</span> <span class="token number">80</span> <span class="token key atrule">targetPort</span> <span class="token punctuation">:</span> <span class="token number">80</span> <span class="token key atrule">type</span> <span class="token punctuation">:</span> LoadBalancer |
To create the Service, run the following command:
1 2 | kubectl create -f my-service.yaml |
To list all running Services, use the following command:
1 2 | kubectl get services |
To delete a Service, use the following command:
1 2 | kubectl delete <span class="token function">service</span> my-service |
4.4. Types of Service
In Kubernetes, there are three main types of Service namely ClusterIP, NodePort and LoadBalancer.
- ClusterIP Service : is the default Service type in Kubernetes. It provides a stable IP address for Pods in the same cluster. ClusterIP Service is only accessible from within the cluster and is used to connect services within the same cluster.
- NodePort Service : is a type of Service that allows access from outside the cluster. It provides an IP address and port on all nodes in the cluster, allowing access to services from outside the cluster. NodePort Service is commonly used for web applications or APIs.
- LoadBalancer Service : is a type of Service that allows load balancing between Pods on multiple Nodes and provides access from outside the cluster. LoadBalancer Service is used when load balancing is required for high traffic applications.
In addition to the above three Service types, there is another Service type, ExternalName Service, which is used to create a DNS domain name for a service outside the cluster. The ExternalName Service does not provide an IP address or port, instead it points to an external DNS domain name. This type of Service is typically used to connect to services outside the cluster, such as to a database or an email service.
4.5. Advantages and disadvantages
The advantages of the service:
- Load Distribution: The service helps distribute the access load to the appropriate Pods, ensuring that the application’s resources are distributed evenly and are highly scalable.
- Abstract Identifier: The Service defines an abstraction and constant about the Pods, making it possible for applications to interact with the Pods easily and consistently.
- Pod Management: Service helps to manage Pods by finding suitable Pods and forwarding requests to those Pods, ensuring that the number of running Pods is always sufficient with the specified number.
- Flexibility: The service supports many connection types such as ClusterIP, NodePort, LoadBalancer and ExternalName, giving users flexibility in managing access to applications.
- Integration with DNS: The Service is integrated with Kubernetes DNS, allowing applications to use the Service’s DNS name instead of having to use the Pod’s IP address.
- Updateability: As Pods change, the Service will also be updated to ensure that it only manages active Pods.
- Multi-instance support: The service supports multi-instance, allowing users to deploy multiple instances of the same application on the same cluster.
- Suitable for distributed applications: The service is suitable for distributed applications, allowing applications to be deployed on different Pods in the system without having to care about IP addresses or other costs. specific Pod details.
- Ensure reliability and stability: Service helps to ensure that the application is always available to users, ensuring the reliability and stability of the application.
- High Availability: Service provides distributed and scalability, ensuring
Disadvantages of the service:
- The Service does not guarantee data consistency, meaning that data may be lost if the Pod is corrupted or deleted.
- Service does not have a self-healing feature. If a Pod fails, the Service will not automatically restore that Pod.
- Service management is difficult when the service is deployed in many clusters.
- As the Service delivers traffic to the Pods, it cannot know if the Pods are capable of handling those requests efficiently.
- Service can cause errors when Pods are not started properly or fail during deployment.
- The service cannot function effectively when the application has a constant change in the number of Pods, resulting in an uneven distribution of traffic.
5. ReplicaSet
5.1. The concept of ReplicaSet
ReplicaSet is a resource Kubernetes uses to ensure a certain number of Pod replicas are running at all times. It helps maintain application stability by automatically replacing faulty or dead Pods.
5.2. Create and manage ReplicaSet
To create a ReplicaSet, you need to create a YAML or JSON file describing the ReplicaSet and use kubectl
to create a ReplicaSet from that file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <span class="token key atrule">apiVersion</span> <span class="token punctuation">:</span> apps/v1 <span class="token key atrule">kind</span> <span class="token punctuation">:</span> ReplicaSet <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> replicaset <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">replicas</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token key atrule">selector</span> <span class="token punctuation">:</span> <span class="token key atrule">matchLabels</span> <span class="token punctuation">:</span> <span class="token key atrule">app</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> app <span class="token key atrule">template</span> <span class="token punctuation">:</span> <span class="token key atrule">metadata</span> <span class="token punctuation">:</span> <span class="token key atrule">labels</span> <span class="token punctuation">:</span> <span class="token key atrule">app</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> app <span class="token key atrule">spec</span> <span class="token punctuation">:</span> <span class="token key atrule">containers</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">name</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> container <span class="token key atrule">image</span> <span class="token punctuation">:</span> my <span class="token punctuation">-</span> image <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span> <span class="token punctuation">:</span> <span class="token number">80</span> |
To create a ReplicaSet, run the following command:
1 2 | kubectl create -f my-replicaset.yaml |
To list all running ReplicaSets, use the following command:
1 2 | kubectl get replicasets |
To delete a ReplicaSet, use the following command:
1 2 | kubectl delete replicaset my-replicaset |
Usually, however, you won’t need to create and manage ReplicaSets directly, as they are automatically managed by Deployments.
Finally, we learned about the basics of Kubernetes resource management, including Pod, Deployment, Service, and ReplicaSet. Understanding these concepts will help you effectively deploy and manage containerized applications on Kubernetes. In particular, the use of Deployment and Service helps to ensure the stability and scalability of the application, as well as optimize the deployment and update process.
5.3. Deployment and ReplicaSet
In Kubernetes, Deployment and ReplicaSet are two important resources used to manage Pods.
Deployment is an application version management resource that enables the deployment of new versions of the application and manages the updating of Pods on the cluster. It provides application version management and automatic updating of Pods on the cluster. When a new version is deployed through Deployment, Pods are automatically created or updated to ensure that the new version of the application is deployed properly.
ReplicaSet is a resource that manages the number of Pods running on the cluster. It ensures that the number of Pods running on the cluster matches the number specified in the ReplicaSet configuration file. When ReplicaSet detects that the number of Pods running on the cluster is incorrect, it will automatically create or delete Pods to ensure that the number of Pods running on the cluster is correct.
Deployment uses ReplicaSet to manage Pods. When deploying a new application through Deployment, it creates a new ReplicaSet to manage the newly created Pods. If any problem occurs and the Pods fail or are deleted, ReplicaSet will automatically create new Pods to ensure that the number of running Pods matches the number specified in the ReplicaSet configuration file.
In a nutshell, Deployment and ReplicaSet are two resources that manage Pods in Kubernetes. Deployment manages application versioning and updating of Pods on the cluster, while ReplicaSet manages the number of Pods running on the cluster and ensures that the number of running Pods matches the number specified in the ReplicaSet configuration file. .
5.4. Advantages and disadvantages
The advantages of replicaSet:
- Ensure the number of Pods is sufficient with the specified number: ReplicaSet helps to ensure that the number of running Pods is always sufficient with the specified number, ensuring the stability and reliability of the application.
- Flexibility to expand and shrink the number of Pods: ReplicaSet allows to expand or shrink the number of Pods flexibly, making the application more efficient and meeting different requirements of users. use.
- Pod Consistency: ReplicaSet ensures that all running Pods have the same configuration, helping to ensure application consistency and stability.
- Distributed: ReplicaSet helps to distribute Pods across multiple nodes in the cluster, helping to optimize resource usage.
- Easy management: ReplicaSet is easily managed through Kubernetes API and Kubernetes resource management tools.
- Auto-update capability: ReplicaSet has the ability to automatically update Pods when changes are made in the application, helping to ensure application continuity.
- Fault Support: ReplicaSet is capable of detecting and handling errors in Pods, helping to ensure the reliability and stability of the application.
- Flexibility: ReplicaSet has the flexibility to define Pod Templates, allowing different types of Pods to be deployed to suit the needs of the application.
Disadvantages of replicaSet:
- ReplicaSet only manages the number of Pods, not the state of the Pods. This means that if a Pod fails or fails, ReplicaSet will create a new Pod to replace it, but it cannot restore the state of the failed Pod.
- When using ReplicaSet, Pods are randomly allocated to the Nodes in the cluster. This can lead to a situation where one Node has too many Pods while some other Nodes have no Pods at all. At that time, there will be an imbalance in the resources and performance of the system.
- ReplicaSet has no auto-expanding or shrinking feature, which means that the number of Pods will not change if not manually configured.
6. Summary
In this article, we learned about 4 important resources in the Kubernetes system: Pod, Deployment, Service and ReplicaSet. We’ve dived into the features and uses of each resource, analyzed their structure and key components, and compared the resources for features and pros and cons.
Resources | Feature | Using |
---|---|---|
Pod | Basic unit for deploying containers in Kubernetes | Use Pods to deploy one or more containers in a common environment and provide shared IP addresses and storage space between containers |
Deployment | Manage Pods Deployment and Updates | Use Deployment to define a new version of your application and update Pods to that new version, providing automatic rollback if there is an error during deployment |
Service | Manage Pods and Provide Distributed Feature | Use Service to manage Pods by defining Pods in an abstract and constant way, making it easy and uniform for applications to interact with Pods. The Service provides distributed functionality by distributing traffic to the appropriate Pods, ensuring that the application’s resources are distributed evenly and are highly scalable. |
ReplicaSet | Manage the number of Pods | Use ReplicaSet to ensure that the number of running Pods is always sufficient with the specified number, ensuring the stability and reliability of the application. ReplicaSet also allows to manage the expansion or contraction of the number of Pods flexibly. |
When using these resources, we need to keep a few important points in mind such as data consistency, flexibility in deploying and managing containerized applications, and self-healing of containers. resources.
To use these resources effectively, we need to understand the features and usage of each resource, and apply them appropriately and in accordance with the requirements of the application. Furthermore, we need to keep in mind the best practices and design rules to optimize the performance and reliability of the resources in the Kubernetes system.
7. References
- Kubernetes Documentation: https://kubernetes.io/docs/home/
- Kubernetes Concepts: https://kubernetes.io/docs/concepts/
- Kubernetes API Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/
- Kubernetes: Up and Running: Dive into the Future of Infrastructure (2nd Edition) by Kelsey Hightower, Brendan Burns, and Joe Beda. O’Reilly Media, 2019.
- Kubernetes in Action by Marko Luksa. Manning Publications, 2018.