Help Center/ Cloud Container Instance/ Developer Guide/ EIP/ Binding a New EIP to a Pod
Updated on 2024-11-01 GMT+08:00

Binding a New EIP to a Pod

Allocating an EIP During Pod Creation

When creating a pod, configure pod-with-eip under annotations. An EIP is automatically assigned and bound to the pod.

The following uses a Deployment named nginx as an example. For details about the parameters, see Table 1.

  • To create a Deployment that uses a dedicated bandwidth, you do not need to specify the bandwidth 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"
  • To create a Deployment that uses a shared bandwidth, you must and only need to specify the bandwidth 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"  
Table 1 Parameter description

Parameter

Description

Mandatory

Constraint

yangtse.io/pod-with-eip

The EIP allocated during pod creation

Yes

This option is enabled only when the parameter value is set to true.

yangtse.io/eip-bandwidth-size

The bandwidth that will be used by the EIP

No

The default value is 5.

yangtse.io/eip-network-type

The bandwidth type

No

The default value is 5_bgp. If your region does not support 5_bgp and this parameter is left empty, an event is reported for the pod.

yangtse.io/eip-charge-mode

Bandwidth billing option

No

The value can be bandwidth (default) or traffic.

yangtse.io/eip-bandwidth-id

ID of the shared bandwidth

Yes (for shared bandwidths)

If you have set this parameter, you do not need to set other values.

yangtse.io/eip-bandwidth-name

Bandwidth name

No

The default value is the same as the EIP name.

Verifying the EIP Allocation

The startup time of the pod may be earlier than the time when the EIP allocation result is returned. During pod startup, the EIP may fail to be bound.

You can use an init container to check whether the EIP is allocated. After the pod IP address is allocated, the container network controller binds an EIP to the pod and returns the allocation result to the annotation (yangtse.io/allocated-ipv4-eip) of the pod. You can configure an init container in the pod and use the downward API to mount the annotation to the init container through a volume to check whether the EIP is allocated. You can configure the init container as follows:

CCI allows EIPs to be automatically bound to pods. EIPs are allocated after pod scheduling is complete. EIP annotations cannot be injected to pods through ENV.

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']

Releasing an EIP During Pod Deletion

When you delete a pod, the EIP bound to it is also released. You can run the following command to delete a pod:

# kubectl delete pod nginx -n $namespace_name