Skip to main content

Comprehensive Guide to Kubernetes

· 7 min read

This guide provides a detailed overview of Kubernetes concepts, examples, use cases, installation YAML examples, and visual representations of the architecture.

Cluster​

A Kubernetes Cluster is a group of nodes (machines) that host and manage containerized applications. Each cluster has at least one master node, which controls the state of the cluster, and multiple worker nodes.

Example​

A cloud-based cluster with one master node and multiple worker nodes in Google Kubernetes Engine (GKE).

Use Case​

An ideal setup for running a production microservices application with high availability.

YAML Structure for Cluster Creation​

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker

Node​

A Node is a single machine within a Kubernetes cluster, capable of running one or more pods.

Example​

In a cloud environment, each node could be a virtual machine instance.

Use Case​

Nodes distribute workloads across the infrastructure.

YAML Snippet​

The node configuration is usually specified by the cluster management tool (e.g., GKE or EKS) rather than in YAML, as it pertains to the infrastructure layer.


Pod​

A Pod is the smallest deployable unit in Kubernetes, which can contain one or more containers.

Example​

A pod running a web server container and a logging container.

Use Case​

Running tightly coupled applications together within the same lifecycle.

YAML Structure for Pod​

apiVersion: v1
kind: Pod
metadata:
name: web-server-pod
spec:
containers:
- name: web-server
image: nginx
- name: logger
image: fluentd

Deployment​

A Deployment is a Kubernetes object for managing a group of identical pods. It provides capabilities like rolling updates and rollback.

Example​

A deployment with three replicas of a web server pod.

Use Case​

Maintaining a stable version of an application by managing pod replicas.

YAML Structure for Deployment​

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.17.4

Service​

A Service provides a stable endpoint for accessing a set of pods.

Example​

A service that exposes an application to the public internet.

Use Case​

Load balancing traffic to an application across multiple pods.

YAML Structure for Service​

apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: LoadBalancer
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80

Ingress​

Ingress manages external access to services, typically using HTTP.

Example​

An ingress with rules to route traffic to different services based on URL paths.

Use Case​

Routing /api to backend services and /static to frontend services on the same domain.

YAML Structure for Ingress​

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /static
pathType: Prefix
backend:
service:
name: static-service
port:
number: 80

ConfigMap​

A ConfigMap stores non-sensitive configuration data.

Example​

A ConfigMap for environment-specific variables.

Use Case​

Keeping configuration separate from the application code.

YAML Structure for ConfigMap​

apiVersion: v1
kind: ConfigMap
metadata:
name: config-demo
data:
DATABASE_URL: "jdbc:mysql://dbhost:3306/mydatabase"
DEBUG_LEVEL: "3"

Secret​

A Secret stores sensitive information, like passwords and tokens.

Example​

A secret containing API keys for external services.

Use Case​

Securing sensitive data without exposing it in the code.

YAML Structure for Secret​

apiVersion: v1
kind: Secret
metadata:
name: api-key-secret
type: Opaque
data:
api-key: c29tZS1yYW5kb20ta2V5Cg==

Persistent Volume (PV) and Persistent Volume Claim (PVC)​

A Persistent Volume provides storage, while a Persistent Volume Claim is a request for that storage.

Example​

A PV representing cloud disk storage and a PVC used by a database pod.

Use Case​

Ensuring data persistence for applications that require durable storage.

YAML Structure for PV and PVC​

apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-storage
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi

Namespace​

A Namespace is a way to divide cluster resources among multiple users.

Example​

Separate namespaces for development, staging, and production environments.

Use Case​

Organizing resources for different environments or teams.

YAML Structure for Namespace​

apiVersion: v1
kind: Namespace
metadata:
name: dev-environment

DaemonSet​

A DaemonSet ensures that specific pods run on each node.

Example​

Running a logging agent on every node in the cluster.

Use Case​

Managing node-wide services like logging or monitoring.

YAML Structure for DaemonSet​

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: logger
spec:
selector:
matchLabels:
name: logger
template:
metadata:
labels:
name: logger
spec:
containers:
- name: fluentd
image: fluent/fluentd

Job and CronJob​

Job runs a one-time task, while CronJob schedules a recurring task.

Example​

A Job to back up a database once and a CronJob to clean up logs daily.

Use Case​

Automating scheduled tasks or batch processing.

YAML Structure for Job and CronJob​

apiVersion: batch/v1
kind: Job
metadata:
name: backup-job
spec:
template:
spec:
containers:
- name: backup
image: backup-tool
restartPolicy: OnFailure

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: daily-cleanup
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cleanup
image: cleanup-tool
restartPolicy: OnFailure

Horizontal Pod Autoscaler (HPA)​

The HPA scales pod replicas automatically based on resource utilization.

Example​

Scaling web server pods based on CPU usage.

Use Case​

Scaling applications to handle variable workloads.

YAML Structure for HPA​

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTarget

Ref:
apiVersion: apps/v1
kind: Deployment
name: web-deployment
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 70

StatefulSet​

A StatefulSet manages stateful applications with unique and persistent storage.

Example​

A StatefulSet for a MongoDB replica set.

Use Case​

Managing stateful applications where each instance requires its own storage.

YAML Structure for StatefulSet​

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 3
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo
ports:
- containerPort: 27017
name: mongo

Volume and StorageClass​

Volumes provide storage to pods, while StorageClass defines types of storage.

Example​

SSD-based storage for a database application.

Use Case​

Choosing storage for applications based on performance requirements.

YAML Structure for StorageClass and Volume​

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2

---
apiVersion: v1
kind: PersistentVolume
metadata:
name: fast-pv
spec:
storageClassName: fast
capacity:
storage: 50Gi
accessModes:
- ReadWriteOnce

NetworkPolicy​

NetworkPolicy defines network access rules between pods.

Example​

Restricting access to specific pods.

Use Case​

Securing inter-service communication.

YAML Structure for NetworkPolicy​

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
ingress: []
egress: []

Custom Resource Definitions (CRDs)​

CRDs extend Kubernetes by allowing users to create custom resources.

Example​

A custom resource to manage database schema migrations.

Use Case​

Adding domain-specific resources for applications.

YAML Structure for CRD​

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: migrations.mycompany.com
spec:
group: mycompany.com
versions:
- name: v1
served: true
storage: true
scope: Namespaced
names:
plural: migrations
singular: migration
kind: Migration

Helm Charts​

Helm is a package manager for Kubernetes, and Helm Charts are pre-configured packages.

Example​

A Helm chart for deploying a WordPress site.

Use Case​

Simplifying application deployment and management.

Example Helm Command​

helm install my-wordpress bitnami/wordpress

These concepts are essential for understanding and operating Kubernetes in production, allowing you to deploy, manage, and scale applications effectively. Each example and YAML configuration highlights the power and flexibility Kubernetes offers in a variety of scenarios.