First time at Zeet?

13 Nov
2023
-
28
min read

A Complete Kubernetes Cheat Sheet (Kubectl Command Cheatsheet)

Master Kubernetes cheat sheet. Simplify complex tasks, streamline deployments, and optimize container orchestration effortlessly.

Jack Dwyer

Product
How To
Content
heading2
heading3
heading4
heading5
heading6
heading7

Share this article

Guide to Getting Started

With the ever-growing complexity of managing and deploying containerized applications, having a comprehensive guide is akin to having a secret weapon. Enter the Kubernetes Cheat Sheet: a must-have resource for any developer or operations engineer navigating the labyrinthine world of Kubernetes. From the fundamentals to advanced concepts, this cheat sheet is your trusted companion, providing a concise and practical roadmap through the Kubernetes jungle.

In this blog, we will take you on a journey of discovery, demystifying the enigmatic language of Kubernetes and empowering you to master its intricacies. Whether you're a seasoned Kubernetes veteran looking to brush up on your skills or a curious beginner seeking a solid foundation, this cheat sheet is designed to unlock the hidden potential of this powerful orchestration tool. So fasten your seatbelt, grab your guide, and let's embark on a voyage through the world of Kubernetes basics, exploring its commandments, tips, and tricks along the way. Get ready to emerge as a Kubernetes virtuoso, armed with the knowledge and expertise to navigate this complex landscape with confidence.

What Is Kubernetes?

A coding setup of a programmer - kubernetes cheat sheet

In modern cloud-native applications, the need for efficient container orchestration has become paramount. Enter Kubernetes, the game-changing technology that has revolutionized the management of containerized applications. In this section, we will explore what Kubernetes is and why it is essential for container orchestration in today's cloud-native landscape.

1. Demystifying Kubernetes

Kubernetes, often referred to as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It was originally developed by Google and has since gained immense popularity due to its ability to simplify complex application architectures and streamline the deployment process. Kubernetes provides a robust framework for managing containers, making it easier for developers to handle the complexities of distributed systems.

2. The Power of Container Orchestration

Containerization has transformed the software development landscape by encapsulating applications and their dependencies into portable units known as containers. Managing these containers at scale can pose challenges. This is where Kubernetes shines, offering a comprehensive solution for container orchestration.

With Kubernetes, developers can define the desired state of their applications through declarative configurations, which Kubernetes then enforces. This eliminates the need for manual intervention and ensures that applications are always running in their desired state. Kubernetes also automates the deployment and scaling of containers, ensuring optimal resource utilization and efficient load balancing.

3. Flexibility and Portability

One of the key advantages of using Kubernetes is its flexibility and portability. Kubernetes provides an abstraction layer that allows applications to be deployed and managed consistently across different environments, such as on-premises data centers, public clouds, or hybrid cloud setups. This enables developers to build applications without worrying about the underlying infrastructure, making it easier to embrace a cloud-native approach.

4. High Availability and Scalability

In cloud-native applications, ensuring high availability and scalability is critical. Kubernetes excels in this aspect by providing fault tolerance and load balancing out of the box. It automatically monitors the health of containers and restarts or replaces them if they fail. Kubernetes enables horizontal scaling by allowing developers to easily replicate containers based on predefined rules, ensuring that applications can handle increased traffic and demand.

5. Service Discovery and Load Balancing

To enable communication between containers and facilitate seamless scaling, Kubernetes offers powerful service discovery and load balancing capabilities. It assigns a unique DNS name to each container, making it easy for other services to discover and connect to them. Kubernetes also distributes incoming traffic across multiple containers using load balancing algorithms, ensuring optimal performance and resource utilization.

6. Rolling Updates and Rollbacks

Updating applications without causing service disruptions is a challenging task. Kubernetes simplifies this process by supporting rolling updates, where new versions of containers are gradually deployed while the old versions are phased out. This ensures zero downtime for applications and allows for easy rollbacks in case of any issues or failures.

7. Monitoring and Logging

Effective monitoring and logging are crucial for troubleshooting and maintaining the health of applications. Kubernetes integrates seamlessly with various monitoring and logging tools, allowing developers to gain insights into the performance and behavior of their applications. This enables proactive monitoring and helps identify and resolve issues before they impact the end-users.

Kubernetes has emerged as the de facto standard for container orchestration in the world of cloud-native applications. Its ability to automate the deployment, scaling, and management of containers has revolutionized the way developers build, deploy, and maintain applications. With its flexibility, scalability, high availability, and numerous other features, Kubernetes empowers organizations to embrace the benefits of containerization and unlock the full potential of modern cloud-native architectures.

Related Reading

Kubernetes Deployment Environment Variables
Kubernetes Deployment Template
What Is Deployment In Kubernetes
Kubernetes Backup Deployment
Scale Down Deployment Kubernetes
Kubernetes Deployment History
Kubernetes Deployment Best Practices
Deployment Apps

The Kubernetes YAML Manifest File

Programmer teaching junior about kubernetes cheat sheet

Kubernetes, a powerful container orchestration platform, uses YAML manifests to define and configure its resources. These manifests provide a declarative approach to describing the desired state of your applications and infrastructure. In this section, we will delve into the various components of a Kubernetes YAML manifest file and how they define resource specifications, empowering you to effectively leverage the Kubernetes cheat sheet.

1. API Version and Kind

The API version identifies the version of the Kubernetes API that the manifest is compatible with. It ensures compatibility across different Kubernetes versions. The "kind" field specifies the type of Kubernetes resource being defined, such as Deployment, Service, or Pod.

Example:

yaml
apiVersion: apps/v1
kind: Deployment

2. Metadata

The metadata section contains information about the resource, including its name, labels, and annotations. Labels help categorize and identify resources, while annotations provide additional metadata for custom usage.

Example:


yaml
metadata:
  name: my-deployment
  labels:
    app: my-app
  annotations:
    my-annotation: some-value

3. Spec

The spec section defines the desired state of the resource. It includes various parameters specific to each resource type. For example, in a Deployment, the spec specifies the number of replicas, the container image, and other deployment-related settings.

Example:


yaml
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:latest
          ports:
            - containerPort: 8080

4. Status

The status section provides information about the current state of the resource as reported by the Kubernetes API server. It is automatically populated and should not be defined in the manifest.

Example:


yaml
status:
  replicas: 3
  conditions:
    - type: Available
      status: "True"

5. Resources and Configurations

Depending on the resource type, additional sections may be present in the manifest. For example, a Service resource may include a "spec.ports" section to define the port mapping, while a PersistentVolumeClaim resource may define storage requirements in the "spec.resources" section.

Example (Service):


yaml
spec:
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 8080


Kubernetes YAML manifests serve as a powerful tool for defining the desired state of your Kubernetes resources. By understanding the various components and their significance within a manifest file, you can effectively configure and manage your applications and infrastructure in a Kubernetes environment. 

Helm

Kubernetes is a powerful orchestration and management tool for containerized applications. Working directly with the Kubernetes API using kubectl commands can be complex and time-consuming. Enter Helm, a package manager for Kubernetes that streamlines the deployment and management of applications by providing a higher-level abstraction.

With Helm, you can package your application along with all its dependencies into a single deployable unit called a "chart." A chart is essentially a collection of Kubernetes manifest files that describe the desired state of your application's resources, such as deployments, services, and config maps.

To install and use Helm, you first need to download and install the Helm binary on your machine. Once installed, you can initialize Helm on your cluster by running the following command:


$ helm init


This command sets up the necessary components on your Kubernetes cluster to enable Helm to manage your applications. Once initialized, you are ready to start using Helm to deploy and manage your applications.

To deploy an application using Helm, you simply need to run the following command:


$ helm install ~chart-name~ ~chart-path~



Here, `<chart-name>` is the name you want to give to the deployment, and `<chart-path>` is the path to the chart package or the directory containing the chart files.

Helm will then take care of deploying all the resources described in the chart onto your Kubernetes cluster, ensuring that your application is up and running according to the desired state.

One of the key advantages of Helm is its ability to manage application upgrades and rollbacks. When you need to update your application to a new version, you can package the updated version of your application as a new chart and then use the Helm upgrade command to perform the update:


$ helm upgrade ~release-name~~chart-path~


Helm will intelligently manage the upgrade process by comparing the new chart to the existing deployment and making the necessary changes to bring your application to the desired state.

In case something goes wrong during the upgrade, Helm provides a way to roll back to the previous version of your application using the rollback command:


$ helm rollback ~release-name~ ~revision-number~


Another powerful feature of Helm is its ability to manage dependencies between charts. You can define dependencies in your chart's `requirements.yaml` file, specifying other charts that your application depends on. Helm will automatically download and install these dependencies before deploying your application.

