TIS0701 - Deployment Exposing the Same Port as the Service Not Found
A Deployment
exposing the same port as the Service
is not found.
To ensure proper traffic flow and connectivity between services and workloads, always ensure that the targetPort
in the Service
matches the containerPort
in the corresponding Deployment
.
Examples
-
Service and Deployment Port Mismatch
Service:
apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: example-namespace
spec:
ports:
- port: 80
targetPort: 8080Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: example-namespace
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
ports:
- containerPort: 9090Explanation: The
Service
is configured to target port8080
in the workload, but theDeployment
exposes port9090
. This mismatch means that the service cannot properly route traffic to the intended workload, resulting in failed connections.
Recommendation
Ensure the port definitions in the workload and service definitions match.
-
Update Deployment to Match the Service Port
Update the
Deployment
to expose the correct port that matches theService
definition.apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: example-namespace
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
ports:
- containerPort: 8080Explanation: By updating the
containerPort
to8080
, theDeployment
now matches thetargetPort
defined in theService
, allowing traffic to be correctly routed from the service to the workload.