title: 38.Etcd安装配置 CreateTime: 2019-07-11 00:00:38 UpdateTime: 2019-07-11 00:00:38 CategoryName: cloudnative --- --- title: "38.Etcd安装配置" date: 2019-07-11T00:00:38+08:00 draft: false tags: ["etcd"] categories: ["cloudnative"] author: "springrain" --- ## 下载安装 ```shell wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz ##解压: mkdir /usr/local/etcd tar xzvf etcd-v3.4.13-linux-amd64.tar.gz -C /usr/local/etcd --strip-components=1 ###查看版本### /usr/local/etcd/etcd --version ###前台启动### /usr/local/etcd/etcd ###另开shell窗口测试### ETCDCTL_API=3 /usr/local/etcd/etcdctl --endpoints=localhost:2379 put foo bar ETCDCTL_API=3 /usr/local/etcd/etcdctl --endpoints=localhost:2379 get foo ###修改默认版本,集群中的其他节点也需要修改### vi /etc/profile export ETCDCTL_API=3 ``` ## 配置 创建 /usr/local/etcd/conf.yml ```yaml ###节点名称### name: etcd-node1 ###指定节点的数据存储目录,这些数据包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未指定-wal-dir,还会存储WAL文件;如果不指定会用缺省目录.### data-dir: /usr/local/etcd/data ###本member侧使用,用于监听etcd客户发送信息的地址.ip为全0代表监听本member侧所有接口### listen-client-urls: http://0.0.0.0:2379 ###本member侧使用,用于监听其他member发送信息的地址.ip为全0代表监听本member侧所有接口### listen-peer-urls: http://0.0.0.0:2380 ####其他member使用,其他member通过该地址与本member交互信息.一定要保证从其他member能可访问该地址.静态配置方式下,该参数的value一定要同时在--initial-cluster参数中存在##### initial-advertise-peer-urls: http://10.0.67.15:2380 ###本member侧使用.描述集群中所有节点的信息,本member根据此信息去联系其他member.### ###memberID的生成受--initial-cluster-token和--initial-advertise-peer-urls影响.### initial-cluster: etcd-node1=http://10.0.67.15:2380,etcd-node2=http://10.0.67.19:2380,etcd-node3=http://10.0.67.21:2380 ###etcd客户使用,客户通过该地址与本member交互信息.一定要保证从客户侧能可访问该地址### advertise-client-urls: http://10.0.67.15:2379 ###用于区分不同集群.本地如有多个集群要设为不同#### initial-cluster-token: etcd-cluster-token ###用于指示本次是否为新建集群.有两个取值new和existing.如果填为existing,则该member启动时会尝试与其他member交互### ###集群初次建立时,要填为new,经尝试最后一个节点填existing也正常,其他节点不能填为existing.### ###集群运行过程中,一个member故障后恢复时填为existing,经尝试填为new也正常.#### initial-cluster-state: new ``` ## 注册成服务 创建/usr/lib/systemd/system/etcd.service ```shell [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target [Service] Type=notify WorkingDirectory=/usr/local/etcd/ # User=etcd ExecStart=/usr/local/etcd/etcd --config-file=/usr/local/etcd/conf.yml Restart=on-failure [Install] WantedBy=multi-user.target ``` ## 更新启动 ```shell systemctl daemon-reload systemctl enable etcd systemctl start etcd ###10.0.67.19,10.0.67.21这两台服务器需要修改conf.yml对应的ip和名称,注册成服务 ``` ## 读写操作 ```shell ###指令参数### /usr/local/etcd/etcdctl -h ###查看集群成员### /usr/local/etcd/etcdctl member list ###集群状态### /usr/local/etcd/etcdctl cluster-health #### etcd v3 版本使用grpc替代 http协议,目前http是beta,不建议使用#### ###增/改### /usr/local/etcd/etcdctl put foo bar ####foo is 'Zm9v' in Base64 bar is 'YmFy'### curl -L http://127.0.0.1:2379/v3beta/kv/put -X POST -d '{"key":"Zm9v","value": "YmFy"}' ###查### /usr/local/etcd/etcdctl get foo curl -L http://127.0.0.1:2379/v3beta/kv/range -X POST -d '{"key":"Zm9v"}' ###删#### /usr/local/etcd/etcdctl del foo ``` ## 其他 ```shell ###最全面的文档### /usr/local/etcd/etcdctl -h ###快照### /usr/local/etcd/etcdctl snapshot save ./my.db /usr/local/etcd/etcdctl --write-out=table snapshot status ./my.db /usr/local/etcd/etcdctl --write-out=table snapshot restore ./my.db ``` ###租约### ###分布式锁### ###watch###