Home

OSX上摆脱vagrant搭建CoreOS集群

一般我们在OSX下使用vagrant+virtualbox部署虚拟机集群做开发测试。现在我们还可以有更轻量的选择——xhyve。xhyve是FreeBSD 下的虚拟技术 bhyve (The BSD Hypervisor) 的OSX移植,在OS X 10.10.3 Yosemite 之后的版本中可以使用。由于xhyve是基于FreeBSD原生的虚拟方案,所以它性能好并且非常的轻量,只有 230 KB,不依赖其他软件或库。当xhyve发布时CoreOS快速的跟进,推出了基于xhyve的coreos-xhyve。这里我们使用corectl——一个coreos-xhyve的命令行工具来搭建CoreOS集群。

安装corectl

最快的方法使用brew

 brew install corectl 

从源码编译

git clone git@github.com:TheNewNormal/corectl.git
cd corectl
make

安装好后运行corectl -h可以看到所支持的命令

corectl -h
CoreOS over OSX made simple.
❯❯❯ http://github.com/TheNewNormal/corectl


Usage:
  corectl [flags]
  corectl [command]

Available Commands:
  rm          Removes one or more CoreOS images from local fs
  kill        Halts one or more running CoreOS instances
  ls          Lists locally available CoreOS images
  load        Loads CoreOS instances defined in an instrumentation file.
  version     Shows corectl version information
  ps          Lists running CoreOS instances
  query       Display information about the running CoreOS instances
  pull        Pulls a CoreOS image from upstream
  run         Starts a new CoreOS instance
  ssh         Attach to or run commands inside a running CoreOS instance
  put         copy file to inside VM

Flags:
      --debug   adds extra verbosity, and options, for debugging purposes and/or power users

Use "corectl [command] --help" for more information about a command.

All flags can also be configured via upper-case environment variables prefixed with "COREOS_"
For example, "--debug" => "COREOS_DEBUG"

运行CoreOS VM

加载镜像

第一次输入sudo corectl run即会自动下载最新的 CoreOS Alpha 镜像并加载运行,镜像文件放置的位置是$HOME/.coreos/

run的参数还有

Usage:
  corectl run [flags]

Aliases:
  run, start

Flags:
      --cdrom string          append an CDROM (.iso) to VM
      --channel string        CoreOS channel (default "alpha")
      --cloud_config string   cloud-config file location (either a remote URL or a local path)
      --cpus int              VM's vCPUS (default 1)
  -d, --detached              starts the VM in detached (background) mode
  -h, --help                  help for run
  -l, --local latest          consumes whatever image is latest locally instead of looking online unless there's nothing available.
      --memory int            VM's RAM, in MB, per instance (1024 < memory < 8192) (default 1024)
  -n, --name string           names the VM. (if absent defaults to VM's UUID)
      --root string           append a (persistent) root volume to VM
      --sshkey string         VM's default ssh key
      --tap string            append tap interface to VM
      --uuid string           VM's UUID (default "random")
      --version string        CoreOS version (default "latest")
      --volume value          append disk volumes to VM (default [])

Global Flags:
      --debug   adds extra verbosity, and options, for debugging purposes and/or power users

All flags can also be configured via upper-case environment variables prefixed with "COREOS_"
For example, "--debug" => "COREOS_DEBUG"

想使用不同的镜像版本可以由--channel alpha/beta/stable指定

数据持久化

先新建一个5G的persistent.img盘

dd if=/dev/zero of= persistent.img  bs=1g count=5

格式化成ext4(需要e2fsprogs工具,安装brew install e2fsprogs)

/usr/local/Cellar/e2fsprogs/1.42.12/sbin/mke2fs  -t ext4 -m0 -F persistent.img

运行时加上

corectl run --name core --volume absolute_or_relative_path/to/persistent.img

启动后persistent.img被挂载在/dev/vda

ssh进入VM将/dev/vda 挂载 在 /data上

corectl ssh core
mkdir /data
sudo mount /dev/vda /data

后面数据就可以写入/data

启动集群

https://discovery.etcd.io 获取token

curl -X GET 'https://discovery.etcd.io/new?size=3'

将token写入loud-config文件

#cloud-config

coreos:
  etcd2:
    # generate a new token for each unique cluster
    # from https://discovery.etcd.io/new?size=n where n = cluster size
    # discovery url to bootstrap the cluster
    discovery: hhttps://discovery.etcd.io/{$TOKEN}
    # multi-region and multi-cloud deployments need to use $public_ipv4
    # list of member’s client urls to advertise information to the rest of the cluster
    advertise-client-urls: http://$public_ipv4:2379
    # this address is used to communicate etcd data around the cluster
    initial-advertise-peer-urls: http://$private_ipv4:2380
    # listen on both the official ports and the legacy ports
    # legacy ports can be omitted if your application doesn't depend on them
    # url to listen for client traffic
    listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
    # url to listen for peer traffic
    listen-peer-urls: http://$private_ipv4:2380,http://$private_ipv4:7001
  fleet:
    public-ip: $public_ipv4
  flannel:
    interface: $public_ipv4
  units:
    - name: etcd2.service
      command: start
    - name: fleet.service
      command: start

启动三个CoreVM

sudo corectl run --cloud_config ./cloud-config-file --name core-1
sudo corectl run --cloud_config ./cloud-config-file --name core-2
sudo corectl run --cloud_config ./cloud-config-file --name core-3

随便ssh进一个VM使用etcdctl member list看到集群已经建立

etcdctl  member list
28fede81eb6ee920: name=78c956f8b901497fbd909c0a4fa4a9f7 peerURLs=http://192.168.64.30:2380 clientURLs=http://192.168.64.30:2379 isLeader=false
a4661fcb2e28c76a: name=1a7f48cd84c540528b0a61483f071307 peerURLs=http://192.168.64.31:2380 clientURLs=http://192.168.64.31:2379 isLeader=false
eebd0c9e16e3e5be: name=fd92329bf3004bf98f3a6553606a00b0 peerURLs=http://192.168.64.32:2380 clientURLs=http://192.168.64.32:2379 isLeader=true

更多信息可以参考这个项目coreos-osx