Инструменты пользователя

Инструменты сайта


docker:swarm:network

Это старая версия документа!


overlay network

Для примера развернем сервер на nodejs на нескольких нодах.

Для начала сбилдим образ из репозитория http://webmastermsk.ru:30000/serg/docker-demo-3.git

📁 docker-demo-3
 +---Dockerfile
 \---Dockerfile
 \---index.js

Dockerfile с nodejs:

FROM node:14-alpine
RUN apk add curl
WORKDIR /opt/app
ADD index.js .
CMD ["node", "./index.js"]

сам сервер index.js

const http = require('http');
const { networkInterfaces } = require('os');
 
const port = 3000;
const requestHandler = (request, response) => {
	const IPs = getIPs();
	response.end(JSON.stringify(IPs));
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
	if (err) {
		return console.log('Ошибка', err)
	}
	console.log(`Сервер запущен на порту ${port}`)
});
 
const getIPs = () => {
	const nets = networkInterfaces();
	const results = {};
 
	for (const name of Object.keys(nets)) {
		for (const net of nets[name]) {
			if (net.family === 'IPv4' && !net.internal) {
				if (!results[name]) {
					results[name] = [];
				}
				results[name].push(net.address);
			}
		}
	}
	return results;
}

собираем образ

vagrant@server3:~$ git clone http://webmastermsk.ru:30000/serg/docker-demo-3.git
Cloning into 'docker-demo-3'...
remote: Enumerating objects: 18, done.
remote: Total 18 (delta 0), reused 0 (delta 0), pack-reused 18
Unpacking objects: 100% (18/18), 5.70 KiB | 233.00 KiB/s, done.
 
vagrant@server3:~$ cd docker-demo-3
 
 
vagrant@server3:~/docker-demo-3$ docker build  -t localhost:5000/ip:latest .
[+] Building 6.0s (9/9) FINISHED                                                                       docker:default
 => [internal] load build definition from Dockerfile                                                             0.1s
 => => transferring dockerfile: 132B                                                                             0.0s
 => [internal] load .dockerignore                                                                                0.0s
 => => transferring context: 2B                                                                                  0.0s
 => [internal] load metadata for docker.io/library/node:14-alpine                                                1.3s
 => CACHED [1/4] FROM docker.io/library/node:14-alpine@sha256:434215b487a329c9e867202ff89e704d3a75e554822e07f3e  0.0s
 => [internal] load build context                                                                                0.0s
 => => transferring context: 779B                                                                                0.0s
 => [2/4] RUN apk add curl                                                                                       4.5s
 => [3/4] WORKDIR /opt/app                                                                                       0.0s
 => [4/4] ADD index.js .                                                                                         0.0s 
 => exporting to image                                                                                           0.1s 
 => => exporting layers                                                                                          0.1s 
 => => writing image sha256:fba7632540bebadbd6b336ba39860933632795dba668fa9ed85b51c0a22d5f3d                     0.0s 
 => => naming to localhost:5000/ip:latest          

Заливаем собранный образ в реестр

vagrant@server3:~/docker-demo-3$ docker push localhost:5000/ip:latest
The push refers to repository [localhost:5000/ip]
18cafcc3f78c: Pushed 
ec5ff2197706: Pushed 
31e6ec6c7217: Pushed 
31f710dc178f: Mounted from api 
a599bf3e59b8: Mounted from api 
e67e8085abae: Mounted from api 
f1417ff83b31: Mounted from api 
latest: digest: sha256:f2f8eea3183aff907842979e68d0d077fd38e13b88f465adcd21d099ff58913b size: 1783
vagrant@server3:~/docker-demo-3$ ls

Поднимаем сервак на 3 нодах на 3000 порту

vagrant@server1:~$ docker service create --publish 3000:3000 --name ip --replicas=3 localhost:5000/ip:latest
zdjrd1q2k15r24tg5jt0rn3dq
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged 
 
vagrant@server1:~$ docker service ls
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
zdjrd1q2k15r   ip         replicated   3/3        localhost:5000/ip:latest   *:3000->3000/tcp
3srd0mspdl9a   registry   replicated   1/1        registry:latest            *:5000->5000/tcp
 
vagrant@server1:~$ docker service ps ip
ID             NAME      IMAGE                      NODE      DESIRED STATE   CURRENT STATE            ERROR     PORTS
fuowub8ay4vk   ip.1      localhost:5000/ip:latest   server5   Running         Running 41 seconds ago             
9hmorz7kom2t   ip.2      localhost:5000/ip:latest   server3   Running         Running 41 seconds ago             
booxxr5fhm8m   ip.3      localhost:5000/ip:latest   server4   Running         Running 38 seconds ago

Тестируем со сторонней машины

Делаем курловый запрос и смотрим что в ответе каждый раз новый ip адрес

werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.11"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.9"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.10"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.11"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.9"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ curl localhost:3000
{"eth0":["10.0.0.10"],"eth1":["172.18.0.3"]}
werwolf@werwolf-System-Product-Name:~$ 
docker/swarm/network.1703015093.txt.gz · Последние изменения: 2023/12/19 22:44 — werwolf