By using Helm in conjunction with kubectl, you can simplify the process of deploying and managing your applications on Kubernetes. Helm provides a higher-level abstraction that allows you to package your applications into charts, manage upgrades and rollbacks, and handle dependencies effortlessly. With Helm, you can focus on what matters most – developing and delivering your applications – while leaving the complexities of Kubernetes management to the tool.

Kubernetes Cluster Management

A wide programming screen - kubernetes cheat sheet

In container orchestration, Kubernetes stands tall as a leading platform for automating deployment, scaling, and management of containerized applications. With its powerful features and robust architecture, Kubernetes enables efficient cluster management, allowing organizations to maximize the potential of their containerized workloads. In this section, we will journey through the intricacies of Kubernetes cluster management, exploring key concepts and providing practical code examples to deepen your understanding.

Understanding Kubernetes Cluster Management

1. Cluster Architecture

At the heart of Kubernetes lies the concept of a cluster, which is a collection of nodes that work together to run containerized applications. A Kubernetes cluster consists of a master node, responsible for managing the cluster, and worker nodes, where the application containers are deployed. The master node controls the scheduling, scaling, and monitoring of containers, while worker nodes execute the containers and handle the runtime environment.

2. Deploying a Kubernetes Cluster

To create a Kubernetes cluster, you can leverage various tools and platforms. One popular approach is to use a managed Kubernetes service provided by cloud providers like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS). Alternatively, you can set up your own cluster using tools like kubeadm, which simplifies the process of creating a cluster from scratch.

Example code for deploying a Kubernetes cluster using kubeadm:


# Step 1: Install kubeadm, kubelet, and kubectl
# Step 2: Initialize the master node


sudo kubeadm init



# Step 3: Set up kubeconfig for the current user


mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config



# Step 4: Join worker nodes to the cluster
# On each worker node, run the following command:


sudo kubeadm join ~master-node-ip~:~master-node-port~ --token ~token~ --discovery-token-ca-cert-hash ~discovery-token-ca-cert-hash~

3. Cluster Configuration

Once your cluster is up and running, you can fine-tune its configuration to meet your specific requirements. Kubernetes provides a wide range of configuration options, including resource allocation, networking, security, and storage. The configuration can be managed through Kubernetes API, command-line tools like kubectl, or by editing YAML files known as manifests.

Example code for scaling a deployment:


yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3  # Scale to 3 replicas
  template:
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        ports:
        - containerPort: 8080


4. Cluster Monitoring and Logging

To ensure the health and performance of your Kubernetes cluster, monitoring and logging play a crucial role. Kubernetes integrates with various monitoring solutions like Prometheus and Grafana, allowing you to collect metrics, visualize cluster resources, and set up alerts. Centralized logging tools like Elasticsearch and Fluentd can be used to aggregate container logs and facilitate troubleshooting.

Example code for deploying Prometheus and Grafana:

yaml
# Deploy Prometheus


kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/prometheus/manifests/prometheus-configmap.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/prometheus/manifests/prometheus-deployment.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/prometheus/manifests/prometheus-service.yaml


# Deploy Grafana


kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/grafana/manifests/grafana-deployment.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/grafana/manifests/grafana-service.yaml

5. Cluster Upgrades

As Kubernetes evolves rapidly, keeping your cluster up to date with the latest versions is crucial for security, bug fixes, and new features. Kubernetes provides a seamless upgrade process, allowing you to upgrade individual components or the entire cluster without disrupting running workloads. It is recommended to test upgrades in a staging environment before applying them to production clusters.

Example code for upgrading a Kubernetes cluster:


bash
# Upgrade kubeadm
sudo apt update
sudo apt-get install -y --allow-change-held-packages kubeadm=1.22.0-00

# Upgrade kubelet and kubectl on all nodes
sudo apt update
sudo apt-get install -y --allow-change-held-packages kubelet=1.22.0-00 kubectl=1.22.0-00

# Upgrade the control plane components
sudo kubeadm upgrade plan
sudo kubeadm upgrade apply v1.22.0

# Upgrade worker nodes
sudo kubeadm upgrade node



Kubernetes cluster management encompasses a wide array of concepts and practices that enable organizations to effectively manage their containerized workloads. From understanding cluster architecture to deploying, configuring, monitoring, and upgrading a cluster, this guide has provided you with a comprehensive understanding of Kubernetes cluster management. Armed with this knowledge, you are well-equipped to embark on your journey towards building and managing scalable and resilient containerized applications with Kubernetes.

Kubernetes Daemonsets

Woman working in a coffee shop on laptop - kubernetes cheat sheet

If you're delving into the world of Kubernetes, you may have come across the term "Daemonsets." But what exactly are Daemonsets, and how do they fit into the Kubernetes ecosystem? In this section, we'll explore the intricacies of Kubernetes Daemonsets and shed light on their significance in managing containerized applications. So, fasten your seatbelt and get ready for an enlightening journey through the realm of Daemonsets.

What are Daemonsets?

At their core, Daemonsets are an essential Kubernetes resource that ensures a specific pod runs on every node within a cluster. Unlike other resources that focus on running a specific number of replicas across the cluster, Daemonsets guarantee the presence of a pod on every node. It's like having a watchful sentinel stationed on each node, diligently performing a specific task or function.

Use Cases for Daemonsets

Daemonsets find their utility in a wide range of scenarios. Here are a few examples:

1. Log Collection

Imagine you have a logging agent that collects logs from each node. Deploying a Daemonset ensures that the logging agent pod runs on every node, seamlessly gathering logs from each container.

2. Monitoring

To effectively monitor the health and performance of your cluster, you might need to deploy monitoring agents on each node. With Daemonsets, you can ensure that these agents are automatically deployed and maintained on every node.

3. Networking

If your application requires specific network plugins or proxies to function properly, Daemonsets can take care of their deployment across all nodes. This ensures consistent network connectivity and smooth communication within the cluster.

4. Security

Daemonsets can be used to enforce security measures like running antivirus software, intrusion detection systems, or other security agents on each node. This helps maintain the integrity and safety of your cluster.

Creating a Daemonset

To grasp the concept better, let's explore how to create a Daemonset. Here's an example YAML manifest that demonstrates the creation of a simple Daemonset:


yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-daemonset
spec:
  selector:
    matchLabels:
      app: my-daemonset
  template:
    metadata:
      labels:
        app: my-daemonset
    spec:
      containers:
      - name: my-pod
        image: my-image:latest



In this example, we define a Daemonset named "my-daemonset" and specify a selector that matches pods labeled with "app: my-daemonset." The template section defines the pod that will be created on each node. In this case, we have a single container named "my-pod" with the image "my-image:latest."

Updating a Daemonset

Updating a Daemonset is similar to updating other Kubernetes resources. You can make changes to the Daemonset's YAML manifest and apply those changes using the `kubectl apply` command. For example, if you need to update the image version of the container in your Daemonset, you can modify the YAML file and run:


shell
kubectl apply -f my-daemonset.yaml


Kubernetes will handle the rolling update process, ensuring that the new version of the container is gradually deployed across the nodes while maintaining the desired state of having a pod on every node.

Scaling and Deleting Daemonsets

Scaling a Daemonset involves adjusting the number of instances running on each node. You can achieve this by modifying the `replicas` field in the Daemonset's YAML manifest and applying the changes.


yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-daemonset
spec:
  selector:
    matchLabels:
      app: my-daemonset
  template:
    metadata:
      labels:
        app: my-daemonset
    spec:
      containers:
      - name: my-pod
        image: my-image:latest
replicas: 5


In this example, we set the `replicas` field to 5, indicating that we want five instances of the pod to run on each node.

To delete a Daemonset, you can use the `kubectl delete` command:


shell
kubectl delete daemonset my-daemonset



This command removes the Daemonset and associated pods from all nodes in the cluster.

Embracing the Power of Kubernetes Daemonsets

As you navigate the complexities of Kubernetes, understanding Daemonsets is crucial for effectively managing containerized applications. From log collection and monitoring to networking and security, Daemonsets offer a powerful mechanism for deploying and maintaining pods on every node within a cluster. By creating, updating, scaling, and deleting Daemonsets, you can harness the full potential of Kubernetes to build robust and scalable infrastructure.

So, venture forth with your newfound knowledge of Daemonsets, and let the sentinel-like power of Kubernetes watch over your clusters!

Kubernetes Deployments

A screen of Visual Studio Code - kubernetes cheat sheet

Deploying applications in a Kubernetes cluster can be a complex task, but with the help of Deployments, it becomes much more manageable. In this section, we will dive into the world of Kubernetes Deployments and explore their various features and functionalities. So let's get started!

