KUBERNETES Study Note-1
KUBERNETES Study Note-1
Study Note
Kubernetes (K8s) is an open-source container orchestration platform. It
automates the deployment, scaling, and management of containerized
applications.
• Self-healing:
If a container crashes or a node fails, Kubernetes restarts or reschedules the
affected Pods automatically.
Kubernetes Architecture
🧠 Control Plane (The brain of the cluster)
The Control Plane is responsible for the overall management of the Kubernetes
cluster. It makes decisions like where to run applications, keeps track of cluster
state, and handles changes.
Key Components:
1. kube-apiserver
o It's the front-end of the Kubernetes control plane.
o All requests (e.g., from kubectl) go through the API server.
o It validates and processes all cluster changes.
2. etcd
o A distributed key-value store used as the cluster's database.
• Minikube
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Line Explanation
- name: nginx The name of the container (used for logs, debugging, etc.)
image: The container image to use. This will pull the official nginx
nginx:1.14.2 version 1.14.2 from Docker Hub.
- containerPort:
Port 80 is the default port for Nginx to listen on.
80
Example output:
This confirms your Pod is running and reachable inside the cluster.
Summary of Concepts
Concept Description
What it shows:
Good for testing or one-off runs. Good for production, scalable apps.
3. What Is a ReplicaSet?
• A ReplicaSet is a Kubernetes controller that ensures a specified number
of pod replicas are running at any given time.
• A Deployment creates and manages ReplicaSets.
• ReplicaSet watches for any missing or crashed pods and brings them
back automatically.
deployment.yaml
apiVersion: apps/v1 # API version for Deployment
kind: Deployment # Type of Kubernetes object
metadata:
name: nginx-deployment # Name of the deployment
labels:
app: nginx # Labels to identify and group
spec:
replicas: 3 # Desired number of Pods
selector:
matchLabels:
app: nginx # Tells ReplicaSet which Pods to manage
template: # Blueprint for Pods
metadata:
labels:
app: nginx # Labels applied to each Pod
spec:
containers:
- name: nginx # Container name
image: nginx:1.14.2 # Docker image to use
ports:
- containerPort: 80 # Container listens on port 80
We can find the syntax from Kubernetes deployment documents in web for this
example nginx image. Also, there is number examples are available at GitHub
repositories also.
6. Autohealing by ReplicaSets
If a pod fails, gets deleted, or crashes, the ReplicaSet immediately detects the
change and automatically creates a new pod to replace it. This self-healing
capability is what makes Kubernetes highly reliable and resilient. Unlike
individual pods—which are ephemeral and will not be recreated if deleted
manually—ReplicaSets guarantee availability by watching over them.
It uses a selector to match the labels of the pods it controls and ensures that
the number of pods matching those labels always equals the number specified
in the replicas field.
For example, if you declare replicas: 3 and one pod is removed or fails, the
ReplicaSet controller will automatically launch a new identical pod to restore
the count to 3. In this way, ReplicaSet plays a crucial role in Kubernetes auto-
healing, ensuring your application is continuously available without manual
intervention.
After deletion:
• The ReplicaSet detects that only 2 Pods are running (but 3 are desired).
• It automatically creates a new pod to maintain the replica count.
This is how Kubernetes maintains desired state. You’ll see a new pod name
appear — that's auto-healing in action!
• CURRENT = 3
• READY = 3
This shows the ReplicaSet is actively ensuring the correct state.
StatefulSet and DaemonSet in Kubernetes
What is a StatefulSet in Kubernetes?
A StatefulSet is used to manage stateful applications, where each Pod needs to
be uniquely identifiable, stable, and persistent.
Key Features:
1. Stable Network Identity: Each Pod gets a persistent hostname like pod-0,
pod-1, etc.
2. Persistent Storage: Each Pod gets its own volume, which stays the same
even if the Pod is deleted and recreated.
3. Ordered Deployment & Scaling:
o Pods are created in order (0, 1, 2…).
o Pods are deleted in reverse order (2, 1, 0…).
4. Use Case: Databases (e.g., MongoDB, Cassandra), Kafka, etc., where each
instance maintains its own data and state.
Example:
If you deploy 3 replicas of a MongoDB cluster using StatefulSet:
• You get: mongo-0, mongo-1, mongo-2
• Each has a unique DNS: mongo-0.mongo-service.default.svc.cluster.local
Kubernetes Services
A Kubernetes Service is an abstract way to expose a group of Pods (typically
running the same application) as a network service. Even though Pods are
ephemeral (they can die and be replaced), a Service gives them a stable IP and
DNS name, allowing other components or users to communicate with them
reliably.
In simple words:
A Service gives a fixed name (DNS) and IP address to a group of Pods, so you
can reach your application reliably, even if the Pods themselves are temporary.
Service Discovery
Service Discovery in Kubernetes is the mechanism by which one part of your
application (like a frontend) can find and communicate with another part (like
a backend) — without needing to hardcode IP addresses.
It answers the question:
"How can one Pod find and talk to another Pod in a scalable, reliable way?"
These labels are arbitrary, meaning you can define your own keys and values, like:
Why Are Labels Important?
Kubernetes objects (like Services, Deployments, ReplicaSets) use selectors to
match against these labels to decide which Pods to target or manage.
Think of it like:
“Find me all Pods where app=nginx” — that’s what a selector does.
Example:
selector:
app: nginx
Summary
Term Definition
Label Key-value metadata to identify Kubernetes objects
Selector A query that matches labels to select specific objects
Service Uses selectors to find Pods to route traffic to
Service The ability for Services to automatically find and connect to
Discovery Pods
1. ClusterIP (Default)
What is it?
• The default type of Service.
• Exposes the application on a virtual IP (ClusterIP) that’s accessible only
inside the cluster.
Use Case:
• Internal communication between:
o Frontend ↔ Backend
o App ↔ Database
• Microservices communicating inside Kubernetes.
How it Works:
• You define the selector to point to Pods (e.g., app=backend).
• Kubernetes assigns a stable ClusterIP like 10.96.0.5.
• Other Pods can reach it using:
o DNS: http://my-service
o Or IP directly: http://10.96.0.5
Security:
• Cannot be accessed from outside the cluster.
• Perfect for secure internal networks.
1. ClusterIP (Default)
What is it?
• The default type of Service.
• Exposes the application on a virtual IP (ClusterIP) that’s accessible only
inside the cluster.
Use Case:
• Internal communication between:
o Frontend ↔ Backend
o App ↔ Database
• Microservices communicating inside Kubernetes.
How it Works:
• You define the selector to point to Pods (e.g., app=backend).
2. NodePort
What is it?
• Makes the Service accessible outside the cluster using:
<NodeIP>:<NodePort>
• Kubernetes allocates a static port (30000–32767) on each Node.
Example:
Let’s say:
type: NodePort
nodePort: 30007
Then:
curl http://<minikube-ip>:30007
Notes:
• Not suitable for production.
3. LoadBalancer Service
A LoadBalancer type service in Kubernetes exposes your application to the
internet via a public IP address and routes incoming traffic through a cloud
provider's external load balancer.
It builds on top of the NodePort and ClusterIP services but adds an external
Load Balancer (LB) in front of them.
Best for production environments where real users need stable and
scalable external access to your app.
Things to Remember
Concern Detail
Cloud provider
Works only in managed Kubernetes like GKE, EKS, AKS
required
Extra cost Cloud Load Balancers are billable
Slow provisioning Initial LB creation might take 1–2 minutes
One LB per service Every LoadBalancer service gets a new LB
You must point your domain (e.g., myapp.com) to the
DNS not included
LB’s IP manually
Kubernetes Ingress
1. What is Ingress?
Ingress is a Kubernetes API object that allows external traffic (HTTP/HTTPS) to
reach internal Kubernetes services in a smart and efficient way.
Instead of assigning each service a separate external IP or port, you define
Ingress rules to handle:
• Path-based routing (/app1 goes to Service A, /app2 to Service B)
• Host-based routing (app1.example.com, app2.example.com)
Real-world Analogy:
• LoadBalancer: Like having a separate road for each shop.
• Ingress: Like a single mall entrance with signboards guiding you to each
shop inside.
4. What is Ingress Controller?
An Ingress Controller is a Kubernetes component (usually a Pod) that:
• Listens to Ingress resources (rules)
• Configures a reverse proxy (like NGINX, Traefik)
• Handles real traffic and routes it correctly
Just creating an Ingress resource is not enough! You must install and run
an Ingress Controller for the Ingress to actually work.
Working:
1. You define an Ingress rule (e.g., /api → service backend)
2. The Ingress Controller sees it
3. It updates its internal reverse proxy
4. Incoming request to /api is routed to the backend service
Ingress
Description Best For
Controller
NGINX
Most popular; works in any
Ingress Minikube, on-prem, cloud
environment
Controller
Lightweight, dynamic config
Dev environments, dynamic
Traefik updates, modern
workloads
dashboard
HAProxy High performance,
High traffic workloads
Ingress enterprise-grade
AWS ALB
Integrates directly with
Ingress AWS EKS
AWS ALB
Controller
Key Feature:
• Data in Secrets is Base64 encoded
• Kubernetes ensures they are stored and transmitted securely
• Kubernetes supports integrating Secrets with volume mounts or
environment variables
Use Secrets for any confidential config that shouldn’t be visible in plaintext.
3. Difference between ConfigMap and Secret
Step-by-Step:
1. Create ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
APP_MODE: production
LOG_LEVEL: info
2. Use it in Pod:
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: myapp
image: myapp:1.0
envFrom:
- configMapRef:
name: my-config
apiVersion: v1
kind: ConfigMap
metadata:
name: config-vol
data:
app.properties: |
APP_MODE=production
LOG_LEVEL=info
apiVersion: v1
kind: Pod
metadata:
name: volume-app
spec:
containers:
- name: app-container
image: myapp:1.0
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: config-vol
Kubernetes will create a file per key inside the /etc/config directory.
For example: /etc/config/app.properties contains the config text.
Example Secret:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
password: cGFzc3dvcmQ= # base64 of "password"
Mount as ENV in Pod:
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: my-secret
key: password
Mount as Volume:
volumeMounts:
- name: secret-volume
mountPath: /etc/secret
volumes:
- name: secret-volume
secret:
secretName: my-secret
Summary
Feature ConfigMap Secret
Purpose App configuration Confidential data
Encoding Plaintext Base64
Access ENV / Volume ENV / Volume
Use When Config you can log Credentials or tokens
Security Not encrypted Designed for security
Storage etcd etcd (can be encrypted)
Explanation:
• namespace: dev: The role is limited to the dev namespace.
• resources: ["pods"]: Only pods are included.
• verbs: What actions are allowed → get (read), list (see all), watch
(observe changes).
Explanation:
• This ClusterRole allows access to nodes, which are not namespace-
scoped resources.
• This permission applies to entire cluster, not just one namespace.
Users’ vs Service Accounts
What Are Users in Kubernetes?
In Kubernetes, users represent real humans (like developers, admins, testers)
who interact with the cluster externally using tools like:
• kubectl
• CI/CD pipelines
• APIs
RBAC Flow:
1. User or Pod makes a request (kubectl, API)
2. Kubernetes authenticates identity
3. RBAC checks:
- Does this identity have permission to do this action?
- via Role/ClusterRole and Binding
4. If yes → access granted
If no → access denied
RBAC Objects:
# Role (dev namespace)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dev
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
# RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods-alice
namespace: dev
subjects:
- kind: User
name: [email protected]
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
# Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: app-space
name: config-reader
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list"]
# RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: config-reader-binding
namespace: app-space
subjects:
- kind: ServiceAccount
name: backend-serviceaccount
roleRef:
kind: Role
name: config-reader
apiGroup: rbac.authorization.k8s.io
The app running in the Pod now securely reads config data without
hardcoding credentials.
Key Takeaways
• Users = external humans, managed by external systems like OIDC.
• ServiceAccounts = internal app identities, managed by Kubernetes.
• RBAC links Roles (what can be done) to Subjects (users or service
accounts) via Bindings.
• Use IDPs for production-grade user management.
• ServiceAccounts are scoped to a namespace, useful for least-privilege
design.