Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a service to a stack after the stack has been deployed

I’m trying to add a service to a stack after the stack was already deployed. But this new service is having trouble communicating with services (redis) inside the stack.

This is my current understanding of stacks and services, please let me know if there are any inaccuracies.

Stacks are an abstraction on top of services that provide useful utilities like DNS so services across a stack can communicate with one another. Stacks allow us to logically separate out groups of services, that might be running on the same swarm (so different development teams can share the same swarm).

I would like to first deploy a stack to a swarm (via compose file) and then periodically add containers like the one described in this article about one-shot containers. These containers are different because they are performing long, stateful operations. They need to be spun up with some initial state, do their work, and then go away. They are different because they don’t need to be replicated, or load balanced.

Essentially what I’m trying to do is:

Launch a “stack” like this:

docker stack deploy --with-registry-auth --compose-file docker-compose.yml my-stack

And then some time later when certain criteria is met, add a container like this:

docker service create -name statefulservice reponame/imagename

And this generally behaves as expected, except statefulservice isn’t able to talk to redis inside my-stack.

I’m confident that statefulservice is engineered correctly because when it’s added to the docker-compose.yml it behaves as expected.

A further detail that may or may not be relevant is that the command to create a new service is issued from a container within the swarm. This happens using the go sdk for docker, and I’m using it the way the one-shot container article described

The reason I suspect this isn’t relevant: I still run into this issue when I do this operation via docker-cli only (and not use the docker sdk for go).

like image 893
Parth Mehrotra Avatar asked Jul 05 '18 20:07

Parth Mehrotra


People also ask

What service mode is used to deploy a single task of a service to a node?

A global service is a service that runs one task on every node. There is no pre-specified number of tasks. Each time you add a node to the swarm, the orchestrator creates a task and the scheduler assigns the task to the new node.

What are the two types of docker swarm services?

Swarm mode has two types of services: replicated and global. For replicated services, you specify the number of replica tasks for the swarm manager to schedule onto available nodes.

What is used to deploy stacks on docker Swarm?

When running Docker Engine in swarm mode, you can use docker stack deploy to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file. The docker stack deploy command supports any Compose file of version “3.0” or above.

What is difference between service and container in docker?

Docker Container is a runtime instance of Docker Image. Docker Service can run one type of Docker Images on various containers locating on different nodes to perform the same functionality. Docker Stack consists of multiple Docker Services.


2 Answers

When you deploy a stack like this:

docker stack deploy --with-registry-auth --compose-file docker-compose.yml my-stack

It creates a network called my-stack_default

So to launch a service that can communicate with services that are in that stack you need to launch them like this:

docker service create -name statefulservice --network my-stack_default reponame/imagename
like image 200
Parth Mehrotra Avatar answered Sep 26 '22 01:09

Parth Mehrotra


It would help to have a working example, but:

My guess is you don't have the services on the same docker network. Even if you don't manually assign stack services to a network (which is fine), The stack command creates one where all services in that stack are attached to. You'll need to specify that overlay network in your subsequent service create commands so they can find each other in DNS and communicate inside the swarm.

For example, if I create a stack called nginx, it will add all those services (unless configured otherwise in stack file) to an overlay network called nginx_default

like image 25
Bret Fisher Avatar answered Sep 27 '22 01:09

Bret Fisher