Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a container to service in stack via rancher REST api

Tags:

rest

rancher

I'm trying to create and add a container to an already existing service in rancher using the rancher REST api.

I would expect that the following request would create a container and add it to the atlas-mosquitto stack and the mosquitto service (see labels).

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'

below is a pretty print of the body for easy readability:

{
  "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
}

below is the response:

{
   "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"
}

based on this response and this label "io.rancher.project_service.name" : "atlas-mosquitto/mosquitto", I would expect the container to be in that service, however it is not. The container is created, but it ends up being created as a standalone container. What I want is for this created container to be DNS addressable and accessible in rancher UI under the specified stack and service.

like image 884
mrabin Avatar asked Jan 18 '17 22:01

mrabin


People also ask

How do I add a service to rancher?

Adding Services with Rancher Compose To get started, you will need to create a docker-compose. yml file and a rancher-compose. yml file. With Rancher Compose, we can launch all the services in the application at once.

How do I check my rancher service?

If you're new to Docker Compose or Rancher Compose, we recommend using the UI to start your services. IF you click on a stack name, you can view the configuration of the entire stack (i.e. the equivalent docker-compose. yml and rancher-compose. yml files of your stack) by clicking on View Config in the stack drop down.

What is a rancher service?

Rancher is a complete software stack for teams adopting containers. It addresses the operational and security challenges of managing multiple Kubernetes clusters, while providing DevOps teams with integrated tools for running containerized workloads.

What is rancher API?

The Rancher API allows developers to access a platform for deploying and managing containers in production. Rancher provides infrastructure services that include networking, storage services, host management, load balancing, and more.


1 Answers

Ok problem solved. to add a container to a specific stack/service you must first modify the service you want the container to be added to. The service needs to have the tag io.rancher.service.selector.container

for example, let's say I want the REST API created container to be added to my mosquitto service. The docker-compose file for the mosquitto service might look something like this:

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

take a closer look at io.rancher.service.selector.container: stackz=mos-test. This tells rancher to add all containers with the label stackz=mos-test to the mosquitto service.

Now that we've specified this, we can create our container via the rancher REST API using a request like this:

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'

pretty printed body:

{
  "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"
  }
}

since we've added the label "stackz": "mos-test" in this request, this adds the container to the mosquitto because this label matches with the io.rancher.service.selector.container label we set on the mosquitto service.

the container is also now directly DNS addressable from anywhere in the rancher environment. The container created from the above request is named foozy. It is DNS addressable as foozy.rancher.internal

like image 60
mrabin Avatar answered Sep 23 '22 00:09

mrabin