docker 多机部署 网络 etcd flannel 部署及配置 ---etcd集群部署

https://www.hi-linux.com/posts/40915.html

https://www.hi-linux.com/posts/49138.html

https://www.hi-linux.com/posts/30481.html

 

3台主机

172.20.72.51
172.20.72.52
172.20.72.53

etcd 

1. etcd安装

etcd在生产环境中一般推荐集群方式部署。本文定位为入门,主要讲讲单节点安装和基本使用。

etcd目前默认使用2379端口提供HTTP API服务,2380端口和peer通信(这两个端口已经被IANA官方预留给etcd);在之前的版本中可能会分别使用4001和7001,在使用的过程中需要注意这个区别。

因为etcd是go语言编写的,安装只需要下载对应的二进制文件,并放到合适的路径就行。

2. 下载 

下载 etcd-v3.1.12-linux-amd64.tar.gz ,因为k8验证过这个版本适用
https://github.com/etcd-io/etcd/releases 

3. 解压

tar xzf etcd-v3.1.12-linux-amd64.tar.gz -C /opt/etcd

解压后是一些文档和两个二进制文件etcd和etcdctl。etcd是server端,etcdctl是客户端
拷贝:etcd和etcdctl 到 /usr/local/bin/

4. 新建两个目录
/local/etcd/config  --配置目录
/local/etcd/etcdata --数据目录

5. 配置etcd集群

config/etcd.conf,请根据实际情况修改

  • etcd1配置示例
1
2
3
4
5
6
7
8
9
10
11
12
# 编辑配置文件
$ vim /local/etcd/config/etcd.conf

ETCD_NAME=etcd1
ETCD_DATA_DIR="/local/etcd/etcdata"
ETCD_LISTEN_PEER_URLS="http://172.20.72.51:2380"
ETCD_LISTEN_CLIENT_URLS="http://172.20.72.51:2379,http://172.20.72.51:4001"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.20.72.51:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://172.20.72.51:2380,etcd2=http://172.20.72.52:2380,etcd3=http://172.20.72.53:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://172.20.72.51:2379,http://172.20.72.51:4001"
  • etcd2配置示例
  • # 编辑配置文件
    $ vim /local/etcd/config/etcd.conf
    
    ETCD_NAME=etcd1
    ETCD_DATA_DIR="/local/etcd/etcdata"
    ETCD_LISTEN_PEER_URLS="http://172.20.72.52:2380"
    ETCD_LISTEN_CLIENT_URLS="http://172.20.72.52:2379,http://172.20.72.52:4001"
    ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.20.72.52:2380"
    ETCD_INITIAL_CLUSTER="etcd1=http://172.20.72.51:2380,etcd2=http://172.20.72.52:2380,etcd3=http://172.20.72.53:2380"
    ETCD_INITIAL_CLUSTER_STATE="new"
    ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster"
    ETCD_ADVERTISE_CLIENT_URLS="http://172.20.72.52:2379,http://172.20.72.52:4001"

 

  • etcd3配置示例
1
2
3
4
5
6
7
8
9
10
11
12
# 编辑配置文件
$ vim /local/etcd/config/etcd.conf

ETCD_NAME=etcd1
ETCD_DATA_DIR="/local/etcd/etcdata"
ETCD_LISTEN_PEER_URLS="http://172.20.72.53:2380"
ETCD_LISTEN_CLIENT_URLS="http://172.20.72.53:2379,http://172.20.72.53:4001"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.20.72.53:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://172.20.72.51:2380,etcd2=http://172.20.72.52:2380,etcd3=http://172.20.72.53:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="hilinux-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://172.20.72.53:2379,http://172.20.72.53:4001"

