Some applications require a pod to maintain a consistent IP address. This topic describes how to configure an Elastic Container Instance (ECI) pod to use a fixed IP address.
How it works
By default, when you create an ECI pod, the system assigns a random IP address to the pod from the CIDR block of the vSwitch specified in the eci-profile. When a pod is released due to a failure, the system releases its IP address and assigns a new one to the replacement pod. In certain scenarios, such as when a service is exposed directly by its IP address, a pod may require a fixed IP address to ensure business continuity.
To address these scenarios, ECI provides the fixed IP address feature. When you create an ECI pod with this feature enabled, the system allocates two IP addresses from the configured vSwitch: a dynamic IP address, which is not visible within the cluster, and a fixed IP address. After the pod is released, the system retains its fixed IP address for a specific period, which is 48 hours by default. If a new pod is created within this retention period that has the same cluster ID, namespace, and pod name as the released pod, and also has the fixed IP address feature enabled, the system assigns the retained IP address to the new pod. This ensures the pod's IP address remains consistent.
In this scenario, you can use a StatefulSet to deploy your application. When a pod in a StatefulSet is released, the system automatically creates a new pod with the same namespace and pod name.
Configuration
Add annotations to the pod metadata to enable the fixed IP address feature and to configure the retention period of the fixed IP address. The following table describes the annotations.
|
Annotation |
Example |
Description |
|
k8s.aliyun.com/eci-fixed-ip |
"true" |
Set to |
|
k8s.aliyun.com/eci-fixed-ip-retain-hour |
"24" |
The number of hours to retain the fixed IP address after the pod is released. If omitted, the default is 48 hours. |
Procedure
-
Create a Service and a StatefulSet.
kubectl apply -f fixedIp.yamlThe following
fixedIp.yamlfile defines a Service and a StatefulSet. The StatefulSet includes an ECI pod with the annotation that enables the fixed IP address feature.apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: selector: matchLabels: app: nginx serviceName: "nginx" minReadySeconds: 10 template: metadata: annotations: k8s.aliyun.com/eci-fixed-ip: "true" # Enable the fixed IP address feature. labels: app: nginx alibabacloud.com/eci: "true" spec: containers: - name: nginx image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/nginx:1.14.2 ports: - containerPort: 80 name: web -
Run the following command to monitor pod status changes.
kubectl get pod -o wide -wThe output shows that the StatefulSet created a pod named
web-0with the IP address172.16.0.129.d:~$ kubectl get pod -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-0 0/1 Pending 0 9s <none> <none> <none> <none> web-0 0/1 Pending 0 20s <none> <none> <none> <none> web-0 0/1 Pending 0 20s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Pending 0 20s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 ContainerCreating 0 69s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 1/1 Running 0 76s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> -
Open a new terminal to connect to the cluster and run the following command to delete the pod in the StatefulSet.
kubectl delete pod web-0 -
Switch back to the first terminal and continue to observe the pod status changes.
The output shows that after the original pod is deleted, the StatefulSet automatically creates a new pod that reuses the original IP address.
NoteThe creation of the new pod may be delayed because the IP address remains occupied until the previous pod is completely deleted.
1:~$ kubectl get pod -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-0 0/1 Pending 0 9s <none> <none> <none> <none> web-0 0/1 Pending 0 20s <none> <none> <none> <none> web-0 0/1 Pending 0 20s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Pending 0 20s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 ContainerCreating 0 69s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 1/1 Running 0 76s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 1/1 Terminating 0 2m37s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Terminating 0 2m39s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Terminating 0 2m55s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Terminating 0 2m55s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Pending 0 0s <none> <none> <none> web-0 0/1 Pending 0 0s <none> <none> <none> web-0 0/1 Pending 0 1s <none> <none> <none> web-0 0/1 Pending 0 1s <none> <none> <none> web-0 0/1 Pending 0 17s <none> <none> <none> web-0 0/1 Pending 0 17s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 Pending 0 17s <none> virtual-kubelet-cn-beijing-h <none> <none> web-0 0/1 ContainerCreating 0 69s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none> web-0 1/1 Running 0 76s 172.16.0.129 virtual-kubelet-cn-beijing-h <none> <none>