This guide walks through deploying a containerized HTTP service on a Kubernetes cluster, covering container images, deployments, services, and ingres configuration.
Building and Pushing the Container Image
First, build the container image for your HTTP server and push it to a container registry. In this example, the image chengfengsunce/httpserver:0.0.1 is pushed to Docker Hub.
Creating the Deployment
A Deployment manages replica pods and ensures the desired number of instances remain running. Below is the YAML configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpserver
labels:
app: httpserver
spec:
replicas: 3
selector:
matchLabels:
app: httpserver
template:
metadata:
labels:
app: httpserver
spec:
containers:
- name: httpserver
image: chengfengsunce/httpserver:0.0.1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 10
resources:
requests:
cpu: 20m
memory: 20Mi
limits:
cpu: 200m
memory: 100Mi
Deploy it using:
kubectl create -f deployment.yaml
Verify the Deployment status:
kubectl get deployment
Creating the Service
A Service provides network acess to the pod group. Create a Service resource for internal cluster communication:
apiVersion: v1
kind: Service
metadata:
name: product-server
spec:
ports:
- name: http
port: 80
targetPort: 80
- name: grpc
port: 81
targetPort: 81
selector:
app: product-server
type: ClusterIP
Apply and verify:
kubectl create -f service.yaml
kubectl get service
Sample output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
product-server ClusterIP 10.107.6.57 <none> 80/TCP,81/TCP 6h10m
Tip: Deployment and Service definitions can be combined in a single YAML file separated by ---.
Configuring Ingress for External Access
To expose services externally, install and configure the ingress-nginx controller. Apply the ingress controller deployment:
kubectl create -f ingress-nginx.yaml
Check the controller pod status:
kubectl get pods -n ingress-nginx
Create a Ingress resource to route external traffic:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-web
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: product.com
http:
paths:
- path: /
backend:
serviceName: product-server
servicePort: 80
- path: /grpc
backend:
serviceName: product-server
servicePort: 81
Deploy and verify:
kubectl create -f ingress.yaml
kubectl get ingress
Sample output:
NAME HOSTS ADDRESS PORTS AGE nginx-web product.com 80 8h
<h2>Verifying Self-Healing Behavior</h2>
<p>Kubernetes automatically replaces failed pods. List all pods in the default namespace:</p>
<code>kubectl get pods</code>
<p>Delete a specific pod to test recovery:</p>
<code>kubectl delete pods product-server-599cfd85cc-ppmhx</code>
<p>Immediately check pod status again:</p>
<code>kubectl get pods</code>
<p>The deleted pod shows terminating status while a new one spawns to maintain the replica count. The Deployment controller detects the pod loss and schedules a replacement, demonstrating the self-healing capability of Kubernetes.</p>