Help Center> Cloud Container Instance> Developer Guide> EIP> Binding an Existing EIP to a Pod
Updated on 2024-02-23 GMT+08:00

Binding an Existing EIP to a Pod

Specifying the EIP ID for a Pod

When creating a pod, enter the annotation of yangtse.io/eip-id. The EIP is automatically bound to the pod.

The following example creates a Deployment named nginx with one pod. The EIP is automatically bound to the pod. Table 1 describes the parameters.

apiVersion: apps/v1 
kind: Deployment  
metadata:      
 annotations:        
   deployment.kubernetes.io/revision: "14"         
   description: ""    
 name: nginx     
 namespace: eip  
spec:   
 ...   
 replicas: 1   
  template:      
   metadata:          
    annotations:             
     yangtse.io/eip-id: 65eb3679-7a8d-4b24-b681-0b661axxxxcb
Table 1 Parameter description

Parameter

Description

Mandatory

Constraint

yangtse.io/eip-id

EIP ID

Yes

The ID that can be queried on the EIP page.

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:

apiVersion: v1
kind: Pod
metadata:
  name: example
  namespace: demo
  annotations:
    yangtse.io/eip-id: 65eb3679-7a8d-4b24-b681-0b661axxxxcb
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']