Kubernetes
Containers
DevOps
Container Management
Kubernetes Deployment

How can I keep a container running on Kubernetes?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Kubernetes and Containers

To maintain a container running on Kubernetes, it’s essential to understand what Kubernetes offers and how it interacts with container technologies like Docker. Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. It abstracts the infrastructure layer and provides a robust framework for deploying applications at scale, enabling continuous development and implementation.

Ensuring Your Container Keeps Running

There are several strategies and best practices to ensure that your Kubernetes container stays running smoothly. We'll explore the fundamental concepts associated with that.

Key Concepts to Keep a Container Running

1. Pods and ReplicaSets

Pods are the smallest deployable units in Kubernetes, comprising one or more containers that share the same network namespace. A ReplicaSet ensures that a specified number of pod replicas are running at any given time. By using a ReplicaSet, you ensure that your application scales and has redundancy, which helps in keeping your container running even if one pod fails.

Example configuration for a ReplicaSet:

yaml
1apiVersion: apps/v1
2kind: ReplicaSet
3metadata:
4  name: my-replicaset
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: my-app
10  template:
11    metadata:
12      labels:
13        app: my-app
14    spec:
15      containers:
16      - name: my-container
17        image: my-image:latest

2. Deployments

Deployments provide declarative updates to applications. They offer a higher-level structure than ReplicaSets and automatically manage ReplicaSets. When you define a Deployment, you set the desired state (e.g., a number of replicas, image versions) and Kubernetes manages the rest.

Example configuration for a Deployment:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: my-deployment
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: my-app
10  template:
11    metadata:
12      labels:
13        app: my-app
14    spec:
15      containers:
16      - name: my-container
17        image: my-image:latest
18        ports:
19        - containerPort: 80

3. Handling Faults and Node Failures

Kubernetes manages node failures or crashes using Node Controllers that monitor the health of nodes. If a node fails, Kubernetes reschedules the pods on other healthy nodes, ensuring continuity of service.

  • Liveness and Readiness Probes: Set up liveness and readiness probes in your pod specification to allow the Kubernetes scheduler to determine when to restart or not route traffic to a particular pod.

Example configuration using probes:

yaml
1livenessProbe:
2  httpGet:
3    path: /healthz
4    port: 8080
5  initialDelaySeconds: 10
6  periodSeconds: 5
7
8readinessProbe:
9  httpGet:
10    path: /ready
11    port: 8080
12  initialDelaySeconds: 10
13  periodSeconds: 5

Key Kubernetes Features for Keeping Containers Alive

FeatureDescription
Pods and ReplicaSetsEnsures a specific number of pods are running at all times.
DeploymentsProvides declarative updates and rollback capabilities.
Liveness ProbesMonitors the health of a container and restarts it if it fails.
Readiness ProbesControls when a pod starts receiving traffic.
Node ControllersDetects node failures and ensures pods are rescheduled on healthy nodes.

Advanced Strategies

Horizontal Pod Autoscaler (HPA)

The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment, or replica set based on observed CPU utilization or custom metrics.

Example configuration for HPA:

yaml
1apiVersion: autoscaling/v1
2kind: HorizontalPodAutoscaler
3metadata:
4  name: my-hpa
5spec:
6  scaleTargetRef:
7    apiVersion: apps/v1
8    kind: Deployment
9    name: my-deployment
10  minReplicas: 1
11  maxReplicas: 10
12  targetCPUUtilizationPercentage: 80

Resource Requests and Limits

Defining resource requests and limits help Kubernetes to make more informed scheduling decisions and avoid resource starvation by ensuring pods have the necessary CPU and memory.

Example configuration using resource requests and limits:

yaml
1resources:
2  requests:
3    memory: "64Mi"
4    cpu: "250m"
5  limits:
6    memory: "128Mi"
7    cpu: "500m"

Conclusion

Running a container on Kubernetes involves thoughtful design and management strategies. By leveraging Kubernetes features such as Pods, ReplicaSets, Deployments, and Probes, you can ensure that your container applications are resilient, scalable, and highly available. Understanding these concepts and effectively applying them will lead to a robust Kubernetes environment that keeps your containers running reliably.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.