1. What are Kubernetes Deployments?

Kubernetes Deployments are a higher-level resource that manages the deployment and scaling of a set of replica Pods. It provides a declarative way to define and update applications, ensuring that the desired state is maintained throughout the deployment process.

2. The Anatomy of a Deployment

A Deployment consists of several key components:

Pod Template

Defines the desired state of the Pods to be deployed.

ReplicaSet

Ensures that the desired number of Pods are running at all times.

Strategy

Determines the update strategy for the Deployment.

Labels

Helps to identify and group related resources.

Selector

Matches Pods with the specified labels.

3. Creating a Deployment

To create a Deployment, you can use a YAML or JSON manifest file. Here's an example of a simple Deployment definition:


yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:latest



In this example, the `replicas` field specifies the desired number of Pods to be created, and the `selector` field identifies the Pods with the label `app: my-app`. The `template` section defines the Pod template with the necessary specifications.

4. Rolling Updates

One of the key features of Deployments is the ability to perform rolling updates. This means that you can update your application without downtime by gradually replacing the old Pods with the new ones. The update strategy can be defined in the `strategy` field of the Deployment manifest.


yaml
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0



In this example, `maxSurge` specifies the maximum number of Pods that can be created over the desired number, and `maxUnavailable` specifies the maximum number of Pods that can be unavailable during the update process.

5. Scaling a Deployment

Scaling a Deployment is as simple as changing the number of replicas in the Deployment manifest. You can use the `kubectl scale` command to scale a Deployment:


shell
kubectl scale deployment my-deployment --replicas=5


This command scales the `my-deployment` Deployment to have 5 replicas.

6. Rolling Back a Deployment

In case something goes wrong during an update, you can easily roll back to a previous version of the Deployment. This can be done using the `kubectl rollout undo` command:


shell
kubectl rollout undo deployment my-deployment


This command rolls back the `my-deployment` Deployment to the previous revision.

7. Managing Deployments with Labels and Selectors

Labels and selectors play a crucial role in managing Deployments. By labeling your resources, you can easily group and manage them. For example, you can list all Deployments with a specific label using the `kubectl get deployment -l <label>` command.

8. Scaling and Load Balancing

Kubernetes Deployments work in conjunction with Services to provide scaling and load balancing capabilities. Services allow external access to the Deployments and distribute the traffic among the Pods. By default, Kubernetes creates a Service for each Deployment, but you can also create your own custom Services.

Kubernetes Deployments are a powerful tool for managing and scaling applications in a Kubernetes cluster. With their declarative approach and rolling update capabilities, they simplify the deployment process and ensure that your application remains available and up to date. By understanding the various features and functionalities of Deployments, you can effectively leverage them to deploy and manage your applications with ease.

Kubernetes Events

A simplistic 2 screen coding setup - kubernetes cheat sheet

Kubernetes is a powerful container orchestration platform that enables the management and scaling of containerized applications. It keeps track of various events that occur within the cluster, providing valuable insights into the state and health of your applications. In this section, we will explore the concept of Kubernetes events in detail, understanding their significance and how to leverage them effectively.

Understanding Kubernetes Events

Kubernetes events are records of changes or occurrences within a cluster. These events capture important information about the state and behavior of various Kubernetes resources, such as pods, services, and nodes. They provide real-time feedback and visibility into what is happening within your cluster, enabling you to troubleshoot issues, monitor the health of your applications, and gain valuable insights for optimizing performance.

Types of Kubernetes Events

Kubernetes events can be categorized into several types based on their source and relevance. Let's explore some of the most common event types:

1. Normal Events

Normal events are informational messages that indicate the successful completion of an operation or a routine activity within the cluster. These events are primarily used for tracking the lifecycle of Kubernetes resources and confirming that desired actions have been carried out as expected. They provide reassurance that everything is functioning as intended.

Example:


Normal   PodScheduled    Successfully assigned my-namespace/my-pod to node-xyz

2. Warning Events

Warning events highlight potential issues or irregularities within the cluster. These events signify that something unexpected or undesirable has occurred, which may require attention. It could be a failed operation, resource constraint, or any other situation that may impact the normal functioning of your applications.

Example:


Warning   FailedMount     MountVolume.SetUp failed for volume "data" : hostPath type check failed: /data is not a directory

3. Error Events

Error events indicate critical failures or errors within the cluster that require immediate attention. These events can be triggered by various factors, such as incorrect configuration, connectivity issues, or resource failures. Monitoring error events is essential for identifying and resolving issues before they impact the availability and performance of your applications.

Example:


Error     FailedScheduling   0/1 nodes are available: 1 node(s) had taints that the pod didn't tolerate.

Significance of Kubernetes Events

Kubernetes events serve as a vital source of information for cluster administrators, developers, and operators. They offer real-time visibility into the state and behavior of resources, aiding in troubleshooting, monitoring, and proactive management. Here are some key reasons why Kubernetes events are significant:

1. Troubleshooting and Debugging

Events provide essential clues for identifying and resolving issues within the cluster. By monitoring events, you can detect errors, failures, and performance bottlenecks, enabling effective troubleshooting and debugging. The information captured in events helps pinpoint the root cause of problems, facilitating swift resolution.

2. Resource Monitoring

Events enable proactive monitoring of the cluster's health and resource utilization. By analyzing events related to resource allocation, pod scheduling, or performance metrics, you can identify potential capacity or performance issues and take corrective actions in a timely manner. This helps ensure optimal resource utilization and reliable application performance.

3. Audit and Compliance

Kubernetes events serve as an audit trail for tracking changes and activities within the cluster. They provide visibility into who performed which operations and when, facilitating compliance with regulatory requirements. By monitoring events, you can maintain a record of all actions taken, ensuring transparency and accountability.

Leveraging Kubernetes Events

To effectively leverage Kubernetes events, consider the following best practices:

1. Centralized Event Collection

Implement a centralized event collection mechanism to aggregate events from all cluster components. This allows for centralized monitoring and analysis, simplifying troubleshooting and providing a holistic view of the cluster's health.

2. Event Filtering and Alerting

Configure event filtering and alerting mechanisms to focus on events that are most relevant and critical. Define thresholds and rules to trigger alerts for warning or error events, enabling proactive identification and resolution of issues.

3. Event Correlation

Correlate events with other monitoring metrics, logs, and performance data to gain a comprehensive understanding of your cluster's behavior. This integrated approach provides deeper insights into the underlying causes of events and facilitates effective troubleshooting.

Kubernetes events are a valuable source of real-time information about the state and behavior of your cluster. By understanding the different types of events and their significance, you can effectively monitor, troubleshoot, and optimize your Kubernetes deployments. Leveraging events as part of your overall monitoring strategy empowers you to ensure the reliability and performance of your applications running in Kubernetes. So embrace the power of Kubernetes events and unlock the full potential of your containerized applications!

Kubernetes Logs

Auth error on a website - kubernetes cheat sheet

Logs play a crucial role in troubleshooting and monitoring applications running on Kubernetes clusters. They provide valuable insights into the behavior and performance of the system, helping us identify and resolve issues swiftly. In this section, we will explore the different aspects of Kubernetes logs and understand how to effectively utilize them for effective management and debugging. So, grab your cup of coffee, sit back, and let's dive into the world of Kubernetes logs!

1. Understanding Kubernetes Logs

Logs in Kubernetes capture the records of events and activities happening within the cluster. These logs are generated by various components, including pods, nodes, containers, and controllers. Kubernetes follows a centralized logging approach, where logs are collected and stored in a centralized location to facilitate easy analysis and troubleshooting.

2. Logging Levels

When dealing with Kubernetes logs, it's essential to understand the concept of logging levels. Logging levels categorize logs based on their severity and importance. The common logging levels in Kubernetes are:

INFO

Informational logs that provide general status and progress updates.

WARNING

Logs indicating potential issues or anomalies that require attention.

ERROR

Logs that highlight critical errors or failures.

DEBUG

Detailed logs useful for tracking down issues during development or debugging.

By understanding the logging levels, you can filter and focus on the relevant logs for efficient troubleshooting.

3. Logging Best Practices

Effective logging practices can streamline the debugging process and improve the overall management of Kubernetes clusters. Here are some best practices to consider:

Log Formatting

Use a consistent log format to ensure easy parsing and analysis. Consider using a structured logging format like JSON or key-value pairs.

Log Metadata

Include relevant metadata such as timestamps, request IDs, and source identifiers to facilitate log correlation and analysis.

Log Levels

Use appropriate logging levels to differentiate between informational, warning, and error logs. This helps prioritize troubleshooting efforts.

