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

clipboard.png

clipboard.png

Etcd具备下面这些属性:算法

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

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

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

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

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

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

具体实施可参考如下步骤:url

集群节点

clipboard.png

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

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

--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。code

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

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

clipboard.png
clipboard.png

相关文章
相关标签/搜索