How to use a host IP from a container?

8/30/2018

In a container running with host networking option it is possible to use a host network interface and its IP from the container and contact external network from this interface and IP. So if a host has several IPs configured, the container can choose which one it uses.

Can I have a similar setup with Kubernetes and let a container use an host IP ?

NB: I need the process to contact an external service from specific IPs, I dont necessarily need those IPs to be assigned to a container from an external view.

-- Stéphane Busso
kubernetes
networking

3 Answers

8/31/2018

hostNetwork=true in pod specification exposes host network to the pod, and container can access network interfaces:

apiVersion: v1
kind: Pod
metadata:
  name: lookup
spec:
  hostNetwork: true
  containers:
    - name: lookup
      image: sbusso/lookup_ips:latest
      ports:
      - containerPort: 9000

To test it: kubectl port-forward lookup 9000 and then go to http://127.0.0.1:9000/ and get network interfaces details:

lo
- 127.0.0.1/8
- ::1/128
eth0
- 10.0.2.15/24
- fe80::a00:27ff:fea1:6e61/64
eth1
- 192.168.99.101/24
- fe80::a00:27ff:fe77:d179/64

Note this option is not recommended in Kubernetes good practices: https://kubernetes.io/docs/concepts/configuration/overview/#services

-- Stéphane Busso
Source: StackOverflow

8/30/2018

As I wrote in Egress IP adress selection :

One of the things that could help you solve it is Istio Egress Gateway so I suggest you look into it.

Otherwise, it is still dependent on particular platform and way to deploy your cluster. For example on AWS you can make sure your egress traffic always leaves from predefined, known set of IPs by using instances with Elastic IPs assigned to forward your traffic (be it regular EC2s or AWS NAT Gateways). Even with Egress above, you need some way to define a fixed IP for this, so AWS ElasticIP (or equivalent) is a must.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow

8/30/2018

I will try to give this a shot. I hope i understand your question. You only need to connect to an external ip from inside a pod/container.

We implemented this by creating a kubernetes service without a selector.

kind: Service 
apiVersion: v1 
metadata:
  name: your-external-system
    

Followed by creating an Endpoint Object with the same name as your service.

kind: Endpoints 
apiVersion: v1 
metadata: 
  name: your-external-system
  subsets: 
    - addresses: 
       - ip: 192.168.0.1 
       ports: 
       - port: 3306
    

Finally in your pod, you can simply refer to the service name in this case (your-external-system)

-- Bal Chua
Source: StackOverflow