Log Rotation

Implement log rotation mechanisms to prevent excessive log file growth and storage exhaustion. Regularly archive or delete old logs to maintain a manageable log size.

Centralized Logging

Utilize a centralized logging solution like Elasticsearch, Fluentd, and Kibana (EFK) or the ELK stack to aggregate and analyze logs from multiple Kubernetes components effectively.

4. Accessing Kubernetes Logs

Kubernetes provides various methods to access logs from different components.



- kubectl Logs: Use the `kubectl logs` command to retrieve logs from a specific pod or container. For example:
 


kubectl logs ~pod-name~



- kubectl Logs with Labels: To retrieve logs from pods that match specific labels, use the `--selector` flag. For example:


kubectl logs --selector=~label-key~=~label-value~

 

- kubectl Logs with Containers: When a pod has multiple containers, specify the container name to fetch logs from a specific container. For example:


  kubectl logs ~pod-name~ -c ~container-name~

 

- Log Aggregation Tools: Employ log aggregation tools like EFK, the ELK stack, or tools specific to your chosen Kubernetes distribution. These tools provide a centralized interface to access and analyze logs from multiple nodes, pods, and containers.

5. Monitoring and Alerting

Monitoring Kubernetes logs is crucial for detecting and resolving issues promptly. Consider integrating monitoring and alerting systems like Prometheus or Grafana to monitor log patterns and raise alerts for anomalies or critical events. These systems can also help you visualize log data and derive meaningful insights.

Kubernetes logs are an invaluable resource for understanding and managing the behavior of your cluster. By applying logging best practices, leveraging the available tools, and implementing a robust log analysis strategy, you can effectively troubleshoot issues, monitor performance, and ensure the smooth operation of your Kubernetes environment. So, log on and embrace the power of Kubernetes logs to conquer any challenges that come your way!

Kubernetes Manifest Files

Woman working on a Chinese app - kubernetes cheat sheet

When it comes to managing and orchestrating containers, Kubernetes is the name that rings loud and clear. As the leading container orchestration platform, Kubernetes provides a robust and scalable solution for deploying and managing containerized applications. One of the key aspects of working with Kubernetes is understanding and leveraging the power of manifest files. 

In this section, we will dive deep into Kubernetes manifest files, exploring their structure, components, and best practices. By the end, you will have a complete understanding of how to harness the full potential of Kubernetes manifest files to achieve seamless container orchestration.

Understanding Kubernetes Manifest Files: A Closer Look

1. What is a Kubernetes Manifest File?

At its core, a Kubernetes manifest file is a declarative configuration file that describes the desired state of a Kubernetes object. These objects can include pods, deployments, services, and many other components of a Kubernetes cluster. By defining the desired state, manifest files serve as a blueprint for Kubernetes to create, update, or delete these objects and ensure the desired configuration is maintained.

2. Anatomy of a Kubernetes Manifest File

A typical Kubernetes manifest file consists of several key sections:

API Version

This section specifies the version of the Kubernetes API that the manifest file is compatible with. For example, "apiVersion: v1".

Kind

The "kind" field denotes the type of Kubernetes object being defined. It can be a pod, deployment, service, or any other supported object. For instance, "kind: Pod".

Metadata

This section provides metadata about the object, such as the name, labels, and annotations. Labels are particularly useful for grouping and selecting objects.

Spec

The "spec" section defines the desired configuration of the object. It includes parameters like the container image, resource requirements, networking settings, and more.

Status

The "status" section is automatically updated by Kubernetes to reflect the current state of the object. It is not included in the manifest file itself but can be viewed through Kubernetes API.

3. Creating a Basic Pod Manifest File

To illustrate the structure of a Kubernetes manifest file, let's examine a basic example of a pod manifest file:


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



In this example, we define a pod named "my-pod" with a single container called "my-container" that uses the "nginx:latest" image. This simple manifest file is enough to create a running pod in a Kubernetes cluster.

4. Best Practices for Writing Manifest Files

When working with Kubernetes manifest files, it is essential to follow certain best practices to ensure maintainability and scalability:

Use version-controlled manifest files

Store your manifest files in version control to track changes and facilitate collaboration.

Leverage labels and annotations

Labels help in organizing and selecting objects, while annotations provide additional metadata. Utilize them effectively to enhance manageability.

Split large manifest files

Rather than having a monolithic manifest file, consider breaking it down into smaller, reusable components for easier maintenance.

Use templates

Kubernetes provides tools like Helm and Kustomize to create reusable templates for manifest files, enabling dynamic configuration and easier deployment.

5. Deploying Manifest Files

To deploy a Kubernetes manifest file, you can use the `kubectl` command-line tool. For example, to create a pod from a manifest file, run the following command:


bash
kubectl apply -f pod.yaml



This command instructs Kubernetes to create or update the pod defined in the "pod.yaml" file. Similarly, you can use `kubectl delete` to remove objects defined in a manifest file.

Kubernetes manifest files are the backbone of managing containerized applications in a Kubernetes cluster. By understanding their structure, components, and best practices, you can unlock the full potential of Kubernetes and achieve seamless container orchestration. With this comprehensive guide, you are well-equipped to dive into the world of Kubernetes manifest files and harness their power to streamline your container deployments.

Related Reading

Kubernetes Deployment Logs
Kubernetes Delete Deployment
Kubernetes Restart Deployment
Kubernetes Blue Green Deployment
Kubernetes Canary Deployment
Kubernetes Update Deployment
Kubernetes Continuous Deployment
Kubernetes Deployment Vs Pod
Kubernetes Daemonset Vs Deployment
Kubernetes Deployment Types
Kubernetes Deployment Strategy Types
Kubernetes Deployment Update Strategy
Kubernetes Update Deployment With New Image
Kubernetes Restart All Pods In Deployment
Kubernetes Deployment Tools

Kubernetes Namespaces

E-commerce programming on a screen - kubernetes cheat sheet

In Kubernetes, namespaces play a vital role in providing a structured and organized environment for managing and isolating resources. Kubernetes namespaces allow you to create virtual clusters within the physical Kubernetes cluster, enabling teams to have their own isolated spaces to work in. This is particularly useful in large-scale deployments where multiple teams or applications coexist within the same cluster.

The purpose of namespaces is to provide a logical separation of resources, ensuring that different teams or users can work independently without interfering with each other. Each namespace acts as a self-contained environment, possessing its own set of resources, such as pods, services, deployments, and more. By creating namespaces, you can prevent resource name collisions and streamline resource management.

To illustrate the concept of namespaces further, let's take a look at a practical example using the kubectl command-line tool.

First, let's create a new namespace called "team-a" using the following command:


kubectl create namespace team-a



This command creates a new namespace named "team-a" within the Kubernetes cluster. Now, any resources created within this namespace will be completely isolated from other namespaces.

To verify the creation of the namespace, you can use the following command:


kubectl get namespaces



This command will display a list of all the namespaces in the cluster, including the newly created "team-a" namespace.

Now, let's create a simple deployment within the "team-a" namespace:


yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: team-a
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app-image:latest



This YAML configuration file describes a basic deployment named "my-app" within the "team-a" namespace. By specifying the namespace, we ensure that this deployment is created exclusively within the "team-a" namespace and does not interfere with resources in other namespaces.

Using namespaces, you can also control access to resources. Kubernetes supports Role-Based Access Control (RBAC), which allows you to define granular permissions for different users or groups within a namespace. By configuring RBAC rules, you can restrict access to certain resources, ensuring that teams or users only have the necessary privileges within their designated namespaces.

Kubernetes namespaces are a powerful feature that allows you to create isolated environments within a Kubernetes cluster. By using namespaces, you can avoid resource conflicts, manage resources efficiently, and control access to specific namespaces. With the help of kubectl, you can easily create namespaces, deploy resources within them, and ensure strict access control within your Kubernetes environment. So, embrace the power of namespaces, and make your Kubernetes journey a breeze!

Kubernetes Nodes

Team meeting on kubernetes cheat sheet

As you embark on your journey to master Kubernetes, understanding the intricacies of its architecture is crucial. One fundamental aspect is Kubernetes Nodes, the building blocks of your cluster. In this section, we will delve into the essence of Nodes, exploring their significance, components, and how to interact with them effectively.

Defining the Essence: What are Kubernetes Nodes?

In Kubernetes, Nodes serve as the workhorses of the cluster. They are the individual machines that form the foundation for running your applications. Each Node houses multiple containers, providing the necessary computing resources for executing your workloads.

The Components of a Node: Beyond the Surface

To grasp the true essence of a Node, let's examine its key components, each playing a vital role in the cluster's ecosystem:

