欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

测试

  • 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