5

我正在尝试使用rancher REST api创建容器并将其添加到rancher中已经存在的服务。

我希望以下请求将创建一个容器并将其添加到 atlas-mosquitto 堆栈和 mosquitto 服务(请参阅标签)。

curl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"count":1, "imageUuid":"docker:dummy/atlas/mosquitto:0.0.8", "name":"atlas-mosquitto_mosquitto_dummy_name", "startOnCreate":true, "labels": {"io.rancher.service.deployment.unit": "acdaf002-e1d0-4625-ba9c-3e1dbc584a14", "io.rancher.project.name": "atlas-mosquitto", "io.rancher.container.pull_image": "always", "io.rancher.scheduler.affinity:container_label_soft_ne": "io.rancher.stack_service.name=atlas-mosquitto/mosquitto", "io.rancher.service.launch.config": "io.rancher.service.primary.launch.config", "io.rancher.project_service.name": "atlas-mosquitto/mosquitto", "io.rancher.stack.name": "atlas-mosquitto", "io.rancher.stack_service.name": "atlas-mosquitto/mosquitto"},  "publishAllPorts":false, "privileged":false, "stdinOpen":false, "tty":false, "readOnly":false' \
'https://rancher-ha.dummy.info/v1/projects/1a29/containers'

下面是一个漂亮的正文,便于阅读:

{
  "count": 1,
  "imageUuid": "docker:dummy\/atlas\/mosquitto:0.0.8",
  "name": "atlas-mosquitto_mosquitto_dummy_name",
  "startOnCreate": true,
  "labels": {
    "io.rancher.service.deployment.unit": "acdaf002-e1d0-4625-ba9c-3e1dbc584a14",
    "io.rancher.project.name": "atlas-mosquitto",
    "io.rancher.container.pull_image": "always",
    "io.rancher.scheduler.affinity:container_label_soft_ne": "io.rancher.stack_service.name=atlas-mosquitto\/mosquitto",
    "io.rancher.service.launch.config": "io.rancher.service.primary.launch.config",
    "io.rancher.project_service.name": "atlas-mosquitto\/mosquitto",
    "io.rancher.stack.name": "atlas-mosquitto",
    "io.rancher.stack_service.name": "atlas-mosquitto\/mosquitto"
  },
  "publishAllPorts": false,
  "privileged": false,
  "stdinOpen": false,
  "tty": false,
  "readOnly": false
}

以下是回复:

{
   "readOnly" : false,
   "created" : "2016-12-07T01:15:04Z",
   "memory" : null,
   "securityOpt" : null,
   "createIndex" : null,
   "logConfig" : null,
   "state" : "creating",
   "links" : {
      "serviceExposeMaps" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/serviceexposemaps",
      "healthcheckInstanceHostMaps" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/healthcheckinstancehostmaps",
      "instances" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/instances",
      "services" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/services",
      "ports" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/ports",
      "hosts" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/hosts",
      "instanceLinks" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/instancelinks",
      "credentials" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/credentials",
      "volumes" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/volumes",
      "account" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/account",
      "serviceEvents" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/serviceevents",
      "stats" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/stats",
      "mounts" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/mounts",
      "targetInstanceLinks" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/targetinstancelinks",
      "self" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139",
      "instanceLabels" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/instancelabels",
      "containerStats" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/containerstats"
   },
   "labels" : {
      "io.rancher.container.pull_image" : "always",
      "io.rancher.project.name" : "atlas-mosquitto",
      "io.rancher.stack.name" : "atlas-mosquitto",
      "io.rancher.stack_service.name" : "atlas-mosquitto/mosquitto",
      "io.rancher.service.deployment.unit" : "acdaf002-e1d0-4625-ba9c-3e1dbc584a14",
      "io.rancher.scheduler.affinity:container_label_soft_ne" : "io.rancher.stack_service.name=atlas-mosquitto/mosquitto",
      "io.rancher.service.launch.config" : "io.rancher.service.primary.launch.config",
      "io.rancher.project_service.name" : "atlas-mosquitto/mosquitto"
   },
   "dnsSearch" : null,
   "startCount" : 0,
   "privileged" : false,
   "workingDir" : null,
   "id" : "1i9139",
   "uuid" : "bc64996c-4a9f-40bd-91bc-e352d84604aa",
   "transitioningProgress" : null,
   "lxcConf" : null,
   "hostId" : null,
   "pidMode" : null,
   "transitioningMessage" : "In Progress",
   "requestedHostId" : null,
   "dataVolumes" : null,
   "startOnCreate" : true,
   "domainName" : null,
   "volumeDriver" : null,
   "cpuSet" : null,
   "restartPolicy" : null,
   "networkContainerId" : null,
   "user" : null,
   "dataVolumeMounts" : null,
   "nativeContainer" : false,
   "deploymentUnitUuid" : null,
   "hostname" : null,
   "tty" : false,
   "capDrop" : null,
   "name" : "atlas-mosquitto_mosquitto_2",
   "removed" : null,
   "primaryIpAddress" : null,
   "cpuShares" : null,
   "imageUuid" : "docker:distribution.dummy.info/atlas/mosquitto:0.0.8",
   "createdTS" : 1481073304000,
   "systemContainer" : null,
   "environment" : {
      "AUTH_SERVICE_ALIAS" : "auth.atlas-auth.rancher.internal",
      "AUTH_SERVICE_PORT" : "8100"
   },
   "stdinOpen" : false,
   "accountId" : "1a29",
   "registryCredentialId" : null,
   "description" : "foo",
   "command" : null,
   "version" : "0",
   "build" : null,
   "healthCheck" : null,
   "expose" : null,
   "entryPoint" : null,
   "publishAllPorts" : false,
   "memorySwap" : null,
   "type" : "container",
   "dataVolumesFrom" : null,
   "actions" : {
      "error" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/?action=error",
      "remove" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/?action=remove",
      "stop" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/?action=stop",
      "setlabels" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/?action=setlabels",
      "start" : "https://rancher-ha.dummy.info/v1/projects/1a29/containers/1i9139/?action=start"
   },
   "blkioDeviceOptions" : null,
   "externalId" : null,
   "healthState" : null,
   "kind" : "container",
   "ports" : null,
   "extraHosts" : null,
   "devices" : null,
   "networkMode" : "managed",
   "capAdd" : null,
   "dns" : null,
   "firstRunning" : null,
   "transitioning" : "yes"
}

