title: Understanding kubernetes port forwarding, port and targetPort
body: <p>So, this is my understanding that <code>port</code> is the port on which a service serves requests and <code>targetPort</code> is the port where your app within the container servers requests.</p>
  <p><code>targetPort</code> is not even required to be specified, it is usually the same as <code>containerPort</code> (I got this from a <code>StackOverflow</code> answer and I verified it by removing <code>targetPort</code>)</p>
  <p>Kubernetes port forwarding forwards the request received on local port to the remote port (where the service runs). <a href="https://kubernetesbyexample.com/pf/" rel="nofollow noreferrer">I have been following this article to understand this better</a></p>
  <p>In my application, the specs were created using <code>helm</code>. My <code>port</code> is set to <code>80</code> and my <code>containerPort</code> is set to <code>5000</code> (It is a very simple Flask app).</p>
  <p>Following the example mentioned in that link the following should work:</p>
  <pre><code>kubectl --namespace default port-forward $POD_NAME 7000:80
  </code></pre>
  <p>but this one works instead:</p>
  <pre><code>kubectl --namespace default port-forward $POD_NAME 7000:5000
  </code></pre>
  <p>Is there something I've not understood correctly? Here's the <code>kubectl describe</code> of my pod:</p>
  <pre><code>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:    &lt;none&gt;
  Status:         Running
  IP:             10.1.2.39
  Controlled By:  ReplicaSet/mock-python-server-9f5b557f5
  Containers:
    master:
      Container ID:   docker://7e258d94c458f47c1add418c7969e77fbaa532c56df7405681e778d5f0e63d01
      Image:          &lt;image&gt;
      Image ID:       &lt;image_id&gt;
      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
  ...
  </code></pre>
  <p>Here's a snippet from <code>deployment.yaml</code>:</p>
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: {{ include &quot;master.fullname&quot; . }}
    labels:
      {{- include &quot;master.labels&quot; . | nindent 4 }}
  spec:
    replicas: {{ .Values.replicaCount }}
    selector:
      matchLabels:
        {{- include &quot;master.selectorLabels&quot; . | nindent 6 }}
    template:
      metadata:
        labels:
          {{- include &quot;master.selectorLabels&quot; . | nindent 8 }}
      spec:
      {{- with .Values.imagePullSecrets }}
        imagePullSecrets:
          {{- toYaml . | nindent 8 }}
      {{- end }}
  
        serviceAccountName: {{ include &quot;master.serviceAccountName&quot; . }}
        securityContext:
          {{- toYaml .Values.podSecurityContext | nindent 8 }}
        containers:
          - name: {{ .Chart.Name }}
            securityContext:
              {{- toYaml .Values.securityContext | nindent 12 }}
            image: &quot;{{ .Values.image.repository }}:{{ .Chart.AppVersion }}&quot;
            imagePullPolicy: {{ .Values.image.pullPolicy }}
            env:
              - name: env
                value: {{ .Values.environment }}
            ports:
              - name: http
                containerPort: 5000 #{{ .Values.flaskPort }}
                protocol: TCP
            livenessProbe:
              httpGet:
                path: /health
                port: http
            readinessProbe:
              httpGet:
                path: /health
                port: http
            resources:
              {{- toYaml .Values.resources | nindent 12 }}
        {{- with .Values.nodeSelector }}
        nodeSelector:
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.affinity }}
        affinity:
          {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
        tolerations:
          {{- toYaml . | nindent 8 }}
      {{- end }}
  
  </code></pre>
  <p>and my <code>service.yaml</code>:</p>
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: {{ include &quot;master.fullname&quot; . }}
    labels:
      {{- include &quot;master.labels&quot; . | nindent 4 }}
  spec:
    type: {{ .Values.service.type }}
    ports:
      - port: 80 #{{ .Values.service.port }}
        targetPort: 5000
        protocol: TCP
        name: http
    selector:
      {{- include &quot;master.selectorLabels&quot; . | nindent 4 }}
  
  </code></pre>
  <p>Where am I going wrong?</p>