etcd 安装测试

 参数

ETCD_NAME :ETCD的节点名,在集群中应该保持唯一,可以使用 hostname。
ETCD_DATA_DIR:ETCD的数据存储目录,服务运行数据保存的路径,默认为 ${name}.etcd。
ETCD_SNAPSHOT_COUNTER:多少次的事务提交将触发一次快照,指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘。
ETCD_HEARTBEAT_INTERVAL:ETCD节点之间心跳传输的间隔,单位毫秒,leader 多久发送一次心跳到 followers。默认值是 100ms。
ETCD_ELECTION_TIMEOUT:该节点参与选举的最大超时时间,单位毫秒,重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms。
ETCD_LISTEN_PEER_URLS:该节点与其他节点通信时所监听的地址列表,多个地址使用逗号隔开,其格式可以划分为scheme://IP:PORT,这里的scheme可以是http、https。和同伴通信的地址,比如 http://ip:2380 ,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost。
ETCD_LISTEN_CLIENT_URLS:该节点与客户端通信时监听的地址列表,对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379 ,客户端会连接到这里和 etcd 交互
ETCD_INITIAL_ADVERTISE_PEER_URLS:该成员节点在整个集群中的通信地址列表,这个地址用来传输集群数据的地址。因此这个地址必须是可以连接集群中所有的成员的。该节点同伴监听地址,这个值会告诉集群中其他节点。
ETCD_INITIAL_CLUSTER:配置集群内部所有成员地址,其格式为:ETCD_NAME=ETCD_INITIAL_ADVERTISE_PEER_URLS,如果有多个使用逗号隔开,集群中所有节点的信息,格式为 node1=http://ip1:2380 ,node2=http://ip2:2380 ,…。注意:这里的 node1 是节点的 –name 指定的名字;后面的 ip1:2380 是 –initial-advertise-peer-urls 指定的值
ETCD_ADVERTISE_CLIENT_URLS:广播给集群中其他成员自己的客户端地址列表
ETCD_INITIAL_CLUSTER_STATE:新建集群的时候,这个值为new;假如已经存在的集群,这个值为 existing。
ETCD_INITIAL_CLUSTER_TOKEN:初始化集群token,创建集群的token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误。

最简单的方式就是通过容器启动etcd(方便的方法)

rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \
  docker rmi gcr.io/etcd-development/etcd:v3.4.4 || true && \
  docker run \
  -p 2379:2379 \
  -p 2380:2380 \
  --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
  --name etcd-gcr-v3.4.4 \
  gcr.io/etcd-development/etcd:v3.4.4 \
  /usr/local/bin/etcd \
  --name s1 \
  --data-dir /etcd-data \
  --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-advertise-peer-urls http://0.0.0.0:2380 \
  --initial-cluster s1=http://0.0.0.0:2380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr

docker exec etcd-gcr-v3.4.4 /bin/sh -c "/usr/local/bin/etcd --version"
docker exec etcd-gcr-v3.4.4 /bin/sh -c "/usr/local/bin/etcdctl version"
docker exec etcd-gcr-v3.4.4 /bin/sh -c "/usr/local/bin/etcdctl endpoint health"
docker exec etcd-gcr-v3.4.4 /bin/sh -c "/usr/local/bin/etcdctl put foo bar"
docker exec etcd-gcr-v3.4.4 /bin/sh -c "/usr/local/bin/etcdctl get foo"
如果机器有外网连接,直接使用docker启动etcd也是很方便的:
[root@localhost setup]# docker run --name etcdquay.io/coreos/etcd:v3.0.15
Unable to find image 'quay.io/coreos/etcd:v3.0.15'locally
v3.0.15: Pulling from coreos/etcd
3690ec4760f9: Pull complete
53b6b297c402: Pull complete
0ee7413b6e7d: Pull complete
0b6d568d289d: Pull complete
f79877e4a632: Pull complete
Digest:sha256:aed90a29fbe7ad0e6629b2ea5a290f5b6efb9b719cec97c756df13f1db3760bf
Status: Downloaded newer image forquay.io/coreos/etcd:v3.0.15
2016-11-16 04:01:09.545844 I | etcdmain: etcdVersion: 3.0.15
2016-11-16 04:01:09.545969 I | etcdmain: Git SHA:fc00305