针对上面几个配置参数做下简单的解释:

  • ETCD_NAME :ETCD的节点名
  • ETCD_DATA_DIR:ETCD的数据存储目录
  • ETCD_SNAPSHOT_COUNTER:多少次的事务提交将触发一次快照
  • ETCD_HEARTBEAT_INTERVAL:ETCD节点之间心跳传输的间隔,单位毫秒
  • ETCD_ELECTION_TIMEOUT:该节点参与选举的最大超时时间,单位毫秒
  • ETCD_LISTEN_PEER_URLS:该节点与其他节点通信时所监听的地址列表,多个地址使用逗号隔开,其格式可以划分为scheme://IP:PORT,这里的scheme可以是http、https
  • ETCD_LISTEN_CLIENT_URLS:该节点与客户端通信时监听的地址列表
  • ETCD_INITIAL_ADVERTISE_PEER_URLS:该成员节点在整个集群中的通信地址列表,这个地址用来传输集群数据的地址。因此这个地址必须是可以连接集群中所有的成员的。
  • ETCD_INITIAL_CLUSTER:配置集群内部所有成员地址,其格式为:ETCD_NAME=ETCD_INITIAL_ADVERTISE_PEER_URLS,如果有多个使用逗号隔开
  • ETCD_ADVERTISE_CLIENT_URLS:广播给集群中其他成员自己的客户端地址列表
  • ETCD_INITIAL_CLUSTER_STATE:初始化集群状态,new表示新建
  • ETCD_INITIAL_CLUSTER_TOKEN:初始化集群token

下面给出常用配置的参数和它们的解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--name:方便理解的节点名称,默认为default,在集群中应该保持唯一,可以使用 hostname
--data-dir:服务运行数据保存的路径,默认为 ${name}.etcd
--snapshot-count:指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘
--heartbeat-interval:leader 多久发送一次心跳到 followers。默认值是 100ms
--eletion-timeout:重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms
--listen-peer-urls:和同伴通信的地址,比如 http://ip:2380,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost!
--listen-client-urls:对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379,客户端会连接到这里和 etcd 交互
--advertise-client-urls:对外公告的该节点客户端监听地址,这个值会告诉集群中其他节点
--initial-advertise-peer-urls:该节点同伴监听地址,这个值会告诉集群中其他节点
--initial-cluster:集群中所有节点的信息,格式为 node1=http://ip1:2380,node2=http://ip2:2380,…。注意:这里的 node1 是节点的 --name 指定的名字;后面的 ip1:2380 是 --initial-advertise-peer-urls 指定的值
--initial-cluster-state:新建集群的时候,这个值为new;假如已经存在的集群,这个值为 existing
--initial-cluster-token:创建集群的token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误

所有以--init开头的配置都是在bootstrap集群的时候才会用到,后续节点的重启会被忽略。

 

6. 创建systemd服务 

1>  创建systemd配置文件

$ cat <<EOF | sudo tee /etc/systemd/system/etcd.service


