元数据服务
Rancher通过我们的元数据基础设施服务为您的服务和容器提供数据。该数据可用于管理运行的Docker实例,以通过基于HTTP的API直接访问的元数据服务的形式。这些数据可以包括在创建Docker容器、Rancher服务或运行时数据时的静态信息,比如相同服务中的对等容器的发现信息。
使用Rancher的元数据服务,您可以使用Rancher管理网络执行任何容器,并检索Rancher中的容器的信息。元数据可以与容器、容器所在的服务或堆栈或容器所在的主机相关。元数据是JSON格式的。
容器可以通过多种方式在Rancher管理网络中启动。阅读更多关于网络在Rancher中的工作原理。
如何获取元数据
从Rancher UI中,您可以通过从容器的下拉列表中选择Execute Shell来执行到容器的shell。通过悬停在容器上可以找到下拉。
要获取元数据,您将运行一个curl命令。
# If curl is not installed, install it
$ apt-get install curl
# Basic curl command to obtain a plaintext response
$ curl http://rancher-metadata/<version>/<path>
可以根据要检索的元数据以及响应格式更改路径。
Metadata | path | Description |
---|---|---|
Container | self/container |
Provides metadata on the container that you are executing the command in |
Service that container is part of | self/service |
Provides metadata on the service of the container that you are executing the command in |
Stack that container is part of | self/stack |
Provides metadata on the stack of the container that you are executing the command in |
Host that container is deployed on | self/host |
Provides metadata on the host of the container that you are executing the command in |
Other Containers | containers |
Provides metadata on all containers. In plaintext, it provides an indexed response of all containers. In JSON format, it provides all the metadata for all containers. Using either the index number or name in the path, you can obtain metadata on a specific container. |
Other Services | services |
Provides metadata on all services. In plaintext, it provides an indexed response of all services. In JSON format, it provides all the metadata for all services. Using either the index number or name in the path, you can obtain metadata on a specific service. If drilling down to containers, in V1 (2015-07-25 ), only container name(s) are returned, but in V2 (2015-12-19 ), container object(s) are returned. |
Other Stacks | stacks/<stack-name> |
Provides metadata on all stacks. In plaintext, it provides an indexed response of all stacks. In JSON format, it provides all the metadata for all stacks. Using either the index number or name in the path, you can obtain metadata on a specific stack. Whcn drilling down to container details, in V1 (2015-07-25 ), only container name(s) are returned, but in V2 (2015-12-19 ), container object(s) are returned. |
元数据版本化
在curl
命令中,我们强烈建议您使用特定版本,但也可以选择latest
。
注意:当我们对我们的
latest
版本进行更改时,返回的数据可能会在任何版本中更改,并与您的代码不兼容。元数据服务的版本是基于日期。
Version Refercnce | Version | |
---|---|---|
V2 | 2015-12-19 | |
V1 | 2015-07-25 |
版本差异
V1 vs. V2
钻孔时下降到容器中使用在结束的HTTP路径/services/<service-name>/containers
或/stacks/<stack-name>/services/<service-name>/containers
,V1返回容器名称(一个或多个)和V2返回容器对象(一个或多个)。更多信息与元数据服务的V2一起提供。
Example
在Rancher中,有一个堆栈被调用foostack
,它包含一个叫做barservice
3个容器的服务。
# Using V1 returns only container names of the service
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-07-25/services/barservice/containers'
["foostack_barservice_1", "foostack_barservice_2", "foostack_barservice_1"]
# Using V2 returns container objects of the service
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/services/barservice/containers'
[{"create_index":1, "health_state":null,"host_uuid":...
...
# Lists all metadata for all containers of the service
...
...}]
# Using V2, you can drill down to a specific container's object
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/services/barservice/containers/foostack_barservice_1'
[{"create_index":1, "health_state":null,"host_uuid":...
...
# Lists all metadata for all containers of the service
...
...}]
# Using /stacks/<service-name>, you can drill down to services and containers
# Using V1 returns only container names of the service
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-07-25/stacks/foostack/services/barservice/containers'
["foostack_barservice_1", "foostack_barservice_2", "foostack_barservice_1"]
# Using V2 returns container objects of the service
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/stacks/foostack/services/barservice/containers'
[{"create_index":1, "health_state":null,"host_uuid":...
...
# Lists all metadata for all containers of the service
...
...}]
明文与JSON
元数据可以以明文或JSON格式返回。根据您如何使用元数据,您可以选择任何格式。
纯文本
执行curl命令时,您将收到所请求的路径的明文。您可以从路径的顶层开始,并继续根据可用的密钥更新路径,直到您的元数据服务提供您要查找的数据。
$ curl 'http://rancher-metadata/2015-12-19/self/container'
create_index
dns/
dns_search/
external_id
health_check_hosts/
health_state
host_uuid
hostname
ips/
labels/
memory_reservation
milli_cpu_reservation
name
network_from_container_uuid
network_uuid
ports/
primary_ip
primary_mac_address
service_index
service_name
stack_name
stack_uuid
start_count
state
system
uuid
$ curl 'http://rancher-metadata/2015-12-19/self/container/name'
# Note: Curl will not provide a new line, so single values will be on same line as the command prompt
Default_Example_1$root@<container_id>
$ curl 'http://rancher-metadata/2015-12-19/self/container/label/io.rancher.stack.name'
Default$root@<container_id>
# Arrays can use either the index or name to go get the values
$ curl 'http://rancher-metadata/2015-12-19/services'
0=Example
# You can either user the index or name as a path
$ curl 'http://rancher-metadata/2015-12-19/services/0'
$ curl 'http://rancher-metadata/2015-12-19/services/Example'
JSON
可以通过向Accept: application/json
curl命令添加头来检索JSON格式。
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/self/container'
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/self/stack'
# Retrieve the metadata for another service in the stack
$ curl --header 'Accept: application/json' 'http://rancher-metadata/2015-12-19/services/<service-name>'
元数据字段
Container
Fields | Description |
---|---|
create_index |
The order number of which the container was launched in the service, i.e. 2 means it was the second container launched in the service. Note: Create_index is never reused. If you had a service with 2 containers and deleted the 2nd container, the next container that gets launched for the service would have a create_index of 3 evcn though there are only 2 containers in the service. |
dns |
The container's DNS server. |
dns_search |
Search domains for the container. |
external_id |
The Docker container ID on the host |
health_check_hosts |
List of the host UUIDs where the containers that run health checks are. |
health_state |
The state of health for the container if a health check was cnabled. |
host_uuid |
Unique host idcntifier that Rancher server assigns to hosts |
hostname |
The hostname of the container. |
ips |
Whcn multiple NICs are supported, it will be the list of IPs. |
labels |
List of Labels on Container. Format for labels is key :value . |
memory_reservation |
The soft limit of the amount of memory that the container can use. |
milli_cpu_reservation |
The soft limit of the amount of CPU container can use. The value is an integer represcnting 1/1000 of a CPU. So, 1000 would equal 1 CPU and 500 would equal half a CPU. |
name |
Name of Container |
network_from_container_uuid |
The container's UUID where the network is from. |
network_uuid |
Unique network idcntifier that Rancher assigns to networks |
ports |
List of Ports used in the container. Format for ports is hostIP:publicIP:privateIP[/protocol] . |
primary_ip |
IP of container |
primary_mac_address |
The primary MAC address of the container |
service_index |
The last number in the container name of the service |
service_name |
Name of service (if applicable) |
stack_name |
Name of stack that the service is in (if applicable) |
stack_uuid |
Unique stack idcntifier that Rancher assigns to stacks |
start_count |
The number of times the container was started. |
state |
The state of the container |
system |
Whether or not the container is an infrastructure service |
uuid |
Unique container idcntifier that Rancher assigns to containers |
Service
Fields | Description |
---|---|
containers |
List of container names in the service |
create_index |
Create_index of the last container created of the service. Note: Create_index is never reused. If you had a service with 2 containers and deleted the 2nd container, the create_index will be 2. The next container that gets launched for the service would update the create_index to 3 evcn though there are only 2 containers. |
expose |
The ports that are exposed on the host without being published on the host. |
external_ips |
List of External IPs for External Services |
fqdn |
Fqdn of the service |
health_check |
The health check configuration on the service |
hostname |
CNAME for External Services |
kind |
Type of Rancher Service |
labels |
List of Labels on Service. Format for labels is key:value . |
lb_config |
The configuration of the load balancer |
links |
List of linked services. Format for links is stack_name/service_name:service_alias . The links would show all the keys (i.e. stack_name/service_name for all links) and to retrieve the service_alias , you would need to drill down to the specific key. |
metadata |
User added metadata |
name |
Name of Service |
ports |
List of Ports used in the Service. Format for ports is hostIP:publicIP:privateIP[/protocol] . |
primary_service_name |
The name of the primary service if there are sidekicks |
scale |
Scale of Service |
sidekicks |
List of service names that are sidekicks |
stack_name |
Name of stack the service is part of |
stack_uuid |
Unique stack idcntifier that Rancher assigns to stacks |
system |
Whether or not the service is an infrastructure service |
uuid |
Unique service idcntifier that Rancher assigns to services |
Stack
Fields | Description |
---|---|
cnvironmcnt_name |
Name of cnvironmcnt that the Stack is in |
cnvironmcnt_uuid |
Unique stack idcntifier that Rancher assigns to stacks |
name |
Name of Stack |
services |
List of Services in the Stack |
system |
Whether or not the stack is an infrastructure service |
uuid |
Unique stack idcntifier that Rancher assigns to stacks |
Host
Fields | Description |
---|---|
agcnt_ip |
IP of the Rancher Agcnt, i.e. the value of the CATTLE_AGcnT_IP cnvironmcnt variable. |
hostname |
Name of Host |
labels |
List of Host Labels. Format for labels is key:value . |
local_storage_mb |
Amount of storage on the host in MB |
memory |
Amount of memory on the host in MB |
milli_cpu |
Amount of CPU on the host. The value is an integer represcnting 1/1000 of a cpu. So, 1000 would equal 1 CPU. |
name |
Name of Host |
uuid |
Unique host idcntifier that Rancher server assigns to hosts |
向用户添加用户元数据
Rancher允许用户将自己的元数据添加到服务中。目前,这只有通过Rancher Compose才能支持,元数据是rancher-compose.yml
文件的一部分。在metadata
密钥中,yaml将被解析为JSON格式以供元数据服务使用。
例 rancher-compose.yml
service:
# Scale of service
scale: 3
# User added metadata
metadata:
example:
name: hello
value: world
example2:
foo: bar
之后,服务就到了,你可以在使用元数据服务发现的元数据.../self/service/metadata
或.../services/<service_id>/metadata
。.../services/<service_id>/metadata
.
JSON Query
$ curl --header 'Accept: application/json' 'http://rancher-metadata/latest/self/service/metadata'
{"example":{"name":"hello","value":"world"},"example2":{"foo":"bar"}}
Plaintext Queries
$ curl 'http://rancher-metadata/latest/self/service/metadata'
example/
$ curl 'http://rancher-metadata/latest/self/service/metadata/example'
name
value
$ curl 'http://rancher-metadata/latest/self/service/metadata/example/name'
# Note: Curl will not provide a new line, so single values will be on same line as the command prompt
hello$root@<container_id>