参考官方的 release 信息整理 docker-compse 编排文件的配置参数,实现一键创建 etcd 容器,并演示相关操作。
docker 部署 etcd
docker-compse.yml 配置文件
1 |
|
docker-compose 参数说明
volumes
我们把宿主机的目录映射到容器的 /etcd-data 目录目的是,每次重新创建容器,数据不会清空
ETCDCTL_API
这个环境变量来指定 etcdctl 的 API 版本,2 和 3 的命令执行方式是不一样的
-
export ETCDCTL_API=2
-
export ETCDCTL_API=3
command
这里的书写方式支持多种,是等效的,我就是使用第三个书写方法。
-
直接一行字符串,这就是我们正常使用的例如
command: bundle exec thin -p 3000
1
command: "/usr/local/bin/etcd --name s1 --data-dir /etcd-data --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379 --initial-advertise-peer-urls http://0.0.0.0:2380 --listen-peer-urls http://0.0.0.0:2380 --initial-cluster-token tkn --initial-cluster s1=http://0.0.0.0:2380 --initial-cluster-state new"
-
方括号数组方式,例如
command: [bundle, exec, thin, -p, 3000]
1
command: ["/usr/local/bin/etcd", "--name", "s1", "--data-dir", "/etcd-data" "--advertise-client-urls", "http://0.0.0.0:2379", "--listen-client-urls", "http://0.0.0.0:2379", "--initial-advertise-peer-urls", "http://0.0.0.0:2380", "--listen-peer-urls", "http://0.0.0.0:2380", "--initial-cluster-token", "tkn", "--initial-cluster", "s1=http://0.0.0.0:2380", "--initial-cluster-state", "new"]
-
yaml 数组方式,我就是使用的这种方式,所以就不写完整的了例如:
1
command: [bundle, exec, thin, -p, 3000]
测试 etcd 服务
-
登入容器内部
docker exec -it etcd-v3 /bin/sh
-
查看 ETCDCTL_API,
echo $ETCDCTL_API
-
查看 etcd 版本
etcd --version
-
查看 etcdctl 版本
etcdctl version
, 如果 api 版本是 2,命令是etcdctl -v
-
put k-v 值
etcdctl put foo bar
-
get k 值
etcdctl foo
-
登出容器,如果宿主机安装了 etcdctl 命令,通过也可 put/get 容器内 etcd 的 k-v 值,因为我们创建容器时候是有端口映射的
MacOS 安装 etcd 以及 etcdctl
安装 etcd 包含 etcd 和 etcdctl, 我这里只是为了使用 etcdctl 命令,网上说也可以使用 brew install etcdctl
这个命令,只安装 etcdctl 命令,但是我测试是不行的,所以就安装了 etcd
1 |
|
参考链接: