Challenge, Medium,  on  KubernetesNetworking

Resolve a Container Port Conflict and Expose Multiple Ports Through a Single Service

Scenario

A Deployment named multi-container is deployed in namespace multi with two containers — primary using nginx:alpine and second using public.ecr.aws/nginx/nginx:stable-alpine.

Both containers have containerPort: 80 defined, which is causing the Pod to fail — two containers in the same Pod cannot listen on the same port using the same protocol.

A ConfigMap named nginx-conf already exists in the namespace with nginx configured to listen on port 80 over HTTP. This existing ConfigMap can be used by updating the listening port for the second container and mounting it into that container, thereby resolving the port conflict within the Pod.

The Deployment manifest is located at:

/home/laborant/multi.yaml

Task

  1. Edit ConfigMap nginx-conf in namespace multi and update the nginx listening port from 80 to 8888
  2. In the manifest, add a volume for nginx-conf ConfigMap and mount it into the second container at /etc/nginx/conf.d
  3. Update the second container's containerPort to 8888 in the manifest
  4. Apply the updated manifest
  5. Create a Service named multi-svc of type NodePort with the following port mapping:
    Port NameProtocolTargetPort
    primary-portTCP80
    second-portTCP8888

Hint 1
  • Use kubectl edit configmap nginx-conf -n multi to update the listening port
  • Edit /home/laborant/multi.yaml to add the volume mount and update the containerPort for the second container
  • Use kubectl apply -f /home/laborant/multi.yaml to apply the changes
  • Use kubectl create service nodeport or edit a manifest to create multi-svc with two named ports

See the official docs: Configure a Pod to Use a ConfigMap


Test Cases