Since people have explained port
and targetPort
in Kubernetes Service definition, I'll add information on how Dockerfile
, Kubernetes Deployment and Kubernetes Ingress come into picture because they are part of a common workflow.
Part 1 - Applications and their Dockerfile
Lets say you're running a Flask server on port 3000 and a Golang server on port 4000. When you containerize these applications using Docker, you'll have to expose ports 3000 and 4000 in their Dockerfile
s:
Python
Application
...
...
if __name__ == "__main__":
app.run(host='0.0.0.0', port=3000)
Dockerfile
FROM python:3.10-alpine
...
...
EXPOSE 3000
CMD ...
Golang
Application
...
...
func main() {
...
log.Fatal(http.ListenAndServe(":4000", nil))
}
Dockerfile
FROM golang:1.18-alpine
...
...
EXPOSE 4000
CMD ...
Part 2 - Dockerfile
s and Kubernetes Deployments
The exposed ports in Dockerfile
s will have to match the containerPort
in the deployment manifests.
Python Deployment Manifest
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-flask-api
spec:
...
...
app: flask-api
template:
metadata:
labels:
app: flask-api
spec:
containers:
- name: flask-api
image: ...
ports:
- containerPort: 3000
...
...
Golang Deployment Manifest
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-backend-api
spec:
...
...
app: go-api
template:
metadata:
labels:
app: go-api
spec:
containers:
- name: go-api
image: ...
ports:
- containerPort: 4000
...
...
Part 3 - Kubernetes Deployments and Services
The containerPort
in the deployment manifests will have to match the targetPort
in the service manifests
Python Service Manifest
apiVersion: v1
kind: Service
metadata:
name: flask-api-service
spec:
type: NodePort
selector:
app: flask-api
ports:
- port: 80
targetPort: 3000
Golang Service Manifest
apiVersion: v1
kind: Service
metadata:
name: go-api-service
spec:
type: NodePort
selector:
app: go-api
ports:
- port: 443
targetPort: 4000
Part 4 - Kubernetes Service and Ingress
The port
in the service manifest will have to match the port number
in the ingress
AWS Ingress for Python and Golang application
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: microservice-app-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
rules:
- host: foo.biz.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: go-api-service
port:
number: 443
- host: bar.biz.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: flask-api-service
port:
number: 80
Flow
An incoming request hits the ingress on a port number
The ingress forwards this request to the service port
The service port maps the port
to a targetPort
From the service targetPort
the request goes to the deployment containerPort
The deployment containerPort
is the application's Docker image, which has this corresponding port exposed in its Dockerfile
And finally, the exposed port in Dockerfile
sends the request to the application