1. Node Name

Every Node possesses a unique identifier or name, facilitating its management within the Kubernetes environment. Whether you need to deploy, monitor, or troubleshoot, this name acts as a beacon, guiding you towards the specific Node in question.

2. Node Status

Understanding the Node's status is crucial for managing the overall health of your cluster. Kubernetes categorizes Nodes into three states: "Ready," "NotReady," and "Unknown." The "Ready" state signifies that the Node is prepared to accept workloads, while "NotReady" indicates a temporary unavailability. The "Unknown" state implies a communication issue between the master and the Node.

3. Labels and Selectors

Labels are key-value pairs that allow you to categorize and organize Nodes based on shared attributes. By leveraging labels and selectors, you can easily target specific subsets of Nodes for deployment, monitoring, or any other desired operation.

4. Taints and Tolerations

Taints are used to repel workloads from specific Nodes, ensuring that they are reserved for specific tasks or have certain requirements. Conversely, tolerations enable Pods to bypass these taints and run on the tainted Node. This mechanism guarantees optimal resource allocation and segregation.

5. Capacity and Allocatable Resources

Nodes possess a finite amount of resources, including CPU, memory, and storage. Kubernetes tracks these resources, allowing you to monitor and allocate them efficiently. Capacity represents the total available resources, while allocatable resources are the subset that Kubernetes can utilize.

6. Kubelet

The Kubelet, a critical component of every Node, acts as the intermediary between the control plane and the Node itself. It ensures that the desired state of Pods specified in the cluster's configuration is maintained on the Node. The Kubelet constantly communicates with the control plane, reporting the Node's status and performing essential tasks like Pod creation, monitoring, and deletion.

7. Container Runtime

Container runtimes, such as Docker or containerd, enable the execution of individual containers within a Node. They provide an isolated environment for running your applications while efficiently utilizing the Node's resources.

Interacting with Nodes: Harnessing the Power of Kubernetes

Now that we have gained a comprehensive understanding of Nodes, let's explore how we can interact with them effectively:

1. Node Management

To manage Nodes efficiently, Kubernetes offers various commands and APIs. Use the `kubectl get nodes` command to view the current Node status, including their names and readiness. Additional options like `kubectl drain` and `kubectl uncordon` allow you to gracefully evict workloads from a Node and bring it back into operation, respectively.

2. Node Affinity and Anti-Affinity

By utilizing Node Affinity and Anti-Affinity rules, you can shape the scheduling behavior of Pods. These rules guide Kubernetes in selecting Nodes based on specified attributes, promoting workload distribution and optimizing resource allocation.

3. Node Monitoring and Troubleshooting

Monitoring the health and performance of Nodes is critical to maintaining a stable cluster. Kubernetes provides a variety of monitoring tools and integrations, such as Prometheus and Grafana, enabling real-time insights into Node metrics and alerts. Logs from the Kubelet and container runtimes can aid in troubleshooting issues related to Nodes.

4. Node Capacity Planning

As your cluster scales, it is essential to perform capacity planning for Nodes. Monitoring resource utilization and considering factors like workload growth and redundancy will help ensure that your cluster can handle the increasing demands effectively. Tools like the Kubernetes Horizontal Pod Autoscaler (HPA) assist in dynamically adjusting the Node capacity based on workload requirements.

Embrace the Power of Kubernetes Nodes

With a firm grasp of Kubernetes Nodes, you are now equipped to harness their power within your cluster. Dive deeper into each component, mastering the nuances and how they interact with other elements of the Kubernetes ecosystem. By embracing the essence of Nodes, you are one step closer to orchestrating your applications with efficiency and scalability.

Kubernetes Pods

Woman programmers collabrating with the help of kubernetes cheat sheet

Understanding the Deployment Commands

In the vast realm of Kubernetes, the deployment of pods is a fundamental task. Pods are the smallest deployable units in Kubernetes, encapsulating one or more containers and sharing their network and storage. To effectively deploy and manage these pods, familiarize yourself with the following kubectl commands.

1. Creating a Pod

To create a new pod, the 'kubectl create' command comes to the rescue. This command allows you to specify the container image, labels, and other configurations required for the pod's creation. Let's take a look at an example:


kubectl create pod my-pod --image=my-container-image



This command creates a new pod named 'my-pod' using the specified container image.

2. Listing Pods

Once you have several pods running, it becomes crucial to be able to list and track them. The 'kubectl get pods' command does just that. This command provides you with an overview of all the pods running within your cluster:


kubectl get pods


This command displays essential information such as the pod name, status, and age. With this information, you can quickly identify and monitor the state of your pods.

Scaling Pods Horizontally

In a dynamic environment, the workload can rapidly increase or decrease. To handle these fluctuations effectively, Kubernetes offers horizontal pod scaling. This technique allows you to adjust the number of pod replicas based on the current workload.

1. Scaling Up

To increase the number of pod replicas, you can use the 'kubectl scale' command. This command enables you to specify the desired number of replicas for a particular deployment. Let's take a look at an example:


kubectl scale deployment/my-deployment --replicas=5



This command scales up the number of replicas for the deployment named 'my-deployment' to five. Now, your workload can be handled more effectively, ensuring optimal performance.

2. Scaling Down

When the workload decreases, it's essential to scale down the number of replicas to avoid unnecessary resource utilization. The 'kubectl scale' command can also be used to achieve this. Here's an example:


kubectl scale deployment/my-deployment --replicas=2



This command scales down the number of replicas for the deployment named 'my-deployment' to two. By dynamically adjusting the number of replicas, you can efficiently allocate resources and optimize your cluster's performance.

Scaling Pods Vertically

Apart from horizontal scaling, Kubernetes also provides the ability to scale pods vertically. Vertical scaling involves adjusting the resources allocated to each individual pod to handle workload changes effectively.

1. Updating Resource Limits

To scale pods vertically, you need to modify the resource limits assigned to each pod. The 'kubectl edit' command allows you to edit the pod's configuration, including resource limits. Here's an example:


kubectl edit pod/my-pod



This command opens the pod configuration in an editor, allowing you to make changes. You can adjust the CPU and memory limits as per your requirements. Once you save the changes, Kubernetes applies them to the pod.

2. Dynamically Scaling

In addition to manually editing pod configurations, Kubernetes also supports dynamic vertical scaling through the Horizontal Pod Autoscaler (HPA) feature. The HPA automatically adjusts the resource limits for pods based on their current usage.

To enable the HPA, you first need to create a HorizontalPodAutoscaler object using the 'kubectl autoscale' command. Here's an example:


kubectl autoscale deployment/my-deployment --min=2 --max=10 --cpu-percent=80



This command creates an HPA for the deployment named 'my-deployment'. It sets the minimum and maximum number of replicas to be maintained between 2 and 10, respectively. It configures the CPU utilization target to be 80%. With the HPA in place, Kubernetes will automatically adjust the pod's resource limits to meet the desired CPU utilization target.

Embrace the Power of Kubernetes

Deploying and managing pods efficiently is crucial in Kubernetes. With the help of kubectl commands, you can effortlessly create, list, and scale pods both horizontally and vertically. By mastering these commands, you unlock the potential to handle changing workloads effectively, ensuring robust performance for your applications. So, dive into the world of Kubernetes and unleash the power of pod management!

Kubernetes Service Accounts

Man working on building an application - kubernetes cheat sheet

In Kubernetes, where containerized applications thrive, security is of paramount importance. As the unyielding threads of cyberspace continually evolve, it becomes crucial to harness the power of Kubernetes Service Accounts to fortify access management. In this section, we will delve into the intricacies of Kubernetes Service Accounts, empowering you with the knowledge to navigate this security landscape.

Understanding Kubernetes Service Accounts

At the core of Kubernetes lie Service Accounts, which enable authentication and authorization between pods and the Kubernetes API server. They provide an identity and a set of credentials for pods to interact securely with the Kubernetes API and other resources within the cluster.

Creating Service Accounts

To create a Service Account, you can use the Kubernetes command-line interface (kubectl) or a YAML file. Let's explore both options:

1. Using kubectl


kubectl create serviceaccount ~service-account-name~

2. Creating a YAML file

   Create a YAML file (e.g., service-account.yaml) with the following content:


  yaml
  apiVersion: v1
  kind: ServiceAccount
  metadata:
    name:



  Apply the YAML file using kubectl:


   kubectl apply -f service-account.yaml

Associating Service Accounts with Pods

Once you have created a Service Account, you can associate it with pods using the `spec.serviceAccountName` field in the pod specification. This allows the pod to leverage the Service Account's credentials and permissions.

