Files

4.3 KiB
Raw Permalink Blame History

Assignment

There is an application that needs to be deployed on Kubernetes cluster under Apache web server. The Nautilus application development team has asked the DevOps team to deploy it. We need to develop a template as per requirements mentioned below:

Create a namespace named as httpd-namespace-nautilus.

Create a deployment named as httpd-deployment-nautilus under newly created namespace. For the deployment use httpd image with latest tag only and remember to mention the tag i.e httpd:latest, and make sure replica counts are 2.

Create a service named as httpd-service-nautilus under same namespace to expose the deployment, nodePort should be 30004.

Note: The kubectl utility on the controlplane has been configured to work with the Kubernetes cluster.

Solution

Apache on Kubernetes — namespace + deployment + service (nautilus)

An httpd Deployment (2 replicas) exposed via a NodePort Service, all inside a dedicated namespace. One multi-document heredoc — no manifest file on disk.

Apply (heredoc → kubectl)

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

kubectl rollout status deployment/httpd-deployment-nautilus -n httpd-namespace-nautilus

How it works

The heredoc apply pattern

  • kubectl apply -f - reads from stdin; --- separates the three documents, applied in order so the Namespace exists before the Deployment and Service land in it.
  • <<'EOF' (delimiter quoted) keeps the manifest literal — the right default for k8s YAML.

Everything scoped to one namespace

All three objects carry namespace: httpd-namespace-nautilus (the Namespace defines it; the Deployment and Service reference it). Creating the namespace first in the same stream avoids a namespaces "..." not found error, and same-namespace scoping lets the Service select the deployment's pods by label directly.

The Deployment

  • name: httpd-deployment-nautilus, image: httpd:latest (tag stated explicitly), replicas: 2 — all exactly as required.
  • containerPort: 80 — Apache serves on 80 inside the container.
  • app: httpd label on the template — ties the Deployment to its pods and the Service to those pods. Container name httpd-container is a free choice.
  • selector.matchLabels: app: httpd equals the template labels — the Deployment↔pod link.

The Service — targetPort 80

  • type: NodePort exposes Apache outside the cluster.

  • selector: app: httpd targets the deployment's two pods by label and load-balances across them.

  • Port mapping:

    • port: 80 — the Service's ClusterIP port.
    • targetPort: 80 — the container port; must be 80 to reach Apache.
    • nodePort: 30004 — the external port on each node, exactly as required (valid 3000032767 range).

    Path: <node-ip>:30004 → Service :80 → one of the two pods :80.

Verify

# Both replicas running in the namespace
kubectl get deployment httpd-deployment-nautilus -n httpd-namespace-nautilus
kubectl get pods -n httpd-namespace-nautilus -l app=httpd

# Service on 30004 with two endpoints
kubectl get service httpd-service-nautilus -n httpd-namespace-nautilus
kubectl get endpoints httpd-service-nautilus -n httpd-namespace-nautilus

Expected — deployment READY 2/2, both pods Running, httpd-service-nautilus NodePort 80:30004/TCP with two endpoints, and Apache reachable at <node-ip>:30004.

Everything is in httpd-namespace-nautilus — keep -n httpd-namespace-nautilus on every command. If endpoints is empty, the service selector doesn't match the pod labels.