core@coreos ~ $ cat <<EOF >hello.service [Unit] Description=My Service After=docker.service [Service] TimeoutStartSec=0 ExecStartPre=-/usr/bin/docker kill hello ExecStartPre=-/usr/bin/docker rm hello ExecStartPre=/usr/bin/docker pull busybox ExecStart=/usr/bin/docker run --name hello busybox /bin/sh -c "trap 'exit 0' INT TERM; while true; do echo Hello World; sleep 1; done" ExecStop=/usr/bin/docker stop hello EOF
core@coreos ~ $ fleetctl load hello.service Unit hello.service inactive Unit hello.service loaded on 0682a8f2.../192.168.50.14
core@coreos ~ $ fleetctl start hello.service Unit hello.service launched on 0682a8f2.../192.168.50.14
core@coreos ~ $ fleetctl status hello.service ● hello.service - My Service Loaded: loaded (/run/fleet/units/hello.service; linked-runtime; vendor preset: disabled) Active: active (running) since Mon 2016-06-20 08:52:43 UTC; 9s ago ........... Jun 20 08:52:43 coreos docker[1355]: Status: Image is up to datefor busybox:latest Jun 20 08:52:43 coreos systemd[1]: Started My Service. Jun 20 08:52:50 coreos docker[1367]: Hello World ........... Jun 20 08:52:52 coreos docker[1367]: Hello World
root@cb8e1cb26a7e:/# curl -L -X PUT http://172.17.0.1:2379/v2/keys/message -d value="I am container" {"action":"set","node":{"key":"/message","value":"I am container","modifiedIndex":47075,"createdIndex":47075}}
etcdctl set /foo-service/container2 localhost:2222
etcdctl set /foo-service/container3 localhost:3333
etcdctl rm /foo-service/container2
etcdctl rm /foo-service/container3
1 2 3 4 5
core@coreos ~ $ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"' "/foo-service/container2" key was updated to "localhost:2222" value by "set" action "/foo-service/container3" key was updated to "localhost:3333" value by "set" action "/foo-service/container2" key was updated to "" value by "delete" action "/foo-service/container3" key was updated to "" value by "delete" action
core@coreos ~ $ sudo systemctl enable myweb.service Created symlink from /etc/systemd/system/multi-user.target.wants/myweb.service to /etc/systemd/system/myweb.service. core@coreos ~ $ sudo systemctl start myweb.service
core@coreos ~ $ sudo systemctl status myweb.service ● myweb.service - My Web Service Loaded: loaded (/etc/systemd/system/myweb.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2016-06-21 07:36:22 UTC; 9s ago Process: 2403 ExecStartPost=/usr/bin/etcdctl set /domains/example.com/%m running (code=exited, status=0/SUCCESS) Process: 2391 ExecStartPre=/usr/bin/docker pull coreos/apache (code=exited, status=0/SUCCESS) Process: 2379 ExecStartPre=/usr/bin/docker rm apache1 (code=exited, status=0/SUCCESS) Process: 2369 ExecStartPre=/usr/bin/docker kill apache1 (code=exited, status=1/FAILURE) Main PID: 2402 (docker) Memory: 11.5M CPU: 5.695s CGroup: /system.slice/myweb.service └─2402 /usr/bin/docker run --name apache1 -p 8081:80 coreos/apache /usr/sbin/apache2ctl -D FOREGROUND ...... Jun 21 07:36:19 coreos docker[2391]: Status: Image is up to datefor coreos/apache:latest Jun 21 07:36:22 coreos etcdctl[2403]: running Jun 21 07:36:22 coreos systemd[1]: Started My Web Service.
# 查詢 container 運作狀態 core@coreos ~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 441cf4a6bf54 coreos/apache "/usr/sbin/apache2ctl" 35 seconds ago Up 26 seconds 0.0.0.0:8081->80/tcp apache1
# 確認 web server 正常服務 core@coreos ~ $ curl http://localhost:8081 <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html>
# 查詢 systemd 寫入 etcd 中的資料 core@coreos ~ $ etcdctl ls /domains/example.com /domains/example.com/72bda15890c14ee89b15214c1b87d71f core@coreos ~ $ etcdctl get /domains/example.com/72bda15890c14ee89b15214c1b87d71f running
# 停止 service core@coreos ~ $ sudo systemctl stop myweb.service
# 查詢 container 狀態 core@coreos ~ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 441cf4a6bf54 coreos/apache "/usr/sbin/apache2ctl" About a minute ago Exited (137) 13 seconds ago apache1
standard unit:會被分派到獨立的單一主機上長時間運行的 process,若是機器掛點了,standard unit 就會被轉移到其他正常的主機上重新啟動並繼續執行
global unit:global unit 將會運行在 cluster 中所有的機器上,這種 unit 適合用在像是 monitoring agent,甚至像是 high level 的 orchestration tool,例如:Kubernetes、Mesos、OpenStack …. 等等。
core@coreos ~ $ fleetctl start /etc/systemd/system/myweb.service Unit myweb.service inactive Unit myweb.service launched on 0682a8f2.../192.168.50.14
core@coreos ~ $ fleetctl list-units UNIT MACHINE ACTIVE SUB myweb.service 0682a8f2.../192.168.50.14 activating start-pre
(...... it takes some time to wait .......)
core@coreos ~ $ fleetctl list-units UNIT MACHINE ACTIVE SUB myweb.service 0682a8f2.../192.168.50.14 active running
core@coreos ~ $ curl http://192.168.50.14:8081 <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html>
# 啟動第 1 個 apache container core@coreos ~/demo $ fleetctl start apache@1 Unit apache@1.service inactive Unit apache@1.service launched on 0682a8f2.../192.168.50.14
# 啟動第 2 個 apache container core@coreos ~/demo $ fleetctl start apache@2 Unit apache@2.service inactive Unit apache@2.service launched on 72bda158.../192.168.50.16
# 檢視 units core@coreos ~/demo $ fleetctl list-units UNIT MACHINE ACTIVE SUB apache@1.service 0682a8f2.../192.168.50.14 active running apache@2.service 72bda158.../192.168.50.16 active running
# 驗證 web 服務有正確啟動 core@coreos ~/demo $ curl http://192.168.50.14 <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html> core@coreos ~/demo $ curl http://192.168.50.16 <html><body><h1>It works!</h1> <p>This is the default web page for this server.</p> <p>The web server software is running but no content has been added, yet.</p> </body></html>
接著把 192.168.50.14 這台機器重新開機,此時 service 將會被 fleet 轉移到正常的機器上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
core@coreos ~/demo $ fleetctl list-units UNIT MACHINE ACTIVE SUB apache@1.service 90c1e40c.../192.168.50.15 activating start-pre apache@2.service 72bda158.../192.168.50.16 active running
core@coreos ~/demo $ fleetctl list-units UNIT MACHINE ACTIVE SUB apache@1.service 90c1e40c.../192.168.50.15 activating start-pre apache@2.service 72bda158.../192.168.50.16 active running
core@coreos ~/demo $ fleetctl list-units UNIT MACHINE ACTIVE SUB apache@1.service 90c1e40c.../192.168.50.15 active running apache@2.service 72bda158.../192.168.50.16 active running
存取這些 HA service 比較好的方式是透過 sidekick container,這類的 container 是用來提供像是 service discovery、load balancer、DNS 等工作,不建議直接存取 service container
# 啟動 service discovery core@coreos ~/demo $ fleetctl start apache_discovery@1 Unit apache_discovery@1.service inactive Unit apache_discovery@1.service launched on 90c1e40c.../192.168.50.15 # 啟動 service discovery core@coreos ~/demo $ fleetctl start apache_discovery@2 Unit apache_discovery@2.service inactive Unit apache_discovery@2.service launched on 72bda158.../192.168.50.16
core@coreos ~/demo $ fleetctl list-units UNIT MACHINE ACTIVE SUB apache@1.service 90c1e40c.../192.168.50.15 active running apache@2.service 72bda158.../192.168.50.16 active running apache_discovery@1.service 90c1e40c.../192.168.50.15 active running apache_discovery@2.service 72bda158.../192.168.50.16 active running
core@coreos ~/demo $ etcdctl ls /services/ --recursive /services/website /services/website/apache@1 /services/website/apache@2