So, this is my understanding that port
is the port on which a service serves requests and targetPort
is the port where your app within the container servers requests.
targetPort
is not even required to be specified, it is usually the same as containerPort
(I got this from a StackOverflow
answer and I verified it by removing targetPort
)
Kubernetes port forwarding forwards the request received on local port to the remote port (where the service runs). I have been following this article to understand this better
In my application, the specs were created using helm
. My port
is set to 80
and my containerPort
is set to 5000
(It is a very simple Flask app).
Following the example mentioned in that link the following should work:
kubectl --namespace default port-forward $POD_NAME 7000:80
but this one works instead:
kubectl --namespace default port-forward $POD_NAME 7000:5000
Is there something I've not understood correctly? Here's the kubectl describe
of my pod:
Name: mock-python-server-9f5b557f5-klxq8 Namespace: default Priority: 0 Node: docker-desktop/192.168.65.3 Start Time: Thu, 21 Jan 2021 13:59:40 -0800 Labels: app.kubernetes.io/instance=mock-python-server app.kubernetes.io/name=mock-python-server pod-template-hash=9f5b557f5 Annotations: <none> Status: Running IP: 10.1.2.39 Controlled By: ReplicaSet/mock-python-server-9f5b557f5 Containers: master: Container ID: docker://7e258d94c458f47c1add418c7969e77fbaa532c56df7405681e778d5f0e63d01 Image: <image> Image ID: <image_id> Port: 5000/TCP Host Port: 0/TCP State: Running Started: Thu, 21 Jan 2021 13:59:43 -0800 Ready: True Restart Count: 0 Limits: cpu: 100m memory: 128Mi Requests: cpu: 100m memory: 128Mi Liveness: http-get http://:http/health delay=0s timeout=1s period=10s #success=1 #failure=3 Readiness: http-get http://:http/health delay=0s timeout=1s period=10s #success=1 #failure=3 Environment: env: LOCAL ...
Here's a snippet from deployment.yaml
:
apiVersion: apps/v1 kind: Deployment spec: template: spec: containers: ports: - name: http containerPort: 5000 protocol: TCP livenessProbe: httpGet: path: /health port: http readinessProbe: httpGet: path: /health port: http
and my service.yaml
:
apiVersion: v1 kind: Service spec: type: ports: - port: 80 targetPort: 5000 protocol: TCP name: http
Where am I going wrong?
https://stackoverflow.com/questions/65837023/understanding-kubernetes-port-forwarding-port-and-targetport January 22, 2021 at 07:01AM
没有评论:
发表评论