Running Service on CoreOS over Docker and Manage It with Fleet

  • Posted on: 18 September 2014
  • By: oon
Running Service on CoreOS with Docker and Manage It with Fleet

Punya server dengan sistem operasi CoreOS sekarang gampang banget, sudah ada di cloud, mau re-install berkali-kalipun gampang dan murah, sejam sekitar $0.007 (kalo nilai tukar USD ke IDR 12.000, kurang lebih itu 84 rupiah per jam).

Nah terus abis install CoreOS ngapain? Ya bagian paling kerennya ini yang ditungguin, jalanin service di atas CoreOS sebagai container menggunakan Docker [1].

core@cx2 ~ $ fleetctl list-machines
MACHINE		IP		METADATA
501b28c1...	10.10.84.88	hostname=cx3,region=sg
53f623f2...	10.10.84.87	hostname=cx2,region=sg
...
core@cx2 ~ $ vim hello.service
core@cx2 ~ $ fleetctl submit hello.service
core@cx2 ~ $ fleetctl list-unit-files
UNIT		HASH	DSTATE		STATE		TARGET
hello.service	0db9192	inactive	inactive	-
core@cx2 ~ $ fleetctl cat hello.service
[Unit]
Description=Hello 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 "while true; do echo Hello World; sleep 2; done"
ExecStop=/usr/bin/docker stop hello
core@cx2 ~ $ fleetctl load hello.service
Unit hello.service loaded on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl list-unit-files
UNIT		HASH	DSTATE	STATE	TARGET
hello.service	0db9192	loaded	loaded	501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl start hello.service
Unit hello.service launched on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl list-units     
UNIT		MACHINE				ACTIVE	SUB
hello.service	501b28c1.../10.10.84.88	active	running
core@cx2 ~ $ fleetctl stop hello.service
Unit hello.service loaded on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl list-units
UNIT		MACHINE				ACTIVE		SUB
hello.service	501b28c1.../10.10.84.88	deactivating	stop
core@cx2 ~ $ fleetctl list-unit-files
UNIT		HASH	DSTATE	STATE	TARGET
hello.service	0db9192	loaded	loaded	501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl unload hello.service
Unit hello.service inactive
core@cx2 ~ $ fleetctl list-units
UNIT	MACHINE	ACTIVE	SUB
core@cx2 ~ $ fleetctl list-unit-files
UNIT		HASH	DSTATE		STATE		TARGET
hello.service	0db9192	inactive	inactive	-
core@cx2 ~ $ fleetctl destroy hello.service
Destroyed hello.service
core@cx2 ~ $ fleetctl list-unit-files
UNIT	HASH	DSTATE	STATE	TARGET

Perhatikan bahwa langkah-langkah yang dilakukan dalam maintain service menggunakan fleet yaitu:

  • fleetctl load ...
  • fleetctl start ...
  • fleetctl stop ...
  • fleetctl unload ...
  • fleetctl destroy ...

Jika diperhatikan dengan seksama, perintah di atas dilakukan pada server cx2, kemudian saat service running, proses dilakukan di cx3 (server CoreOS lain dalam cluster tersebut).

Nah selanjutnya untuk memastikan bahwa service yang dieksekusi running well, bisa dilihat dari langkah-langkah berikut.


core@cx2 ~ $ fleetctl load hello.service
Unit hello.service loaded on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl start hello.service
Unit hello.service launched on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl status hello.service
โ— hello.service - Hello Service
   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime)
   Active: active (running) since Wed 2014-11-26 16:07:05 UTC; 5s ago
  Process: 2422 ExecStartPre=/usr/bin/docker pull busybox (code=exited, status=0/SUCCESS)
  Process: 2412 ExecStartPre=/usr/bin/docker rm hello (code=exited, status=0/SUCCESS)
  Process: 2406 ExecStartPre=/usr/bin/docker kill hello (code=exited, status=0/SUCCESS)
 Main PID: 2432 (docker)
   CGroup: /system.slice/hello.service
           โ””โ”€2432 /usr/bin/docker run --name hello busybox /bin/sh -c while true; do echo Hello World; sleep 2; done

Nov 26 16:07:04 cx3 docker[2406]: hello
Nov 26 16:07:04 cx3 docker[2412]: hello
Nov 26 16:07:05 cx3 docker[2422]: busybox:latest: The image you are pulling has been verified
Nov 26 16:07:05 cx3 docker[2422]: Status: Image is up to date for busybox:latest
Nov 26 16:07:05 cx3 systemd[1]: Started Hello Service.
Nov 26 16:07:05 cx3 docker[2432]: Hello World
Nov 26 16:07:07 cx3 docker[2432]: Hello World
Nov 26 16:07:09 cx3 docker[2432]: Hello World
core@cx2 ~ $ 
core@cx2 ~ $ 
core@cx2 ~ $ fleetctl journal hello.service

Nov 26 16:07:31 cx3 docker[2432]: Hello World
Nov 26 16:07:33 cx3 docker[2432]: Hello World
Nov 26 16:07:35 cx3 docker[2432]: Hello World
Nov 26 16:07:37 cx3 docker[2432]: Hello World
Nov 26 16:07:39 cx3 docker[2432]: Hello World
Nov 26 16:07:41 cx3 docker[2432]: Hello World
Nov 26 16:07:43 cx3 docker[2432]: Hello World
Nov 26 16:07:45 cx3 docker[2432]: Hello World
Nov 26 16:07:47 cx3 docker[2432]: Hello World
Nov 26 16:07:49 cx3 docker[2432]: Hello World
core@cx2 ~ $ 
core@cx2 ~ $ fleetctl stop hello.service
Unit hello.service loaded on 501b28c1.../10.10.84.88
core@cx2 ~ $ fleetctl unload hello.service
Unit hello.service inactive
core@cx2 ~ $ fleetctl destroy hello.service
Destroyed hello.service
core@cx2 ~ $ 

Selamat mencoba sendiri membuat service sederhana seperti hello.service ini :)

Percobaan berikutnya adalah membuat service yang bisa berfungsi dalam high-availability [2][4].

[1]https://www.docker.com/whatisdocker/
[2]https://coreos.com/docs/launching-containers/launching/launching-contain...
[3]https://www.digitalocean.com/community/tutorials/how-to-use-fleet-and-fl...
[4]https://coreos.com/using-coreos/clustering/

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.