
# 为Pod动态创建EIP
#### EIP随Pod创建
创建Pod时，填写pod-with-eip的annotation后，EIP会随Pod自动创建并绑定至该Pod。
以下示例创建一个名为nginx的Pod，EIP将随Pod自动创建并绑定至Pod，关键参数含义见[表1]。
- 创建独占带宽类型的Pod，无需指定带宽ID，示例如下：
  ```
  kind: Pod
  apiVersion: cci/v2
  metadata:
    name: nginx
    namespace: ns-test
    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: traffic  # EIP计费模式
      yangtse.io/eip-bandwidth-name: "xxx"   # 带宽名称
  spec:
    containers:
      - name: container1
        image: nginx:stable-alpine-perl
        resources:
          limits:
            cpu: '1'
            memory: 2G
          requests:
            cpu: '1'
            memory: 2G
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
    restartPolicy: Always
    terminationGracePeriodSeconds: 30
    dnsPolicy: Default
    securityContext: {}
    imagePullSecrets:
      - name: imagepull-secret
  ```
  
 表1独占带宽EIP跟随Pod创建的annotation配置 
| 参数                            | 必选/可选 | 参数说明                                                                                        | 取值范围                                                                                                                                                                                                                                                                                                                                                 |
|:---|:---|:---|:---|
| 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-name | 可选    | 带宽名称。                                                                                       | 默认值同该EIP的名称。 - 1-64个字符，支持数字、字母、中文、_(下划线)、-（中划线）、.（点）  - 最小长度：1  - 最大长度：64                                  |
   
- 创建共享带宽类型的Pod，必须指定带宽ID且只需填写该字段，示例如下：
  ```
  kind: Pod
  apiVersion: cci/v2
  metadata:
    name: nginx
    namespace: ns-test
    annotations:
      yangtse.io/pod-with-eip: "true"  # EIP跟随Pod创建
      yangtse.io/eip-bandwidth-id: "5xxxxxx4-a801-4bfd-9004-5fxxxxxxxx19"  # EIP共享带宽ID
  spec:
    containers:
      - name: container1
        image: nginx:stable-alpine-perl
        resources:
          limits:
            cpu: '1'
            memory: 2G
          requests:
            cpu: '1'
            memory: 2G
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
    restartPolicy: Always
    terminationGracePeriodSeconds: 30
    dnsPolicy: Default
    securityContext: {}
    imagePullSecrets:
      - name: imagepull-secret
  ```
  
 表2共享带宽类型EIP跟随Pod创建的annotation配置 
| 参数                          | 必选/可选     | 参数说明                    | 取值范围                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| yangtse.io/pod-with-eip     | 必选        | 是否需要跟随Pod创建EIP并绑定到该Pod。 | - false：关闭此功能。  - true：开启此功能。                                                                                                                                                                      |
| yangtse.io/eip-bandwidth-id | 必选（共享型带宽） | 共享带宽ID                  | 填写该字段时，无需填写其他值。                                                                                                                                                                                                                                                                                                                                               |
| yangtse.io/eip-network-type | 可选        | 公网IP类型。                 | 默认值为5_bgp，如果region不支持5_bgp，同时该字段未填写时，pod会报event。具体类型以各区域配置为准，详情请参见弹性公网IP控制台的购买页面。 例如，"华东-上海一"区域支持以下类型： - 5_bgp：全动态BGP  - 5_sbgp：静态BGP   |
   
#### 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是否已经分配成功，关键参数含义见[表2]。具体您可以参考以下示例配置init container：
![](https://support.huaweicloud.com/usermanual-cci2/public_sys-resources/notice_3.0-zh-cn.png)
CCI当前提供Pod自动绑定EIP的能力，实际的EIP分配发生在Pod调度完成之后，因此当前不支持通过ENV将EIP的Annotation注入至Pod中。
```
kind: Pod
apiVersion: cci/v2
metadata:
  name: nginx
  namespace: ns-test
  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: traffic
    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一起被删除。通过ccictl删除Pod指令如下：
```
# ccictl delete pod $pod_name -n $namespace_name
```
