文档首页> 云容器实例 CCI> 开发指南> EIP> 为Pod动态创建EIP
更新时间:2024-02-08 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_g-vm
            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"  
表1 参数说明

参数

参数含义

必选/可选

约束

yangtse.io/pod-with-eip

EIP随Pod创建

必选

字段填写值为"true"时,才会开启该功能。

yangtse.io/eip-bandwidth-size

指定EIP带宽

可选

默认值为5。

yangtse.io/eip-network-type

指定带宽类型

可选

默认值为5_bgp,若region不支持5_bgp,同时该字段未填写时,pod会报event。

yangtse.io/eip-charge-mode

指定收费类型

可选

默认值bandwidth,可选值traffic。

yangtse.io/eip-bandwidth-id

指定共享带宽id

必选(共享型带宽)

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

yangtse.io/eip-bandwidth-name

指定带宽名

可选

默认值同该EIP的名称。

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是否已经分配成功。具体您可以参考以下示例配置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_g-vm
    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一起被删除。删除Pod指令如下:

# kubectl delete pod nginx -n $namespace_name
分享:

    相关文档

    相关产品