CoreOS實踐(1)—CoreOS初體驗

YY哥發表於2014-09-08

CoreOS主要包含以下一些東西:

(1)最小的OS:kernel+systemd

(2)使用Docker執行應用

(3)使用fleet管理叢集

(4)使用etcd實現服務發現:一個分散式的K/V儲存引擎儲存配置資料

 

下載vagrant的coreos配置檔案

$ git clone https://github.com/coreos/coreos-vagrant.git

$vagrant up

$ vagrant status

Current machine states:

 

core-01                   running (virtualbox)

core-02                   running (virtualbox)

core-03                   running (virtualbox)

 

$ ssh-add ~/.vagrant.d/insecure_private_key

Identity added: /Users/yy/.vagrant.d/insecure_private_key (/Users/yy/.vagrant.d/insecure_private_key)

 

$ vagrant ssh core-01

Last login: Sun Sep  7 16:23:31 2014 from 10.0.2.2

CoreOS (beta)

core@core-01 ~ $ fleetctl list-machines

MACHINE           IP          METADATA

542a4130... 172.17.8.101     -

bcb90abf...  172.17.8.102     -

e6a8eaa9...  172.17.8.103     -

 

讀寫etcd

core@core-01 ~ $ etcdctl set first-etcd-key "Hello World"

Hello World

core@core-01 ~ $ etcdctl get first-etcd-key

Hello World

 

在core-02上可以讀到first-etcd-key對應的值:

core@core-02 ~ $ etcdctl get first-etcd-key

Hello World

也可以直接提供的API讀取相應的key:

core@core-02 ~ $ curl -L http://127.0.0.1:4001/v1/keys/first-etcd-key

{"action":"get","key":"/first-etcd-key","value":"Hello World","index":2264}

 

使用fleetctl管理服務

core@core-01 ~ $ cat 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 "while true; do echo Hello World; sleep 1; done"

ExecStop=/usr/bin/docker stop hello

core@core-01 ~ $ fleetctl load hello.service

Job hello.service loaded on 542a4130.../172.17.8.101

core@core-01 ~ $ fleetctl start hello.service

Job hello.service launched on 542a4130.../172.17.8.101

 

檢視服務狀態

core@core-01 ~ $ fleetctl status hello.service

● hello.service - My Service

   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime)

   Active: active (running) since Mon 2014-09-08 04:21:13 UTC; 1min 25s ago

  Process: 1117 ExecStartPre=/usr/bin/docker pull busybox (code=exited, status=0/SUCCESS)

  Process: 1108 ExecStartPre=/usr/bin/docker rm hello (code=exited, status=1/FAILURE)

  Process: 1046 ExecStartPre=/usr/bin/docker kill hello (code=exited, status=1/FAILURE)

 Main PID: 1185 (docker)

   CGroup: /system.slice/hello.service

           └─1185 /usr/bin/docker run --name hello busybox /bin/sh -c while true; do echo Hello World; sleep 1; done

 

Sep 08 04:22:28 core-01 docker[1185]: Hello World

Sep 08 04:22:29 core-01 docker[1185]: Hello World

Sep 08 04:22:30 core-01 docker[1185]: Hello World

Sep 08 04:22:31 core-01 docker[1185]: Hello World

Sep 08 04:22:32 core-01 docker[1185]: Hello World

Sep 08 04:22:33 core-01 docker[1185]: Hello World

Sep 08 04:22:34 core-01 docker[1185]: Hello World

Sep 08 04:22:35 core-01 docker[1185]: Hello World

Sep 08 04:22:36 core-01 docker[1185]: Hello World

Sep 08 04:22:37 core-01 docker[1185]: Hello World

 

core@core-01 ~ $ docker ps

CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS               NAMES

fa7084105f19        busybox:buildroot-2014.02   /bin/sh -c 'while tr   2 minutes ago       Up 2 minutes                            hello

 

在另一個coreos檢視服務狀態

$ vagrant ssh core-02 -- -A

Last login: Mon Sep  8 04:35:40 2014 from 10.0.2.2

CoreOS (beta)

core@core-02 ~ $ fleetctl list-units

UNIT            DSTATE              TMACHINE                STATE          MACHINE                         ACTIVE

hello.service       launched     542a4130.../172.17.8.101    launched     542a4130.../172.17.8.101    active

core@core-02 ~ $ fleetctl status hello.service

● hello.service - My Service

   Loaded: loaded (/run/fleet/units/hello.service; linked-runtime)

   Active: active (running) since Mon 2014-09-08 04:21:13 UTC; 15min ago

  Process: 1117 ExecStartPre=/usr/bin/docker pull busybox (code=exited, status=0/SUCCESS)

  Process: 1108 ExecStartPre=/usr/bin/docker rm hello (code=exited, status=1/FAILURE)

  Process: 1046 ExecStartPre=/usr/bin/docker kill hello (code=exited, status=1/FAILURE)

 Main PID: 1185 (docker)

   CGroup: /system.slice/hello.service

           └─1185 /usr/bin/docker run --name hello busybox /bin/sh -c while true; do echo Hello World; sleep 1; done

 

Sep 08 04:36:20 core-01 docker[1185]: Hello World

Sep 08 04:36:21 core-01 docker[1185]: Hello World

Sep 08 04:36:22 core-01 docker[1185]: Hello World

Sep 08 04:36:23 core-01 docker[1185]: Hello World

Sep 08 04:36:24 core-01 docker[1185]: Hello World

Sep 08 04:36:25 core-01 docker[1185]: Hello World

Sep 08 04:36:26 core-01 docker[1185]: Hello World

Sep 08 04:36:27 core-01 docker[1185]: Hello World

Sep 08 04:36:28 core-01 docker[1185]: Hello World

Sep 08 04:36:29 core-01 docker[1185]: Hello World

core@core-02 ~ $ fleetctl destroy hello.service             

Destroyed hello.service

 

主要參考

[1] https://coreos.com/docs/quickstart/


作者:YY哥 
出處:http://www.cnblogs.com/hustcat/ 
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。

相關文章