Search code examples
kubernetesamazon-eksamazon-elbelastic-ip

How to provide elastic ip to aws eks for external service with type loadbalancer?


I am using eks 1.16. I have one service which has type Loadbalancer with internet-facing.

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
  name: kafka-test-3-0-external
  labels:
    helm.sh/chart: kafka-0.21.5
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: kafka-broker
    app.kubernetes.io/name: kafka
    app.kubernetes.io/instance: kafka-test-3
    pod: "kafka-test-3-0"
spec:
  type: LoadBalancer
  ports:
    - name: external-broker
      port: 19092
      targetPort: 19092
      protocol: TCP
#
  selector:
    app.kubernetes.io/component: kafka-broker
    app.kubernetes.io/name: kafka
    app.kubernetes.io/instance: kafka-test-3
    statefulset.kubernetes.io/pod-name: "kafka-test-3-0"

I want to provide elastic-ip/static-ip to that external service.

Problem: With Loadbalancer type, when I delete the service it will delete loadbalancer. When I install once again then it will create new load balancer with new ip.

Usecase: If I will uninstall/delete that service still the IP must be same.

How can I provide elastic-ip/static-ip?


Solution

  • There is one annotation to provide elastic-ip for network-load-balancer

    service.beta.kubernetes.io/aws-load-balancer-eip-allocations: eipalloc-05666791973f6a240

    If you're using Amazon EKS 1.16 or later, you can assign Elastic IP addresses to the Network Load Balancer by adding the following annotation. Replace the (including <>) with the Allocation IDs of your Elastic IP addresses. The number of Allocation IDs must match the number of subnets used for the load balancer.

    Note: You can not use this annotation for the internal load balancer.

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        external-dns.alpha.kubernetes.io/hostname: "kafka-test-3-1.kafka.internal"
        external-dns.alpha.kubernetes.io/ttl: "60"
        service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp
        service.beta.kubernetes.io/aws-load-balancer-type: nlb
        service.beta.kubernetes.io/aws-load-balancer-eip-allocations: eipalloc-022b9722973f6a222
      name: kafka-test-3-1-external
      labels:
        helm.sh/chart: kafka-0.21.5
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/component: kafka-broker
        app.kubernetes.io/name: kafka
        app.kubernetes.io/instance: kafka-test-3
        pod: "kafka-test-3-1"
    spec:
      type: LoadBalancer
      ports:
        - name: external-broker
          port: 19092
          targetPort: 19092
          protocol: TCP
    #
      selector:
        app.kubernetes.io/component: kafka-broker
        app.kubernetes.io/name: kafka
        app.kubernetes.io/instance: kafka-test-3
        statefulset.kubernetes.io/pod-name: "kafka-test-3-1"
    

    It will always use elasticIp for that loadbalancer.

    For more eks annotations.

    https://docs.aws.amazon.com/eks/latest/userguide/load-balancing.html