etcd主要功能是分佈式的存儲鍵值,優勢很少說了,分佈是集羣,自動選舉等等,自行百度,主要說下配置方法,折騰了幾天,終於優勢眉目了,記錄下操做方法,本文參考了以下連接html
http://www.javashuo.com/article/p-fhxjkzzs-eh.html
http://www.361way.com/etcd-cluster/5468.html
https://n40lab.wordpress.com/2016/08/01/installing-coreos-etcd-server-on-centos-7/linux
建立了4個虛擬機,虛擬機中系統爲centos7(虛擬機使用的docker)git
for((i=1,i<5,i++))
do
docker run -d --name etcd$i --hostname=etcd$i --privileged=true centos7 /usr/sbin/init
docker exec -it etcd$i bash -c 「yum update -y&& yum install net-tools -y」
done
hostname ip
etcd1 192.168.0.5
etcd2 192.168.0.6
etcd3 192.168.0.7
etcd4 192.168.0.8
將上述ip表添加到/etc/hosts中
yum install etcd -y
github
# systemctl enable etcd&&systemctl start etcd # etcdctl set hello world world # etcdctl get hello world
這裏的集羣模式是指徹底集羣模式,固然也能夠在單機上經過不一樣的端口,部署僞集羣模式,只是那樣作只適合測試環境,生產環境考慮到可用性的話須要將etcd實例分佈到不一樣的主機上,這裏集羣搭建有三種方式,分佈是靜態配置,etcd發現,dns發現。默認配置運行etcd,監聽本地的2379端口,用於與client端交互,監聽2380用於etcd內部交互。etcd啓動時,集羣模式下會用到的參數以下:docker
- –name
- etcd集羣中的節點名,這裏能夠隨意,可區分且不重複就行
- –listen-peer-urls
- 監聽的用於節點之間通訊的url,可監聽多個,集羣內部將經過這些url進行數據交互(如選舉,數據同步等)
- –initial-advertise-peer-urls
- 建議用於節點之間通訊的url,節點間將以該值進行通訊。
- –listen-client-urls
- 監聽的用於客戶端通訊的url,一樣能夠監聽多個。
- –advertise-client-urls
- 建議使用的客戶端通訊url,該值用於etcd代理或etcd成員與etcd節點通訊。
- –initial-cluster-token etcd-cluster-1
- 節點的token值,設置該值後集羣將生成惟一id,併爲每一個節點也生成惟一id,當使用相同配置文件再啓動一個集羣時,只要該token值不同,etcd集羣就不會相互影響。
- –initial-cluster
- 也就是集羣中全部的initial-advertise-peer-urls 的合集
- –initial-cluster-state new
- 新建集羣的標誌,初始化狀態使用 new,創建以後改此值爲 existing
# vim /lib/systemd/system/etcd.service [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target [Service] Type=notify WorkingDirectory=/var/lib/etcd/ EnvironmentFile=-/etc/etcd/etcd.conf User=etcd # set GOMAXPROCS to number of processors ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd \ --name=\"${ETCD_NAME}\" \ --data-dir=\"${ETCD_DATA_DIR}\" \ --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \ --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" \ --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" \ --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \ --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\" \ --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\"" Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
# vim /etc/etcd/etcd.conf
ETCD_NAME=etcd1
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
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://etcd1:2380"
ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd02=http://etcd2:2380,etcd03=http://etcd3:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://etcd1:2379"
配置好3臺虛擬機以後同時啓動etcd服務# systemctl start etcd
正常狀況能夠查看到成員列表etcdctl member list
vim
# etcdctl member list 3b05c2e4e4c104b4: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false bd388e7810915853: name=etcd2 peerURLs=http://etcd2:2380 clientURLs=http://etcd3:2379 isLeader=true d282ac2ce600c1ce: name=etcd3 peerURLs=http://etcd3:2380 clientURLs=http://etcd2:2379 isLeader=false
yum雖然也能用,仍是二進制包的方法操做簡單些。centos
下載穩定版本的etcd二進制包安全
mkdir /var/lib/etcd;mkdir /etc/etcd; groupadd -r etcd; useradd -r -g etcd -d /var/lib/etcd -s /sbin/nologin -c "etcd user" etcd;chown -R etcd:etcd /var/lib/etcd
ETCD_VERSION=`curl -s -L https://github.com/coreos/etcd/releases/latest | grep linux-amd64\.tar\.gz | grep href | cut -f 6 -d '/' | sort -u`; ETCD_DIR=/opt/etcd-$ETCD_VERSION; mkdir $ETCD_DIR;curl -L https://github.com/coreos/etcd/releases/download/$ETCD_VERSION/etcd-$ETCD_VERSION-linux-amd64.tar.gz | tar xz --strip-components=1 -C $ETCD_DIR; ln -sf $ETCD_DIR/etcd /usr/bin/etcd && ln -sf $ETCD_DIR/etcdctl /usr/bin/etcdctl; etcd --version
四臺虛擬機新建etcd服務啓動腳本bash
[root@etcd1 /]# vim /lib/systemd/system/etcd.service
[Unit]
Description=etcd service
After=network.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
ExecStart=/usr/bin/etcd
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
etcd靜態模式集羣配置(即知道須要哪幾臺虛擬機須要創建集羣)etcd1,etcd2與etcd3創建集羣后etcd4加入集羣。
修改etcd1配置文件
[root@etcd1 /]# cat /etc/etcd/etcd.conf ETCD_NAME=etcd1 ETCD_DATA_DIR="/var/lib/etcd/default.etcd" 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://etcd1:2380" ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd02=http://etcd2:2380,etcd03=http://etcd3:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://etcd1:2379"
修改etcd2配置文件
[root@etcd2 /]# cat /etc/etcd/etcd.conf ETCD_NAME=etcd2 ETCD_DATA_DIR="/var/lib/etcd/default.etcd" 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://etcd2:2380" ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd02=http://etcd2:2380,etcd03=http://etcd3:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://etcd2:2379"
修改etcd3配置文件
[root@etcd3 /]# cat /etc/etcd/etcd.conf ETCD_NAME=etcd3 ETCD_DATA_DIR="/var/lib/etcd/default.etcd" 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://etcd3:2380" ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd02=http://etcd2:2380,etcd03=http://etcd3:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://etcd3:2379"
配置好3臺虛擬機以後同時啓動etcd服務# systemctl start etcd
正常狀況能夠查看到成員列表etcdctl member list
服務器
[root@etcd1 /]# etcdctl member list 3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false
etcd配置過程當中遇到的問題,添加節點,刪除節點,備份數據
### 本地鏈接報錯
[root@etcd2 /]# etcdctl member list Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused ; error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused
若是出現如上的錯誤,是由於ETCD_LISTEN_CLIENT_URLS參數沒有配置http://127.0.0.1:2379而致使的,因此這裏我使用了0.0.0.0表明了監控全部地址。
除此以外仍上述報錯,當配置的很混亂的時候能夠刪掉/var/lib/etcd/中的default.etcd文件,前提是數據不重要哈。還要注意default.etcd文件夾權限改成etcd:etcdchown -R etcd:etcd default.etcd
此步很重要,另外還須要細心檢查/etc/etcd/etcd.conf和/lib/systemd/system/etcd.service是否有錯誤。改完以後再重啓etcd服務(systemctl start etcd
)就ok了。
在現有集羣etcd三臺虛擬機中任何一臺操做添加新節點etcd04
[root@etcd1 /]# etcdctl member list 3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false [root@etcd1 /]# etcdctl member add etcd04 http://etcd4:2380 Added member named etcd04 with ID ec8c708bb5b2fdc6 to cluster ETCD_NAME="etcd04" ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd03=http://etcd3:2380,etcd02=http://etcd2:2380,etcd04=http://etcd4:2380" ETCD_INITIAL_CLUSTER_STATE="existing" 注意此處三條必需要與etcd04配置文件(/etc/etcd/etcd.conf)相符合 [root@etcd1 /]# etcdctl member list 3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false ec8c708bb5b2fdc6[unstarted]: peerURLs=http://etcd4:2380
啓動etcd服務以前修改etcd04配置文件
[root@etcd4 /]# vim /etc/etcd/etcd.conf ETCD_NAME=etcd04 ETCD_DATA_DIR="/var/lib/etcd/default.etcd" 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://etcd4:2380" ETCD_INITIAL_CLUSTER="etcd01=http://etcd1:2380,etcd02=http://etcd2:2380,etcd03=http://etcd3:2380,etcd04=http://etcd4:2380" ETCD_INITIAL_CLUSTER_STATE="existing" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://etcd4:2379"
若是/var/lib/etcd/中存在default.etcd必定要刪除,否側etcd服務啓動失敗(由於集羣中給etcd04分配的物理id與現有庫中的id不相同)
[root@etcd4 /]# rm -rf /var/lib/etcd/default.etcd/
[root@etcd4 /]# systemctl start etcd
[root@etcd4 /]# etcdctl member list #啓動服務後再查詢etcd04已加入集羣中
3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false
bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true
d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false
ec8c708bb5b2fdc6: name=etcd04 peerURLs=http://etcd4:2380 clientURLs=http://etcd4:2379 isLeader=false
現有集羣節點是4個
[root@etcd1 /]# etcdctl member list
3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false
b28ea7a9615a2c72: name=etcd04 peerURLs=http://etcd4:2380 clientURLs=http://etcd4:2379 isLeader=false
bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true
d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false
在除etcd04節點外的任意一臺中操做刪除etcd04節點
[root@etcd1 /]# etcdctl member remove b28ea7a9615a2c72
Removed member b28ea7a9615a2c72 from cluster
[root@etcd1 /]# etcdctl member list
3b05c2e4e4c104b4: name=etcd01 peerURLs=http://etcd1:2380 clientURLs=http://etcd1:2379 isLeader=false
bd388e7810915853: name=etcd03 peerURLs=http://etcd3:2380 clientURLs=http://etcd3:2379 isLeader=true
d282ac2ce600c1ce: name=etcd02 peerURLs=http://etcd2:2380 clientURLs=http://etcd2:2379 isLeader=false
etcd的數據應該算是很安全的,即使出現單點故障,數據也不會丟失,前提是節點不在同一臺服務器中
etcd的數據備份比較。。。網上的方法etcdctl backup --data-dir /var/lib/etcd/default.etcd/ --backup-dir /home/etcd_backup
備份完以後,發現沒有db文件,好尷尬啊,全部我直接cp了,cp -r /var/lib/etcd/default.etcd/ /home/etcd_backup
chown -R etcd:etcd default.etcd
嗯,就能夠了。