Here's an example of a pod specification YAML file associating a Service Account named "my-service-account":


yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  serviceAccountName: my-service-account
  containers:
  - name: my-container
    image: my-image

Accessing Service Account Tokens

Kubernetes automatically creates a Service Account token for each Service Account. This token can be used to authenticate and authorize requests made by pods.

To access the Service Account token within a pod, you can leverage environment variables or mounted secrets.

1. Environment Variables

Kubernetes provides the Service Account token through the `TOKEN` environment variable.
 


yaml
  env:
  - name: TOKEN
    valueFrom:
      secretKeyRef:
        name: default-token
        key: token

2. Mounted Secrets

Kubernetes creates a secret with the Service Account token. You can mount this secret as a file within the pod's filesystem, enabling easy access.
 


  yaml
  volumeMounts:
  - name: token
    mountPath: /var/run/secrets/kubernetes.io/serviceaccount
    readOnly: true

  volumes:
  - name: token
    secret:
      secretName: default-token

Managing Service Account Permissions

By default, Kubernetes assigns a Service Account to the `default` namespace. You can create and manage Service Accounts in any namespace. You can grant different permissions to Service Accounts using Role-Based Access Control (RBAC).

To grant permissions to a Service Account, you need to create a Role or ClusterRole and bind it to the Service Account using a RoleBinding or ClusterRoleBinding.

Here's an example of creating a Role and a RoleBinding to grant a Service Account named "my-service-account" read-only access to a specific namespace:


yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: my-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]

---

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: my-role-binding
subjects:
- kind: ServiceAccount
  name: my-service-account
  namespace: my-namespace
roleRef:
  kind: Role
  name: my-role
  apiGroup: rbac.authorization.k8s.io



In Kubernetes, Service Accounts serve as powerful guardians of access management. Armed with the knowledge acquired in this section, you can confidently navigate the intricate realms of Kubernetes Service Accounts, fortifying the security of your containerized applications. Harness the potential they bring forth and safeguard your Kubernetes clusters with finesse and resilience.

Kubernetes Replication Controllers

Woman working on laptop in a library - kubernetes cheat sheet

Kubernetes Replication Controllers are an essential component of managing and scaling applications in a Kubernetes cluster. They ensure that a specified number of pod replicas are running at all times, providing fault tolerance and high availability. In this section, we will explore the key concepts and features of Replication Controllers, along with practical examples to solidify your understanding.

What is a Replication Controller?

A Replication Controller is a fundamental building block in Kubernetes that ensures the desired number of pod replicas are continuously running. It monitors the state of pods and automatically adjusts their numbers to match the desired state defined by the user. If a pod fails or is terminated, the Replication Controller replaces it with a new one to maintain the desired replica count.

Creating a Replication Controller

To create a Replication Controller, you need to define its desired state, including the pod template and the number of replicas. Here's an example YAML definition:


apiVersion: v1
kind: ReplicationController
metadata:
  name: my-rc
spec:
  replicas: 3
  selector:
    app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image
          ports:
            - containerPort: 8080



In this example, we define a Replication Controller named "my-rc" with three replicas. It uses a pod template with the label selector "app: my-app" to identify the pods managed by the Replication Controller. The template includes a single container named "my-container" using the "my-image" image and exposing port 8080.

Scaling a Replication Controller

One of the main benefits of using Replication Controllers is the ability to scale the number of pod replicas dynamically. Scaling can be achieved through the `kubectl scale` command or by updating the Replication Controller's YAML definition.

To scale the "my-rc" Replication Controller to five replicas using `kubectl`:


kubectl scale rc/my-rc --replicas=5



Alternatively, you can update the Replication Controller's YAML definition and apply the changes:


apiVersion: v1kind: ReplicationControllermetadata:  name: my-rcspec:  replicas: 5



Either method will trigger the Replication Controller to create or terminate pods accordingly, ensuring the desired replica count is maintained.

Updating a Replication Controller

Replication Controllers allow you to update the pod template of your application without downtime. You can modify the image, command, environment variables, or any other attributes of the pod template. When updating a Replication Controller, Kubernetes creates new pods with the updated configuration and terminates the old pods gradually, ensuring a smooth transition.

To update the image of the "my-container" in the "my-rc" Replication Controller:


kubectl set image rc/my-rc my-container=new-image:latest



This command updates the image of the "my-container" to "new-image:latest". Kubernetes will create new pods with the updated image and terminate the old pods, ensuring the desired replica count is maintained throughout the process.

Managing Failures

One of the key features of Replication Controllers is their ability to handle pod failures and maintain the desired replica count. When a pod fails or becomes unavailable, the Replication Controller automatically replaces it with a new pod.

Kubernetes monitors the health of pods using readiness probes and liveness probes. By defining appropriate probes in the pod template, you can ensure that unhealthy pods are terminated and replaced automatically. This ensures the overall availability and reliability of your application.

Deleting a Replication Controller

If you no longer need a Replication Controller, you can delete it using the `kubectl delete` command:


kubectl delete rc/my-rc



This command deletes the "my-rc" Replication Controller and all its associated pods. Make sure to take this action only when you are certain that the Replication Controller is no longer needed.

We have explored the key concepts and features of Kubernetes Replication Controllers. We have learned how to create, scale, update, and manage Replication Controllers, enabling fault tolerance and high availability for our applications. With this newfound knowledge, you are equipped to harness the power of Replication Controllers in your Kubernetes deployments.

Kubernetes ConfigMaps & Secrets

Man working on programming with kubernetes cheat sheet

ConfigMaps in Kubernetes are a powerful tool for managing configuration data that is separate from application code. They provide a way to decouple configuration details from the containerized application, allowing for more flexibility and easy updates. With ConfigMaps, you can store configuration data such as environment variables, command-line arguments, and even entire configuration files. Let's explore how ConfigMaps work and how kubectl helps with their management.

To create a ConfigMap using kubectl, you can use the `kubectl create configmap` command followed by the name of the ConfigMap and either a file or literal values. Here's an example of creating a ConfigMap from a file:


shell
kubectl create configmap my-config --from-file=config.txt



This command creates a ConfigMap named "my-config" using the contents of the "config.txt" file. You can also create a ConfigMap from literal values using the `--from-literal` flag:


shell
kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2



Once you have created a ConfigMap, you can use it in a Pod's specification by referencing it in the `env` section or as a volume mount. Here's an example of using a ConfigMap as environment variables:


yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
      envFrom:
        - configMapRef:
            name: my-config



In this example, the ConfigMap named "my-config" is referenced using the `envFrom` field, which automatically sets environment variables based on the keys and values in the ConfigMap.

Safeguard Sensitive Data with Secrets in Kubernetes

In addition to handling configuration data, Kubernetes also provides a way to manage sensitive information such as passwords, API keys, and certificates using Secrets. Secrets are similar to ConfigMaps but are specifically designed to store confidential data. They are stored as base64-encoded data within the cluster, ensuring that the sensitive information is not exposed in plain text.

To create a Secret using kubectl, you can use the `kubectl create secret` command followed by the type of Secret and the data. There are various types of Secrets available, such as generic, TLS, and Docker registry. Here's an example of creating a generic Secret:


shell
kubectl create secret generic my-secret --from-literal=password=secretpassword --from-file=cert.pem



This command creates a generic Secret named "my-secret" with a literal value for the "password" key and a file named "cert.pem" as the value for another key. The Secret data is automatically base64-encoded by kubectl.

Similar to ConfigMaps, Secrets can be used in a Pod's specification by referencing them in the `env` section or as a volume mount. Here's an example of using a Secret as environment variables:


yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
      env:
        - name: PASSWORD
          valueFrom:
            secretKeyRef:
              name: my-secret
              key: password



In this example, the Secret named "my-secret" is referenced using the `secretKeyRef` field. The value of the "password" key in the Secret is set as an environment variable named "PASSWORD" in the Pod.

Simplified Management with kubectl

Kubectl, the Kubernetes command-line tool, provides a convenient way to manage ConfigMaps and Secrets. It offers a wide range of commands and options to create, update, and delete these resources. Some of the key commands are:


  • `kubectl create configmap`: Creates a ConfigMap from a file or literal values.
  • `kubectl create secret`: Creates a Secret from various data sources.
  • `kubectl get configmaps`: Retrieves a list of ConfigMaps in the cluster.
  • `kubectl get secrets`: Retrieves a list of Secrets in the cluster.
  • `kubectl describe configmap`: Provides detailed information about a specific ConfigMap.
  • `kubectl describe secret`: Provides detailed information about a specific Secret.
  • `kubectl delete configmap`: Deletes a ConfigMap from the cluster.
  • `kubectl delete secret`: Deletes a Secret from the cluster.



