assign static IP to LoadBalancer service using k8s on aws

10/22/2017

Objective: create a k8s LoadBalancer service on AWS whose IP is static

I have no problem accomplishing this on GKE by pre-allocating a static IP and passing it in via loadBalancerIP attribute:

$ kubectl apply -f - <<EOF
apiVersion: v1
kind: Service
metadata:
  name: dave
loadBalancerIP: 17.18.19.20
...etc...

But doing same in AWS results in externalIP stuck as <pending> and an error in the Events history

Removing the loadBalancerIP value allows k8s to spin up a Classic LB:

$ kubectl describe svc dave
Type:                   LoadBalancer
IP:                     100.66.51.123
LoadBalancer Ingress:   ade4d764eb6d511e7b27a06dfab75bc7-1387147973.us-west-2.elb.amazonaws.com
...etc...

but AWS explicitly warns me that the IPs are ephemeral (there's sometimes 2), and Classic IPs don't seem to support attaching static IPs

Thanks for your time

-- 333kenshin
amazon-ec2
kubernetes

1 Answer

7/23/2019

as noted by @Quentin, AWS Network Load Balancer now supports K8s

https://aws.amazon.com/blogs/opensource/network-load-balancer-support-in-kubernetes-1-9/

Network Load Balancing in Kubernetes

Included in the release of Kubernetes 1.9, I added support for using the new Network Load Balancer with Kubernetes services. This is an alpha-level feature, and as of today is not ready for production clusters or workloads, so make sure you also read the documentation on NLB before trying it out. The only requirement to expose a service via NLB is to add the annotation service.beta.kubernetes.io/aws-load-balancer-type with the value of nlb.

A full example looks like this:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
spec:
  externalTrafficPolicy: Local
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
type: LoadBalancer
-- 333kenshin
Source: StackOverflow