Create a Simple YAML Manifest
Before creating your first YAML manifest, let's take a moment to understand the key Kubernetes objects you will work with. These objects help manage and orchestrate your applications:
Understanding Kubernetes Objects
- Pod: The smallest deployable unit in Kubernetes. A pod encapsulates one or more containers, shared storage, and a network identity. It represents a single instance of a running process in your cluster.
- Deployment: A higher-level abstraction that manages pods. Deployments provide features like scaling, rolling updates, and rollback capabilities. They ensure that the desired number of pod replicas are running.
- Service: An abstraction that defines a logical set of pods and a policy to access them. Services enable communication between different components of your application or with external clients.
Hereโs a diagram to clarify their relationships:
graph TD;
A[Deployment] -->|Manages| B[Pods]
B -->|Contains| C[Containers]
B -->|Communicates via| D[Services]
D -->|Exposes| E[External Clients]
Understanding these objects is crucial as you proceed to define their configurations in YAML.
YAML Manifest Overview
A YAML manifest in Kubernetes is a declarative way to define the desired state of resources. Using YAML has several benefits:
- Declarative Management: You define the "what" (desired state), and Kubernetes handles the "how" (implementation).
- Version Control: YAML files can be stored in version control systems (e.g., Git), allowing easy tracking of changes.
- Reusability: YAML manifests can be reused across environments (e.g., development, testing, production) with minor modifications.
With this understanding, you are ready to create your first YAML manifest.
Creating a YAML Manifest
Navigate to your project directory:
cd ~/project
Create a new directory for your Kubernetes manifests:
mkdir -p k8s-manifests
cd k8s-manifests
Now, create a YAML file for a simple NGINX pod:
nano nginx-pod.yaml
Paste the following content into the file:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Press Ctrl+X
, then Y
, and Enter
to save and exit the file.
Let's break down the YAML manifest:
apiVersion
: Specifies the Kubernetes API version used for the resource definition.
kind
: Defines the resource type (Pod in this case).
metadata
: Provides information about the resource, such as its name and labels.
spec
: Describes the desired state of the resource.
containers
: Lists the containers to run in the pod.
image
: Specifies the container image to use.
ports
: Exposes the container's port.
Apply the manifest to create the pod:
kubectl apply -f nginx-pod.yaml
Example output:
pod/nginx-pod created
Verify the pod creation:
kubectl get pods
kubectl describe pod nginx-pod
Example output for kubectl get pods
:
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 1m
Now, let's create a more complex deployment manifest:
nano nginx-deployment.yaml
Paste the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Key differences from the pod manifest:
kind: Deployment
: Creates a deployment that manages multiple pods
replicas: 3
: Ensures 3 identical pods are running
selector
: Helps the deployment manage the correct pods
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
Verify the deployment:
kubectl get deployments
kubectl get pods
Example output:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m
NAME READY STATUS RESTARTS AGE
nginx-deployment-xxx-yyy 1/1 Running 0 1m
nginx-deployment-xxx-zzz 1/1 Running 0 1m
nginx-deployment-xxx-www 1/1 Running 0 1m