干货 | 手把手教你Etcd的云端部署

干货 | 手把手教你Etcd的云端部署

干货 | 手把手教你Etcd的云端部署

Etcd具有下面这些属性:

  • 完全复制:集群中的每个节点都可以使用完整的存档
  • 高可用性:Etcd可用于避免硬件的单点故障或网络问题
  • 一致性:每次读取都会返回跨多主机的最新写入
  • 简单:包括一个定义良好、面向用户的API(gRPC)
  • 安全:实现了带有可选的客户端证书身份验证的自动化TLS
  • 快速:每秒10000次写入的基准速度
  • 可靠:使用Raft算法实现了存储的合理分布

自从2014年成为Kubernetes的一部分以来,Etcd社区呈现指数级的增长。CoreOS、谷歌、Redhat、IBM、思科、华为等等均是Etcd的贡献成员。其中AWS、谷歌云平台和Azure等大型云提供商成功在生产环境中使用了Etcd。

Etcd在Kubernetes中的工作是为分布式系统安全存储关键数据。它最著名的是Kubernetes的主数据存储,用于存储配置数据、状态和元数据。由于Kubernetes通常运行在几台机器的集群上,因此它是一个分布式系统,需要Etcd这样的分布式数据存储。内网部署同一网段情况下访问很方便。

但当集群基于云部署的时候客户端多要跨网络访问集群。今天,我们会专门为大家介绍两个跨网络访问方案:

方案一:每个Etcd节点拥有公网ip,通过指定--advertise-client-urls 参数通过公网IP广播地址

方案二:Etcd节点无公网ip,通过网关及ssh tunnel转发请求

具体实施可参考以下步骤:

集群节点

干货 | 手把手教你Etcd的云端部署

通过internetwork访问,每个Etcd节点都有公网IP

如需要通过internet访问Etcd集群,必须配置 --advertise-client-urls 为内网ip和外网IP例如:

--advertise-client-urls http://10.0.64.100:2379,http://125.94.39.48:2380

集群配置

./etcd --name etcd0 --initial-advertise-peer-urls http://10.0.64.100:2380 \
      --listen-peer-urls http://0.0.0.0:2380 \
      --listen-client-urls http://0.0.0.0:2379  \
      --advertise-client-urls http://10.0.64.100:2379,http://125.94.39.48:2380 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
      --initial-cluster-state new >> etcd.log 2>&1 &
    
    ./etcd --name etcd1 --initial-advertise-peer-urls http://10.0.64.101:2380  \
      --listen-peer-urls http://0.0.0.0:2380 \
      --listen-client-urls http://0.0.0.0:2379 \
      --advertise-client-urls http://10.0.64.101:2379,http://125.94.39.105:2380  \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
      --initial-cluster-state new  >> etcd.log 2>&1 &
    
    ./etcd --name etcd2 --initial-advertise-peer-urls http://10.0.64.102:2380 \
      --listen-peer-urls http://0.0.0.0:2380 \
      --listen-client-urls  http://0.0.0.0:2379  \
      --advertise-client-urls http://10.0.64.102:2379,http://59.37.136.50:2380 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
      --initial-cluster-state new  >> etcd.log 2>&1 &

访问集群

export ETCDCTL_API=3

#内网访问
etcdctl  --endpoints=http://10.0.64.100:2379,http://10.0.64.101:2379,http://10.0.64.102:2379 member list

#公网访问
etcdctl  --endpoints=http://125.94.39.48:2379,http://125.94.39.105:2379,http://59.37.136.50:2379 member list
curl http://125.94.39.48:2379/v2/keys/message

通过网关访问集群,集群无公网IP,gateway有公网IP。

Etcd集群配置

luster-token etcd-cluster-1 \
  --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
  --initial-cluster-state new >> etcd.log 2>&1 &

./etcd --name etcd1 --initial-advertise-peer-urls http://10.0.64.101:2380  \
  --listen-peer-urls http://0.0.0.0:2380 \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://10.0.64.101:2379  \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
  --initial-cluster-state new  >> etcd.log 2>&1 &

./etcd --name etcd2 --initial-advertise-peer-urls http://10.0.64.102:2380 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --listen-client-urls  http://0.0.0.0:2379  \
  --advertise-client-urls http://10.0.64.102:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster etcd0=http://10.0.64.100:2380,etcd1=http://10.0.64.101:2380,etcd2=http://10.0.64.102:2380 \
  --initial-cluster-state new  >> etcd.log 2>&1 &

开启gateway

etcd gateway start --endpoints=http://10.0.64.100:2379,http://10.0.64.101:2379,http://10.0.64.102:2379   >> etcd_gateway.log 2>&1 &

验证集群

export ETCDCTL_API=3
etcdctl  --endpoints=http://10.0.64.100:2379,http://10.0.64.101:2379,http://10.0.64.102:2379 member list
etcdctl  --endpoints=http://127.0.0.1:23790 member list

创建ssh tunnel

# 有公网ip地址主机上执行
ssh  -g -f -N -L 23690:127.0.0.1:23790 root@127.0.0.1

通过公网访问网关

export ETCDCTL_API=3
etcdctl  --endpoints=http://157.255.51.197:23690 member list
etcdctl  --endpoints=http://157.255.51.197:23690 put foo bar
etcdctl  --endpoints=http://157.255.51.197:23690 get foo

欢迎点击“京东云”了解更多精彩

干货 | 手把手教你Etcd的云端部署
干货 | 手把手教你Etcd的云端部署

相关推荐