The metadata of an elastic container instance (ECI) provides details about the instance and its containers, such as their names, IDs, IP addresses, and network interface information. You can use this data to manage and operate the containers. This topic describes how to retrieve this metadata from within a running container.
Method 1: Access metadata via the MetaServer
-
Connect to the container. For more information, see Connect to an ECI instance by using Workbench.
-
Access the metadata.
NoteIf curl is not installed in the container environment, install it first.
curl http://100.100.100.200/latest/meta-data/<metadata>Replace
<metadata>with the specific metadata item that you want to query. For example, to retrieve the instance ID, run the following command:curl http://100.100.100.200/latest/meta-data/instance-idThis table lists the basic metadata items that you can retrieve for an ECI instance.
Parameter
Description
/dns-conf/nameservers
The instance's DNS configuration.
/eipv4
The instance's Elastic IP Address (IPv4).
/hostname
The instance's hostname. This corresponds to the ContainerGroupName.
/instance-id
The instance ID.
/mac
The instance's MAC address.
/network/interfaces/
A list of MAC addresses for the network interfaces.
/network/interfaces/macs/[mac]/network-interface-id
The network interface ID. Replace [mac] with the MAC address of the instance.
/network/interfaces/macs/[mac]/netmask
The subnet mask for the network interface.
/network/interfaces/macs/[mac]/vswitch-cidr-block
The IPv4 CIDR block of the vSwitch to which the network interface belongs.
/network/interfaces/macs/[mac]/vpc-cidr-block
The IPv4 CIDR block of the VPC to which the network interface belongs.
/network/interfaces/macs/[mac]/private-ipv4s
A list of private IPv4 addresses assigned to the network interface.
/network/interfaces/macs/[mac]/vpc-ipv6-cidr-blocks
The IPv6 CIDR block of the VPC to which the network interface belongs. This item is available only for instances in a VPC for which IPv6 is enabled.
/network/interfaces/macs/[mac]/vswitch-id
The ID of the vSwitch to which the network interface belongs.
/network/interfaces/macs/[mac]/vpc-id
The ID of the VPC to which the network interface belongs.
/network/interfaces/macs/[mac]/primary-ip-address
The primary private IP address of the network interface.
/network/interfaces/macs/[mac]/gateway
The IPv4 gateway address for the network interface.
/instance/max-netbw-egress
The maximum outbound internal bandwidth of the instance type. Unit: Kbit/s.
/instance/max-netbw-ingress
The maximum inbound internal bandwidth of the instance type. Unit: Kbit/s.
/network/interfaces/macs/[mac]/ipv6s
A list of IPv6 addresses assigned to the network interface. This item is available only for instances in a VPC for which IPv6 is enabled.
/network/interfaces/macs/[mac]/ipv6-gateway
The IPv6 gateway address of the VPC to which the network interface belongs.
/network/interfaces/macs/[mac]/vswitch-ipv6-cidr-block
The IPv6 CIDR block of the vSwitch to which the network interface belongs. This item is available only for instances in a VPC for which IPv6 is enabled.
/private-ipv4
The private IPv4 address of the instance.
/ntp-conf/ntp-servers
The address of the NTP server.
/owner-account-id
The ID of the Alibaba Cloud account that owns the instance.
/region-id
The instance's region.
/serial-number
The serial number of the instance.
/vpc-id
The ID of the VPC where the instance is located.
/vpc-cidr-block
The CIDR block of the VPC where the instance is located.
/vswitch-cidr-block
The CIDR block of the vSwitch where the instance is located.
/vswitch-id
The ID of the vSwitch where the instance is located.
/zone-id
The instance's zone.
/ram/security-credentials/[role-name]
The STS temporary credentials for the instance's RAM role. These are available only if a RAM role is attached. Replace [role-name] with the RAM role name. If you omit [role-name], the system returns the name of the attached RAM role.
Method 2: Configure container environment variables
You can also retrieve instance information by setting environment variables for the containers in an ECI instance. This method provides access to metadata such as the instance ID, instance name, region, zone, and container name.
|
Key |
Value |
Description |
|
eci_id |
__ECI_ID__ |
The ID of the instance. |
|
eci_name |
__ECI_NAME__ |
The name of the instance. |
|
region_id |
__REGION_ID__ |
The region where the instance is located. |
|
zone_id |
__ZONE_ID__ |
The zone where the instance is located. |
|
container_name |
__CONTAINER_NAME__ |
The name of the container. |
Example configuration:
OpenAPI
When you call the CreateContainerGroup API operation to create an ECI instance, you can set environment variables by using the Container.N.EnvironmentVar.N.Key and Container.N.EnvironmentVar.N.Value parameters. The following example shows how to configure the parameters:
ContainerGroupName=test-env
Container.1.Image=registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
Container.1.Name=container-1
# Set custom 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 ECI instance in the Elastic Container Instance console, in the Container Configurations section, select a container, expand its Advanced Settings, and then enable the Environment Variable switch to add environment variables. For example, you can add the following environment variables:
eci_id:__ECI_ID__(Instance ID)eci_name:__ECI_NAME__(Instance name)region_id:__REGION_ID__(Region ID)zone_id:__ZONE_ID__(Zone ID)container_name:__CONTAINER_NAME__(Container name)
After you create the ECI instance, connect to the container to verify the configuration. For more information, see Connect to an ECI instance by using Workbench.
The following example shows the output of the env command in the container:
Elastic Container Service!
# env
HOSTNAME=test-env
ECI CONTAINER TYPE=normal
zone_id=cn-shanghai-f
HOME=/root
eci_name=test-env
container_name=container-1
TERM=xterm
NGINX_VERSION=1.14.2-1~stretch
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=1.14.2.0.2.6-1~stretch
PWD=/
eci_id=eci-uf63a8afxxx5bx
region_id=cn-shanghai
#