Assign External IP to a Kubernetes Service

Jérôme Pin picture Jérôme Pin · Jun 13, 2017 · Viewed 47.8k times · Source

EDIT: The whole point of my setup is to achieve (if possible) the following :

  • I have multiple k8s nodes
  • When I contact an IP address (from my company's network), it should be routed to one of my container/pod/service/whatever.
  • I should be able to easily setup that IP (like in my service .yml definition)

I'm running a small Kubernetes cluster (built with kubeadm) in order to evaluate if I can move my Docker (old)Swarm setup to k8s. The feature I absolutely need is the ability to assign IP to containers, like I do with MacVlan.

In my current docker setup, I'm using MacVlan to assign IP addresses from my company's network to some containers so I can reach directly (without reverse-proxy) like if it's any physical server. I'm trying to achieve something similar with k8s.

I found out that:

  • I have to use Service
  • I can't use the LoadBalancer type, as it's only for compatible cloud providers (like GCE or AWS).
  • I should use ExternalIPs
  • Ingress Resources are some kind of reverse proxy ?

My yaml file is :

apiVersion: apps/v1beta1
kind: Deployment
metadata:
      name: nginx-deployment
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
      nodeSelector:
        kubernetes.io/hostname: k8s-slave-3
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  externalIPs: 
    - A.B.C.D

I was hopping that my service would get the IP A.B.C.D (which is one of my company's network). My deployment is working as I can reach my nginx container from inside the k8s cluster using it's ClusterIP.

What am I missing ? Or at least, where can I find informations on my network traffic in order to see if packets are coming ?

EDIT :

$ kubectl get svc
NAME            CLUSTER-IP     EXTERNAL-IP       PORT(S)   AGE
kubernetes      10.96.0.1      <none>            443/TCP   6d
nginx-service   10.102.64.83   A.B.C.D           80/TCP    23h

Thanks.

Answer

Chuk Lee picture Chuk Lee · Jan 10, 2019

If this is just for testing, then try

kubectl port-forward service/nginx-service 80:80

Then you can

curl http://localhost:80