Associate an EIP with a Pod
This topic describes how to associate an EIP with a Pod in an ACK Serverless cluster or on a virtual node.
Prerequisites
-
Create an ACK Serverless cluster or deploy a virtual node chart in a Kubernetes cluster.
-
Make sure that the security group of the cluster allows traffic on required ports. In this example, port 80 is required.
-
Upgrade the ack-virtual-node component to v1.0.0.7-aliyun or later.
-
You can associate an EIP with a Pod only upon creation. You cannot add or modify this setting for an existing Pod.
Background
ACK Serverless and virtual nodes allow you to associate an EIP with a Pod. This feature simplifies deployment and service access for certain serverless container applications.
-
Allows a Pod to access the internet without a VPC NAT gateway.
-
Allows a Pod to expose a public service without creating a Service.
-
Provides a more flexible and dynamic way to bind a Pod to an EIP.
Associate an EIP with a Pod
You can associate an EIP with a Pod using one of two methods.
Method 1: Automatically assign an EIP
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the navigation pane on the left, choose .
-
On the Deployments page, click Create from YAML and select a sample template or customize one.
You can use the following YAML sample to create a Pod. In this example, setting the k8s.aliyun.com/eci-with-eip annotation to "true" instructs ACK Serverless and virtual nodes to automatically create and bind an EIP to the Pod.
apiVersion: v1 kind: Pod metadata: name: nginx annotations: k8s.aliyun.com/eci-with-eip: "true" # k8s.aliyun.com/eip-bandwidth: '5' # Note: Do not include a unit when you specify the bandwidth. spec: containers: - image: nginx:alpine imagePullPolicy: Always name: nginx ports: - containerPort: 80 name: http protocol: TCP restartPolicy: OnFailureNote-
You can use the k8s.aliyun.com/eip-bandwidth annotation to specify the bandwidth of the EIP. The default value is 5, and the unit is Mbit/s.
-
You can also use the k8s.aliyun.com/eip-common-bandwidth-package-id annotation to add the EIP to an EIP bandwidth plan.
-
If you create a Deployment, a different EIP is associated with each Pod in the Deployment. Use this operation with caution.
-
-
Click Create.
-
On the cluster management page, in the navigation pane on the left, choose to view the Pod's status.
-
On the right side of the target pod, click Edit YAML, and the Edit YAML file opens.
NoteThe IP address in the
k8s.aliyun.com/allocated-eipAddress: 47.110.XX.XXannotation is the EIP's public IP address. -
Enter
http://<IP_address>in your browser to access the nginx welcome page.Here,
<IP_address>is the IP address from thek8s.aliyun.com/allocated-eipAddress: 47.110.XX.XXannotation.The browser displays the default nginx welcome page with the message Welcome to nginx!, confirming that nginx is running correctly.
NoteWith this method, an EIP is dynamically allocated for the Pod and is released when the Pod is deleted.
-
Optional: To specify an ISP, set the
k8s.aliyun.com/eip-ispannotation.The ISP field specifies the line type. The default value is
BGP. For more information, see AllocateEipAddressPro.The following is a sample YAML configuration:
apiVersion: v1 kind: Pod metadata: name: nginx annotations: k8s.aliyun.com/eci-with-eip: "true" k8s.aliyun.com/eip-isp: "BGP" spec: containers: - image: nginx:alpine name: nginx ports: - containerPort: 80 name: http protocol: TCP restartPolicy: OnFailure
Method 2: Specify an EIP instance ID
-
Log in to the VPC console to apply for an EIP. For more information, see Apply for an EIP.
NoteThe EIP and the cluster must be in the same region.
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the navigation pane on the left, choose .
-
On the Deployments page, click Create from YAML and select a sample template or customize one.
Use the following YAML template to create a Pod. In this example, set the Pod annotation k8s.aliyun.com/eci-eip-instanceid to the EIP instance ID.
apiVersion: v1 kind: Pod metadata: name: nginx annotations: "k8s.aliyun.com/eci-eip-instanceid": "<youreipInstanceId>" spec: containers: - image: nginx:alpine imagePullPolicy: Always name: nginx ports: - containerPort: 80 name: http protocol: TCP restartPolicy: OnFailureNote-
Replace
<youreipInstanceId>with the EIP instance ID that you obtained in Step 1. -
If you set annotations for both automatic EIP assignment and a specific EIP instance ID, the automatic assignment takes precedence.
-
-
Click Create.
-
On the cluster management page, in the navigation pane on the left, choose to view the Pod's status.
-
Enter
http://<IP_address>in your browser to access the nginx welcome page.NoteHere,
<IP_address>is the IP address of the EIP from Step 1.