Inter container communication in Kubernetes multi container Pod

3/8/2019

I have a pod with 3 containers A, B, and C. I would like to access service in Container A and B from C. Neither localhost:<port> is working nor the 127.0.0.1.

my yaml

apiVersion: "v1"
kind: Pod
metadata:
  name: web3
  labels:
    name: web
    app: demo
spec:
  containers:
    - name: client
      image: ubuntu
      command: ['cat']
      tty: true
    - name: apache1
      image: nimmis/apache-php5
      ports:
        - containerPort: 8080
          name: apacheport1
          protocol: TCP
    - name: apache2
      image: nimmis/apache-php5
      command: ['cat']
      tty: true
      ports:
        - containerPort: 8088
          name: apacheport2
          protocol: TCP

what I am doing

kubectl apply -f example.yaml
kubectl exec -it web3 -c client bash

and then try to reach other 2 services

root@web3:/# curl http://localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
root@web3:/# curl http://localhost:8088
curl: (7) Failed to connect to localhost port 8088: Connection refused
root@web3:/# curl http://localhost:80

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <!--
    Modified from the Debian original for Ubuntu

Questions How to make the first 2 curl work. (I do not want to use the service since my use case is only for testing purpose) Why there is an open port 80 when I haven't exposed the port.

-- Ram Kamath
kubernetes
kubernetes-pod

1 Answer

3/8/2019

The point is that with nimmis/apache-php5 Apache is listening on port 80. So, it's port 80 which is exposed. Through containerPort: <P> you are not saying to expose container's port 80 to <P>, but rather exposing port <P> itself. Also, as written in the docs, Not specifying a port here DOES NOT prevent that port from being exposed..

I did not find a way to map an internal container port to a different port in the pod. However, you may map the internal container port to a host port through field hostPort.

apiVersion: "v1"
kind: Pod
metadata:
name: web3
labels:
name: web
app: demo
spec:
containers:
- name: client
  image: ubuntu
  command: ['cat']
  tty: true
- name: apache1
  image: nimmis/apache-php5
  ports:
    - containerPort: 80
      name: apacheport1
      hostPort: 8002
      protocol: TCP
- name: apache2
  image: nimmis/apache-php5
  command: ['cat']
  tty: true
  ports:
    - containerPort: 80
      hostPort: 8001
      name: apacheport2
      protocol: TCP

Then you get the IP of the node, e.g., on Minikube

$ minikube ip  # e.g., 192.168.97.100

and check that, from client, you can access the Apache services:

$ kubectl exec -it web3 -c client bash
# apt-get update && apt-get install curl
# curl 192.168.99.100:8002 
-- metaphori
Source: StackOverflow