Coreos Small cluster > Пример запуска coreos кластера с контейнерами docker, приложением, базой данных и прокси сервером

По материалам из видео курса:

Getting Started with CoreOS [29 Nov 2016, ENG]

Советы по улучшению, принимаются.


PS. Исходники с Dockerfile, можно взять здесь:

https://github.com/sysadm-ru/coreos-docker-examples/tree/master/02

Они могу понадобиться, если захочется собрать собственные контейнеры или просто посмотреть примеры.


Database and Web

[email protected]

https://github.com/sysadm-ru/coreos-docker-examples/blob/master/02/coreos-rethinkdb/rethinkdb-discovery%40.service



[Unit]
Requires=docker.service
After=docker.service

[Service]
EnvironmentFile=/etc/environment
ExecStart=/bin/sh -c "while true; do etcdctl set /services/rethinkdb/${COREOS_PRIVATE_IPV4} ${COREOS_PRIVATE_IPV4} --ttl 60; sleep 45; done"
ExecStop=/usr/bin/etcdctl rm /services/rethinkdb/${COREOS_PRIVATE_IPV4}

[X-Fleet]
[email protected]%i.service


[email protected]

https://github.com/sysadm-ru/coreos-docker-examples/blob/master/02/coreos-rethinkdb/rethinkdb%40.service



[Unit]
[email protected]%i.service
[email protected]%i.service

