<p>I am basically trying to run a react js app which is mainly composed of 3 services namely postgres db, API server and UI frontend(served using nginx).Currently the app works as expected in the development mode using docker-compose but when i tried to run this in the production using kubernetes,I was not able to access the api server of the app(CONNECTION REFUSED). </p>

<p>Since I want to run in this in production using kubernetes, I created yaml files for each of the services and then tried running them using kubectl apply.I have also tried this with and without using the persistent volume for the api server.But none of this helped.</p>

<p>Docker-compose file(This works and i am able to connect to api server at port 8000)</p>

<pre><code>version: "3"

services:
  pg_db:
    image: postgres
    networks: 
      - wootzinternal
    ports:
      - 5432
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=postgres
      - POSTGRES_DB=wootz
    volumes:
      - wootz-db:/var/lib/postgresql/data
  apiserver:
    image: wootz-backend
    volumes:
      - ./api:/usr/src/app
      - /usr/src/app/node_modules
    build:
      context: ./api
      dockerfile: Dockerfile
    networks: 
      - wootzinternal
    depends_on:
      - pg_db
    ports:
      -  '8000:8000'
  ui:
    image: wootz-frontend
    volumes:
      - ./client:/usr/src/app
      - /usr/src/app/build
      - /usr/src/app/node_modules
    build:
      context: ./client
      dockerfile: Dockerfile
    networks:
      - wootzinternal
    ports:
      - '80:3000'

volumes:
  wootz-db:

networks:
  wootzinternal:
    driver: bridge

</code></pre>

<p>My api server yaml for running in kubernetes(This doesn't work and I cant connect to the api server at port 8000)</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: apiserver
  labels:
    app: apiserver
spec:

  ports:
  - name: apiport
    port: 8000
    targetPort: 8000

  selector:
    app: apiserver
    tier: backend    

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apiserver
  labels:
    app: apiserver
spec:
  selector:
    matchLabels:
      app: apiserver
      tier: backend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: apiserver
        tier: backend
    spec:

      containers:
      - image: suji165475/devops-sample:corspackedapi
        name: apiserver
        env:
        - name: POSTGRES_DB_USER
          value: postgres
        - name: POSTGRES_DB_PASSWORD
          value: password
        - name: POSTGRES_DB_HOST
          value: postgres
        - name: POSTGRES_DB_PORT
          value: "5432"
        ports:
        - containerPort: 8000
          name: myport
</code></pre>

<p>What changes should I make to my api server yaml(kubernetes). so that I can access it on port 8000. Currently I am getting a connection refused error.</p>

<p>The default service on Kubernetes is <code>ClusterIP</code> that is used to have service inside the cluster but not having that exposed to outside.</p>

<p>That is your service using the <code>LoadBalancer</code> type:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: apiserver
  labels:
    app: apiserver
spec:
  type: LoadBalancer
  ports:
  - name: apiport
    port: 8000
    targetPort: 8000
  selector:
    app: apiserver
    tier: backend
</code></pre>

<p>With that, you can see how the service expects to have an external IP address by running <code>kubectl describe service apiserver</code></p>

<p>In case you want to have more control of how your requests are routed to that service you can add an Ingress in front of that same service.</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    app: apiserver
  name: apiserver
spec:
  rules:
  - host: apiserver.example.com
    http:
      paths:
      - backend:
          serviceName: apiserver
          servicePort: 8000
        path: /*
</code></pre>
