<p>This is the way I understand the flow in question:</p>

<ol>
<li>When requesting a kubernetes service (via http for example) I am using port 80.</li>
<li>The request is forwarded to a pod (still on port 80)</li>
<li>The port forwards the request to the (docker) container that exposes port 80</li>
<li>The container handles the request</li>
</ol>

<p>However my container exposes a different port, let's say 3000.
How can make a port mapping like 80:3000 in step 2 or 3?</p>

<p>There are confusing options like <code>targetport</code> and <code>hostport</code> in the kubernetes docs which didn't help me. <code>kubectl port-forward</code> seems to forward only my local (development) machine's port to a specific pod for debugging.</p>

<p>These are the commands I use for setting up a service in the google cloud:</p>

<pre><code>kubectl run test-app --image=eu.gcr.io/myproject/my_app --port=80
kubectl expose deployment test-app --type="LoadBalancer"
</code></pre>

<p>I found that I needed to add some arguments to my second command:</p>

<pre><code>kubectl expose deployment test-app --type="LoadBalancer" --target-port=3000 --port=80
</code></pre>

<p>This creates a service which directs incoming http traffic (on port 80) to its pods on port 3000.</p>

<p>A nicer way to do this whole thing is with yaml files <code>service.yaml</code> and <code>deployment.yaml</code> and calling</p>

<pre><code>kubectl create -f deployment.yaml
kubectl create -f service.yaml
</code></pre>

<p>where the files have these contents</p>

<pre><code># deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: test-app
    spec:
      containers:
        - name: user-app
          image: eu.gcr.io/myproject/my_app
          ports:
            - containerPort: 3000
</code></pre>

<p>and</p>

<pre><code># service.yaml
apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    app: test-app
  ports:
  - port: 80
    targetPort: 3000
  type: LoadBalancer
</code></pre>

<p>Note that the selector of the service must match the label of the deployment.</p>