By using these commands, you can easily manage and manipulate ConfigMaps and Secrets within your Kubernetes cluster, streamlining the process of configuration management and safeguarding sensitive data.

ConfigMaps and Secrets in Kubernetes are powerful tools that simplify configuration management and secure sensitive data. With kubectl, you can easily create, update, and delete these resources, making it convenient to manage them within your Kubernetes cluster. Whether you need to store configuration details or protect confidential information, ConfigMaps and Secrets, along with kubectl, provide a reliable and efficient solution.

Kubernetes Services

In Kubernetes, Services play a pivotal role in enabling seamless load balancing and network access within a cluster. With the help of kubectl, the command-line interface for Kubernetes, managing and configuring Services becomes an effortless task. Let's delve into the workings of Services and explore how kubectl facilitates their creation and management.

What is a Kubernetes Service?

A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy by which to access them. It acts as a stable endpoint to access a specific set of Pods, providing load balancing and network access. By abstracting away the underlying infrastructure, Services ensure that applications can communicate with each other reliably, regardless of the Pods' location within a cluster.

Creating and Managing Services with kubectl

The kubectl command-line tool empowers Kubernetes administrators to effortlessly create and manage Services. With a few simple commands, you can create, update, and delete Services as per your requirements.

To create a Service using kubectl, you can utilize the `kubectl expose` command. This command exposes a deployment as a new Service. For example:


bash
kubectl expose deployment --type=LoadBalancer --port=



In this command, you specify the deployment name, the type of Service (LoadBalancer, ClusterIP, etc.), and the port number. kubectl then creates a Service that forwards traffic to the specified deployment, providing load balancing and network access.

Once the Service is created, you can use kubectl to manage it. For instance, you can update the Service to scale the number of Pods it targets using the `kubectl scale` command:


bash
kubectl scale deployment --replicas=



By adjusting the number of replicas, you can fine-tune the load balancing capabilities of the Service, ensuring optimal performance for your applications.

Configuring and Managing Ingress Resources with kubectl

In addition to Services, Kubernetes offers Ingress resources to control external access to Services within a cluster. Ingress allows you to define rules for routing external traffic to Services, acting as a powerful layer for managing access and traffic flow.

kubectl simplifies the configuration and management of Ingress resources through intuitive commands. You can create an Ingress resource by applying a YAML file using the `kubectl apply` command:


bash
kubectl apply -f ~ingress-yaml-file~



This command applies the configuration specified in the YAML file, creating the Ingress resource and defining the rules for external access to the associated Services.

To manage Ingress resources, you can utilize the `kubectl get` and `kubectl describe` commands. These commands provide valuable insights into the current state of Ingress resources, helping you identify any issues or inconsistencies.

For example, to view information about all Ingress resources in the cluster, you can use the following command:


bashkubectl get ingress



This command displays a list of all Ingress resources, including their names, rules, and associated Services. With this information at your fingertips, you can easily monitor and troubleshoot external access to your Services.

Unlocking the Full Potential of Kubernetes with Services and Kubectl

Services and kubectl form a formidable alliance, enabling the creation, configuration, and management of load balancing and network access within Kubernetes clusters. By harnessing the power of Services and utilizing the intuitive commands provided by kubectl, you can ensure seamless communication between Pods, enhance application performance, and effortlessly control external access to your Services. Embrace the power of Kubernetes and wield the might of Services with kubectl as your trusted companion.

Persistent Volumes (PVs) & Persistent Volume Claims (PVCs)

A software house with team of programmers - kubernetes cheat sheet

When it comes to managing stateful applications in Kubernetes, persistent volumes (PVs) and persistent volume claims (PVCs) play a crucial role. PVs provide a way to store data in a cluster, while PVCs are used to request specific resources from PVs. In this section, we will explore the key `kubectl` commands for managing PVs and PVCs effectively.

1. Creating Persistent Volumes

To create a persistent volume, we can use the `kubectl create` command along with a YAML file that describes the PV's properties. Here's an example:


shell
kubectl create -f pv.yaml



The `pv.yaml` file contains the specifications for the PV, including its capacity, access modes, and storage class. After executing the command, Kubernetes will create the persistent volume based on the provided configurations.

2. Viewing Persistent Volumes

To view the list of existing persistent volumes in the cluster, we can use the `kubectl get` command with the `pv` resource type:


shell
kubectl get pv


This command will display a table with information about each PV, including its name, capacity, access modes, and status. We can use this information to monitor the available PVs in the cluster.

3. Creating Persistent Volume Claims

To create a persistent volume claim, we can use the `kubectl create` command with a YAML file similar to the one used for PVs:


shell
kubectl create -f pvc.yaml



The `pvc.yaml` file specifies the desired capacity, access modes, and storage class for the PVC. Once the command is executed, Kubernetes will create a PVC and bind it to an available PV that matches the requested parameters.

4. Viewing Persistent Volume Claims

To view the list of existing persistent volume claims in the cluster, we can use the `kubectl get` command with the `pvc` resource type:


shell
kubectl get pvc



This command will display a table with details about each PVC, including its name, capacity, access modes, and status. By monitoring this information, we can ensure that the PVCs are successfully bound to the desired PVs.

5. Attaching Persistent Volume Claims to Pods

To attach a PVC to a pod, we need to include a `volumes` section in the pod's YAML file and reference the PVC in a `persistentVolumeClaim` field:


yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
      volumeMounts:
        - name: my-volume
          mountPath: /data
  volumes:
    - name: my-volume
      persistentVolumeClaim:
        claimName: my-claim



With this configuration, Kubernetes will automatically mount the PV associated with the PVC to the specified directory inside the container.

6. Deleting Persistent Volumes and Persistent Volume Claims

To delete a PV or PVC, we can use the `kubectl delete` command with the respective resource type and name:


shell
kubectl delete pv my-pv
kubectl delete pvc my-pvc



This command will remove the PV or PVC from the cluster, releasing the associated resources. Be cautious when deleting PVs, as it might result in permanent data loss.

Managing PVs and PVCs for stateful applications in Kubernetes requires a good understanding of the `kubectl` commands related to these resources. By using the provided commands, we can easily create, view, attach, and delete PVs and PVCs, allowing us to effectively manage the storage needs of our stateful applications in Kubernetes.

Kubernetes ReplicaSets

One of the main reasons why Kubernetes has gained so much popularity is its ability to manage application deployments with ease. With the help of Deployments and ReplicaSets, Kubernetes provides a seamless mechanism for rolling updates and rollbacks. In this section, we will explore how the kubectl command-line tool supports these essential features.

Rolling Updates

Rolling updates allow you to update your application while minimizing downtime. Kubernetes achieves this by incrementally updating the ReplicaSets, ensuring that the new version of the application is rolled out gradually across the cluster. This approach allows for a smooth transition without causing any disruptions to the end users.

To perform a rolling update using kubectl, you can use the `kubectl set image` command. Let's say we have a Deployment named "myapp" and we want to update the container image to a new version:


kubectl set image deployment/myapp myapp=myregistry/myapp:latest



This command instructs Kubernetes to update the Deployment named "myapp" by replacing the container image with the new version, `myregistry/myapp:latest`. Kubernetes will automatically manage the rolling update process, ensuring that the new version is gradually deployed across the cluster.

You can also specify additional parameters to control the rolling update process. For example, you can set the maximum number of Pods that can be unavailable during the update using the `--max-unavailable` flag:


kubectl set image deployment/myapp myapp=myregistry/myapp:latest --max-unavailable=1



This command ensures that only one Pod is unavailable at a time during the update, minimizing the impact on the application's availability.

Rollbacks

Sometimes, things don't go as planned, and you may need to roll back to a previous version of your application. Kubernetes makes it straightforward to perform rollbacks using the `kubectl rollout` command.

To roll back a Deployment to a previous revision, you can use the `kubectl rollout undo` command. For example, let's say we want to roll back the "myapp" Deployment to the previous revision:


kubectl rollout undo deployment/myapp



This command instructs Kubernetes to undo the last rollout of the Deployment named "myapp," effectively rolling back to the previous revision. Kubernetes will automatically manage the process, ensuring that the previous version of the application is deployed across the cluster.

If you want to roll back to a specific revision, you can use the `--to-revision` flag:


kubectl rollout undo deployment/myapp --to-revision=2



This command rolls back the "myapp" Deployment to the revision with the specified number.

In addition to rolling back Deployments, you can also roll back ReplicaSets using the same `kubectl rollout undo` command. This flexibility allows you to easily manage and revert changes for different components of your application.