[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target

[Service]
User=root
Type=notify
EnvironmentFile=-/local/etcd/config/etcd.conf
ExecStart=/local/etcd/etcd3.1.12/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.targ

 2> 启动etcd

systemctl daemon-reload && systemctl enable etcd && systemctl start etcd

3台机器都执行

 

报错: Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused

https://www.cnblogs.com/lkun/p/9486156.html

配置docker网络flannel时,配置etcd的key的时候出现以下错误

Error:  client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
; error #1: dial tcp 127.0.0.1:2379: getsockopt: connection refused

error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
error #1: dial tcp 127.0.0.1:2379: getsockopt: connection refused

解决办法:

修改etcd的配置文件:

vim /etc/etcd/etcd.conf

后面配置http://127.0.0.1:2379,与本机自己进行通信,  ETCD_LISTEN_CLIENT_URLS="http://172.20.72.53:2379,http://172.20.72.53:4001"

改成

ETCD_LISTEN_CLIENT_URLS="http://172.20.72.53:2379,http://127.0.0.1:2379"

然后重启etcd服务即可

 

3>  测试etcd集群

启动完成后,在任意节点执行etcdctl member list可列所有集群节点信息,如下所示(忽略ip)

ac3dcec0b630e55b: name=etcd195 peerURLs=http://172.20.72.55:2380 clientURLs=http://172.20.72.55:2379,http://172.20.72.55:4001 isLeader=true
b8b181cee786501a: name=etcd196 peerURLs=http://172.20.72.50:2380 clientURLs=http://172.20.72.50:2379,http://172.20.72.50:4001 isLeader=false
f51d03ab481334c2: name=default peerURLs=http://172.20.72.48:2380 clientURLs=http://172.20.72.48:2379,http://172.20.72.48:4001 isLeader=false


3> etcd集群基本管理

  • 查看集群健康状态
  • $ etcdctl --endpoints "http://192.168.2.210:2379"  cluster-health
    member a3ba19408fd4c829 is healthy: got healthy result from http://192.168.2.212:2379
    member a8589aa8629b731b is healthy: got healthy result from http://192.168.2.210:2379
    member e4a3e95f72ced4a7 is healthy: got healthy result from http://192.168.2.211:2379
    cluster is healthy

     

  • 查看集群成员  在任一节点上执行,可以看到集群的节点情况,并能看出哪个是leader节点。

4> 基本操作

指定某个键的值。例如:

1
2
$ etcdctl set /testdir/testkey "Hello world"
Hello world

支持的选项包括:

1
2
3
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为0)则永不超时
--swap-with-value value 若该键现在的值是value,则进行设置操作
--swap-with-index '0'   若该键现在的索引值是指定索引,则进行设置操作
  • get

获取指定键的值。例如:

1
2
$ etcdctl get /testdir/testkey
Hello world

当键不存在时,则会报错。例如:

1
2
$ etcdctl get /testdir/testkey2
Error:  100: Key not found (/testdir/testkey2) [5]

支持的选项为:

1
2
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性。
  • update

当键存在时,更新值内容。例如:

1
2
$ etcdctl update /testdir/testkey "Hello"
Hello

当键不存在时,则会报错。例如:

1
2
$ etcdctl update /testdir/testkey2 "Hello"
Error:  100: Key not found (/testdir/testkey2) [6]

支持的选项为:

1
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时。
  • rm

删除某个键值。例如:

1
2
$ etcdctl rm /testdir/testkey
PrevNode.Value: Hello

当键不存在时,则会报错。例如:

1
2
$ etcdctl rm /testdir/testkey
Error:  100: Key not found (/testdir/testkey) [7]

支持的选项为:

1
2
3
4
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value  检查现有的值是否匹配
--with-index '0'检查现有的index是否匹配
  • mk

如果给定的键不存在,则创建一个新的键值。例如:

1
2
$ etcdctl mk /testdir/testkey "Hello world"
Hello world

当键存在的时候,执行该命令会报错,例如:

1
2
$ etcdctl mk /testdir/testkey "Hello world"
Error:  105: Key already exists (/testdir/testkey) [8]

支持的选项为:

1
--ttl '0'  超时时间(单位为秒),不配置(默认为 0)。则永不超时
  • mkdir

如果给定的键目录不存在,则创建一个新的键目录。例如:

1
$ etcdctl mkdir testdir2

当键目录存在的时候,执行该命令会报错,例如:

1
2
$ etcdctl mkdir testdir2
Error:  105: Key already exists (/testdir2) [9]

支持的选项为:

1
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
  • setdir

创建一个键目录。如果目录不存在就创建,如果目录存在更新目录TTL。

1
$ etcdctl setdir testdir3

支持的选项为:

1
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
  • updatedir

更新一个已经存在的目录。

1
$ etcdctl updatedir testdir2

支持的选项为:

1
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
  • rmdir

删除一个空目录,或者键值对。

1
2
$ etcdctl setdir dir1
$ etcdctl rmdir dir1

若目录不空,会报错:

1
2
3
4
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error:  108: Directory not empty (/dir) [17]
  • ls

列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。

例如:

1
2
3
4
5
6
7
$ etcdctl ls
/testdir
/testdir2
/dir

$ etcdctl ls dir
/dir/testkey

支持的选项包括:

1
2
3
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加/进行区分

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值