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.

Background information

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

Procedure

You can use the following two methods to attach an EIP to a pod.

Method 1: Automatically assign an elastic IP address

  1. Log on to the Container Service console.
  2. In the left-side navigation pane under Container Service - Kubernetes, choose Applications > Deployments.The Deployments page appears.
  3. 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.
    yaml
    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.
  4. In the left-side navigation pane, choose Applications > Pods to go to the Pods page.
    Pods
  5. Select a container, and choose More > Edit. The Edit YAML file appears.
    Note The IP address of k8s.aliyun.com/allocated-eipAddress: 47.110.XX.XX in the YAML file is the public IP address of the EIP.
  6. 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.

    Nginx welcome page
    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

  1. Log on to the VPC console to purchase an EIP. For more information, see Create an EIP.
    EIP
    Note The EIP and the cluster must be in the same region.
  2. Log on to the Container Service console.
  3. In the left-side navigation pane under Container Service - Kubernetes, choose Applications > Deployments. The Deployments page appears.
  4. 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.
    yaml
    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.
  5. In the left-side navigation pane, choose Applications > Pods to go to the Pods page.
    Container Groups
  6. Enter http://ip address in the browser to visit the Nginx welcome page.
    Nginx welcome page
    Note http://ip address is the IP address of the EIP instance obtained in step 1.