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
- Edit ConfigMap
nginx-confin namespacemultiand update the nginx listening port from80to8888 - In the manifest, add a volume for
nginx-confConfigMap and mount it into thesecondcontainer at/etc/nginx/conf.d - Update the
secondcontainer'scontainerPortto8888in the manifest - Apply the updated manifest
- Create a Service named
multi-svcof typeNodePortwith the following port mapping:Port Name Protocol TargetPort primary-portTCP 80second-portTCP 8888
Hint 1
- Use
kubectl edit configmap nginx-conf -n multito update the listening port - Edit
/home/laborant/multi.yamlto add the volume mount and update the containerPort for thesecondcontainer - Use
kubectl apply -f /home/laborant/multi.yamlto apply the changes - Use
kubectl create service nodeportor edit a manifest to createmulti-svcwith two named ports
See the official docs: Configure a Pod to Use a ConfigMap