更新时间:2025-04-30 GMT+08:00

为Pod动态创建EIP

EIP随Pod创建

创建Pod时,填写pod-with-eip的annotation后,EIP会随Pod自动创建并绑定至该Pod。

以下示例创建一个名为nginx的无状态负载,EIP将随Pod自动创建并绑定至Pod,关键参数含义见表1

  • 创建独占带宽类型的Deployment,无需指定带宽ID,示例如下:
    apiVersion: apps/v1
    kind: Deployment 
    metadata:   
      annotations:   
        deployment.kubernetes.io/revision: "14"   
        description: "" 
      name: nginx  
      namespace: eip 
    spec:
      ...   
      template: 
        metadata:   
          annotations:    
            yangtse.io/pod-with-eip: "true"  
            yangtse.io/eip-bandwidth-size: "5" 
            yangtse.io/eip-network-type: 5_bgp
            yangtse.io/eip-charge-mode: bandwidth
            yangtse.io/eip-bandwidth-name: "xxx"
  • 创建共享带宽类型的Deployment,必须指定带宽ID且只需填写该字段,示例如下:
    apiVersion: apps/v1
    kind: Deployment 
    metadata:   
      annotations:   
        deployment.kubernetes.io/revision: "14"   
        description: "" 
      name: nginx  
      namespace: eip 
    spec:
      ...   
      template:    
        metadata:   
          annotations:
            yangtse.io/pod-with-eip: "true"  
            yangtse.io/eip-bandwidth-id: "xxx"  

Pod的EIP准备就绪

Pod业务容器的启动时间可能早于EIP分配结果返回成功时间,在Pod启动过程中EIP可能会绑定失败。

通过在init container中可检查EIP是否已经分配成功。容器网络控制器会在Pod IP分配后,为Pod绑定EIP并返回分配结果至Pod的Annotation(yangtse.io/allocated-ipv4-eip),通过Pod配置init container并使用downwardAPI,把yangtse.io/allocated-ipv4-eip annotation通过volume挂载到init container里,可检查EIP是否已经分配成功,关键参数含义见表1。具体您可以参考以下示例配置init container:

CCI当前提供Pod自动绑定EIP的能力,实际的EIP分配发生在Pod调度完成之后,因此当前不支持通过ENV将EIP的Annotation注入至Pod中。

apiVersion: v1
kind: Pod
metadata:
  name: example
  namespace: demo
  annotations:
    yangtse.io/pod-with-eip: "true"  
    yangtse.io/eip-bandwidth-size: "5" 
    yangtse.io/eip-network-type: 5_bgp
    yangtse.io/eip-charge-mode: bandwidth
    yangtse.io/eip-bandwidth-name: "xxx"
spec:
  initContainers:
  - name: init
    image: busybox:latest
    command: ['timeout', '60', 'sh', '-c', "until grep -E '[0-9]+' /etc/eipinfo/allocated-ipv4-eip; do echo waiting for allocated-ipv4-eip; sleep 2; done"]
    volumeMounts:
        - name: eipinfo
          mountPath: /etc/eipinfo
  volumes:
    - name: eipinfo
      downwardAPI:
        items:
          - path: "allocated-ipv4-eip"
            fieldRef:
              fieldPath: metadata.annotations['yangtse.io/allocated-ipv4-eip']
表1 关键参数说明

参数

必选/可选

参数说明

取值范围

yangtse.io/pod-with-eip

必选

是否需要跟随Pod创建EIP并绑定到该Pod。

  • false:关闭此功能。
  • true:开启此功能。

yangtse.io/eip-bandwidth-size

可选

带宽大小,单位为Mbit/s。

默认值为5。具体类型以各区域配置为准,详情请参见弹性公网IP控制台的购买页面。

yangtse.io/eip-network-type

可选

公网IP类型。

默认值为5_bgp,如果region不支持5_bgp,同时该字段未填写时,pod会报event。具体类型以各区域配置为准,详情请参见弹性公网IP控制台的购买页面。

例如,“华东-上海一”区域支持以下类型:

  • 5_bgp:全动态BGP
  • 5_sbgp:静态BGP

yangtse.io/eip-charge-mode

可选

按流量计费或按带宽计费。

建议填写该参数。若该参数为空,表示不指定计费模式,则以该区域下弹性公网IP接口的默认值为准。

默认值bandwidth,可选值traffic。

  • bandwidth:按带宽计费
  • traffic:按流量计费

yangtse.io/eip-bandwidth-id

必选(共享型带宽)

共享带宽ID

填写该字段时,无需填写其他值。

yangtse.io/eip-bandwidth-name

可选

带宽名称。

默认值同该EIP的名称。

  • 1-64个字符,支持数字、字母、中文、_(下划线)、-(中划线)、.(点)
  • 最小长度:1
  • 最大长度:64

EIP随Pod删除

当Pod被删除时,EIP会随Pod一起被删除。删除Pod指令如下:

# kubectl delete pod nginx -n $namespace_name