Associate an EIP with a Pod

Updated at:

Associate an Elastic IP Address (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 the required ports. In this example, port 80 must be open.

Note
  • You must upgrade the ack-virtual-node add-on to v1.0.0.7-aliyun or later.

  • You can associate an EIP with a Pod only when you create the Pod. Adding or modifying this setting when you update a Pod does not take effect.

Background information

ACK Serverless and virtual nodes support associating an EIP with a Pod. This feature makes deploying and accessing certain serverless container applications simpler and more convenient.

  • Allows a single Pod to access the internet without a VPC NAT gateway.

  • Allows a single Pod to be exposed to the internet without creating a Service.

  • Binds a Pod to an EIP more flexibly and dynamically.

Associate an EIP with a Pod

You can associate an EIP with a Pod by using one of the following two methods.

Method 1: Automatically assign an EIP

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  3. On the Deployments page, click Create from YAML and select a sample template or customize one.

    You can use the following YAML sample template to create a Pod. In this example, when you set k8s.aliyun.com/eci-with-eip to true, ACK Serverless and virtual nodes automatically allocate an EIP for the Pod and bind it 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: OnFailure
    Note
    • 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 bind the EIP to shared bandwidth.

    • If you create a Deployment, a different EIP is associated with each Pod in the Deployment. Use this operation with caution.

  4. Click Create.

  5. In the left-side navigation pane of the cluster management page, choose Workloads > Pods to view the status of the Pod.

  6. On the right side of the target Pod, click Edit YAML and edit the YAML configuration in the dialog box that appears.

    Note

    In the YAML file, the IP address in k8s.aliyun.com/allocated-eipAddress: 47.110.XX.XX is the public IP address of the EIP.

  7. Enter http://IP address in your browser to access the nginx welcome page.

    Here, http://IP address is the IP address in k8s.aliyun.com/allocated-eipAddress: 47.110.XX.XX in the YAML file.

    The browser displays the default nginx welcome page with the message Welcome to nginx!, which indicates that nginx is installed and running properly.

    Note

    With this method, the EIP is dynamically allocated and has the same lifecycle as the Pod. When you delete the Pod, the dynamically allocated EIP is also deleted.

  8. Optional: To specify an ISP line for an Elastic Container Instance (ECI), set the k8s.aliyun.com/eip-isp annotation.

    The ISP field indicates the line type. The default value is BGP. For more information, see AllocateEipAddressPro.

    The following is a sample YAML template:

    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

  1. Log on to the VPC console to purchase an EIP. For instructions, see Apply for EIPs.

    Note

    The EIP that you purchase must be in the same region as the cluster.

  2. Log on to the ACK console. In the left navigation pane, click Clusters.

  3. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

  4. On the Deployments page, click Create from YAML and select a sample template or customize one.

    You can use the following YAML sample 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: OnFailure
    Note
    • <youreipInstanceId> must be replaced with the EIP instance ID that you obtained in Step 1.

    • If you configure both automatic EIP assignment and a specified EIP instance ID, the manually specified EIP does not take effect.

  5. Click Create.

  6. In the left-side navigation pane of the cluster management page, choose Workloads > Pods to view the status of the Pod.

  7. Enter http://IP address in your browser to access the nginx welcome page.

    Note

    Here, http://IP address is the IP address of the EIP that you purchased in Step 1.