Category: Kubernetes

  • A beginner’s guide to containerization and container orchestration with Docker and Kubernetes.

    A beginner’s guide to containerization and container orchestration with Docker and Kubernetes.

    Containerization and container orchestration are two of the most essential technologies for modern software development. These technologies have revolutionized the way we build, deploy, and manage applications. In this article, we will provide a beginner’s guide to containerization and container orchestration using Docker and Kubernetes.

    What is Containerization?

    Containerization is the process of encapsulating an application and its dependencies into a single package called a container. The container provides a consistent and isolated environment for running the application. The containerization technology enables the developers to build, test and deploy applications more quickly and reliably.

    What is Docker?

    Docker is a containerization platform that provides an easy and efficient way to build, ship, and run containers. With Docker, you can package an application along with its dependencies and runtime environment into a container. Docker containers are lightweight, portable, and can run consistently across different environments.

    How to Create a Docker Container?

    Creating a Docker container involves the following steps:

    Step 1: Create a Dockerfile

    A Dockerfile is a simple text file that contains instructions for building a Docker image. The Dockerfile specifies the base image, copies the application code, installs the dependencies, and exposes the required ports.

    Step 2: Build a Docker Image

    Once you have created a Dockerfile, you can build a Docker image using the Docker build command. The Docker build command reads the instructions from the Dockerfile and creates a Docker image.

    Step 3: Run a Docker Container

    Once you have created a Docker image, you can run a Docker container using the Docker run command. The Docker run command starts a new container based on the specified Docker image.

    What is Container Orchestration?

    Container orchestration is the process of managing, deploying, and scaling containerized applications. It involves managing the lifecycle of containers, scheduling containers on a cluster of nodes, and ensuring that the containers are running and healthy.

    What is Kubernetes?

    Kubernetes is a container orchestration platform that automates the deployment, scaling, and management of containerized applications. Kubernetes provides a platform-agnostic way of deploying and managing containers.

    How to Use Kubernetes?

    Using Kubernetes involves the following steps:

    Step 1: Create a Kubernetes Deployment

    A Kubernetes deployment defines how many replicas of a container should be running and what container image to use.

    Step 2: Create a Kubernetes Service

    A Kubernetes service provides a stable IP address and DNS name for accessing the containers.

    Step 3: Scale the Kubernetes Deployment

    Kubernetes provides a simple way to scale the deployment up or down, depending on the traffic.

    Step 4: Monitor the Kubernetes Deployment

    Kubernetes provides several tools for monitoring the containers, including logs, metrics, and events.

    Conclusion:

    Containerization and container orchestration are critical technologies that have transformed software development. Docker provides an easy and efficient way to package and run containers, while Kubernetes automates the deployment, scaling, and management of containerized applications. By using Docker and Kubernetes, developers can build, test, and deploy applications more quickly and reliably. We hope that this beginner’s guide has provided you with a good understanding of containerization and container orchestration.

  • Kubernetes Zero Downtime Deployment

    Kubernetes Zero Downtime Deployment

    Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. One of the key features of Kubernetes is the ability to perform zero downtime deployment, which means deploying a new version of an application without causing any disruption to end-users. In this blog post, we will explore the concept of zero downtime deployment in Kubernetes and how it can be achieved.

    What is Zero Downtime Deployment?

    Zero downtime deployment is the process of deploying a new version of an application without causing any downtime or service interruption. This means that end-users can continue to use the application without any interruption, even while the new version is being deployed. This is particularly important for applications that require high availability, as downtime can lead to loss of revenue and customer dissatisfaction.

    How Kubernetes Achieves Zero Downtime Deployment

    Kubernetes achieves zero downtime deployment through a technique called Rolling Updates. Rolling Updates allow Kubernetes to deploy new versions of an application gradually, one instance at a time, while keeping the existing instances running. This means that Kubernetes can update an application without taking it offline.

    Rolling Updates work by creating a new ReplicaSet with the updated version of the application and gradually increasing the number of replicas in the new ReplicaSet while decreasing the number of replicas in the old ReplicaSet. This process continues until all replicas in the old ReplicaSet have been replaced by replicas in the new ReplicaSet. Once the process is complete, the old ReplicaSet is deleted, and the new ReplicaSet takes over.

    To achieve zero downtime deployment, Kubernetes uses the following steps:

    • Create a new ReplicaSet: Kubernetes creates a new ReplicaSet with the updated version of the application.
    • Gradually increase the number of replicas in the new ReplicaSet: Kubernetes gradually increases the number of replicas in the new ReplicaSet, one instance at a time.
    • Gradually decrease the number of replicas in the old ReplicaSet: Kubernetes gradually decreases the number of replicas in the old ReplicaSet, one instance at a time.
    • Verify the health of the new ReplicaSet: Kubernetes verifies the health of the new ReplicaSet to ensure that all instances are running correctly.
    • Delete the old ReplicaSet: Once the new ReplicaSet has been fully deployed and verified, Kubernetes deletes the old ReplicaSet.

    Example

    here’s an example of how Kubernetes achieves zero downtime deployment:

                           +---+    +---+    +---+    +---+
                           |   |    |   |    |   |    |   |
    Old ReplicaSet (v1)    | 1 |    | 2 |    | 3 |    | 4 |
                           |   |    |   |    |   |    |   |
                           +---+    +---+    +---+    +---+
    
                           +---+
    New ReplicaSet (v2)    |   |
                           +---+
    

    Let’s say you have a web application running in Kubernetes with four instances, each serving user requests. You want to deploy a new version of the application without causing any downtime. Here’s how Kubernetes achieves this:

    • Create a new ReplicaSet: Kubernetes creates a new ReplicaSet with the updated version of the application.
                           +---+
    New ReplicaSet (v2)    | 1 |
                           +---+
    
    • Gradually increase the number of replicas in the new ReplicaSet: Kubernetes starts creating new instances of the updated application in the new ReplicaSet, while keeping the old ReplicaSet running. At this point, you have four instances running in the old ReplicaSet and zero instances in the new ReplicaSet.
                           +---+    +---+
    New ReplicaSet (v2)    | 1 |    | 2 |
                           +---+    +---+
    
                           +---+    +---+    +---+
    Old ReplicaSet (v1)    | 1 |    | 2 |    | 3 |
                           +---+    +---+    +---+
    
    
    • Gradually decrease the number of replicas in the old ReplicaSet: Kubernetes starts scaling down the old ReplicaSet, one instance at a time. For example, it may start by scaling down the old ReplicaSet to three instances and scaling up the new ReplicaSet to one instance. At this point, you have three instances running in the old ReplicaSet and one instance in the new ReplicaSet.
                           +---+    +---+    +---+
    Old ReplicaSet (v1)    | 1 |    | 2 |    | 3 |
                           +---+    +---+    +---+
    
                           +---+    +---+    +---+    +---+
    New ReplicaSet (v2)    | 1 |    | 2 |    | 3 |    | 4 |
                           +---+    +---+    +---+    +---+
    
    • Verify the health of the new ReplicaSet: Kubernetes verifies the health of the new ReplicaSet to ensure that all instances are running correctly. If any issues arise, Kubernetes can stop the deployment and roll back to the previous version.
    • Continue scaling down the old ReplicaSet: Kubernetes continues scaling down the old ReplicaSet and scaling up the new ReplicaSet until all instances in the old ReplicaSet have been replaced by instances in the new ReplicaSet. At this point, you have zero instances running in the old ReplicaSet and four instances in the new ReplicaSet.
                           +---+    +---+    +---+    +---+
    Old ReplicaSet (v1)    |   |    | 2 |    | 3 |    | 4 |
                           +---+    +---+    +---+    +---+
    
                           +---+    +---+    +---+    +---+
    New ReplicaSet (v2)    | 1 |    |   |    |   |    |   |
                           +---+    +---+    +---+    +---+
    
    • Delete the old ReplicaSet: Once the new ReplicaSet has been fully deployed and verified, Kubernetes deletes the old ReplicaSet.
                           +---+    +---+    +---+    +---+
    New ReplicaSet (v2)    | 1 |    | 2 |    | 3 |    | 4 |
                           +---+    +---+    +---+    +---+

    By using this process, Kubernetes can deploy new versions of an application gradually, one instance at a time, while keeping the existing instances running. This ensures that the application remains available to end-users during the deployment process, without causing any disruption to the user experience.

    Benefits of Zero Downtime Deployment

    Zero downtime deployment offers several benefits, including:

    • Increased availability: Zero downtime deployment ensures that the application remains available to end-users during the deployment process.
    • Reduced risk: By gradually deploying the new version of the application, Kubernetes reduces the risk of service disruption and enables quick rollback if issues arise.
    • Improved user experience: Zero downtime deployment ensures that end-users can continue to use the application without any interruption, leading to a better user experience.

    Conclusion

    Zero downtime deployment is a critical feature of Kubernetes that enables the deployment of new versions of an application without causing any disruption to end-users. Kubernetes achieves this through Rolling Updates, a technique that gradually deploys new versions of an application while keeping the existing instances running. By using zero downtime deployment, organizations can increase the availability of their applications, reduce risk, and improve the user experience.

  • How to Pass the AWS Certified Solution Architect Associate Exam in Two Month: A Practical Guide

    How to Pass the AWS Certified Solution Architect Associate Exam in Two Month: A Practical Guide

    Amazon Web Services (AWS) is a cloud computing platform that provides a wide range of services, including computing, storage, and database services, to name a few. As the demand for cloud computing continues to grow, the need for certified professionals who can manage these services efficiently also increases. AWS Certified Solution Architect Associate Exam is an entry-level certification exam that validates the candidate’s knowledge of AWS architectural principles and services. In this blog post, we will discuss how to pass the AWS Certified Solution Architect Associate Exam in two months with a practical guide but first, we need to understand why.

    Why you should get the AWS Certified Solution Architect Associate certificate?

    Passing the AWS Certified Solution Architect Associate Exam can benefit your career in several ways. Here are a few reasons why you should consider getting certified:

    • Increased Career Opportunities: AWS is the leading cloud computing platform, and companies are increasingly adopting it for their infrastructure needs. As a certified AWS Solution Architect Associate, you will have a competitive advantage over non-certified professionals in the job market. This certification can help you land job roles such as AWS Solutions Architect, Cloud Engineer, and Cloud Infrastructure Architect.
    • Enhanced Credibility: AWS Solution Architect Associate certification is a globally recognized and respected credential. It demonstrates your knowledge and skills in designing and deploying scalable, highly available, and fault-tolerant systems on AWS. This certification can enhance your credibility and increase your professional reputation.
    • Higher Salary: Certified professionals generally earn higher salaries than their non-certified counterparts. According to a survey conducted by Global Knowledge, AWS Solution Architect Associate certified professionals earn an average salary of $130,883 per year. This certification can help you negotiate a higher salary or secure a job with a higher pay scale.
    • Continuous Learning: AWS regularly updates its services and features, and certified professionals are required to stay up-to-date with these changes. This certification requires you to continue learning and improving your skills, which can help you stay relevant in the industry.

    The AWS Certified Solution Architect Associate Exam is a challenging exam that requires dedication, commitment, and a solid understanding of AWS services and architecture principles.

    While the exam is challenging, it is not impossible to pass. With the right study plan, practice, and dedication, you can increase your chances of passing the exam. The practical guide outlined in this blog post can help you prepare for the exam in two months and increase your chances of passing.

    Step 1: Understanding the Exam

    The AWS Certified Solution Architect Associate Exam tests the candidate’s knowledge of AWS architectural principles and services, as well as their ability to design and deploy scalable, highly available, and fault-tolerant systems on AWS. The exam consists of 65 multiple-choice questions that need to be answered within 130 minutes. The exam fee is $150, and the passing score is 720 out of 1000.

    Step 2: Setting a Study Plan

    To pass the AWS Certified Solution Architect Associate Exam, you need to create a study plan that works for you. Since the exam covers a wide range of topics, it is essential to set realistic study goals and stick to them. A two-month study plan should be sufficient for most candidates.

    Week 1-2: AWS Fundamentals

    Week 1-2 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning the fundamentals of AWS. In this section, you will learn about AWS core services, cloud computing basics, and AWS architecture principles. Here are some topics to focus on during this period:

    • AWS Core Services: You should start by learning about the core services of AWS, including Elastic Compute Cloud (EC2), Simple Storage Service (S3), and Relational Database Service (RDS). These services are fundamental to most AWS applications and are essential for designing scalable and highly available systems.
    • Cloud Computing Basics: You should also learn about the basics of cloud computing, including the different deployment models (Public, Private, and Hybrid Clouds) and service models (Infrastructure as a Service, Platform as a Service, and Software as a Service).
    • AWS Architecture Principles: You should learn about AWS architecture principles, including designing for availability, scalability, and fault tolerance. This includes understanding the different AWS regions and availability zones and how to design your application for maximum availability and resilience.
    • AWS Security: You should learn about AWS security best practices, including identity and access management, network security, and data encryption. You should also understand how to secure your AWS infrastructure against common security threats.
    • Hands-on Practice: In addition to studying the theory, you should also practice using AWS services through the AWS Free Tier. This will help you become familiar with the AWS console and give you practical experience with using AWS services.

    During this period, you should aim to complete the AWS Certified Cloud Practitioner Exam (if you haven’t already done so). This exam covers the fundamentals of AWS and will give you a solid foundation for preparing for the AWS Certified Solution Architect Associate Exam.

    Week 1-2 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning the fundamentals of AWS, including core services, cloud computing basics, AWS architecture principles, security, and hands-on practice. By mastering these topics, you will be well-prepared for the rest of your study plan.

    Week 3-4: Compute Services

    Week 3-4 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS compute services. In this section, you will learn about the different compute services offered by AWS, including Elastic Compute Cloud (EC2), Elastic Container Service (ECS), Elastic Kubernetes Service (EKS), and Lambda. Here are some topics to focus on during this period:

    • Elastic Compute Cloud (EC2): You should start by learning about EC2, which is a scalable and highly available compute service that allows you to launch and manage virtual machines (instances) in the cloud. You should learn about the different types of instances, instance purchasing options, storage options, and networking options available in EC2.
    • Elastic Container Service (ECS) and Elastic Kubernetes Service (EKS): You should also learn about containerization and how to deploy and manage containerized applications on AWS using ECS and EKS. You should learn about the different deployment options, load balancing, and scaling options available in these services.
    • Lambda: You should learn about serverless computing and how to use AWS Lambda to run your code without provisioning or managing servers. You should learn about the different trigger options available in Lambda, including API Gateway, S3, and CloudWatch Events.
    • Autoscaling: You should also learn about autoscaling and how to automatically adjust the number of instances running in your application based on demand. You should learn about the different types of autoscaling policies available in AWS and how to configure them.
    • Hands-on Practice: As with the previous week, you should also practice using these services through the AWS Free Tier. This will help you become familiar with the AWS console and give you practical experience with using these services.

    During this period, you should also start practicing for the AWS Certified Solution Architect Associate Exam by taking practice tests and reviewing sample questions. This will help you become familiar with the exam format and the types of questions you can expect to see on the actual exam.

    Week 3-4 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS compute services, including EC2, ECS, EKS, Lambda, and autoscaling. By mastering these topics, you will be well-prepared for the compute-related questions that may appear on the exam.

    Week 5-6: Storage Services

    Week 5-6 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS storage services. In this section, you will learn about the different storage services offered by AWS, including Simple Storage Service (S3), Elastic Block Store (EBS), Glacier, and Elastic File System (EFS). Here are some topics to focus on during this period:

    • Simple Storage Service (S3): You should start by learning about S3, which is a highly scalable and durable object storage service. You should learn about the different storage classes available in S3, including S3 Standard, S3 Intelligent-Tiering, S3 Standard-Infrequent Access, and S3 One Zone-Infrequent Access. You should also learn about S3 security, including access control policies, encryption options, and bucket policies.
    • Elastic Block Store (EBS): You should also learn about EBS, which provides block-level storage volumes for use with EC2 instances. You should learn about the different volume types available in EBS, including General Purpose SSD (GP2), Provisioned IOPS SSD (IO1), and Throughput Optimized HDD (ST1).
    • Glacier: You should learn about Glacier, which is a low-cost archival storage service. You should learn about the different storage classes available in Glacier, including Glacier Standard, Glacier Expedited, and Glacier Bulk.
    • Elastic File System (EFS): You should also learn about EFS, which provides scalable file storage for use with EC2 instances. You should learn about the different performance modes available in EFS, including General Purpose and Max I/O. You should also learn about EFS security, including access control policies and encryption options.
    • Hands-on Practice: As with the previous weeks, you should also practice using these services through the AWS Free Tier. This will help you become familiar with the AWS console and give you practical experience with using these services.

    During this period, you should also continue practicing for the AWS Certified Solution Architect Associate Exam by taking practice tests and reviewing sample questions. This will help you become more familiar with the exam format and the types of questions you can expect to see on the actual exam.

    Week 5-6 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS storage services, including S3, EBS, Glacier, and EFS. By mastering these topics, you will be well-prepared for the storage-related questions that may appear on the exam.

    Week 7-8: Network Services and Security

    Week 7-8 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS network services and security. In this section, you will learn about the different networking services offered by AWS, including Virtual Private Cloud (VPC), Route 53, Direct Connect, and Elastic Load Balancing (ELB). You will also learn about security-related topics, including Identity and Access Management (IAM), Key Management Service (KMS), and AWS Organizations. Here are some topics to focus on during this period:

    • Virtual Private Cloud (VPC): You should start by learning about VPC, which is a logical isolated section of the AWS Cloud that allows you to launch AWS resources in a virtual network. You should learn about VPC components, including subnets, security groups, and network ACLs. You should also learn about VPC peering, VPC endpoints, and NAT gateway.
    • Route 53: You should learn about Route 53, which is a scalable and highly available Domain Name System (DNS) service. You should learn about how to create and manage DNS records, including A records, CNAME records, and MX records.
    • Direct Connect: You should also learn about Direct Connect, which provides dedicated network connections between your on-premises data center and AWS. You should learn about the different connection options available in Direct Connect, including dedicated connections and hosted connections.
    • Elastic Load Balancing (ELB): You should learn about ELB, which distributes incoming traffic across multiple targets, such as EC2 instances or containers. You should learn about the different types of load balancers available in ELB, including Application Load Balancer, Network Load Balancer, and Classic Load Balancer.
    • Identity and Access Management (IAM): You should also learn about IAM, which provides centralized control of AWS resources. You should learn about IAM users, groups, and roles, and how to use IAM policies to control access to AWS resources.
    • Key Management Service (KMS): You should learn about KMS, which provides managed encryption keys that you can use to encrypt your data stored in AWS. You should learn about the different types of keys available in KMS, including customer master keys (CMKs) and data encryption keys (DEKs).
    • AWS Organizations: You should also learn about AWS Organizations, which allows you to manage multiple AWS accounts centrally. You should learn about how to create and manage AWS accounts, and how to use service control policies (SCPs) to control access to AWS services.
    • Hands-on Practice: As with the previous weeks, you should also practice using these services through the AWS Free Tier. This will help you become familiar with the AWS console and give you practical experience with using these services.

    Week 7-8 of preparing for the AWS Certified Solution Architect Associate Exam focuses on learning about AWS network services and security, including VPC, Route 53, Direct Connect, ELB, IAM, KMS, and AWS Organizations. By mastering these topics, you will be well-prepared for the networking and security-related questions that may appear on the exam.

    Step 3: Practice, Practice, Practice

    One of the most critical steps in preparing for the AWS Certified Solution Architect Associate Exam is practicing. You should take as many practice exams as possible to get a feel for the exam format and the types of questions that will be asked. AWS provides a free practice exam on their website, which you should take before the actual exam. Additionally, there are many third-party practice exams available, such as those from Whizlabs, Udemy, and A Cloud Guru.

    Step 4: Stay Up-to-Date with AWS Services

    AWS regularly releases new services and features, so it is essential to stay up-to-date with these changes. You should subscribe to AWS newsletters and blogs to keep up with the latest news and updates. Additionally, you should regularly review the AWS documentation to ensure that you are familiar with the latest features and services.

    Step 5: Exam Day

    Step 5: Exam Day is the final step in your journey to becoming an AWS Certified Solution Architect Associate. Here are some tips to help you prepare for and succeed on exam day:

    • Review your notes and study materials: On the day before the exam, take some time to review your notes and study materials. This will help refresh your memory on the topics you have been studying and help you identify any areas where you may need to focus your attention.
    • Get a good night’s sleep: It’s important to be well-rested on exam day, so make sure to get a good night’s sleep. Try to go to bed early, and avoid consuming caffeine or alcohol before bedtime.
    • Eat a healthy breakfast: On the morning of the exam, make sure to eat a healthy breakfast. This will help give you the energy you need to stay focused and alert during the exam.
    • Arrive early: Plan to arrive at the testing center at least 30 minutes before your scheduled exam time. This will give you plenty of time to check in, review the exam rules, and get settled before the exam begins.
    • Bring the necessary materials: Make sure to bring a valid form of government-issued identification, such as a passport or driver’s license, to the testing center. You should also bring a pen and paper, as well as any other materials allowed by the testing center.
    • Stay calm and focused: During the exam, it’s important to stay calm and focused. If you encounter a question that you don’t know the answer to, don’t panic. Take a deep breath, and move on to the next question. You can always come back to difficult questions later.
    • Pace yourself: The AWS Certified Solution Architect Associate Exam consists of 65 questions, and you have 130 minutes to complete the exam. This means you have an average of just over two minutes per question. Make sure to pace yourself, and don’t spend too much time on any one question.
    • Review your answers: After you have answered all of the questions, take some time to review your answers. Make sure you have answered every question, and double-check your answers to ensure they are accurate.
    • Celebrate your success: After you have completed the exam, take some time to celebrate your success. Becoming an AWS Certified Solution Architect Associate is a significant accomplishment, and you should be proud of your hard work and dedication.

    Conclusion

    Passing the AWS Certified Solution Architect Associate Exam requires dedication, commitment, and a solid understanding of AWS services and architecture principles. It also requires a significant amount of time and effort. By following the practical guide outlined in this blog post, you can prepare for the exam in two months and increase your chances of passing. Remember to set a realistic study plan, practice, stay up-to-date with AWS services, and arrive early on exam day. Good luck with your exam!

  • How to Pass the CKAD Exam in One Month: A Practical Guide

    How to Pass the CKAD Exam in One Month: A Practical Guide

    The Certified Kubernetes Application Developer (CKAD) exam is designed to test your skills in developing and deploying applications on Kubernetes. If you are planning to take the CKAD exam, you may be wondering how to best prepare for it in a short amount of time. In this article, we will provide you with practical details on how to pass the CKAD exam in one month.

    Understand the Exam Objectives

    Before starting your preparation for the CKAD exam, it is crucial to understand the exam objectives. The CKAD exam tests your knowledge and skills in the following areas:

    • Core Kubernetes Concepts
    • Configuration
    • Multi-Container Pods
    • Observability
    • Pod Design
    • Services & Networking
    • State Persistence

    Understanding the exam objectives will help you to focus your study efforts and create a study plan.

    Create a Study Plan

    To pass the CKAD exam in one month, you need to create a study plan that covers all the exam objectives. Here’s an example study plan:

    Week 1:

    • Study Kubernetes core concepts, including Pods, Deployments, and Services.
    • Practice creating and managing Kubernetes objects.

    Week 2:

    • Study Configuration and Multi-Container Pods, including ConfigMaps and Secrets.
    • Practice creating and managing Kubernetes objects.

    Week 3:

    • Study Pod Design and Observability, including Liveness Probes and Logging.
    • Practice creating and managing Kubernetes objects.

    Week 4:

    • Study Services & Networking, including Service Discovery and Network Policies.
    • Study State Persistence, including Persistent Volumes and Persistent Volume Claims.
    • Practice creating and managing Kubernetes objects.
    • Remember to schedule your study time around your work and personal commitments. It is also essential to take regular breaks to avoid burnout.

    Practice, Practice, Practice

    The key to passing the CKAD exam is practice. You need to practice creating and managing Kubernetes objects, troubleshooting common issues, and developing and deploying applications on Kubernetes.

    There are several ways to practice for the CKAD exam:

    • Use the Kubernetes documentation – The Kubernetes documentation is an excellent resource for learning Kubernetes concepts and commands.
    • Use online labs – There are many online labs available that provide a Kubernetes environment for practicing.
    • Use practice exams – Practice exams can help you to familiarize yourself with the exam format and test your knowledge.
    • Join a study group – Joining a study group can provide you with support, motivation, and additional resources.

    Useful Tips for the Exam Day

    On the day of the exam, there are several things you can do to help you pass:

    • Get a good night’s sleep – Being well-rested will help you to stay focused during the exam.
    • Read the instructions carefully – Make sure you understand the instructions and requirements of each task.
    • Manage your time – The CKAD exam is a time-limited exam, so manage your time wisely.
    • Don’t panic – If you get stuck on a task, take a deep breath, and try to think logically about how to proceed.
    • Use the Kubernetes documentation – The Kubernetes documentation is available during the exam, so make use of it.

    Conclusion

    Passing the CKAD exam in one month is achievable with the right study plan and practice. Understanding the exam objectives, creating a study plan, and practicing regularly will help you to succeed. Remember to take regular breaks and use resources such as the Kubernetes documentation, online labs, and practice exams. On the day of the exam, stay calm, manage your time wisely, and use the available resources. Good luck!

  • How to Pass the CKA Exam in One Month: A Practical Guide

    How to Pass the CKA Exam in One Month: A Practical Guide

    Group of graduates celebrating by throwing caps in the air during a sunny day.

    The Certified Kubernetes Administrator (CKA) exam is a challenging certification that validates your Kubernetes skills and knowledge. If you’re preparing to take the CKA exam, you may be wondering how to best prepare for it in a short amount of time. In this article, we’ll provide you with practical details on how to pass the CKA exam in one month.

    Understand the Exam Objectives

    Before you start studying, it’s essential to understand the exam objectives. The CKA exam tests your knowledge and skills in the following areas:

    • Kubernetes core concepts
    • Kubernetes networking
    • Kubernetes scheduling
    • Kubernetes security
    • Kubernetes cluster maintenance
    • Kubernetes troubleshooting

    Understanding the exam objectives will help you to focus your study efforts and create a study plan.

    Create a Study Plan

    To pass the CKA exam in one month, you’ll need to create a study plan that covers all the exam objectives. Here’s an example study plan:

    Week 1:

    • Study Kubernetes core concepts, including Pods, Deployments, Services, and ConfigMaps.
    • Practice creating and managing Kubernetes objects.

    Week 2:

    • Study Kubernetes networking, including Services, Ingress, and NetworkPolicies.
    • Practice creating and managing Kubernetes networking objects.

    Week 3:

    • Study Kubernetes scheduling, including Nodes, Pods, and the Kubernetes Scheduler.
    • Practice creating and managing Kubernetes scheduling objects.

    Week 4:

    • Study Kubernetes security, including Authentication, Authorization, and Admission Control.
    • Practice creating and managing Kubernetes security objects.
    • Study Kubernetes cluster maintenance and troubleshooting.
    • Practice troubleshooting common Kubernetes issues.

    Remember to schedule your study time around your work and personal commitments. It’s also essential to take regular breaks to avoid burnout.

    Practice, Practice, Practice

    The key to passing the CKA exam is practice. You’ll need to practice creating and managing Kubernetes objects, troubleshooting common issues, and securing your Kubernetes cluster.

    There are several ways to practice for the CKA exam:

    • Use the Kubernetes documentation – The Kubernetes documentation is an excellent resource for learning Kubernetes concepts and commands.
    • Use online labs – There are many online labs available that provide a Kubernetes environment for practicing.
    • Use practice exams – Practice exams can help you to familiarize yourself with the exam format and test your knowledge.
    • Join a study group – Joining a study group can provide you with support, motivation, and additional resources.

    Useful Tips for the Exam Day

    On the day of the exam, there are several things you can do to help you pass:

    • Get a good night’s sleep – Being well-rested will help you to stay focused during the exam.
    • Read the instructions carefully – Make sure you understand the instructions and requirements of each task.
    • Manage your time – The CKA exam is a time-limited exam, so manage your time wisely.
    • Don’t panic – If you get stuck on a task, take a deep breath, and try to think logically about how to proceed.
    • Use the Kubernetes documentation – The Kubernetes documentation is available during the exam, so make use of it.

    Conclusion

    Passing the CKA exam in one month is achievable with the right study plan and practice. Understanding the exam objectives, creating a study plan, and practicing regularly will help you to succeed. Remember to take regular breaks and use resources such as the Kubernetes documentation, online labs, and practice exams. On the day of the exam, stay calm, manage your time wisely, and use the available resources. Good luck!

  • Kubernetes Pod, ReplicaSet and Deployment

    Kubernetes Pod, ReplicaSet and Deployment

    What is Kubernetes?

    Kubernetes is an open source container orchestration engine for automating deployment, scaling, and management of containerized applications. It’s supported by all hyperscaller cloud providers and widely used by different companies. Amazon, Google, IBM, Microsoft, Oracle, Red Hat, SUSE, Platform9, IONOS and VMware offer Kubernetes-based platforms or infrastructure as a service (IaaS) that deploy Kubernetes.

    Pod

    A Pod is the smallest Kubernetes deployable computing unit. It contains one or more containers with shared storage and network. Usually, Pods have a one-to-one relationship with containers. To scale up, we add more Pods and to scale down, we delete pods. We don’t add more containers to a pod for scaling purposes.

    A Pod can have multiple containers with different types. We use a multi-container Pod when the application needs a helper container to run side by side with it. This helper container will be created when the application container is created and it will be deleted if the application container is deleted. They also share the same network (which means that they can communicate with each other using localhost) and same storage (using volumes).

    We can create a pod using the following command,

    $ kubectl run nginx --image nginx

    create a Pod named nginx using the nginx docker image

    This command will create a Pod named nginx using the nginx docker image available in docker-hub. To confirm the pod is created successfully, We can run the following command that will list pods in the default namespace,

    $ kubectl get pods
    NAME    READY   STATUS    RESTARTS   AGE
    nginx   1/1     Running   0          15s

    list pods in default namespace.

    We also can create Pods using yaml configuration file,

    apiVersion: v1
    kind: Pod
    metadata:
      name: redis-pod
    spec:
      containers:
      - name: redis-container
        image: redis
        ports:
        - containerPort: 6379
    

    redis-pod.yaml

    To create this redis Pod, run the following command,

    $ kubectl create -f redis-pod.yaml

    create redis pod from yaml file

    We also can create a multi-container Pod using yaml file,

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        run: web-app
      name: web-app
      namespace: default
    spec:
      containers:
      - image: web-app
        imagePullPolicy: Always
        name: web-app
      - image: log-agent
        imagePullPolicy: IfNotPresent
        name: log-agent
      priority: 0
      restartPolicy: Always
    

    multi-container-pod.yaml

    To create this Pod, run the following command,

    $ kubectl create -f multi-container-pod.yaml

    create multi-container pod from yaml file

    This will deploy a logging agent alongside with our web app container to process the logs and send it to a central logging service for example. This pattern is called sidecar.

    Replicaset

    A replicaset ensures that the specified number of Pods (replicas) are running at all times. If a Pod goes down for any reason, It automatically creates a new one using the template specified in the yaml file. Here is an example replicaset definition file,

    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      labels:
        app: nginx
      name: nginx-replicaset
      namespace: default
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - image: nginx
            imagePullPolicy: Always
            name: nginx
          dnsPolicy: ClusterFirst
          restartPolicy: Always

    nginx-replicaset.yaml

    To create this repicaset, run the following command,

    $ kubectl create -f nginx-replicaset.yaml

    create nginx replicaset from yaml file

    Note: In order for the replicaset to work, the spec.selector.matchLabels must match the spec.template.labels because the replicaset uses this template to create pods when needed.

    Deployment

    A Deployment is the recommended choice when it comes to deploy stateless applications in Kubernetes. It automatically creates a replicaset under the hood to ensure the specified number of Pods are running at all times. It also describe how to deploy a new version using deployment strategy, here is an example yaml definition file for a deployment,

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: example-nginx-deploy
      labels:
        app: nginx-deploy
    spec:
      replicas: 5
      strategy:
        type: RollingUpdate
      selector:
        matchLabels:
          app: nginx-pod
      template:
        metadata:
          labels:
            app: nginx-pod
        spec:
          containers:
          - name: nginx-container
            image: nginx:1.14.2
            ports:
            - containerPort: 8080

    nginx-deploy.yaml

    To create this nginx Deployment, run the following command,

    nginx-deploy.yaml redis-pod.yaml

    create redis pod from yaml file

    This will create a deployment named example-nginx-deploy using the nginx docker image available in docker-hub. To confirm the deployment is created successfully, We can run the following command that will list deployments in the default namespace,

    $ kubectl get deploy
    NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
    example-nginx-deploy   5/5     5            5           63s

    list deployments in default namespace.

    This will create a ReplicaSet under the hood to make sure all replicas are up all time,

    $ kubectl get rs
    NAME                             DESIRED   CURRENT   READY   AGE
    example-nginx-deploy-bbc95f979   5         5         5       2m19s

    list replicasets in default namespace.

    It will also create 5 replicas using the same docker image specified in the yaml definition file, It uses the name of the deployment as a prefix for the name of the pod as shown below,

    $ kubectl get pods
    NAME                                   READY   STATUS    RESTARTS   AGE
    example-nginx-deploy-bbc95f979-2g7kh   1/1     Running   0          97s
    example-nginx-deploy-bbc95f979-dlq8l   1/1     Running   0          2m6s
    example-nginx-deploy-bbc95f979-gb97h   1/1     Running   0          97s
    example-nginx-deploy-bbc95f979-n6xdj   1/1     Running   0          97s
    example-nginx-deploy-bbc95f979-pwphh   1/1     Running   0          97s

    list pods in default namespace.

    Note: In order for the deployment to work, the spec.selector.matchLabels must match the spec.template.labels because the deployment uses this template to create pods when the current number of pods doesn’t match the desired number of pods.

    Scaling a Deployment

    When creating a Deployment, We need to specify the number or replicas (default is one). Sometimes we need to scale up (increase the number of replicas) or scale down (decrease the number of replicas) manually. We can do this by running the following commands:

    Scaling up example-nginx-deply from 5 replicas to 10 replicas:

    $ kubectl scale deployment example-nginx-deploy --replicas 10
    deployment.apps/example-nginx-deploy scaled

    Scaling up example-nginx-deply from 5 replicas to 10 replicas

    To confirm, We can list all pods in the default namespace. As you can see below, There are five new replicas created.

    $ kubectl get pods
    NAME                                    READY   STATUS              RESTARTS   AGE
    example-nginx-deploy-5674dfc4b7-7zzpr   0/1     ContainerCreating   0          3s
    example-nginx-deploy-5674dfc4b7-8dhhx   0/1     ContainerCreating   0          3s
    example-nginx-deploy-5674dfc4b7-8v8mw   1/1     Running             0          32m
    example-nginx-deploy-5674dfc4b7-95j9p   1/1     Running             0          32m
    example-nginx-deploy-5674dfc4b7-gl97j   0/1     ContainerCreating   0          3s
    example-nginx-deploy-5674dfc4b7-gtq4w   1/1     Running             0          32m
    example-nginx-deploy-5674dfc4b7-kcj2f   0/1     ContainerCreating   0          3s
    example-nginx-deploy-5674dfc4b7-m4wkq   0/1     ContainerCreating   0          3s
    example-nginx-deploy-5674dfc4b7-tsw6s   1/1     Running             0          32m
    example-nginx-deploy-5674dfc4b7-xfg7q   1/1     Running             0          32m

    list pods in default namespace.

    Scaling down example-nginx-deply from 10 replicas to 3 replicas:

    $ kubectl scale deployment example-nginx-deploy --replicas 3
    deployment.apps/example-nginx-deploy scaled

    Scaling down example-nginx-deply from 10 replicas to 3 replicas

    To confirm, We can list all pods in the default namespace. As you can see below, There are only three replicas running and the rest are terminating.

    $ kubectl get pods
    NAME                                    READY   STATUS              RESTARTS   AGE
    example-nginx-deploy-5674dfc4b7-6pncj   1/1     Terminating   0          33s
    example-nginx-deploy-5674dfc4b7-8v8mw   1/1     Running       0          39m
    example-nginx-deploy-5674dfc4b7-95j9p   1/1     Running       0          39m
    example-nginx-deploy-5674dfc4b7-9r7kv   1/1     Terminating   0          33s
    example-nginx-deploy-5674dfc4b7-gtq4w   1/1     Running       0          39m
    example-nginx-deploy-5674dfc4b7-lgwnj   1/1     Terminating   0          33s
    example-nginx-deploy-5674dfc4b7-m4hc8   1/1     Terminating   0          33s
    example-nginx-deploy-5674dfc4b7-zr485   1/1     Terminating   0          33s

    list pods in default namespace.

    Zero downtime Deployment

    The main point of using a deployment in Kubernetes is that we can easily deploy a new version of our application using the deployment strategy defined in the deployment and we can also roll it back easily if this new version didn’t work as expected with zero downtime.

    We use .spec.strategy to specify how we want to deploy this new version. We have two types of deployment strategies, Recreate and RollingUpdateRollingUpdate is the default value.

    Recreate Deployment Strategy means that all current pods with the old version will be killed and new pods will be created using the new version. This will cause the application to be down for sometime (depends on how long it takes to start the app). This is not the preferred option because of the downtime.

    RollingUpdate Deployment Strategy means that it will run both versions (the old and new one) for sometime until the deployment is completed. based on the .spec.strategy.rollingUpdate.maxUnavailable and .spec.strategy.rollingUpdate.maxSurge values, it will create new pods with the new version and delete old pods with the old version. if we configured this correctly, we can assure that the deployment is zero downtime deployment.

    Here is an example to make it more clear,

    spec:
      replicas: 10
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 30%
          maxUnavailable: 20%

    rolling update configuration

    In this example deployment, we have ten replicas and RollingUpdate strategy with maxSurge is 30% (it can also be an absolute number) which means that this deployment can run 30% more replicas if needed (instead of creating 10 replicas, it can create 13 replicas temporary for both old and new versions) and maxUnavailable is 20% (it can also be an absolute number) which is the maximum number of unavailable pods during the update process (20% of ten replicas is two pods)

    The default value for maxSurge and maxUnavailable is 25%.

    Rollout and Rollback a Deployment

    To deploy a new version of a deployment we use the following command,

    $ kubectl set image deployment/example-nginx-deploy nginx-container=nginx:1.16.1 --record
    deployment.apps/example-nginx-deploy image updated

    deploy nginx 1.16.1

    Note: The record flag is deprecated but there is no alternative to it yet. for more info check this.

    To check the status of the deployment,

    $ kubectl rollout status deployment/example-nginx-deploy
    Waiting for deployment "example-nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 2 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 2 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 2 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "example-nginx-deploy" rollout to finish: 4 of 5 updated replicas are available...
    deployment "example-nginx-deploy" successfully rolled out

    check the status of the rollout

    As you can see from the logs, it first created two pods with the new version. Once they are up and running it starts to terminate old pods (using old version) and create new pods (using new version) one by one to make sure the application is up and running at all times.

    we can see this more clearly from the deployment events,

    $ kubectl describe deployments
    
    
    Name:                   example-nginx-deploy
    Namespace:              default
    CreationTimestamp:      Sun, 18 Sep 2022 11:42:41 +0200
    Labels:                 app=example-nginx-deploy
    Annotations:            deployment.kubernetes.io/revision: 2
    Selector:               app=example-nginx-deploy
    Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  20% max unavailable, 30% max surge
    Pod Template:
      Labels:  app=example-nginx-deploy
      Containers:
       nginx:
        Image:        nginx:1.16.1
        Port:         <none>
        Host Port:    <none>
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   example-nginx-deploy-78788d9bbd (5/5 replicas created)
    Events:
      Type    Reason             Age    From                   Message
      ----    ------             ----   ----                   -------
      Normal  ScalingReplicaSet  8m13s  deployment-controller  Scaled up replica set example-nginx-deploy-78788d9bbd to 2
      Normal  ScalingReplicaSet  8m13s  deployment-controller  Scaled down replica set example-nginx-deploy-bbc95f979 to 4
      Normal  ScalingReplicaSet  8m13s  deployment-controller  Scaled up replica set example-nginx-deploy-78788d9bbd to 3
      Normal  ScalingReplicaSet  4m57s  deployment-controller  Scaled down replica set example-nginx-deploy-bbc95f979 to 3
      Normal  ScalingReplicaSet  4m57s  deployment-controller  Scaled up replica set example-nginx-deploy-78788d9bbd to 4
      Normal  ScalingReplicaSet  4m53s  deployment-controller  Scaled down replica set example-nginx-deploy-bbc95f979 to 2
      Normal  ScalingReplicaSet  4m53s  deployment-controller  Scaled up replica set example-nginx-deploy-78788d9bbd to 5
      Normal  ScalingReplicaSet  4m49s  deployment-controller  Scaled down replica set example-nginx-deploy-bbc95f979 to 1
      Normal  ScalingReplicaSet  4m46s  deployment-controller  Scaled down replica set example-nginx-deploy-bbc95f979 to 0

    Let’s say nginx 1.16.1 is a buggy version and we want to rollback to the previous version. First we need to check the rollout history,

    kubectl rollout history deployment/example-nginx-deploy
    deployment.apps/example-nginx-deploy
    REVISION  CHANGE-CAUSE
    1         <none>
    3         kubectl set image deployment/example-nginx-deploy nginx=nginx:1.14.2 --record=true
    4         kubectl set image deployment/example-nginx-deploy nginx=nginx:1.16.1 --record=true

    To rollback to previous version, Run the following command,

    kubectl rollout undo deployment/example-nginx-deploy
    deployment.apps/example-nginx-deploy rolled back

    rollback to nginx 1.14.2

    Conclusion

    A Pod is the smallest Kubernetes deployable computing unit. It contains one or more containers with shared storage and network. A replicaset ensures that the specified number of Pods (replicas) are running at all times. If a Pod goes down for any reason, It automatically creates a new one using the template specified in the yaml file. A Deployment is the recommended choice when it comes to deploy stateless applications in Kubernetes. It automatically creates a replicaset under the hood to ensure the specified number of Pods are running at all times. It can be easily scaled up or down using kubectl scale command. It also enables us to easily rollout a new version of our application using the specified deployment strategy and rollback to previous version if needed.

  • Kubernetes Autoscaling: HPA vs VPA, A Complete Guide

    Kubernetes Autoscaling: HPA vs VPA, A Complete Guide

    What is Kubernetes?

    Kubernetes is an open source container orchestration engine for automating deployment, scaling, and management of containerized applications. It’s supported by all hyperscaller cloud providers and widely used by different companies. Amazon, Google, IBM, Microsoft, Oracle, Red Hat, SUSE, Platform9, IONOS and VMware offer Kubernetes-based platforms or infrastructure as a service (IaaS) that deploy Kubernetes.

    Vertical Scaling

    Vertical scaling means increasing the amount of CPU and Ram that’s used by a single instance of your application.

    For example, if we deployed our application to a virtual machine (or an EC2 instance) with 8 Gib of Ram and 1 CPUs, and our application is getting more traffic, we can vertically scale the app by increasing the Ram to 16 Gib and adding one more CPU.

    A drawback to this approach is that it has limits. at some point you won’t be able to scale more. That’s why we need horizontal scaling as well.

    Horizontal Scaling

    Horizontal scaling means increasing the number of instances that run your application.

    For example, if we deployed our application to a virtual machine (or an EC2 instance), and our application is getting more traffic, we can horizontally scale the app by adding one more instance and use a load balancer to split the traffic between them.

    If you are using a cloud provider (like AWS), theoretically, you can add an unlimited number of instances (of course it’s going to cost some money).

    Why do we need Autoscaling?

    Autoscaling means automatically scaling your application, horizontally or vertically, based on a metric(s) like CPU or memory utilization without human intervention.

    We need autoscaling because we want to respond to increasing traffic as quickly as possible. We also want to save money and run as few instances with as little resources as possible.

    In Kubernetes, We use Vertical Pod Autoscaler (VPA) and Horizontal Pod Autoscaler (HPA) to achieve autoscaling.

    Install the metrics server

    For Horizontal Pod Autoscaler and Vertical Pod Autoscaler to work, we need to install the metrics server in our cluster. It collects resource metrics from Kubelets and exposes them in Kubernetes apiserver through Metrics API. Metrics API can also be accessed by kubectl top, making it easier to debug autoscaling pipelines.

    To install,

    $ kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
    

    To verify installation,

    $ kubectl top pods -n kube-system
    

    This should return all metrics for pods in kube-system namespace.

    Vertical Pod Autoscaler

    Vertical Pod Autoscaler (VPA) allows you to increase or decrease your pods’ resources (RAM and CPU) based on a selected metric. The Vertical Pod Autoscaler ( VPA ) can suggest the Memory/CPU requests and Limits. It can also automatically update the Memory/CPU requests and Limits if this is enabled by the user. This will reduce the time taken by the engineers running the Performance/Benchmark testing to determine the correct values for CPU and memory requests/limits.

    VPA doesn’t come with kubernetes by default so we need to install it first,

    $ git clone https://github.com/kubernetes/autoscaler.git
    $ cd autoscaler/vertical-pod-autoscaler/
    $ ./hack/vpa-up.sh 
    $ kubectl get po -n kube-system | grep -i vpa
    
    NAME READY STATUS RESTARTS AGE
    vpa-admission-controller-dklptmn43-44klm 1/1 Running 0 1m11s
    vpa-recommender-prllenmca-gjf53 1/1 Running 0 1m50s
    vpa-updater-ldee3597h-fje44 1/1 Running 0 1m48s
    

    install VPA

    Example

    Create a redis deployment and request too much memory and cpu.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis-example-deployment
    spec:
      template:
        metadata:
          labels:
            app: redis-example-app
        spec:
          containers:
          — name: example-app
            image: redis
            resources:
              limits:
                cpu: 900m
                memory: 4Gi
              requests:
                cpu: 700m
                memory: 2Gi

    redis-example-deployment.yaml

    Then, we need to run the following command to create the deployment,

    $ kubectl create -f redis-example-deployment.yaml

    create redis deployment

    Next step will be to create the VPA,

    apiVersion: autoscaling.k8s.io/v1beta1
    kind: VerticalPodAutoscaler
    metadata:
      name: redis-example-deployment-vpa
    spec:
      targetRef:
        apiVersion: "apps/v1"
        kind:       Deployment
        name:       redis-example-deployment
      updatePolicy:
        updateMode: "Off"
      resourcePolicy:
        containerPolicies:
          - containerName: "example-app"
            minAllowed:
              cpu: "100m"
              memory: "50Mi"
            maxAllowed:
              cpu: "600m"
              memory: "500Mi"

    redis-example-deployment-vpa.yaml

    As you can see, VPA definition file consists of three parts,

    • targetRef which defines the target of this VPA. It should match the deployment we created earlier.
    • updatePolicy it tells the VPA how to update the target resource
    • resourcePolicy, optional. This allow us to be more flexible by defining minimum and maximum resources for a container or to run of autoscaling for a specific container using containerPolicies

    VPA update Policy

    Here are all valid options for updateMode in VPA:

    • Off – VPA will only provide the recommendations, then we need to apply them manually if we want to. This is best if we want to use VPA just to give us an idea how much resources our application needs.
    • Initial – VPA only assigns resource requests on pod creation and never changes them later. It will still provide us with recommendations.
    • Recreate – VPA assigns resource requests on pod creation time and updates them on existing pods by evicting and recreating them.
    • Auto – It automatically recreates the pod based on the recommendation. It’s best to use PodDisruptionBudget to ensure that the one replica of our deployment is up at all the time without any restarts. This will eventually ensure that our application is available and consistent. For more information please check this.

    Horizontal Pod Autoscaler

    Horizontal Pod Autoscaling (HPA) that allows you to increase or decrease the number of pods in a deployment automatically based on a selected metric. HPA comes with kubernetes by default.

    Example

    Create an nginx deployment and make sure you define resources request and limits,

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: example-nginx-deploy
    spec:
      selector:
        matchLabels:
          app: nginx-hpa-deploy
      replicas: 2
      template:
        metadata:
          labels:
            app: nginx-hpa-deploy
        spec:
          containers:
          - name: nginx-container
            image: nginx
            ports:
            - containerPort: 8080
            resources:
              limits:
                cpu: 400m
                memory: 2Gi
              requests:
                cpu: 100m
                memory: 1Gi

    example-nginx-deploy.yaml

    Then we need to run the following command to create the deployment,

    $ kubectl create -f example-nginx-deploy.yaml

    create nginx deployment

    Next step will be to create the HPA,

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: example-nginx-hpa
    spec:
      maxReplicas: 100
      minReplicas: 1
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: example-nginx-deploy
      targetCPUUtilizationPercentage: 55

    example-nginx-hpa.yaml

    This HPA will use CPU utilization to scale the deployment. If it’s more than 55%, it will scale up. If it’s less than 55%, it will scale down. To create the HPA, we need to run the following command:

    $ kubectl create -f example-nginx-hpa.yaml

    create nginx deployment

    There are a lot more configurations we can use to make our HPA more stable and useful. Here’s an example with common configuration:

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: example-hpa
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: example-nginx-deploy
      minReplicas: 1
      maxReplicas: 100
      metrics:
      - type: Resource
        resource:
          name: memory
          target:
            type: Utilization
            averageUtilization: 70
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 70
      behavior:
        scaleDown:
          stabilizationWindowSeconds: 300
          policies:
          - type: Percent
            value: 10
            periodSeconds: 60
          selectPolicy: Min
        scaleUp:
          stabilizationWindowSeconds: 0
          policies:
          - type: Percent
            value: 10
            periodSeconds: 60
          - type: Pods
            value: 4
            periodSeconds: 15
          selectPolicy: Max
    

    hpa.yaml

    In this example, we use CPU and Memory Utilization. There’s also the possibility to add more metrics if we want. We also defined scaleUp and scaleDown behaviors to tell kubernetes how we want to do it. For more info please check this.

    Custom metrics

    In some applications, scaling based on memory or CPU utilization is not that important, probably it does some blocking tasks (live calling external API) which doesn’t consume much resources. In this case, scaling based on the number of requests makes more sense.

    Since we are using autoscaling/v2 API version, We can configure a HPA to scale based on a custom metric (that is not built in to Kubernetes or any Kubernetes component). The HPA controller then queries for these custom metrics from the Kubernetes API.

    Conclusion

    Autoscaling is a powerful feature. It allows us to easily adopt our application to handle load change automatically without any human intervention. We can use VerticalPodAutoscaler to help us determine the resources needed for our application. We also can use HPA to add or remove replicas dynamically based on CPU or/and memory utilization. It’s also possible to scale based on a custom metric like RPS (number of requests per second) or number of messages in a queue if we use event driven architecture.

  • Startup, Liveness and Readiness Probes in Kubernetes: A Practical Guide

    Startup, Liveness and Readiness Probes in Kubernetes: A Practical Guide

    What is Kubernetes?

    Kubernetes is an open source container orchestration engine for automating deployment, scaling, and management of containerized applications. It’s supported by all hyperscaller cloud providers and widely used by different companies. Amazon, Google, IBM, Microsoft, Oracle, Red Hat, SUSE, Platform9, IONOS and VMware offer Kubernetes-based platforms or infrastructure as a service (IaaS) that deploy Kubernetes.

    What is a prope in Kubernetes?

    A Prope is a health check that is triggered by the kublet to automatically determine if a pod can accept traffic or not. There are four options

    • httpGet: HTTP check based on the response status code. Any code greater than or equal to 200 and less than 400 indicates success. Any other code indicates failure.
    • exec: Check the command’s exit status. If it’s zero (0), it indicates success otherwise it’s considered failure.
    • tcpSocket: The kubelet will attempt to open a TCP socket connection to your container on the specified port. If it connects successfully, the container is considered healthy, otherwise it’s a failure.
    • grpc: The kubelet will use gRPC health checking protocol to check if your container is able to handle RPC calls or not.

    Prope common fields

    • initialDelaySeconds: Number of seconds after the container has started before the probes are initiated. Defaults to zero (0) seconds.
    • periodSeconds: How often (in seconds) to perform the probe. Default to 10 seconds.
    • timeoutSeconds: Number of seconds after which the probe times out. Defaults to 1 second.
    • successThreshold: Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness and startup Probes.
    • failureThreshold: When a probe fails, Kubernetes will try failureThreshold times before giving up. Defaults to 3.

    Startup Prope

    A startup probe verifies whether the application within a container is started. It runs before any other probe, and, unless it finishes successfully, disables other probes. If a container fails its startup probe, then the container is killed and follows the pod’s restartPolicy.

    This type of probe is only executed at startup, unlike readiness probes, which are run periodically.

    The startup probe is configured in the spec.containers.startupprobe attribute of the pod configuration.

    Readiness Prope

    A readiness prope verifies whether the application within a container is ready to accept traffic. If it fails for failureThreshold times, the pod will be restarted. It is configured in the spec.containers.readinessprobe attribute of the pod configuration.

    Liveness Prope

    A liveness prope verifies whether the application within a container is healthy. If it fails for failureThreshold times, the pod will be killed and restarted. It is configured in the spec.containers.livenessprobe attribute of the pod configuration.

    Examples

    Here is a deployment that uses startup, readiness and liveness http propes:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: http-example-deployment
    spec:
      template:
        metadata:
          labels:
            app: example-app
        spec:
          containers:
          — name: example-app
            image: nginx
            startupProbe:
              httpGet:
                path: /start
                port: named-port
              failureThreshold: 30
              periodSeconds: 10
            readinessProbe:
              httpGet:
                path: /ready
                port: 8080
              successThreshold: 3
            livenessProbe:
              httpGet:
                path: /health
                port: 8080
                httpHeaders:
                  - name: example
                    value: header
              initialDelaySeconds: 3
              periodSeconds: 3

    Kubernetes deployment uses startup, readiness and liveness http propes.

    In this example, We use different endpoint for readiness and liveness propes. This is the best practice because in the readiness prope, we might need to check all dependencies are up which might take some time and resources but in the liveness prope, since it’s called periodically, we want to get a response as quickly as possible to respond to deadlock fast.

    Here is a deployment that uses readiness exec prope and liveness tcpSocket prope:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: http-example-deployment
    spec:
      template:
        metadata:
          labels:
            app: example-app
        spec:
          containers:
          — name: example-app
            image: nginx
            readinessProbe:
              exec:
                command:
                  - cat
                  - /tmp/healthy
              initialDelaySeconds: 5
              periodSeconds: 5
            livenessProbe:
              tcpSocket:
                port: 8080
              initialDelaySeconds: 15
              periodSeconds: 20

    Kubernetes deployment uses readiness exec command prope and liveness tcp socket propes.

    Here is an example pod that uses grpc liveness prope:

    apiVersion: v1
    kind: Pod
    metadata:
      name: example-pod
    spec:
      containers:
      - name: grpc-prope
        image: nginx
        ports:
        - containerPort: 2379
        livenessProbe:
          grpc:
            port: 2379
            service: my-service
          initialDelaySeconds: 10

    example Pod uses gRPC liveness probe

    Conclusion

    Using readiness and liveness propes is recommended to enable kubernetes to start sending traffic to your container only when it’s ready to handle them. It also helps your application to recover automatically when a deadlock occurs but you need to configure your readiness or liveness prope correctly because it might cause your application to never start if the  failureThreshold is too low for example. it also might cause your application to take long time when restarting if the initialDelaySeconds or periodSeconds are too high.