All Products
Search
Document Center

Elastic Container Instance:Obtain metadata from a container

Last Updated:Aug 26, 2024

The metadata of an elastic container instance provides information about the instance and the containers on the instance to facilitate the usage and management of the containers. The information provided by the metadata includes the basic information of the elastic container instance, such as the instance name and instance ID, and the network information of the elastic container instance, such as the IP address and network interface controllers (NICs) of the instance. This topic describes how to obtain the metadata of an elastic container instance from a container and present the metadata to running containers.

Method 1: Use MetaServer to access metadata

  1. Connect to a container. For more information, see Connect to an elastic container instance.

  2. Access metadata.

    Note

    If curl is not installed in the container, install curl.

    curl http://100.100.100.200/latest/meta-data/<metadata>

    Replace <metadata> with the metadata item that you want to query. For example, replace <metadata> with instance-id.

    curl http://100.100.100.200/latest/meta-data/instance-id

    The following table describes the metadata items of an elastic container instance that you can query.

    Metadata item

    Description

    /dns-conf/nameservers

    The Domain Name System (DNS) configurations of the elastic container instance.

    /eipv4

    The elastic IPv4 address of the elastic container instance.

    /hostname

    The hostname of the elastic container instance, which is the ContainerGroupName value.

    /instance-id

    The ID of the elastic container instance.

    /mac

    The media access control (MAC) address of the elastic container instance.

    /network/interfaces/

    The MAC addresses of the NICs.

    /network/interfaces/macs/[mac]/network-interface-id

    The ID of the NIC. Replace [mac] with the MAC address of the elastic container instance.

    /network/interfaces/macs/[mac]/netmask

    The subnet mask of the NIC.

    /network/interfaces/macs/[mac]/vswitch-cidr-block

    The IPv4 CIDR block of the vSwitch to which the NIC is connected.

    /network/interfaces/macs/[mac]/vpc-cidr-block

    The IPv4 CIDR block of the virtual private cloud (VPC) to which the NIC belongs.

    /network/interfaces/macs/[mac]/private-ipv4s

    The private IPv4 addresses assigned to the NIC.

    /network/interfaces/macs/[mac]/vpc-ipv6-cidr-blocks

    The IPv6 CIDR block of the VPC to which the NIC belongs. This item is applicable only to the elastic container instances that reside in VPCs and to which IPv6 addresses are assigned.

    /network/interfaces/macs/[mac]/vswitch-id

    The ID of the vSwitch that resides in the same VPC as the security group of the NIC.

    /network/interfaces/macs/[mac]/vpc-id

    The ID of the VPC in which the security group of the NIC resides.

    /network/interfaces/macs/[mac]/primary-ip-address

    The primary private IP address of the NIC.

    /network/interfaces/macs/[mac]/gateway

    The IPv4 gateway address of the NIC.

    /instance/max-netbw-egress

    The maximum outbound internal bandwidth of the elastic container instance. Unit: Kbit/s.

    /instance/max-netbw-ingerss

    The maximum inbound internal bandwidth of the elastic container instance. Unit: Kbit/s.

    /network/interfaces/macs/[mac]/ipv6s

    The IPv6 addresses assigned to the NIC. This item is applicable only to elastic container instances that reside in VPCs and to which IPv6 addresses are assigned.

    /network/interfaces/macs/[mac]/ipv6-gateway

    The IPv6 gateway address of the VPC to which the NIC belongs.

    /network/interfaces/macs/[mac]/vswitch-ipv6-cidr-block

    The IPv6 CIDR block of the vSwitch to which the NIC is connected. This item is applicable only to elastic container instances that reside within VPCs and to which IPv6 addresses are assigned.

    /private-ipv4

    The private IPv4 address of the elastic container instance.

    /ntp-conf/ntp-servers

    The address of the Network Time Protocol (NTP) server.

    /owner-account-id

    The ID of the Alibaba Cloud account to which the elastic container instance belongs.

    /region-id

    The region ID of the elastic container instance.

    /serial-number

    The serial number of the elastic container instance.

    /vpc-id

    The ID of the VPC in which the elastic container instance resides.

    /vpc-cidr-block

    The CIDR block of the VPC in which the elastic container instance resides.

    /vswitch-cidr-block

    The CIDR block of the vSwitch to which the elastic container instance is connected.

    /vswitch-id

    The ID of the vSwitch to which the elastic container instance is connected.

    /zone-id

    The zone ID of the elastic container instance.

    /ram/security-credentials/[role-name]

    The temporary Security Token Service (STS) credentials generated for the Resource Access Management (RAM) role of the elastic container instance. You can obtain the temporary STS credentials only after you specify a RAM role for an elastic container instance. Replace [role-name] with the name of the RAM role. If you do not replace [role-name] with the name of the RAM role, the name of the elastic container instance is returned.

Method 2: Configure environment variables for a container

You can obtain the information about an elastic container instance by configuring environment variables for a container on the instance. The metadata items of an elastic container instance that can be obtained by using this method include the instance ID, instance name, region ID of the instance, zone ID of the instance, and container name.

Key

Value

Description

eci_id

__ECI_ID__

The ID of the elastic container instance.

eci_name

__ECI_NAME__

The name of the elastic container instance.

region_id

__REGION_ID__

The region ID of the elastic container instance.

zone_id

__ZONE_ID__

The zone ID of the elastic container instance.

container_name

__CONTAINER_NAME__

The name of the container.

Examples:

OpenAPI

When you call the CreateContainerGroup operation to create an elastic container instance, you can use the Container.N.EnvironmentVar.N.Key and Container.N.EnvironmentVar.N.Value parameters to configure environment variables, as shown in the following sample code:

ContainerGroupName=test-env
Container.1.Image=registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
Container.1.Name=container-1
# Configure environment variables.
Container.1.EnvironmentVar.1.Key=eci_id
Container.1.EnvironmentVar.2.Key=eci_name
Container.1.EnvironmentVar.3.Key=region_id
Container.1.EnvironmentVar.4.Key=zone_id
Container.1.EnvironmentVar.5.Key=container_name
Container.1.EnvironmentVar.1.Value=__ECI_ID__
Container.1.EnvironmentVar.2.Value=__ECI_NAME__
Container.1.EnvironmentVar.3.Value=__REGION_ID__
Container.1.EnvironmentVar.4.Value=__ZONE_ID__
Container.1.EnvironmentVar.5.Value=__CONTAINER_NAME__

Console

When you create an elastic container instance in the Elastic Container Instance console, you can select a container in the Container Configurations section, expand the Advanced Settings of the container, turn on Environment Variable, and then add environment variables, as shown in the following figure.

元数据.png

After the elastic container instance is created, you can connect to the container to check whether the configured environment variables have taken effect. For more information, see Connect to an elastic container instance.view