Understanding Kubernetes Objects and YAML Manifests
Kubernetes uses objects to represent the desired state of your applications and cluster resources. These objects are defined using YAML manifests, which describe what you want Kubernetes to create and manage. Understanding these objects and their YAML definitions is essential for deploying and scaling applications effectively.
What are Kubernetes Objects?
Kubernetes objects are persistent entities in the cluster. They describe the desired state of workloads, networking, and configurations. The Kubernetes control plane continuously works to ensure the actual state matches the desired state defined in these objects.
Common Kubernetes Objects
- Pod: The smallest deployable unit, representing one or more containers.
- Deployment: Manages stateless applications and ensures replicas are maintained.
- Service: Exposes Pods and provides stable networking.
- ConfigMap: Stores configuration data.
- Secret: Stores sensitive information like passwords or tokens.
- Ingress: Manages external HTTP/HTTPS access to services.
- StatefulSet: Manages stateful applications like databases.
- DaemonSet: Ensures Pods run on all nodes for tasks like logging or monitoring.
YAML Manifests
A YAML manifest is a configuration file that defines a Kubernetes object. It specifies apiVersion, kind, metadata, and spec. Kubernetes reads this file and creates or updates the object accordingly.
Basic Structure of a YAML Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Explanation: This manifest defines a Deployment named my-app with 2 replicas running Nginx containers.
Flowchart: YAML to Object Lifecycle
User writes YAML ---> kubectl apply ---> API Server
| |
v v
etcd stores state <--- Controller Manager ensures desired state
|
v
Scheduler assigns Pods ---> Worker Nodes run containers
Explanation: The YAML manifest is applied using kubectl. The API Server validates it, etcd stores the configuration, and the control plane ensures the cluster matches the desired state.
Real-Time Example
Suppose a developer wants to deploy a payment service in an e-commerce platform. They create a Deployment YAML manifest with 3 replicas of the payment microservice. Kubernetes ensures these Pods are always running, and a Service manifest exposes them to other microservices like checkout and order management.
Common Mistakes
- Incorrect indentation in YAML files causing parsing errors.
- Using wrong apiVersion leading to validation failures.
- Not defining labels properly, breaking Service-to-Pod communication.
- Hardcoding sensitive data instead of using Secrets.
- Forgetting resource limits, leading to cluster instability.
Interview Notes
Q1: What is a Kubernetes YAML manifest?
Answer: A YAML manifest is a configuration file that defines Kubernetes objects. It specifies the desired state, which Kubernetes continuously works to maintain.
Q2: Difference between Deployment and StatefulSet?
Answer: Deployment is used for stateless applications where replicas are interchangeable. StatefulSet is used for stateful applications requiring stable identities and persistent storage.
Q3: How does Kubernetes apply a YAML manifest?
Answer: The manifest is submitted via kubectl to the API Server, stored in etcd, and enforced by the Controller Manager and Scheduler.
Q4: Example Interview Task
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
Explanation: This YAML defines a ClusterIP service exposing Pods labeled my-app on port 80.
Advanced Notes
- Custom Resource Definitions (CRDs): Extend Kubernetes with custom objects.
- Operators: Automate complex application management using CRDs and controllers.
- Helm Charts: Package multiple YAML manifests for easy deployment.
- RBAC: Define access control policies using YAML manifests.
Summary
Kubernetes objects represent the desired state of applications and resources. YAML manifests define these objects in a structured way. Understanding Pods, Deployments, Services, ConfigMaps, and Secrets is essential for building reliable applications. Flowcharts and examples show how manifests are applied and enforced. Avoiding common mistakes and mastering YAML prepares developers for real-world projects and interviews.