etcd使用教程.md
程序员文章站
2022-07-13 22:19:52
...
下载
service
直接下载二进制文件,解压执行
https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz
客户端
客户端工具列表
https://coreos.com/etcd/docs/latest/integrations.html
python
- api v2版本
下载源码https://github.com/jplana/python-etcd,执行sudo python setup.py install
编译安装 - api v3版本
- 源码 https://github.com/kragniz/python-etcd3、
- 稳档 https://python-etcd3.readthedocs.io/en/latest/installation.html
用源码编译一直报错, 用sudo pip install etcd3
安装,在ubuntu16.04下安装成功,在14.04下安装失败。
测试
- A机器 (192.168.31.247)
./etcd --name infra0 --initial-advertise-peer-urls http://192.168.31.247:2380 \
--listen-peer-urls http://192.168.31.247:2380 \
--listen-client-urls http://192.168.31.247:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.31.247:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.31.247:2380,infra1=http://192.168.31.150:2380 \
--initial-cluster-state new
- B机器(192.168.31.150)
/home/kgbot/kgbot_st/etcd/etcd --name infra1 --initial-advertise-peer-urls http://192.168.31.150:2380 \
--listen-peer-urls http://192.168.31.150:2380 \
--listen-client-urls http://192.168.31.150:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://192.168.31.150:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://192.168.31.247:2380,infra1=http://192.168.31.150:2380 \
--initial-cluster-state new
- 检测
./etcdctl member list
./etcdctl cluster-health
Python脚本测试(使用api v2版本)
- 在A机器上
import etcd
client = etcd.Client(port=2379)
client.write('/a/b', '12|1|1.6')
- 在B机器上
import etcd
client = etcd.Client(port=2379)
result = client.read('/a/b')
print(result.value)
配置
在ubuntu16.04下配置
创建配置文件/usr/lib/systemd/system/etcd.service,内容如下
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/home/kg/kg_db/etcd
#EnvironmentFile=-/home/kg/kg_st/etcd/etcd.conf
User=kg
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /home/kg/kg_st/etcd/etcd --config-file=/home/kg/kg_st/etcd/etcd.conf"
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
其中/home/kg/kg_st/etcd/etcd.conf参考https://github.com/etcd-io/etcd/blob/master/etcd.conf.yml.sample, 主要修改如下参数
name: 'kg150'
listen-peer-urls: http://192.168.31.150:2380,http://127.0.0.1:2380
listen-client-urls: http://192.168.31.150:2379,http://127.0.0.1:2379
initial-advertise-peer-urls: http://192.168.31.150:2380
advertise-client-urls: http://192.168.31.150:2379
initial-cluster: kg247=http://192.168.31.247:2380,kg150=http://192.168.31.150:2380
initial-cluster-token: 'etcd-cluster'
initial-cluster-state: 'new'
启动方式
/home/kg/kg_st/etcd/etcd --config-file=/home/kg/kg_st/etcd/etcd.conf
或
sudo systemctl daemon-reload
sudo systemctl start etcd
sudo systemctl stop etcd
sudo systemctl enable etcd
上一篇: etcd集群的搭建
下一篇: js 点击切换显示隐藏状态
推荐阅读