參考官方的 release 信息整理 docker-compse 編排文件的配置參數,實現一鍵創建 etcd 容器,並演示相關操作。
docker 部署 etcd
docker-compse.yml 配置文件
version: '2.2'
services:
etcd:
image: quay.io/coreos/etcd:v3.3.12
container_name: etcd-v3
ports:
- 2379:2379
- 2380:2380
environment:
ETCDCTL_API: 3
volumes:
- /Users/owenliu/lehui/data/etcd/etcd-data:/etcd-data
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"
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
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]
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 數組方式,我就是使用的這種方式,所以就不寫完整的了例如:
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
brew install etcd
參考鏈接: