# 为Pod配置EIP
#### 使用场景
CCE Autopilot集群中，Pod使用的是VPC的弹性网卡/辅助弹性网卡，可直接绑定弹性公网IP。
为方便用户在CCE内直接为Pod关联弹性公网IP，用户只需在创建Pod时，配置annotation（yangtse.io/pod-with-eip: "true"），弹性公网IP就会随Pod自动创建并绑定至该Pod。
#### 约束限制
- 绑定EIP的Pod，如果要被公网成功访问，需要添加放通相应请求流量的安全组规则。
- 单个Pod只能绑定单个EIP。
- 创建Pod时，可指定相关的annotation配置EIP的属性，创建完成后，更新EIP相关的annotation均无效。
- 与Pod关联的EIP不要通过弹性公网IP的console或API直接操作（修改名称/删除/解绑/绑定/转包周期等操作），否则可能导致EIP功能异常。
- 自动创建的EIP被手动删除后，会导致网络异常，需要重建Pod。
 
#### EIP跟随Pod创建
创建Pod时，填写pod-with-eip的annotation后，EIP会随Pod自动创建并绑定至该Pod。
以下示例创建一个名为nginx的无状态负载，EIP将随Pod自动创建并绑定至Pod。具体字段含义请参见[表2]。
- 创建Deployment时自动创建**独占带宽** 类型的EIP，无需指定带宽ID，示例如下：
  ```
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx
  spec:
    replicas: 3
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
        annotations:
          yangtse.io/pod-with-eip: "true"  # EIP跟随Pod创建
          yangtse.io/eip-bandwidth-size: "5"   # EIP带宽
          yangtse.io/eip-network-type: 5_bgp  # EIP类型
          yangtse.io/eip-charge-mode: bandwidth  # EIP计费模式
          yangtse.io/eip-bandwidth-name: <eip_bandwidth_name>  # EIP带宽名称
      spec:
        containers:
          - name: container-0
            image: nginx:alpine
            resources:
              limits:
                cpu: 250m
                memory: 500Mi
              requests:
                cpu: 250m
                memory: 500Mi
        imagePullSecrets:
          - name: default-secret
  ```
   表1独占带宽EIP跟随Pod创建的annotation配置 
  | annotation                     | 是否可选 | 默认值     | 参数说明                                                                                                                        | 取值范围                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
  |:---|:---|:---|:---|:---|
  | yangtse.io/pod-with-eip         | 必选   | false    | 是否需要跟随Pod创建EIP并绑定到该Pod。                                                                                                    | "false"或"true"。                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
  | yangtse.io/eip-bandwidth-size | 可选    | 5       | 带宽大小，单位为Mbit/s。                                                                                                             | 具体范围以各区域配置为准，根据带宽的计费类型不同可能存在差异，详情请参见弹性公网IP控制台的购买页面。 例如，"华东-上海一"区域按带宽计费类型的带宽大小范围为1Mbit/s\~2000Mbit/s、按流量计费类型的带宽大小范围为1Mbit/s\~300Mbit/s。                                                                                                                                                                                                                                                                 |
  | yangtse.io/eip-network-type     | 可选    | 5_bgp  | 公网IP类型。                                                                                                                    | 具体类型以各区域配置为准，详情请参见弹性公网IP控制台的购买页面。 例如，"华东-上海一"区域支持以下类型： - 5_bgp：全动态BGP  - 5_sbgp：静态BGP   |
  | yangtse.io/eip-charge-mode     | 可选    | 空      | 按流量计费或按带宽计费。 **建议填写该参数。**若该参数为空，表示不指定计费模式，则以该区域下弹性公网IP接口的默认值为准。 | - bandwidth：按带宽计费  - traffic：按流量计费                                                                                                                                                                                |
  | yangtse.io/eip-bandwidth-name     | 可选   | Pod名称 | 带宽名称。                                                                                                                       | 1-64个字符，支持数字、字母、中文、_(下划线)、-（中划线）、.（点）。                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
     
  
- 创建Deployment时自动创建**共享带宽** 类型的EIP，必须指定共享带宽ID，示例如下：
  ```
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx
  spec:
    replicas: 3
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
        annotations:
          yangtse.io/pod-with-eip: "true"  # EIP跟随Pod创建
          yangtse.io/eip-network-type: 5_bgp  # EIP类型
          yangtse.io/eip-bandwidth-id: <eip_bandwidth_id>  # EIP共享带宽ID
      spec:
        containers:
          - name: container-0
            image: nginx:alpine
            resources:
              limits:
                cpu: 250m
                memory: 500Mi
              requests:
                cpu: 250m
                memory: 500Mi
        imagePullSecrets:
          - name: default-secret
  ```
   表2共享带宽类型EIP跟随Pod创建的annotation配置 
  | annotation                   | 是否可选       | 默认值   | 参数说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | 取值范围                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
  |:---|:---|:---|:---|:---|
  | yangtse.io/pod-with-eip    | 必选         | false | 是否需要跟随Pod创建EIP并绑定到该Pod。                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | "false"或"true"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | yangtse.io/eip-network-type   | 可选        | 5_bgp  | 公网IP类型。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | - 5_bgp  - 5_union  - 5_sbgp 具体类型以各区域配置为准，详情请参见弹性公网IP控制台。    |
  | yangtse.io/eip-bandwidth-id | 使用共享型带宽时必选 | 空    | 已有的带宽ID。 - 不填写该字段时，则默认使用独占带宽的EIP。独占带宽EIP的参数设置请参见[表1]。  - 填写该字段时，只允许同时指定yangtse.io/eip-network-type字段，且该字段为可选。   | -                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
     
  
 
#### 检查Pod的EIP就绪
容器网络控制器会在Pod IP分配后，为Pod绑定EIP并回写分配结果至Pod的annotation（yangtse.io/allocated-ipv4-eip），Pod业务容器的启动时间可能早于EIP分配结果回写成功时间。
您可以尝试为Pod配置init container并使用downwardAPI类型的存储卷把yangtse.io/allocated-ipv4-eip的annotation通过volume挂载到init container里，并在init container中检查EIP是否已经分配成功。您可以参考以下示例配置init container。
```
apiVersion: v1
kind: Pod
metadata:
  name: example
  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']
...
```
#### EIP跟随Pod删除
当Pod被删除时，自动创建的EIP会随Pod一起被删除。
