首页 > 代码库 > CoreOS实践(1)—CoreOS初体验

CoreOS实践(1)—CoreOS初体验

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/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

CoreOS实践(1)—CoreOS初体验