|
| 1 | +Docker Stack |
| 2 | + |
| 3 | +This is used for creating a muiltcontainer architecture using docker compose and deploy it in the swarm cluster. |
| 4 | + |
| 5 | +Docker compose + swarm = docker stack. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +Commands |
| 10 | + |
| 11 | +1.To see list of stack |
| 12 | + |
| 13 | +# docker stack ls |
| 14 | + |
| 15 | +2.To create a stck |
| 16 | + |
| 17 | +# docker stack deploy -c stack_filename/docker_composefilename stackname |
| 18 | + |
| 19 | +3.To see list of nodes where the stack service is running |
| 20 | + |
| 21 | +# docker stack ps stackname |
| 22 | + |
| 23 | +4.To see list of services in docker stack |
| 24 | + |
| 25 | +# docker stack service stack_name |
| 26 | + |
| 27 | +5.To delete a stck |
| 28 | + |
| 29 | +# docker stack rm stack_name |
| 30 | + |
| 31 | + |
| 32 | +Example: |
| 33 | + |
| 34 | +Create a directory==>goinside the directory |
| 35 | + |
| 36 | +Create a yml file ==> vim stack1.yml |
| 37 | + |
| 38 | +--- |
| 39 | +version: 3 |
| 40 | + |
| 41 | +services: |
| 42 | + mydb: |
| 43 | + image: mysql:5 |
| 44 | + environment: |
| 45 | + MYSQL_ROOT_PASSWORD: edureka |
| 46 | + |
| 47 | + myworsdpress: |
| 48 | + image: wordpress |
| 49 | + ports: |
| 50 | + -5050:80 |
| 51 | + deploy: |
| 52 | + replicas: 3 |
| 53 | +... |
| 54 | + |
| 55 | +To deploy the stack service |
| 56 | + |
| 57 | +# docker stack deploy -c stack1.yml wordpress |
| 58 | + |
| 59 | +Here -c : allows you to create docker compose file with any name |
| 60 | + |
| 61 | +3.To see where the stack replicas are running |
| 62 | + |
| 63 | +# docker stack ps wordpress |
| 64 | + |
| 65 | +3 replicas of wordpress and 1 mysql replica will be created |
| 66 | + |
| 67 | +4.To remove the stack |
| 68 | + |
| 69 | +# docker stack rm wordpress |
| 70 | + |
| 71 | +Example2: |
| 72 | + |
| 73 | +Starting a jenkins linked with 2 tomcat servers, creating a stack file and adding details like no: of replicas and which service to run on which node |
| 74 | + |
| 75 | +# vim stack2.yml |
| 76 | +--- |
| 77 | +version: 3 |
| 78 | + |
| 79 | +services: |
| 80 | + jenkins: |
| 81 | + image: jenkins |
| 82 | + ports: |
| 83 | + -5050:8080 |
| 84 | + deploy: |
| 85 | + replicas: 2 |
| 86 | + placement: |
| 87 | + constraints: |
| 88 | + -node.hostname == Manager |
| 89 | + |
| 90 | + qaserver: |
| 91 | + image: tomcat |
| 92 | + ports: |
| 93 | + -6060:8080 |
| 94 | + deploy: |
| 95 | + replicas: 2 |
| 96 | + placement: |
| 97 | + constraints: |
| 98 | + -node.hostname == Worker1 |
| 99 | + |
| 100 | + prodserver: |
| 101 | + image: tomcat |
| 102 | + ports: |
| 103 | + -7070:8080 |
| 104 | + deploy: |
| 105 | + replicas: 3 |
| 106 | + placement: |
| 107 | + constraints: |
| 108 | + -node.hostname == Worker2 |
| 109 | + |
| 110 | +… |
| 111 | + |
| 112 | +:wq! |
| 113 | + |
| 114 | +# docker stack deploy -c stack2.yml stack-ci-cd |
| 115 | + |
| 116 | +# docker stack ps stackci-cd |
| 117 | + |
| 118 | +So all the replicas will be created will be running on thesre respective manager, worker 1 and worker 2 node as mentioned in stack file |
| 119 | + |
| 120 | +Here under constraints we have give 1 node, but if you want we can give more than 1 node also, so if one node is not available then the service will start running on another node. |
| 121 | +If in conatraint you have given only 1 node, then if the node is out of resources or is down, then the services will wait until that node comes alive again. |
| 122 | + |
| 123 | +Example2: Advance: |
| 124 | +Create a docker stack file to setup the selenium testing environment and also put an upper limit on the h/w allocation |
| 125 | + |
| 126 | +--- |
| 127 | +version: '3' |
| 128 | +services: |
| 129 | + hub: |
| 130 | + image: selenium/hub |
| 131 | + ports: |
| 132 | + -4444:4444 |
| 133 | + deploy: |
| 134 | + replicas: 1 |
| 135 | + resources: |
| 136 | + limits: |
| 137 | + cpus: "0.1" |
| 138 | + memory: "200M" |
| 139 | + chrome: |
| 140 | + image: selenium/node-chrome-debug |
| 141 | + ports: |
| 142 | + -5901:5900 |
| 143 | + deploy: |
| 144 | + replicas: 2 |
| 145 | + resources: |
| 146 | + limits: |
| 147 | + cpus: "0.01" |
| 148 | + memory: "100M" |
| 149 | + |
| 150 | +firefox: |
| 151 | + image: selenium/node-firefox-debug |
| 152 | + ports: |
| 153 | + -5901:5900 |
| 154 | + deploy: |
| 155 | + replicas: 1 |
| 156 | + |
| 157 | +… |
| 158 | + |
| 159 | +2.To deploy the services from above stack file: |
| 160 | + |
| 161 | +# docker stack deploy -c stack3.yml selenium |
| 162 | + |
| 163 | +3.View the services |
| 164 | + |
| 165 | +# docker stack ps selenium |
| 166 | + |
0 commit comments