<p>I am doing a lab about <em>kubernetes</em> in <em>google cloud</em>, so my task is deploy two <em>nginx</em> servers in one pod, however I have a issue.</p>

<p>One of the pods can not starts, as PORT or IP is using buy another nginx container, I need to change it in yaml file, please give me a solution, thank you in advance</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: first-container
    image: nginx
  - name: second-container
    image: nginx

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)

E  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

E  2019/01/21 11:04:47 [emerg] 1#1: still could not bind()

E  nginx: [emerg] still could not bind()
</code></pre>

<p>In kubernetes the container in pods share single network namespace. To simplify, two container cannot listen to same port, in same pod.</p>

<p>So in order to two nginx container within same pod, you need to run them on different port. One nginx can run on 80 and other on 81.</p>

<p>So we will run <code>first-container</code> with default nginx config and for <code>second-container</code> we will be running with below config. </p>

<ul>
<li>default.conf</li>
</ul>

<pre><code>server {
    listen       81;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

</code></pre>

<ul>
<li>Create a configmap from this <code>default.conf</code></li>
</ul>

<pre><code>kubectl create configmap nginx-conf --from-file default.conf
</code></pre>

<ul>
<li>Create a pod as following.</li>
</ul>

<pre><code>
apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  restartPolicy: Never
  volumes:
  - name: config
    configMap:
      name: nginx-conf
  containers:
  - name: first-container
    image: nginx
    ports:
    - containerPort: 80
  - name: second-container
    image: nginx
    ports:
    - containerPort: 81
    volumeMounts:
    - name: config
      mountPath: /etc/nginx/conf.d
</code></pre>

<ul>
<li><p>Deploy the pod.</p></li>
<li><p>Now exec into the pod and try to ping on <code>localhost:80</code> and <code>localhost:81</code> it will work.
Let me know, if you need any more help in it.</p></li>
</ul>
