K8S : Understanding port, targetPort, nodePort and containerPort
Deployment of Pod in a K8S cluster is not enough, you have to configure a service, ingress and an ingress-backed LoadBalancer to route the traffic to the app deployed in the pod. For the system to work properly, it is imperative to understand the concepts of various ports.
nodePort
(works at node level)
This setting
makes the service visible outside the Kubernetes cluster by the node’s IP
address and the port number declared in this property. The service also has to
be of type NodePort (if this field isn’t specified, Kubernetes will allocate a
node port automatically).
port(work
at service level)
Expose the
service on the specified port internally within the cluster. That is, the
service becomes visible on this port, and will send requests made to this port
to the pods selected by the service.
targetPort(work
at pod level)
This is the
port on the pod that the request gets sent to. Your application needs to be
listening for network requests on this port for the service to work.
containerPort(work
at container level) – (not in scope of Service object, defined in Deployment
object)
This is the port where process inside the container is being hosted.
Example -
Content of service manifest file:-
apiVersion: v1
kind: Service
metadata:
name: {{ include "apps.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "apps.name" . }}
helm.sh/chart: {{ include "apps.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: 20000
targetPort: 8084
nodePort: 30101
protocol: TCP
name: ui
selector:
app: {{ include "apps.fullname" . }}
Content of deployment manifest file:-
apiVersion: apps/v1
kind: Deployment
metadata:
name: appX-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: appA
spec:
containers:
- name: appA
image: "appA:v1.0"
imagePullPolicy: abcdPullSecret
ports:
- name: http
containerPort: 8084
protocol: TCP
livenessProbe:
tcpSocket:
port: 8084
initialDelaySeconds: 10
periodSeconds: 20
readinessProbe:
tcpSocket:
port: 8084
initialDelaySeconds: 15periodSeconds: 30
Comments
Post a Comment