Manifest File in Kubernetes: A Step-by-Step Example
A Manifest File in Kubernetes is like a blueprint for deploying applications. It's a crucial component of Kubernetes (K8s) that defines the desired state of your cluster. Written in YAML or JSON, manifest files tell K8s exactly what you want to run and how you want it to run. Let's dive into the world of Kubernetes manifests, using real-life examples to illustrate their power.
Before diving into specifics, let'scidos some basics. Kubernetes uses declarative management, meaning you specify what you want, not how to achieve it. This is done through manifests. Now, let's discuss two key types of Kubernetes objects that manifest files describe: Pods and Deployments.
Pod Manifests
A Pod is the smallest and simplest unit of the Kubernetes Platform that you create or deploy. It represents a single instance of a running process on your K8s cluster. Here's a basic Pod manifest example:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
In this example, we're creating a Pod named 'nginx-pod' that runs the NGINX image. Now, let's discuss how to create a multi-container Pod:
Multi-Container Pods
Kubernetes allows creating Pods with multiple containers that need to run in the same context. Here's an example of a manifest that runs both an NGINX web server and a Redis cache:
apiVersion: v1
kind: Pod
metadata:
name: nginx-redis
spec:
containers:
- name: nginx
image: nginx:latest
- name: redis
image: redis:alpine
Now, let's discuss another critical Kubernetes object – Deployments. Deployments ensure that a specified number of Pod Template objects are running. Here's an example:
Deployment Manifests
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
template:
metadata:
spec:
image: nginx:latest
In this example, we're creating a Deployment named 'nginx-deployment' that guarantees there are always 3 Pods running the NGINX image. If a Pod crashes or is deleted, the Deployment will create a new one.
Deployment Updates
Deployments also allow you to update your application without downtime. Here's how you could update the NGINX image to a specific version:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
template:
metadata:
spec:
image: nginx:1.16.1
By applying this manifest, K8s will gradually update all three NGINX Pods without any downtime. Isn't that amazing?
Kubernetes manifests are powerful tools that allow you to describe your desired state in a clear, organized way. Whether you're managing simple Pods or complex Deployments, they're an essential part of the K8s ecosystem. Now, go forth and create manifests to your heart's content!