Skip to content

Kubernetes Level 03 Day 01: Deploy Apache Web Server

This document outlines the solution for Kubernetes Level 03 Day 01. The objective was to provision a highly available Apache web server (httpd) within a dedicated namespace and expose it via a specific NodePort for external access.

Table of Contents


Task Overview

Objective: Deploy an Apache stack in a custom namespace with specific port mapping.

  • Namespace: httpd-namespace-nautilus
  • Deployment:
    • Name: httpd-deployment-nautilus
    • Replicas: 2
    • Image: httpd:latest
  • Service:
    • Name: httpd-service-nautilus
    • Type: NodePort
    • NodePort: 30004

Step-by-Step Solution

1. Create the Namespace

The first step is to create the logical isolation layer for the Apache deployment.

Command:

kubectl create namespace httpd-namespace-nautilus

2. Define the Application Manifest

We will create a single YAML file containing both the Deployment and the Service. This ensures that the Pod labels and Service selector are perfectly aligned within the same namespace.

Command:

vi apache-deploy.yaml

Content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment-nautilus
  namespace: httpd-namespace-nautilus
  labels:
    app: apache-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apache-app
  template:
    metadata:
      labels:
        app: apache-app
    spec:
      containers:
      - name: httpd-container
        image: httpd:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: httpd-service-nautilus
  namespace: httpd-namespace-nautilus
spec:
  type: NodePort
  selector:
    app: apache-app
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30004

3. Apply and Verify

Apply the manifest to the cluster.

Command:

kubectl apply -f apache-deploy.yaml

Verify the Deployment:

kubectl get all -n httpd-namespace-nautilus
* Expected Output: * 2 Pods in Running state. * 1 Deployment showing 2/2 available. * 1 Service showing type NodePort with mapping 80:30004/TCP.

Test Accessibility: You can test the deployment by curling the NodePort from the control plane:

curl http://localhost:30004
# Result: <html><body><h1>It works!</h1></body></html>


Deep Dive: Component Breakdown

The Namespace Constraint

By specifying namespace: httpd-namespace-nautilus in the metadata of both resources, we ensure they are grouped together. If you run kubectl get pods without the -n flag, you will not see these pods, as they are isolated from the default namespace.

Replica Management

Setting replicas: 2 ensures high availability. If one Apache pod crashes or the node it is running on fails, the Deployment controller will automatically spin up a new instance to maintain the desired count of 2.

NodePort Mapping

The Service acts as the entry point. * port: 80: The internal port other services in the cluster use to talk to Apache. * targetPort: 80: The port inside the container where Apache is listening. * nodePort: 30004: The static port opened on every node's IP address to allow traffic from outside the Kubernetes cluster.