安装

端口 2379,2380/tcp
apt-get install etcd -y
systemctl start etcd
systemctl enable etcd
systemctl status etcd

# 测试是否安装成功
etcd --version
# 测试etcd服务是否开启 
curl http://127.0.0.1:2379/version
检测
etcdctl cluster-health
etcdctl ls 运行检测: etcdctl 在/usr/bin 可运行
设置环境变量
vi /etc/profile   #可跳过
#最后添加一行 export ETCDCTL_API=3
source /etc/profile #更新环境变量 

报错设置目录权限
chown etcd:etcd -R /var/lib/etcd    或者加上/default.etcd

测试安装成功
etcd --version #查看etcd版本
# 测试etcd服务是否开启成功
curl http://127.0.0.1:2379/version
curl http://172.17.214.105:2379/version #局域网测试
etcdctl ls #运行检测: etcdctl 在/usr/bin 可全局运行
etcdctl cluster-health #健康检测

服务配置

查看服务信息
cat /usr/lib/systemd/system/etcd.service
修改配置文件
vim /etc/default/etcd
ETCD_NAME="etcd5"
ETCD_DATA_DIR="/var/lib/etcd/default"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.17.214.105:2380"
ETCD_INITIAL_CLUSTER="etcd5=http://172.17.214.105:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://172.17.214.105:2379"

vim /etc/profile 
最后添加一行
export ETCDCTL_API=3 #2.0版本使用 export ETCDCTL_API=2
source /etc/profile #载入

#删除现在配置文件
rm -rf /var/lib/etcd/default
mkdir -p /var/lib/etcd/default
chown etcd:etcd -R /var/lib/etcd/default  #设置目录权限【重点可能报错】

systemctl restart etcd 

#查看集群成员信息(注意127.0.0.1或者localhost在其它机器上可能调用失败)
etcdctl member list
在其它机器上测试
curl http://172.17.214.105:2379/version

插入数据测试

查看调试信息
etcdctl --endpoints=http://172.17.214.105:2379  --debug ls
插入数据:
etcdctl --endpoints=http://172.17.214.105:2379 put /test "123"
查看指定key的值
etcdctl --endpoints=http://172.17.214.105:2379  get /test
删除
etcdctl --endpoints=http://172.17.214.105:2379  del /test
#目录
etcdctl --endpoints=http://172.17.214.105:2379  get /testdir/testkey1

读取etcd中存储的所有数据
etcdctl --endpoints=172.17.214.105:2379 get / --prefix
所属同一集群的所有节点:
etcdctl --endpoints=172.17.214.105:2379  member list

手动运行方法一

  • 临时运行不退出 占用控制台
etcd --config-file=/root/etcd51.conf
vim etcd51.conf
name: etcd5
data-dir: /var/lib/etcd/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://127.0.0.1:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://127.0.0.1:2380
initial-cluster: etcd5=http://127.0.0.1:2380,etcd6=http://172.17.214.106:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new

#重启需要删除目录
rm -rf /var/lib/etcd/default
mkdir /var/lib/etcd/default

手动运行方法二

  • 临时运行不退出
sudo etcd --name "etcd5" --data-dir "/var/lib/etcd/default"  --listen-client-urls http://0.0.0.0:2379  --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls 'http://0.0.0.0:2380'  --initial-cluster-state 'new' --initial-cluster-token 'etcd-cluster' --initial-advertise-peer-urls 'http://172.17.214.105:2380' --initial-cluster 'etcd5=http://172.17.214.105:2380'

方法三 docker部署

vim docker-compose
version: '3.8'
services:
  etcd:
    image: 'quay.io/coreos/etcd:v3.5.4'
    restart: always
    container_name: etcd_server
    #volumes: 
    #  - './data:/bitnami/etcd/data'
    ports:
      - "2379:2379"
      - "2380:2380"
    environment:
      - advertise-client-urls= http://172.17.214.105:2379
      - initial-advertise-peer-urls= http://172.17.214.105:2380 
      - listen-client-urls= http://0.0.0.0:2379
      - listen-peer-urls= http://0.0.0.0:2380
      - initial-cluster-token= etcd-cluster
      - initial-cluster-state= new
      #- initial-cluster= "etcdnode1=http://172.17.214.105:2380,etcdnode2=172.17.214.106:2380,etcdnode3=172.17.214.107:2380"

    networks:
      - etcd-net
networks:
  etcd-net:
    driver: bridge