Kubernetes - 4
Kubernetes - 4
Tejas Pokale
1
INDEX
Sr.no TOPICS COVER
1 Introduction to Kubernetes
2 Kubernetes Installation
3 Pod creation
4 Kubernetes service
5 Replication Controller
6 Replica Set
7 Deployment
8 Health Probe
9 Namespace
10 Volume
11 DaemonSet
12 Job
13 StatefulSet
14 Horizontal PodAutoscaling
15 Ingress
2
Introduction to Kubernetes
Kubernetes (K8s) is an open-source platform for automating the deployment, scaling, and
management of containerized applications. Originally developed by Google and now maintained by the
Cloud Native Computing Foundation (CNCF), Kubernetes has become the industry standard for
container orchestration.
Why Kubernetes?
5. Rolling Updates and Rollbacks: Ensures smooth updates with minimal downtime.
Core Components
• Pod: The smallest deployable unit, which can contain one or more containers.
3
Why Use Kubernetes?
Applications of Kubernetes
4
Kubernetes Installation on Ubuntu Linux
Step 1: Update and Install Required Dependencies
5
Step 2: Setup Master Node
mkdir -p $HOME/.kube
• The kubeadm token create --print-join-command command generates the token and join
command for worker nodes.
• Open port 6443 on both master and worker nodes to ensure communication between them.
Run the kubeadm join command generated on the master node on each Worker Node:
Run the following on the Master Node to check the status of the nodes:
This command should display all nodes (master and workers) with their status.
6
Check pods
Create a directory
mkdir nginxpod
Nano nginxpod.yml
7
Nginxpod.yml file
Sudo crictl ps
8
Kubectl pet pod -o wide
9
Kubernetes Services
A Service in Kubernetes allows stable communication between applications (Pods) and external users
or systems by abstracting the underlying Pods' dynamic nature.
1. ClusterIP (Default):
o Used for internal communication between Pods and other cluster components.
2. NodePort:
3. LoadBalancer:
4. ExternalName:
10
nano myservice.yml
myservice.yml
11
Kubectl get pod
Check on browse
12
LOAD BALANCER
nano loadwalaservice.yml
13
Launch the load balancer manually
14
Edit security group
Create new index.htm page and show loadbalancing. pod name is different but labale is same
15
Copy ip and paste it to browser
• Replication Controller:
• ReplicaSet:
16
o Manages pod replicas with more flexible selectors.
• Scaling:
o Autoscaling: Pods are scaled automatically based on metrics like CPU usage (via
Horizontal Pod Autoscaler)
Replication Controller
Mkdir replicationcontrollerwala
Cd replicationcontrollerwala
nano myreplica.yml
Kubectl get rc
17
To check self healing
18
Change lable
Cp myreplica.yml youreplica.yml
Cp myreplica.yml ourreplica.yml
19
Nano yourreplica.yml
Nano ourreplica.yml
Scaling:
21
nano myreplica.yml
nano ourreplica.yml
22
Kubectl apply -f myreplica.yml
23
To show selectors
Change lables
cp myreplica.yml yourreplica.yml
cp myreplica.yml ourreplical.yml
24
nano yourreplica.yml
nano ourreplica.yml
25
ReplicaSet
Cd replicasetwala
Nano replica.yml
26
Nanoourreplica.yml
Nano yourreplica.yml
28
DEPLOYMENT
Mkdir deploymentwala
Cd mydeploymentwala
nano mynginx.yml
29
Mynginx.yml
30
Version (change by .yml file)
Nano mynginx.yml
31
Statergy
Nano sat.yml
32
HEALTHPROBE
In Kubernetes, a health probe is a mechanism used by the Kubernetes system to monitor the health of
applications running in a container. Kubernetes uses these probes to determine if a container is
healthy or unhealthy. There are two main types of health probes:
1. Liveness Probe
o If the liveness probe fails, Kubernetes kills the container and restarts it (based on the
pod's restart policy).
o Use Case: To detect and remedy situations where the application is in a deadlock or not
responding.
2. Readiness Probe
o If the readiness probe fails, Kubernetes removes the pod from the Service's endpoints,
so it doesn’t receive traffic.
o Use Case: To ensure that the application has completed its startup process and is ready
to handle requests.
3. Startup Probe
• It ensures that the application is fully initialized before Kubernetes considers it "live."
• During the startup phase, Liveness Probes are disabled to avoid restarting the container
prematurely.
• Once the Startup Probe succeeds, Kubernetes switches to using the Liveness Probe (if
configured) for ongoing health checks.
33
Liveness Probe
Liveness Probe
34
Livenessprobe.yml
35
Readiness Probe
36
Startup Probe:
37
Namespaces in Kubernetes
A namespace in Kubernetes is a virtual cluster that provides logical isolation for resources within the
same physical cluster. It is primarily used for organizing and managing workloads, enabling multi-
tenancy, and implementing access controls in Kubernetes environments
Purpose of Namespaces
4. Multi-Tenancy: Enable multiple users or teams to share the same Kubernetes cluster securely.
Default Namespaces
User-Defined Namespaces
Users can create custom namespaces to organize resources for specific purposes, such as:
Key Features
2. Scoped Resources: Namespaces group resources such as pods, services, and deployments.
3. Role-Based Access Control (RBAC): Assign granular access permissions per namespace.
4. Resource Quotas: Limit CPU, memory, and other resource usage within a namespace.
38
How Namespaces Work
• Resources are scoped to a namespace unless they are cluster-wide (e.g., nodes, storage
classes).
• Users can specify the namespace in commands or set a default namespace for ease of use.
Managing Namespaces
39
Namespace by using.ymlfile
40
Volumes in Kubernetes
A volume in Kubernetes is a directory that can be accessed by containers in a pod. It enables data to
persist across container restarts and allows containers within the same pod to share data. Volumes
address the ephemeral nature of container storage, making it suitable for applications requiring
stateful data or configuration.
Purpose of Volumes
2. Data Sharing: Enable data sharing between containers in the same pod.
1. EmptyDir
o Definition: A temporary volume that is created when a pod starts and deleted when the
pod stops.
o Use Case: Suitable for temporary data such as caches, scratch space, or intermediate
files.
o Key Characteristics:
2. HostPath
o Definition: A volume that maps a file or directory from the node’s filesystem into the pod.
o Use Case: Useful for accessing specific files or directories on the host machine, such as
logs or system-level resources.
o Key Characteristics:
▪ Tightly coupled with the host node, making pods less portable.
3. PersistentVolumeClaim (PVC)
41
o Definition: A mechanism to request and claim persistent storage resources. PVCs bind
to PersistentVolumes, which represent actual storage, such as network storage or cloud-
based volumes.
o Use Case: Suitable for applications requiring persistent storage that survives pod
restarts or re-creation.
o Key Characteristics:
4. ConfigMap
o Use Case: Ideal for storing non-sensitive configuration data such as environment
settings, configuration files, or command-line arguments.
o Key Characteristics:
5. Secret
o Key Characteristics:
42
EmptyDir
HostPath
43
Nfs
44
Config map
45
By using another imge nginx
Config.file
46
47
Secret
48
PersistentVolumeClaim (PVC)
49
50
DaemonSet in Kubernetes
A DaemonSet in Kubernetes is a controller that ensures a copy of a specific pod runs on all (or
selected) nodes in the cluster. It is typically used to deploy background system-level services or
applications that need to run on every node.
Purpose of DaemonSet
1. System Monitoring: Deploying monitoring agents (e.g., Prometheus Node Exporter, Fluentd).
3. Networking: Setting up network components such as DNS services or proxies (e.g., CoreDNS,
kube-proxy).
4. Custom Node Tasks: Running tasks specific to each node, such as node health checks or
backups.
1. One Pod per Node: Ensures exactly one pod is running per eligible node.
2. Node Selection: Can restrict pods to specific nodes using labels and node selectors.
3. Automatic Updates: New nodes automatically get the DaemonSet pods upon joining the cluster.
4. Controlled Deletion: When deleted, the associated pods are removed from the nodes.
1. Deployment:
o DaemonSets deploy pods based on a defined configuration, and Kubernetes ensures that
one pod is scheduled per node.
2. Targeting Nodes:
o Affinity Rules: Fine-tunes node selection using affinity and anti-affinity rules.
3. Updates:
51
Creating a DaemonSet
52
Jobs in Kubernetes
A Job in Kubernetes is a controller that ensures a specified number of pods successfully complete a
task. Unlike Deployments, which maintain a set number of running pods, Jobs are used for batch
processing and tasks that have a definite completion point. Once the task is completed, the pods are
terminated.
Purpose of Jobs
1. Batch Processing: Running tasks that need to be completed once (e.g., data processing,
backups).
2. One-Time Operations: Executing one-off tasks that don't need to be continuously running.
4. ETL Tasks: Running Extract, Transform, Load (ETL) jobs that are periodic or triggered.
1. Task Completion: Jobs ensure that a task is successfully completed, retrying pods in case of
failure.
2. Pod Management: Creates one or more pods and tracks their completion.
3. Parallel Execution: Supports running multiple pods in parallel to complete the task faster.
4. Pod Retry Mechanism: Automatically retries failed pods until the desired number of
completions is achieved.
5. Graceful Termination: When the task completes, the associated pods are terminated.
o A Job will continue running pods until the specified number of completions is reached.
o This number can be defined using the completions field in the Job's specification.
2. Pod Management:
o The Job controller creates pods to run the specified task and monitors their completion.
3. Retry Mechanism:
o If a pod fails to complete the task, the Job controller will retry the pod. The number of
retries can be controlled via the backoffLimit field.
53
4. Parallelism:
o Jobs can be configured to run multiple pods simultaneously using the parallelism field.
This can speed up completion when the task is divisible
54
55
CronJob in Kubernetes
A CronJob in Kubernetes is a resource used to run jobs on a scheduled basis, much like cron jobs in
Linux. It allows you to run batch jobs or tasks at specific times or intervals, providing powerful
scheduling capabilities in Kubernetes.
Purpose of CronJob
1. Scheduled Tasks: Run tasks at regular intervals, such as backups, database cleanup, and
periodic reporting.
3. Task Automation: Eliminate the need for manual intervention for repetitive tasks.
1. Cron-like Scheduling:
o CronJobs are based on a cron expression (a string that defines time intervals), which
specifies when the job should run.
o The cron format is: * * * * * (minute, hour, day of month, month, day of week).
2. Job Creation:
o CronJobs automatically create Jobs according to the schedule defined by the cron
expression.
o The job runs at the specified time and completes its task.
3. Job Execution:
o The CronJob creates a new Job and ensures that the associated task (specified in the
Job template) is executed as a pod.
4. Concurrency Policy:
o CronJobs allow you to control how multiple jobs should be handled when the scheduled
time occurs:
▪ Forbid: Prevents a new job from starting if the previous one hasn't completed.
▪ Replace: Terminates any currently running job and starts a new one.
56
5. Time Zone:
Creating a CronJob
*****
|||||
57
StatefulSet in Kubernetes
A StatefulSet in Kubernetes is used to manage stateful applications that need stable identities and
persistent storage. This is different from a regular deployment where pods are stateless. StatefulSets
are ideal for applications like databases that require data to be saved and remain consistent even if
the pod is restarted or moved.
1. Stable Identity: Each pod in a StatefulSet gets a unique name (like mysql-0, mysql-1, etc.) that
doesn't change, even if the pod restarts.
2. Persistent Storage: Each pod gets its own dedicated storage that is preserved even if the pod
is rescheduled to another node.
3. Order of Deployment: Pods are created, updated, and deleted in a specific order. This is helpful
for apps that need to start in a particular sequence (e.g., databases).
4. Scaling: Pods in a StatefulSet are scaled in order (first pod-0, then pod-1, etc.), and scaling down
happens in reverse order.
1. Stable Pod Names: Pods in a StatefulSet get stable, unique names that help other services
identify them easily.
58
2. Storage Persistence: Each pod has a PersistentVolume associated with it, which ensures the
data is retained even when pods are deleted or moved.
3. Ordered Deployment and Shutdown: Pods are created and deleted in a specific order, which is
important for some applications (like databases) where certain pods need to start before others.
4. Rolling Updates: When updating a StatefulSet, Kubernetes updates the pods one at a time,
ensuring minimal disruption.
59
Horizontal Pod Autoscaler
Horizontal Pod Autoscaler (HPA) in Kubernetes automatically adjusts the number of pods in a
deployment based on resource usage (like CPU, memory) or custom metrics. It ensures efficient
resource utilization and handles varying workloads effectively.
Key Points:
• Scales Pods: Automatically increases or decreases the number of pods based on application demand.
• Metrics-Based: Relies on Metrics Server or custom metrics (e.g., CPU, memory, or external metrics).
• Easy Configuration: Managed using YAML files or Kubernetes CLI commands for flexibility.
60
61
An Ingress in Kubernetes is an API object that manages external access to services within a cluster,
typically HTTP and HTTPS traffic. It acts as a smart router, allowing you to define rules for directing
traffic to the appropriate backend services.
62
THE END
63