更新时间:2024-10-16 GMT+08:00
分享

为负载均衡类型的Service配置自定义EIP

通过CCE自动创建的带有EIP的ELB,可以通过添加Service的annotation(kubernetes.io/elb.custom-eip-id)完成ELB的EIP的自定义配置。

前提条件

  • 已创建Kubernetes集群,且集群版本满足以下要求:
    • v1.23集群:v1.23.18-r0及以上
    • v1.25集群:v1.25.13-r0及以上
    • v1.27集群:v1.27.10-r0及以上
    • v1.28集群:v1.28.8-r0及以上
    • v1.29集群:v1.29.4-r0及以上
    • v1.30集群:v1.30.1-r0及以上
  • 您需要使用kubectl连接到集群,详情请参见通过kubectl连接集群

约束与限制

  • 自定义EIP仅支持Service更新场景下配置,且Service的annotation中包含kubernetes.io/elb.eip-id。
  • 自定义的EIP必须是未绑定状态。
  • 配置自定义EIP后,如果ELB上的已有EIP是由CCE创建ELB时自动创建的且未被其他资源使用时,删除Service时会自动将EIP删除;如果ELB上的已有EIP是由您手动创建,删除Service时仅解绑EIP,您需要手动删除原先的EIP。

通过kubectl命令行创建

  1. 请参见通过kubectl连接集群,使用kubectl连接集群。
  2. 在创建Service时自动创建一个使用EIP的ELB,详情请参见通过kubectl命令行创建-自动创建ELB

    以使用独享型ELB的Service场景为例,查看该Service的YAML配置如下:
    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kubernetes.io/elb.autocreate: '{"type":"public","bandwidth_name":"aaaaa","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_g-vm","name":"xxx","available_zone":["xxx"],"elb_virsubnet_ids":["fc0c61cd-c987-49c4-99a4-b7d816b57581"],"l7_flavor_name":"","l4_flavor_name":"L4_flavor.elb.pro.max","vip_subnet_cidr_id":"cf35b03f-c6ca-4f75-aa70-e2166cb1f800"}'
        kubernetes.io/elb.eip-id: 8560972c-2cc5-4699-94d6-e46f146eb73d     # 表示创建ELB时自动创建的EIP的ID
        kubernetes.io/elb.class: performance
        kubernetes.io/elb.id: 0e78a84a-7deb-4747-aeb6-09b6a820b001
      labels:
        app: test-svc
        version: v1
      name: test-eip
      namespace: default
    spec:
      allocateLoadBalancerNodePorts: true
      clusterIP: 10.247.93.235
      clusterIPs:
      - 10.247.93.235
      externalTrafficPolicy: Cluster
      internalTrafficPolicy: Cluster
      ipFamilies:
      - IPv4
      ipFamilyPolicy: SingleStack
      loadBalancerIP: *.*.*.*
      ports:
      - name: cce-service-0
        nodePort: 31354
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: test-svc
        version: v1
      sessionAffinity: None
      type: LoadBalancer
    status:
      loadBalancer:
        ingress:
        - ip: *.*.*.*
        - ip: 192.168.0.15

  3. 修改该Service配置,添加annotation(kubernetes.io/elb.custom-eip-id)。

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kubernetes.io/elb.autocreate: '{"type":"public","bandwidth_name":"aaaaa","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_g-vm","name":"xxx","available_zone":["xxx"],"elb_virsubnet_ids":["fc0c61cd-c987-49c4-99a4-b7d816b57581"],"l7_flavor_name":"","l4_flavor_name":"L4_flavor.elb.pro.max","vip_subnet_cidr_id":"cf35b03f-c6ca-4f75-aa70-e2166cb1f800"}'
        kubernetes.io/elb.eip-id: 8560972c-2cc5-4699-94d6-e46f146eb73d     # 表示创建ELB时自动创建的EIP的ID
        kubernetes.io/elb.custom-eip-id: 88c197a1-cb85-4b38-b672-1d60dc5d00db  # 自定义的EIP的ID    
        kubernetes.io/elb.class: performance
        kubernetes.io/elb.id: 0e78a84a-7deb-4747-aeb6-09b6a820b001
      labels:
        app: test-svc
        version: v1
      name: test-eip
      namespace: default
    spec:
      allocateLoadBalancerNodePorts: true
      clusterIP: 10.247.93.235
      clusterIPs:
      - 10.247.93.235
      externalTrafficPolicy: Cluster
      internalTrafficPolicy: Cluster
      ipFamilies:
      - IPv4
      ipFamilyPolicy: SingleStack
      loadBalancerIP: *.*.*.*
      ports:
      - name: cce-service-0
        nodePort: 31354
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: test-svc
        version: v1
      sessionAffinity: None
      type: LoadBalancer
    status:
      loadBalancer:
        ingress:
        - ip: *.*.*.*
        - ip: 192.168.0.15
    表1 关键参数说明

    参数

    参数类型

    描述

    kubernetes.io/elb.custom-eip-id

    String

    自定义EIP的ID,您可以前往EIP控制台查看。该EIP必须是处于可绑定状态。

  4. Service更新成功后,重新查看Service。

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kubernetes.io/elb.autocreate: '{"type":"public","bandwidth_name":"aaaaa","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_g-vm","name":"xxx","available_zone":["xxx"],"elb_virsubnet_ids":["fc0c61cd-c987-49c4-99a4-b7d816b57581"],"l7_flavor_name":"","l4_flavor_name":"L4_flavor.elb.pro.max","vip_subnet_cidr_id":"cf35b03f-c6ca-4f75-aa70-e2166cb1f800"}'
        kubernetes.io/elb.eip-id: 8560972c-2cc5-4699-94d6-e46f146eb73d     # 表示创建ELB时自动创建的EIP的ID
        kubernetes.io/elb.custom-eip-id: 88c197a1-cb85-4b38-b672-1d60dc5d00db  # 自定义的EIP的ID   
        kubernetes.io/elb.custom-eip-status: '{"id":"88c197a1-cb85-4b38-b672-1d60dc5d00db","public_ip_address":"2.2.2.2"}' # 自定义的EIP配置成功后,记录了配置的EIP的ID和IP地址
        kubernetes.io/elb.class: performance
        kubernetes.io/elb.id: 0e78a84a-7deb-4747-aeb6-09b6a820b001
      labels:
        app: test-svc
        version: v1
      name: test-eip
      namespace: default
    spec:
      allocateLoadBalancerNodePorts: true
      clusterIP: 10.247.93.235
      clusterIPs:
      - 10.247.93.235
      externalTrafficPolicy: Cluster
      internalTrafficPolicy: Cluster
      ipFamilies:
      - IPv4
      ipFamilyPolicy: SingleStack
      loadBalancerIP: 2.2.2.2
      ports:
      - name: cce-service-0
        nodePort: 31354
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: test-svc
        version: v1
      sessionAffinity: None
      type: LoadBalancer
    status:
      loadBalancer:
        ingress:
        - ip: 2.2.2.2
        - ip: 192.168.0.15

相关文档