… …

 

通过docker命令查看一下镜像和进程:
[root@localhost ~]# docker ps
CONTAINER ID       IMAGE                         COMMAND                 CREATED             STATUS              PORTS               NAMES
392fa38235b0       quay.io/coreos/etcd:v3.0.15  "/usr/local/bin/etcd"  29 seconds ago      Up 28seconds       2379-2380/tcp       etcd
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
quay.io/coreos/etcd  v3.0.15            2bdf33352107        4 daysago          43.43 MB

方法一、

闭防火墙,如果开启防火墙,则最好打开2379和4001端口
[root@node-1 ~]# systemctl disable firewalld.service
[root@node-1 ~]# systemctl stop firewalld.service
  
  
安装etcd
k8s运行依赖etcd,需要先部署etcd,下面采用yum方式安装:
[root@node-1 ~]# yum install etcd -y
yum安装的etcd默认配置文件在/etc/etcd/etcd.conf,编辑配置文件:
[root@node-1 ~]# cp /etc/etcd/etcd.conf /etc/etcd/etcd.conf.bak
[root@node-1 ~]# cat /etc/etcd/etcd.conf
#[member]
ETCD_NAME=master                                            #节点名称
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"                  #数据存放位置
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"             #监听客户端地址
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://etcd:2379,http://etcd:4001"           #通知客户端地址
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
    
启动etcd并验证状态
[root@node-1 ~]# systemctl start etcd
    
[root@node-1 ~]# ps -ef|grep etcd
etcd     28145     1  1 14:38 ?        00:00:00 /usr/bin/etcd --name=master --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://0.0.0.0:2379,http://0.0.0.0:4001
root     28185 24819  0 14:38 pts/1    00:00:00 grep --color=auto etcd
[root@node-1 ~]# lsof -i:2379
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
etcd    28145 etcd    6u  IPv6 1283822      0t0  TCP *:2379 (LISTEN)
etcd    28145 etcd   18u  IPv6 1284133      0t0  TCP localhost:53203->localhost:2379 (ESTABLISHED)
........
    
[root@node-1 ~]# etcdctl set testdir/testkey0 0
0
[root@node-1 ~]# etcdctl get testdir/testkey0
0
[root@node-1 ~]# etcdctl -C http://etcd:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy
[root@node-1 ~]# etcdctl -C http://etcd:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy

方法二

ETCD_VER=v3.4.4

# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version
# start a local etcd server
/tmp/etcd-download-test/etcd

# write,read to etcd
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 put foo bar
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 get foo

方法三