By using the kubectl command-line tool, you can effortlessly perform rolling updates and rollbacks for applications deployed on Kubernetes. The ability to update and roll back versions seamlessly ensures that your application remains highly available and resilient. Kubernetes, with its powerful features and intuitive command-line interface, continues to be the go-to platform for managing containerized applications at scale.

Kubernetes StatefulSet

Mobile infront of laptop for app mirroring - kubernetes cheat sheet

In Kubernetes, the StatefulSet is a powerful and essential tool for managing stateful applications. It provides the ability to deploy and scale stateful workloads, ensuring stability and resilience. To embark on this journey of understanding, let us dive into the intricacies of Kubernetes StatefulSet and explore its various aspects.

1. What is a StatefulSet?

A StatefulSet is a Kubernetes resource that allows the deployment and scaling of stateful applications. It provides guarantees for ordering and uniqueness of pod creation and deletion, ensuring that each pod has a stable network identity and persistent storage.

2. Ordering and Uniqueness

Unlike traditional deployments of stateless applications, a StatefulSet ensures that pods are created and terminated in a specific order. This is crucial for stateful applications that rely on a consistent sequence of events. Each pod in a StatefulSet also receives a unique network identity and hostname, enabling seamless communication and integration.

3. Stable Network Identity

Every pod within a StatefulSet is assigned a stable hostname and DNS address. This enables applications to have a consistent way of accessing each other, regardless of pod restarts or scaling events. With this stability, stateful applications can rely on consistent network communication without any interruption.

4. Persistent Storage

StatefulSet facilitates the use of persistent volumes to ensure data persistence for stateful applications. Each pod within a StatefulSet can be associated with a unique volume, allowing data to persist across pod restarts or rescheduling. This capability is crucial for applications that rely on persistent data storage, such as databases or file systems.

5. Headless Service

To enable seamless communication between pods within a StatefulSet, a headless service can be created. The headless service exposes individual pod IP addresses as separate DNS records, allowing direct communication between pods. This eliminates the need for load balancing or proxying, providing efficient communication within the stateful application.

6. Scaling StatefulSets

Scaling a StatefulSet involves adding or removing replicas of pods. When scaling up, new pods are created in the specified order, ensuring the consistency and integrity of the stateful application. Scaling down follows the reverse order, gracefully terminating pods while maintaining the desired state. This allows for seamless horizontal scaling and efficient resource utilization.

In this example, a StatefulSet named "my-app" is defined with three replicas. Each pod within the StatefulSet is associated with a "data-volume" persistent volume. The pods are created in order, and the "my-app-container" container runs the "my-app-image" image.

StatefulSets provide the foundation for running stateful applications in a resilient and scalable manner. With their ordering guarantees, stable network identity, and persistent storage capabilities, StatefulSets empower developers to unleash the true potential of their stateful workloads in the Kubernetes ecosystem.

Kubernetes Common Options

Kubernetes, the open-source container orchestration platform, has gained immense popularity in recent years due to its ability to manage the deployment, scaling, and management of containerized applications. And while Kubernetes offers a vast array of features and options, it can be overwhelming for newcomers to grasp the full extent of its capabilities.

We will explore Kubernetes Common Options, which are essential for understanding and utilizing the power of Kubernetes effectively. From basic concepts to advanced features, let's dive into the world of Kubernetes!

1. Pods: The Building Blocks of Kubernetes

Pods are the fundamental units of deployment in Kubernetes. They represent a single instance of a running process within the cluster. Pods can contain one or more containers that share the same network namespace, storage, and context. To create a pod, you can define a YAML or JSON file containing the pod's configuration. Here's an example:


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

2. Deployments: Managing Application Lifecycle

Deployments in Kubernetes provide a declarative way to manage the lifecycle of applications. They allow you to define the desired state of your application and Kubernetes takes care of maintaining that state. Deployments enable easy scaling, rolling updates, and rollbacks of your application. Here's an example of a deployment definition:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.21
          ports:
            - containerPort: 80


3. Services: Exposing Pods to the Outside World

Services in Kubernetes provide a way to expose your pods to the outside world. They act as a stable network endpoint for accessing your application. Services can be of different types, such as ClusterIP, NodePort, and LoadBalancer. Here's an example of a service definition:


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

4. Secrets: Managing Sensitive Information

Kubernetes provides a secure way to manage sensitive information, such as API keys, passwords, and certificates, through Secrets. Secrets are stored securely within the cluster and can be mounted as files or environment variables in your pods. Here's an example of a secret definition:


apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

5. ConfigMaps: Managing Configuration Data

ConfigMaps allows you to decouple your application configuration from your containers. They provide a way to store and manage configuration data, such as environment variables, command-line arguments, and configuration files. Here's an example of a ConfigMap definition:


apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  app.ini: |
    [database]
    host = db.example.com
    port = 5432
    user = username
    password = password

6. Namespaces: Isolating Resources

Namespaces provide a way to isolate and divide your Kubernetes resources into logical groups. They help in organizing and managing complex deployments. Namespaces prevent naming conflicts and allow for resource quota management within each namespace. Here's an example of creating a namespace:


apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

7. Labels and Selectors: Grouping Resources

Labels and Selectors are key concepts in Kubernetes that allow you to group and select resources based on their metadata. Labels are key-value pairs attached to resources, while selectors enable you to filter resources based on label expressions. Labels and selectors are used extensively in deployments, services, and other Kubernetes objects.

8. Rolling Updates and Rollbacks: Ensuring Application Availability

Kubernetes provides seamless rolling updates and rollbacks for your applications. Rolling updates allow you to update your application without any downtime by gradually replacing the old pods with new ones. In case of issues, rollbacks can be performed quickly to restore the previous stable state of your application.

9. Horizontal Pod Autoscaling: Scaling Based on Resource Usage

Horizontal Pod Autoscaling (HPA) automatically scales the number of pods based on resource utilization metrics, such as CPU and memory usage. HPA ensures efficient resource utilization and enables your application to handle varying traffic loads.

10. Persistent Volumes and Persistent Volume Claims: Managing Storage

Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) are used to manage and provision storage in Kubernetes. PVs provide a way to abstract the underlying storage infrastructure, while PVCs are used by pods to request storage resources.

With these common options and concepts in Kubernetes, you now have a solid foundation to tackle your container orchestration needs. Kubernetes is a vast ecosystem with many additional features and options. Continuous exploration and learning will enable you to unlock the full potential of Kubernetes and leverage its power to its fullest extent. Happy deploying!

Become a 1% Developer Team With Zeet

A development team giving update to members - kubernetes cheat sheet

Welcome to Zeet, where we empower startups and small to mid-sized businesses to unlock the full potential of their cloud and Kubernetes investments. At Zeet, we understand the unique challenges faced by companies like yours - the need for cost-effective solutions, streamlined operations, and a strong engineering team that can contribute to your success.

Empower Your Team

We have developed a comprehensive cheat sheet that will become an invaluable resource for your engineering team. This cheat sheet will not only provide them with quick and easy access to essential information, but it will also help them become more proficient in Kubernetes and enable them to make meaningful contributions to your business.

User-Friendly Mastery

Our cheat sheet covers a wide range of topics, including installation and setup, core Kubernetes concepts, deployment strategies, networking, scaling, and troubleshooting. It is designed to be user-friendly and easy to navigate, allowing your team to find the information they need in a matter of seconds. Whether they are new to Kubernetes or looking to deepen their knowledge, our cheat sheet will serve as a reliable companion throughout their journey.

Optimizing Efficiency

By leveraging the power of Kubernetes, you can optimize your cloud infrastructure and maximize its efficiency. Kubernetes allows you to automate the deployment, scaling, and management of your applications, saving you time and resources. With our cheat sheet, you can easily navigate the complexities of Kubernetes and ensure that your applications are running smoothly and efficiently.

Supporting Growth

At Zeet, we are committed to helping your engineering team become strong individual contributors. We believe that by providing them with the tools and resources they need, you can unleash their full potential and drive your business forward. Our Kubernetes cheat sheet is just one example of how we support your team's growth and success.

So why wait? Join Zeet today and empower your engineering team with our Kubernetes cheat sheet. Together, we can unlock the true potential of your cloud infrastructure and help your business thrive.

Related Reading

Kubernetes Service Vs Deployment
Kubernetes Rollback Deployment
Deployment As A Service
Kubernetes Deployment Env
Deploy Kubernetes Dashboard

Subscribe to Changelog newsletter

Jack from the Zeet team shares DevOps & SRE learnings, top articles, and new Zeet features in a twice-a-month newsletter.

Thank you!

Your submission has been processed
Oops! Something went wrong while submitting the form.