<p>I am deploying a microservices application, I have two services in the backend tier. They are login and recipes. When I attempt to call the recipes service, I get the login service.</p>
<pre><code>Forwarded URL from service recipes-service: http://localhost:8082
Forwarded URL from service login-service: http://localhost:8081
</code></pre>
<p>If I visit <code>http://localhost:8082</code>, it is the login service. Can only one service be assigned to the backend? if so, how can I have multiple services (without say, ingress)?</p>
<h3>Here is the program structure:</h3>
<pre><code>kubernetes-manifests:
--- frontend_service.deployment.yaml
--- frontend_service.service.yaml 
--- login_service.deployment.yaml
--- login_service.service.yaml
--- recipes_service.deployment.yaml
--- recipes_service.service.yaml
</code></pre>
<p>login_service.service.yaml:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: login-service
  labels:
    app: smart-grocer-nodejs
    tier: backend
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs
    tier: backend
  ports:
  - port: 8081
    targetPort: http-server
</code></pre>
<p>recipes_service.service.yaml</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: recipes-service
  labels:
    app: smart-grocer-nodejs
    tier: backend
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs
    tier: backend
  ports:
  - port: 8082
    targetPort: http-server
</code></pre>
<p>Additionally, if I call kubectl logs  I get the following error:</p>
<pre><code>npm ERR! code ELIFECYCLE
npm ERR! errno 137
npm ERR! recipes@1.0.0 start: `node app.js`
npm ERR! Exit status 137
</code></pre>
<p>Although the src for recipes works outside of Kubernetes (both with and without the docker file)</p>
<p>It then states:</p>
<pre><code>npm ERR! A complete log of this run can be found in:
npm ERR!     /home/cnb/.npm/_logs/2020-11-18T18_08_27_622Z-debug.log
</code></pre>
<p>Although I do not know how to get to that file</p>

ZanyCactus
<p>A Kubernetes Service is an abstraction which defines a logical set of Pods running somewhere in your cluster, that all provide the same functionality.  Pods can be configured to talk to the Service, and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.</p>
<p>In both your services you have the same selectors, to distinguish put different labels into deployments then add corresponding selectors to  services. For example:</p>
<p><strong>login_service.deployment.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-login-deployment-name
spec:
  selector:
    matchLabels:
      app: smart-grocer-nodejs-login
  replicas: number-of-replicas
  template:
    metadata:
      labels:
        app: smart-grocer-nodejs-login
    spec:
      containers:
      - name: ...
        ...
</code></pre>
<p>corresponding service <strong>login_service.service.yaml:</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: login-service
  labels:
    app: smart-grocer-nodejs-login
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs-login
  ports:
  - port: 8081
    targetPort: http-server
</code></pre>
<p>This specification will create a Service which targets port <code>http-server</code> on any Pod of deployment with the <code>app: smart-grocer-nodejs-login</code> label, and expose it on an abstracted Service port (<code>targetPort</code>: is the port the container accepts traffic on, <code>port</code>: is the abstracted Service port, which can be any port other pods use to access the Service).  When the node receives a request on the static port &quot;selects&quot; pods with the label <code>app</code> set to <code>smart-grocer-nodejs-login</code> and forward the request to one of them.</p>
<p>for <strong>recipes_service.deployment.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-recipes-deployment-name
spec:
  selector:
    matchLabels:
      app: smart-grocer-nodejs-recipes
  replicas: number-of-replicas
  template:
    metadata:
      labels:
        app: smart-grocer-nodejs-recipes
    spec:
      containers:
      - ...
        ...
</code></pre>
<p>corresponding service <strong>recipes_service.service.yaml</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: recipes-service
  labels:
    app: smart-grocer-nodejs-recipes
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs-recipes
  ports:
  - port: 8082
    targetPort: http-server
</code></pre>
<p>This specification will create a Service which targets TCP port <code>http-server</code> on any Pod of deployment with the <code>app: smart-grocer-nodejs-recipes</code> label, and expose it on an abstracted Service port (<code>targetPort</code>: is the port the container accepts traffic on, <code>port</code>: is the abstracted Service port, which can be any port other pods use to access the Service).  When the node receives a request on the static port &quot;select&quot; pods with the label <code>app</code> set to <code>smart-grocer-nodejs-recipes</code> and forward the request to one of them.</p>
<p>Take a look: <a href="https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/" rel="nofollow noreferrer">application-connection-to-service</a>.</p>