下载软件包(https://github.com/etcd-io/etcd/releases

wget https://github.com/etcd-io/etcd/releases/download/v3.4.4/etcd-v3.4.4-linux-amd64.tar.gz
tar zxvf etcd-v3.4.4-linux-amd64.tar.gz
cd etcd-v3.4.4-linux-amd64

#将解压后的etcd和etcdctl移动到$GOPATH/bin目录下,可以直接使用etcd和etcdctl命令
$mv etcd-v3.4.4-linux-amd64/etcd* /$GOPATH/bin

很多人抱怨没有etcd 命令 是因为没有把加压包里的etcd、etcdctl 复制到 /$GOPATH/bin 里

2. 创建一个服务描述文件,放入systemd的服务目录下

cat /usr/lib/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target

 

[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd

 

[Install]
WantedBy=multi-user.target

3. etcd的配置

cat /etc/etcd/etcd.conf
# [member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"

4. 给etcd创建一个工作目录:

[root@localhost etcd]# mkdir /var/lib/etcd

5. 启动etcd,并测试

[root@localhost etcd]# systemctl daemon-reload
[root@localhost etcd]# systemctl start etcd
[root@localhost etcd]# systemctl enable etcd
Created symlink from/etc/systemd/system/multi-user.target.wants/etcd.service to/usr/lib/systemd/system/etcd.service.
[root@localhost etcd]# systemctl status etcd
[root@localhost etcd]# etcdctl cluster-health
member 8e9e05c52164694d is healthy: got healthyresult from http://localhost:2379
cluster is healthy


应用

注:CRUD即Create,Read,Update,Delete是符合REST风格的一套API操作。

set
指定某个键的值。例如:
$ etcdctl set /testdir/testkey "Hello world"
Hello world
支持的选项包括:
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为0)则永不超时
--swap-with-value value 若该键现在的值是value,则进行设置操作
--swap-with-index '0'   若该键现在的索引值是指定索引,则进行设置操作

get
获取指定键的值。例如:
$ etcdctl get /testdir/testkey
Hello world
当键不存在时,则会报错。例如:
$ etcdctl get /testdir/testkey2
Error:  100: Key not found (/testdir/testkey2) [5]
支持的选项为:
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性。

update
当键存在时,更新值内容。例如:
$ etcdctl update /testdir/testkey "Hello"
Hello
当键不存在时,则会报错。例如:
$ etcdctl update /testdir/testkey2 "Hello"
Error:  100: Key not found (/testdir/testkey2) [6]
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时。

rm
删除某个键值。例如:
$ etcdctl rm /testdir/testkey
PrevNode.Value: Hello
当键不存在时,则会报错。例如:
$ etcdctl rm /testdir/testkey
Error:  100: Key not found (/testdir/testkey) [7]
支持的选项为:
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value  检查现有的值是否匹配
--with-index '0'检查现有的index是否匹配

mk
如果给定的键不存在,则创建一个新的键值。例如:
$ etcdctl mk /testdir/testkey "Hello world"
Hello world
当键存在的时候,执行该命令会报错,例如:
$ etcdctl mk /testdir/testkey "Hello world"
Error:  105: Key already exists (/testdir/testkey) [8]
支持的选项为:
--ttl '0'  超时时间(单位为秒),不配置(默认为 0)。则永不超时

mkdir
如果给定的键目录不存在,则创建一个新的键目录。例如:
$ etcdctl mkdir testdir2
当键目录存在的时候,执行该命令会报错,例如:
$ etcdctl mkdir testdir2
Error:  105: Key already exists (/testdir2) [9]
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。

setdir
创建一个键目录。如果目录不存在就创建,如果目录存在更新目录TTL。
$ etcdctl setdir testdir3
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。

updatedir
更新一个已经存在的目录。
$ etcdctl updatedir testdir2
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。

rmdir
删除一个空目录,或者键值对。
$ etcdctl setdir dir1
$ etcdctl rmdir dir1
若目录不空,会报错:
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error:  108: Directory not empty (/dir) [17]

ls
列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。
例如:
$ etcdctl ls
/testdir
/testdir2
/dir

$ etcdctl ls dir
/dir/testkey
支持的选项包括:
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加/进行区分
非数据库操作

backup
备份etcd的数据。
$ etcdctl backup --data-dir /var/lib/etcd  --backup-dir /home/etcd_backup
支持的选项包括:
--data-dir  etcd的数据目录
--backup-dir 备份到指定路径

watch
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
例如:用户更新testkey键值为Hello watch。
$ etcdctl get /testdir/testkey
Hello world
$ etcdctl set /testdir/testkey "Hello watch"
Hello watch
$ etcdctl watch testdir/testkey
Hello watch
支持的选项包括:
--forever  一直监测直到用户按CTRL+C退出
--after-index '0' 在指定index之前一直监测
--recursive 返回所有的键值和子键值

exec-watch
监测一个键值的变化,一旦键值发生更新,就执行给定命令。
例如:用户更新testkey键值。
$ etcdctl exec-watch testdir/testkey -- sh -c 'ls'
config	Documentation  etcd  etcdctl  README-etcdctl.md  README.md  READMEv2-etcdctl.md
支持的选项包括:
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值

member
通过list、add、remove命令列出、添加、删除etcd实例到etcd集群中。
查看集群中存在的节点
$ etcdctl member list
8e9e05c52164694d: name=dev-master-01 peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true
删除集群中存在的节点
$ etcdctl member remove 8e9e05c52164694d
Removed member 8e9e05c52164694d from cluster
向集群中新加节点
$ etcdctl member add etcd3 http://192.168.1.100:2380
Added member named etcd3 with ID 8e9e05c52164694d to cluster
发布了62 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/cojn52/article/details/104831512