This topic describes how to attach an Elastic IP Address (EIP) to a pod in a serverless Kubernetes cluster or on a virtual node in the cluster.
Context
Alibaba Cloud Serverless Kubernetes service and virtual nodes support attaching an EIP to a pod. This feature helps to facilitate the deployment and services of Serverless container applications.
- To allow a single pod to access public networks, you do not need to configure NAT gateways for the VPC.
- You do not need to create services to allow a single pod to expose public network services.
- Pods and EIPs can be bound in a more flexible way.
Prerequisites
- You have created a Serverless Kubernetes cluster. Or, a Deploy ack-virtual-node in an ACK cluster is created in the Kubernetes cluster.
- Make sure that the security group for the cluster has opened the relevant port (in this example, port 80)
Procedure
You can use the following two methods to attach an EIP to a pod.
Method 1: Automatically assign an elastic IP address
In the left-side navigation pane under Container Service - Kubernetes, choose
.The Deployments page appears.Select the cluster and namespace, select a sample template or Custom from the Sample Template drop-down list, and click Create.
You can use the following YAML template to create a pod: In this example, you can set k8s.aliyun.com/eci-with-eip to true. The Serverless Kubernetes service or virtual node automatically assigns an EIP and binds the EIP to the pod.
apiVersion: v1 kind: Pod metadata: name: nginx annotations: "k8s.aliyun.com/eci-with-eip": "true" spec: containers: - image: registry-vpc.cn-hangzhou.aliyuncs.com/jovi/nginx:alpine imagePullPolicy: Always name: nginx ports: - containerPort: 80 name: http protocol: TCP restartPolicy: OnFailure
Note- You can use the annotation k8s.aliyun.com/eip-bandwidth to specify the bandwidth of the EIP. The default value is 5 and the unit is M.
- Note that if you are creating a Deployment, an EIP will be assigned to each pod in that Deployment. Exercise caution when you perform this operation.
In the left-side navigation pane, choose
to go to the Pods page.Select a container, and choose
. The Edit YAML file appears.Note The IP address ofk8s.aliyun.com/allocated-eipAddress: 47.110.XX.XX
in the YAML file is the public IP address of the EIP.Enter http://ip address in the browser to visit the Nginx welcome page.
http://ip address is the IP address of
k8s.aliyun.com/allocated-eipAddress: 47.110.XX.XX
in the YAML file.Note An EIP is dynamically assigned in this method, and the lifecycle of the EIP is the same as that of the pod. When a pod is deleted, the EIP dynamically assigned to that pod is also deleted.
Method 2: Specify the ID of an elastic IP address instance
Log on to the VPC console to purchase an EIP. For more information, see Apply for new EIPs.
Note The EIP and the cluster must be in the same region.In the left-side navigation pane under Container Service - Kubernetes, choose
. The Deployments page appears.Select the cluster and namespace, select a sample template or Custom from the Sample Template drop-down list, and click Create.
You can use the following YAML template to create a pod: In this example, you can specify the pod annonation k8s.aliyun.com/eci-eip-instanceid as the ID of the EIP instance.
apiVersion: v1 kind: Pod metadata: name: nginx annotations: "k8s.aliyun.com/eci-eip-instanceid": "<youreipInstanceId>" spec: containers: - image: registry-vpc.cn-hangzhou.aliyuncs.com/jovi/nginx:alpine imagePullPolicy: Always name: nginx ports: - containerPort: 80 name: http protocol: TCP restartPolicy: OnFailure
Note You need to replace<youreipInstanceId>
with the ID of the EIP instance obtained in step 1.In the left-side navigation pane, choose
to go to the Pods page.Enter http://ip address in the browser to visit the Nginx welcome page.
Note http://ip address is theIP address of the EIP instance obtained in step 1.