[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=10m
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=-/usr/bin/mkdir -p /data/rethinkdb
ExecStartPre=/usr/bin/docker pull rethinkdb
ExecStart=/bin/sh -c '/usr/bin/docker run --name %p-%i  \
    -p 18080:18080               \
    -p 28015:28015               \
    -p 29015:29015               \
    -v /data/rethinkdb/:/data/                          \
    rethinkdb rethinkdb --bind all                \
    --http-port 18080                                   \
    --canonical-address ${COREOS_PRIVATE_IPV4}          \
    $(/usr/bin/etcdctl ls /services/rethinkdb |         \
        xargs -I {} /usr/bin/etcdctl get {} |           \
        sed s/^/"--join "/ | sed s/$/":29015"/ |        \
       tr "\n" " ")'
ExecStop=/usr/bin/docker stop %p-%i
Restart=on-failure

[X-Fleet]
[email protected]%i.service


$ fleetctl start [email protected]{6..7} [email protected]{6..7}


$ fleetctl list-units
UNIT				MACHINE				ACTIVE	SUB
[email protected]	3222ccf3.../172.17.8.104	active	running
[email protected]	50ba5eaf.../172.17.8.106	active	running
[email protected]		3222ccf3.../172.17.8.104	active	running
[email protected]		50ba5eaf.../172.17.8.106	active	running


$ curl 172.17.8.106:18080

Можно подключиться браузером:
http://172.17.8.106:18080

$ etcdctl ls /services/rethinkdb
/services/rethinkdb/172.17.8.104
/services/rethinkdb/172.17.8.106


$ etcdctl ls /services/rethinkdb | xargs -I {} etcdctl get {}
172.17.8.106
172.17.8.104


$ etcdctl ls /services/rethinkdb | xargs -I {} etcdctl get {} | sed s/^/"--join "/ | sed s/$/":29015"/
--join 172.17.8.104:29015
--join 172.17.8.106:29015


$ etcdctl ls /services/rethinkdb | xargs -I {} etcdctl get {} | sed s/^/"--join "/ | sed s/$/":29015"/ | tr "\n" " "
--join 172.17.8.104:29015 --join 172.17.8.106:29015


http://172.17.8.106:4001/v2/keys/services/rethinkdb - rethinkdb api


WebServer

https://github.com/sysadm-ru/coreos-docker-examples/blob/master/02/coreos-gettingstarted-web/helloweb-discovery%40.service



[Unit]
Requires=docker.service
After=docker.service

[Service]
EnvironmentFile=/etc/environment
ExecStart=/bin/sh -c "while true; do etcdctl set /services/web/${COREOS_PRIVATE_IPV4} ${COREOS_PRIVATE_IPV4} --ttl 60; sleep 45; done"
ExecStop=/usr/bin/etcdctl rm /services/web/${COREOS_PRIVATE_IPV4}

[X-Fleet]
[email protected]%i.service


https://github.com/sysadm-ru/coreos-docker-examples/blob/master/02/coreos-gettingstarted-web/helloweb%40.service


[Unit]
[email protected]%i.service
[email protected]%i.service

[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=10m
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=/usr/bin/docker pull marley/coreos-gettingstarted-web
ExecStart=/bin/sh -c '/usr/bin/docker run --name %p-%i  \
    -p 8080:8080                                        \
    -e COREOS_PRIVATE_IPV4=${COREOS_PRIVATE_IPV4}       \
    marley/coreos-gettingstarted-web'
ExecStop=/usr/bin/docker stop %p-%i
Restart=always

[X-Fleet]
[email protected]%i.service


$ fleetctl start [email protected]{3..5} [email protected]{3..5}


// следим за ходом выполнения

$ fleetctl journal -f --lines=100 [email protected]


$ fleetctl list-units
UNIT				MACHINE				ACTIVE	SUB
[email protected]	6d8e85f3.../172.17.8.101	active	running
[email protected]	8a88c8ac.../172.17.8.107	active	running
[email protected]	b386939f.../172.17.8.102	active	running
[email protected]		6d8e85f3.../172.17.8.101	active	running
[email protected]		8a88c8ac.../172.17.8.107	active	running
[email protected]		b386939f.../172.17.8.102	active	running
[email protected]	3222ccf3.../172.17.8.104	active	running
[email protected]	50ba5eaf.../172.17.8.106	active	running
[email protected]		3222ccf3.../172.17.8.104	active	running
[email protected]		50ba5eaf.../172.17.8.106	active	running


Подключился к приложению:

$ curl http://172.17.8.101:8080/greeting
"Hello, User! From 172.17.8.101"


Load Balancing

https://github.com/sysadm-ru/coreos-docker-examples/blob/master/02/coreos-gettingstarted-lb/hellolb%40.service


[Unit]
Requires=docker.service
After=docker.service

[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=10m
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=/usr/bin/docker pull marley/coreos-gettingstarted-lb
ExecStart=/bin/sh -c '/usr/bin/docker run --name %p-%i  \
    -p 8888:8888                                        \
    -e COREOS_PRIVATE_IPV4=${COREOS_PRIVATE_IPV4}       \
    marley/coreos-gettingstarted-lb'
ExecStop=/usr/bin/docker stop %p-%i
Restart=always

[X-Fleet]
[email protected]%i.service


$ fleetctl start [email protected]{1..2}

// следим за ходом выполнения

$ fleetctl journal -f --lines=100 [email protected]


$ fleetctl list-units
UNIT				MACHINE				ACTIVE	SUB
[email protected]		c983059a.../172.17.8.103	active	running
[email protected]		d65c18f4.../172.17.8.105	active	running
[email protected]	6d8e85f3.../172.17.8.101	active	running
[email protected]	8a88c8ac.../172.17.8.107	active	running
[email protected]	b386939f.../172.17.8.102	active	running
[email protected]ervice		6d8e85f3.../172.17.8.101	active	running
[email protected]		8a88c8ac.../172.17.8.107	active	running
[email protected]		b386939f.../172.17.8.102	active	running
[email protected]	3222ccf3.../172.17.8.104	active	running
[email protected]	50ba5eaf.../172.17.8.106	active	running
[email protected]		3222ccf3.../172.17.8.104	active	running
[email protected]		50ba5eaf.../172.17.8.106	active	running


$ curl http://172.17.8.103:8888/greeting
"Hello, User! From 172.17.8.107"

[email protected] ~ $ curl http://172.17.8.103:8888/greeting
"Hello, User! From 172.17.8.101"

[email protected] ~ $ curl http://172.17.8.103:8888/greeting
"Hello, User! From 172.17.8.102"[email protected] ~ $


Меняется веб сервер к которому подключаемся.


Посмотрим что внутри Load Balancer

$ fleetctl ssh [email protected]


$ docker exec -it hellolb-1 bash


# cat /opt/lb/nginx.conf


events {
    worker_connections 4096;
}

http {
    upstream backend {
        server 172.17.8.107:8080;
        server 172.17.8.101:8080;
        server 172.17.8.102:8080;
    }
    server {
        listen 8888;
        location / {
            proxy_pass http://backend;
        }
    }
}