<p>So I've got my backend and front-end as seperate containers in the one <strong>Kubernetes Deployment</strong>.</p>

<p>At the moment I'm having to access the front-end &amp; backend via different <strong>ports</strong>. </p>

<p>E.g <code>example.com:5000 = frontend</code> &amp; <code>example.com:7000 = backend</code></p>

<p>I'm wondering how I can setup my front-end container to run on <code>www.example.com</code> &amp; my backend container to run on <code>api.example.com</code></p>

<p>I'm using gcp (google cloud), have setup my dns properly &amp; am having to access the services (web apps) using the ports I assigned to each of them (5000=frontend,7000=backend).</p>

<p>I'm thinking of a possible solution which is manual, but am wondering whether there is something built into <strong>Kubernetes</strong>. This solution would be:</p>

<p>I'd setup an nginx container in my Kubernetes cluster that would run on port 80, so any request that comes through would be redirected to the appropriate ports:</p>

<p>E.g I could have <code>api.example.com point to &lt;my_cluster_ip&gt;/backend</code> &amp; the same for my front-end <code>&lt;my_cluster_ip&gt;/frontend</code> and let nginx point /backend to <strong>port 5000</strong> and /frontend to <strong>port 7000</strong></p>

<p>I'm hoping there is something built into kubernetes that I can use? Here is my deployment config as it stands:</p>

<pre><code>{
  "apiVersion": "extensions/v1beta1",
  "kind": "Deployment",
  "metadata": {
    "name": "my_container"
  },
  "spec": {
    "replicas": 1,
    "template": {
      "metadata": {
        "labels": {
          "app": "my_app"
        }        
      },
      "spec": {
        "containers": [
          {
            "name": "backend",
            "image": "backend_url",
            "ports": [
              {
                "containerPort": 7000
              }
            ],
            "imagePullPolicy": "Always",
            "env": [
              {
                "name": "NODE_PORT",
                "value": "7000"
              },
              {
                "name": "NODE_ENV",
                "value": "production"
              }
            ]
          },
          {
            "name": "frontend",
            "image": "frontend_url",
            "ports": [
              {
                "containerPort": 5000
              }
            ],
            "imagePullPolicy": "Always",
            "env": [
              {
                "name": "PORT",
                "value": "5000"
              },
              {
                "name": "NODE_ENV",
                "value": "production"
              }
            ]
          }
        ]
      }
    }
  }
}
</code></pre>

<p>Well, for starters, you should not base exposing your service on Deployment. To do that, you should cover your Deployment(s) with Service(s). Read up on <a href="http://kubernetes.io/docs/user-guide/services/" rel="nofollow noreferrer">http://kubernetes.io/docs/user-guide/services/</a> for that.</p>

<p>When you go through the lecture, you might notice that it is perfectly possible to set two services that match the same backing pods (Endpoints) but on different port (ie. front:80->5000 api:80->7000). The problem is, that it still is exposing your work only inside the k8s cluster. To publish it externally you can use Service of type NodePort or LoadBalancer (first one has the disadvantage of using high ports to expose your services to the public, second one will be a separate LB (hence IP) per service).</p>

<p>What I personally prefer to publicly expose services is using Ingress/IngressController <a href="http://kubernetes.io/docs/user-guide/ingress/" rel="nofollow noreferrer">http://kubernetes.io/docs/user-guide/ingress/</a></p>

<p>Finally, when you split your solution with two services (front/api) you will see that there is no real reason to keep them together in one deployment/pod. If you separate them as two distinct deployments, you will get a more flexible architecture, and more fine grained control over your solution.</p>
