<p>I managed to create and deploy a k8s cluster with minikube, running 4 replicas of a simple hello-world node.js app, using the following configuration.</p>

<p>Dockerfile for the app:</p>

<pre><code>FROM ubuntu:latest

RUN apt-get update
RUN apt-get -qq update
RUN apt-get install -y nodejs npm
# TODO could uninstall some build dependencies

# debian installs `node` as `nodejs`
RUN update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10

COPY package.json package.json

RUN npm install

COPY . .

CMD ["node", "app.js"]
</code></pre>

<p>k8s Deployment yml:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-deployment
  labels:
    app: node-app
spec:
  replicas: 4
  selector:
    matchLabels:
      app: node-app
  template:
    metadata:
      labels:
        app: node-app
    spec:
      containers:
      - name: node-app
        image: my-repo/ubuntu-node:sectry 
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: node-service
  labels:
    app: node-app
spec:
  type: NodePort
  ports:
  - port: 3000
    protocol: TCP
  selector:
    app: node-app
</code></pre>

<p>My question is how can add an nginx container for it? I know k8s cluster already load-balancing, but I really wish to use NGINX features.</p>

<p>I guess you want to use nginx as a reverse proxy to your Node.js app. If it is the case, you can use one of the following ways to achieve it. </p>

<p><strong>Option 1</strong></p>

<ol>
<li><p>Build nginx and your node.js app into one Docker image. In this image, configure nginx as a reverse proxy and forward the request to your node.js app. For example, the following nginx configure forwards the request to port 3000 in the same container. </p>

<pre><code>server {
    listen      80;
    server_name localhost;

    location / {
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass  http://127.0.0.1:3000;
    }
}
</code></pre></li>
<li><p>You can then deploy this image to k8s cluster, and create a service for it. </p></li>
</ol>

<p><strong>Option 2</strong></p>

<ol>
<li>Create 2 docker images: 1 for nginx and 1 for your node.js.</li>
<li>Deploy both of them to k8s and create a service for each of them. Use <code>ClusterIP</code> as the service type for node.js image, and <code>LoadBalancer</code> for nginx image. </li>
<li>Configure nginx as the reverse proxy, and forward the request to the corresponding cluster ip of the service for node.js image. </li>
</ol>

<p>To test it on minikube, Option 1 is easier. Option 2 is recommended for a production k8s cluster. </p>
