Pods
November 8, 2024About 3 min
Pods
Info
In YAML, the -
symbol is used when a key contains a list (multiple items). In your example, env
is a list, and each environment variable (a combination of name
and value
) is an object in the env
list. Therefore, you need to use -
before each environment variable.
Creating resources(pod here)
apiVersion: v1
kind: Pod
metadata:
name: nginx-demo
labels:
type: app
version: 1.0.0
namespace: 'default'
spec:
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
command:
- nginx
- g
- 'daemon off;'
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: JVM_OPTS
value: '-Xms128m -Xmx128m'
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
restartPolicy: OnFailure
You can see that the Pod was created successfully. However, using kubectl get po shows an error. After modifying the Nginx version to latest and changing the command to ["nginx", "-g", "daemon off;"], the Pod started successfully.
In Kubernetes, probes are used to monitor the health and status of containers running within a pod. There are three main types of probes:
Liveness Probe: Determines if a container is running. If it fails, Kubernetes restarts the container.
Readiness Probe: Checks if a container is ready to handle requests. If it fails, the container is removed from the service endpoints.
Startup Probe: Ensures the application has started successfully. This is useful for applications that take a long time to initialize, preventing false failures in the liveness and readiness probe
Example with Probes
apiVersion: v1
kind: Pod
metadata:
name: nginx-demo
labels:
type: app
version: 1.0.0
namespace: 'default'
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
startupProbe: # Startup Probe to check if the app starts
httpGet:
path: /index.html
port: 80
failureThreshold: 3
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
readinessProbe: # Readiness Probe to check if the app is ready
httpGet:
path: /index.html
port: 80
failureThreshold: 3
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
livenessProbe: # Liveness Probe to check if the app is alive
httpGet:
path: /index.html
port: 80
failureThreshold: 3
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
command: ["nginx", "-g", "daemon off;"]
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: JVM_OPTS
value: '-Xms128m -Xmx128m'
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
restartPolicy: OnFailure
Info
initialDelaySeconds: 60 — The initialization time before starting the probe.
timeoutSeconds: 2 — The timeout duration for the probe.
periodSeconds: 5 — The interval between each probe check.
successThreshold: 1 — The number of successful checks required to consider the probe successful.
failureThreshold: 2 — The number of failed checks required to consider the probe failed.
Pods Lifecycle
A Pod in Kubernetes goes through several states during its lifecycle. Understanding the lifecycle helps in managing Pods effectively. Here's a breakdown of the key stages:
Pending:
- A Pod is created, but it is not yet scheduled or running on a node. This state can occur due to scheduling delays or if resources are not available on the nodes.
- The Pod transitions from the Pending state when it is assigned to a node and the containers are initialized.
Running:
- Once a Pod has been scheduled on a node, and its containers have been started, the Pod enters the Running state.
- In this state, the containers are actively running, and the Pod is ready to serve requests (unless it’s waiting for initialization or readiness checks).
Succeeded:
- The Pod has successfully completed its work and all its containers have exited with a zero exit status.
- Pods with a
restartPolicy
set toNever
orOnFailure
will enter this state once the containers complete successfully.
Failed:
- This state occurs when the Pod’s containers have terminated with a non-zero exit code and Kubernetes is unable to restart them based on the configured restart policy.
- The Pod will remain in this state unless manually deleted or configured for restart.
Unknown:
- This state means that the kubelet cannot determine the state of the Pod due to issues communicating with the node.
- This could happen in scenarios where the node is not responding.
Example
apiVersion: v1
kind: Pod
metadata:
name: nginx-demo
labels:
type: app
version: 1.0.0
namespace: default
spec:
terminationGracePeriodSeconds: 30 # Grace period for termination (in seconds)
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
command: ["nginx", "-g", "daemon off;"]
workingDir: /usr/share/nginx/html
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: JVM_OPTS
value: '-Xms128m -Xmx128m'
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
restartPolicy: Always
lifecycle:
postStart: # Executes after the container starts
exec:
command:
- "/bin/sh"
- "-c"
- "echo 'Container has started successfully!'"
preStop: # Executes before the container is terminated
exec:
command:
- "/bin/sh"
- "-c"
- "echo 'Container is stopping, performing cleanup tasks...'"