How to create basic Kubernetes deployments

KubernetesKubernetesBeginner
Practice Now

Introduction

This tutorial will guide you through the essential concepts and practical aspects of Kubernetes, the leading open-source container orchestration platform. You will learn the fundamentals of Kubernetes, its architecture and key components, as well as how to deploy and manage containerized applications on a Kubernetes cluster. By the end of this tutorial, you will have a solid understanding of Kubernetes and be able to create basic deployments to run your applications in a scalable and resilient environment.

Kubernetes Fundamentals

Kubernetes Basics

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides a robust and scalable infrastructure for running and managing distributed systems.

Kubernetes Architecture

Kubernetes follows a master-worker architecture. The Kubernetes master is responsible for managing the overall cluster, while the worker nodes run the containerized applications. The key components of the Kubernetes architecture include:

graph TD A[Kubernetes Master] --> B[API Server] A --> C[Controller Manager] A --> D[Scheduler] A --> E[etcd] B --> F[Worker Nodes] F --> G[Kubelet] F --> H[Container Runtime] F --> I[Kube-proxy]

Kubernetes Components

  1. API Server: The central control point that exposes the Kubernetes API and processes RESTful requests.
  2. Controller Manager: Responsible for maintaining the desired state of the cluster by monitoring the API server.
  3. Scheduler: Assigns pods to suitable nodes based on resource requirements and constraints.
  4. etcd: A distributed key-value store that holds the state of the Kubernetes cluster.
  5. Kubelet: The agent running on each node, responsible for managing the lifecycle of pods.
  6. Container Runtime: The software responsible for running containers, such as Docker or containerd.
  7. Kube-proxy: Manages network connectivity between services and pods within the cluster.

Kubernetes Concepts

  1. Pods: The smallest deployable units in Kubernetes, representing one or more containers.
  2. Deployments: Declarative way to manage the lifecycle of stateless applications.
  3. Services: Provide a stable network endpoint for accessing applications within the cluster.
  4. Volumes: Provide a way to persist data beyond the lifecycle of a pod.
  5. ConfigMaps and Secrets: Decouple application configuration from the container image.

Kubernetes Use Cases

Kubernetes is widely used for:

  • Deploying and scaling containerized applications
  • Implementing continuous integration and continuous deployment (CI/CD) pipelines
  • Running and managing microservices-based architectures
  • Providing a platform for running stateful and stateless applications
  • Enabling hybrid and multi-cloud deployments

Kubernetes Deployment and Management

Kubernetes Deployment

Kubernetes provides several resource types for deploying and managing applications, including:

  1. Pods: The basic unit for running containers in Kubernetes. Pods can contain one or more containers that share the same network and storage resources.

  2. Deployments: Declarative way to manage the lifecycle of stateless applications. Deployments ensure that a specified number of pod replicas are running at all times.

graph LR A[Deployment] --> B[ReplicaSet] B --> C[Pod] B --> D[Pod] B --> E[Pod]
  1. Services: Provide a stable network endpoint for accessing applications within the cluster. Services can load-balance traffic across multiple pods.

Kubernetes Scaling

Kubernetes supports both manual and automatic scaling of applications:

  1. Manual Scaling: Manually update the desired number of replicas in a Deployment or ReplicaSet.

  2. Horizontal Pod Autoscaling (HPA): Automatically scales the number of pod replicas based on CPU utilization or other custom metrics.

graph LR A[Deployment] --> B[ReplicaSet] B --> C[Pod] B --> D[Pod] B --> E[Pod] F[Horizontal Pod Autoscaler] --> B

Kubernetes Networking

Kubernetes provides a robust networking model to enable communication between pods and services:

  1. Pod Networking: Each pod gets its own IP address, allowing pods to communicate with each other directly.

  2. Service Networking: Services provide a stable network endpoint for accessing applications within the cluster, abstracting away the details of individual pods.

  3. Ingress: Ingress resources provide advanced routing and load balancing capabilities for exposing services to the outside world.

Kubernetes Management

Kubernetes provides several tools and APIs for managing the cluster and deployed applications:

  1. Kubectl: The command-line tool for interacting with the Kubernetes API server.
  2. Kubernetes Dashboard: A web-based UI for managing the cluster and deployed applications.
  3. Helm: A package manager for deploying and managing Kubernetes applications.

Kubernetes Practical Hands-on

Kubernetes Installation

To get started with Kubernetes, you'll need to set up a Kubernetes cluster. You can do this by installing a Kubernetes distribution like minikube or kind on your local machine, or by using a managed Kubernetes service like Google Kubernetes Engine (GKE) or Amazon Elastic Kubernetes Service (EKS).

Here's an example of how to install minikube on Ubuntu 22.04:

curl -LO 
sudo install minikube-linux-amd64 /usr/local/bin/minikube
minikube start

Kubernetes Pod Creation

Once you have a Kubernetes cluster set up, you can start deploying applications. The basic unit of deployment in Kubernetes is a Pod, which represents one or more containers that share the same network and storage resources.

Here's an example of creating a simple nginx pod:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

Save this as nginx-pod.yaml and apply it to your cluster:

kubectl apply -f nginx-pod.yaml

Kubernetes Configuration

Kubernetes provides several ways to configure your applications, including:

  1. ConfigMaps: For storing non-sensitive configuration data.
  2. Secrets: For storing sensitive information like passwords or API keys.
  3. Volumes: For providing persistent storage to your applications.

Here's an example of creating a ConfigMap and using it in a Pod:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: production
  APP_LOG_LEVEL: info
---
apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
  - name: app
    image: myapp:v1
    envFrom:
    - configMapRef:
        name: app-config

Kubernetes Troubleshooting

When things go wrong, Kubernetes provides several tools and commands for troubleshooting:

  1. Kubectl: Use kubectl get, kubectl describe, and kubectl logs to inspect the state of your cluster and deployed resources.
  2. Kubernetes Dashboard: The web-based UI can provide a visual overview of your cluster and help you identify issues.
  3. Kubernetes Events: Monitor the events generated by the Kubernetes control plane to identify errors or warnings.

By following these practical hands-on steps, you'll be well on your way to becoming a Kubernetes expert!

Summary

In this tutorial, you have learned the fundamentals of Kubernetes, including its architecture, key components, and essential concepts like pods, deployments, and services. You have gained practical experience in deploying and managing containerized applications on a Kubernetes cluster, setting the foundation for more advanced Kubernetes usage and deployment scenarios. With this knowledge, you are now equipped to continue your journey in mastering Kubernetes and leveraging its powerful capabilities to streamline your application deployment and management processes.

Other Kubernetes Tutorials you may like