基于此响应和此标签"io.rancher.project_service.name" : "atlas-mosquitto/mosquitto",我希望容器在该服务中,但事实并非如此。容器已创建,但最终被创建为独立容器。我想要的是这个创建的容器可以在指定堆栈和服务下的 Rancher UI 中进行 DNS 寻址和访问。

4

1 回答 1

6

好的问题解决了。要将容器添加到特定堆栈/服务,您必须首先修改要添加容器的服务。服务需要有标签io.rancher.service.selector.container

例如,假设我希望将 REST API 创建的容器添加到我的 mosquitto 服务中。mosquitto 服务的 docker-compose 文件可能如下所示:

moquitto:
  image: distribution.foofee.com/atlas/mosquitto:0.0.23
  environment:
    AUTH_SERVICE_PORT: 8100
    AUTH_SERVICE_ALIAS: auth.atlas-auth.rancher.internal
  labels:
    io.rancher.service.selector.container: stackz=mos-test

仔细看看io.rancher.service.selector.container: stackz=mos-test。这告诉牧场主将所有带有标签的容器添加stackz=mos-test到 mosquitto 服务中。

现在我们已经指定了这一点,我们可以通过 Rancher REST API 使用如下请求创建我们的容器:

curl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"count":1, "imageUuid":"docker:distribution.foo.com/atlas/mosquitto:0.0.23", "name":"foozy", "startOnCreate":true, "command":[], "publishAllPorts":false, "privileged":false, "stdinOpen":false, "tty":false, "restartPolicy":null, "readOnly":false, "build":null, "networkMode":"managed", "labels": {"stackz": "mos-test", "io.rancher.container.network": "true", "io.rancher.container.dns": "true"}}' \
'https://rancher.us-west-2.foo.com/v1/projects/1a5/containers'

漂亮的印刷体:

{
  "count": 1,
  "imageUuid": "docker:distribution.foo.com\/atlas\/mosquitto:0.0.23",
  "name": "foozy",
  "startOnCreate": true,
  "command": [

  ],
  "publishAllPorts": false,
  "privileged": false,
  "stdinOpen": false,
  "tty": false,
  "restartPolicy": null,
  "readOnly": false,
  "build": null,
  "networkMode": "managed",
  "labels": {
    "stackz": "mos-test",
    "io.rancher.container.network": "true",
    "io.rancher.container.dns": "true"
  }
}

因为我们"stackz": "mos-test"在这个请求中添加了标签,所以这会将容器添加到 mosquitto,因为这个标签与io.rancher.service.selector.container我们在 mosquitto 服务上设置的标签匹配。

该容器现在也可以从 Rancher 环境中的任何位置直接进行 DNS 寻址。根据上述请求创建的容器名为 foozy。它是 DNS 可寻址的foozy.rancher.internal

于 2017-01-18T23:02:09.610 回答