zoukankan      html  css  js  c++  java
  • etcd入门

    简介

    • etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库。

    • etcd内部采用raft协议作为一致性算法,基于Go语言实现。

    • etcd 作为服务发现系统, 有以下特点:

      • 简单: 安装配置简单, 且提供了HTTP API进行交互, 使用简单。
      • 安全: 支持SSL证书验证
      • 快速: 根据官方提供的benchmark数据, 单实例支持每秒2k+读操作
      • 可靠: 采用raft算法, 实现分布式系统数据的可用性和一致性
    • etcd是构建Kubernetes集群的一个重要组件。

      img

    与其他服务发现系统对比

    • Zookeeper使用的是基于mult-paxos的简化版zab(Zookeeper Atomic Broadcast), etcd 和 consul则是基于raft算法, 同样也是mult-paxos的一种简化。

      Feature(特征) Consul Zookeeper etcd euerka
      服务健康检查 服务状态, 内存, 硬盘 (弱)长连接, keepalive 连接心跳 可配支持
      多数据中心 支持 -- -- --
      kv存储服务 支持 支持 支持 --
      一致性 raft zab(paxos) raft --
      cap ca cp cp ap
      使用接口(多语言能力) 支持http和dns 客户端 http/grpc http(sidecar)
      watch支持 全量/支持long polling 支持 支持 long polling 支持 long polling/大部分增量
      自身监控 metrics -- metrics metrics
      安全 acl/https acl https支持(弱) 1.0不支持, 2.0支持了, 但是闭源了
      spring cloud集成 已支持 已支持 已支持 已支持
      • 服务的健康检查
        • Euraka 使用时需要显式配置健康检查支持
        • Zookeeper,Etcd 则在失去了和服务进程的连接情况下任务不健康
        • Consul 相对更为详细点,比如内存是否已使用了90%,文件系统的空间是不是快不足了。
      • 多数据中心支持
        • Consul 通过 WAN 的 Gossip(流言算法, Cassandra, 比特币底层的协议) 协议,完成跨数据中心的同步;而且其他的产品则需要额外的开发工作来实现;
      • KV存储服务
        • 除了 Eureka ,其他几款都能够对外支持 k-v 的存储服务
        • 提供存储服务,能够较好的转化为动态配置服务。
      • 产品设计中 CAP 理论的取舍
        • Eureka 典型的 AP,作为分布式场景下的服务发现的产品较为合适,服务发现场景的可用性优先级较高,一致性并不是特别致命。
        • 其次 CA 类型的场景 Consul,也能提供较高的可用性,并能 k-v store 服务保证一致性。
        • 而Zookeeper, Etcd则是CP类型 牺牲可用性,在服务发现场景并没太大优势。
      • 多语言能力与对外提供服务的接入协议
        • Zookeeper的跨语言支持较弱,其他几款支持 http11 提供接入的可能。Euraka 一般通过 sidecar的方式提供多语言客户端的接入支持。Etcd 还提供了Grpc的支持。 Consul除了标准的Rest服务api,还提供了DNS的支持。
      • Watch的支持 (客户端观察到服务提供者变化)
        • Zookeeper 支持服务器端推送变化, Eureka2.0也支持, 但是2.0闭源了, 这就很坑。Eureka 1.x,Consul,Etcd则都通过长轮询的方式来实现变化的感知。
      • 自身集群的监控
        • 除了 Zookeeper ,其他几款都默认支持 metrics,运维者可以搜集并报警这些度量信息达到监控目的;
      • 安全
        • Consul,Zookeeper 支持ACL,另外 Consul,Etcd 支持安全通道https.
      • Spring Cloud的集成
        • 目前都有相对应的 boot starter, 提供了集成能力。
        • 总的来看,目前Consul 自身功能,和 spring cloud 对其集成的支持都相对较为完善,而且运维的复杂度较为简单(没有详细列出讨论)
        • Eureka 设计上比较符合场景,但还需持续的完善。
        • Etcd 和 Zookeeper 提供的能力非常相似,在软件生态中所处的位置也几乎是一样的,可以互相替代的。
          • 都是通用的一致性元信息存储
          • 都提供watch机制用于变更通知和分发
          • 都被分布式系统用来作为共享信息存储
        • 二者除了实现细节,语言,一致性协议上的区别,最大的区别在周边生态圈
          • Zookeeper 是apache下的,用java写的,提供rpc接口,最早从hadoop项目中孵化出来,在分布式系统中得到广泛使用(hadoop, spark, storm, kafka, flink 等)。
          • Etcd 是coreos公司旗下的开源产品,比较新,以其简单好用的rest接口以及活跃的社区俘获了一批用户,在新的一些集群中得到使用(比如kubernetes)。
            • 虽然etcd v3为了性能也改成二进制rpc接口了,但其易用性上比 Zookeeper 还是好一些。
          • 而 Consul 的目标则更为具体一些,Etcd 和 Zookeeper 提供的是分布式一致性存储能力,具体的业务场景需要用户自己实现,比如服务发现,比如配置变更。
          • Consul 以服务发现和配置变更为主要目标,同时附带了kv存储。
        • 在软件生态中,越抽象的组件适用范围越广,但同时对具体业务场景需求的满足上肯定有不足之处。

    应用场景

    • etcd比较多的应用场景是用于服务发现, 要解决的是分布式系统中最常见的问题之一,即在同一个分布式集群中的进程或服务如何才能找到对方并建立连接。

    • 从本质上说,服务发现就是要了解集群中是否有进程在监听upd或者tcp端口,并且通过名字就可以进行查找和链接。

    • 要解决服务发现的问题,需要下面三大支柱,缺一不可。

      • 一个强一致性、高可用的服务存储目录。
        • 基于Ralf算法的etcd天生就是这样一个强一致性、高可用的服务存储目录。
      • 一种注册服务和健康服务健康状况的机制。
        • 用户可以在etcd中注册服务,并且对注册的服务配置key TTL,定时保持服务的心跳以达到监控健康状态的效果。
      • 一种查找和连接服务的机制。
        • 通过在etcd指定的主题下注册的服务业能在对应的主题下查找到。
        • 为了确保连接,我们可以在每个服务机器上都部署一个proxy模式的etcd,这样就可以确保访问etcd集群的服务都能够互相连接。

      img

    单机版安装

    • 文件下载地址: https://github.com/etcd-io/etcd/releases/

    • 下载的是已经编译好的二进制文件, 上传后解压到指定目录。

      tar -zxvf etcd-v3.3.18-linux-amd64.tar.gz -C /opt/ronnie/
      
    • 修改目录名

      cd /opt/ronnie
      mv etcd-v3.3.18-linux-amd64/ etcd
      
    • 解压后是一些文档和两个二进制文件etcd和etcdctl。etcd是server端,etcdctl是客户端。

      root@node01:/opt/ronnie# cd etcd/
      root@node01:/opt/ronnie/etcd# ll
      total 39492
      drwxr-xr-x  3 ronnie ronnie     4096 11月 27 12:40 ./
      drwxr-xr-x 10 root   root       4096 11月 28 20:04 ../
      drwxr-xr-x 10 ronnie ronnie     4096 11月 27 12:40 Documentation/
      -rwxr-xr-x  1 ronnie ronnie 22373024 11月 27 12:40 etcd*
      -rwxr-xr-x  1 ronnie ronnie 17991872 11月 27 12:40 etcdctl*
      -rw-r--r--  1 ronnie ronnie    38864 11月 27 12:40 README-etcdctl.md
      -rw-r--r--  1 ronnie ronnie     7262 11月 27 12:40 README.md
      -rw-r--r--  1 ronnie ronnie     7855 11月 27 12:40 READMEv2-etcdctl.md
      
    • 如果在测试环境,启动一个单节点的etcd服务,只需要运行etcd命令就行。

      root@node01:/opt/ronnie/etcd# ./etcd
      2019-11-28 20:07:10.486651 I | etcdmain: etcd Version: 3.3.18
      2019-11-28 20:07:10.486702 I | etcdmain: Git SHA: 3c8740a79
      2019-11-28 20:07:10.486706 I | etcdmain: Go Version: go1.12.9
      2019-11-28 20:07:10.486708 I | etcdmain: Go OS/Arch: linux/amd64
      2019-11-28 20:07:10.486711 I | etcdmain: setting maximum number of CPUs to 6, total number of available CPUs is 6
      2019-11-28 20:07:10.486727 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
      2019-11-28 20:07:10.487188 I | embed: listening for peers on http://localhost:2380
      2019-11-28 20:07:10.487250 I | embed: listening for client requests on localhost:2379
      2019-11-28 20:07:10.488932 I | etcdserver: name = default
      2019-11-28 20:07:10.488956 I | etcdserver: data dir = default.etcd
      2019-11-28 20:07:10.488994 I | etcdserver: member dir = default.etcd/member
      2019-11-28 20:07:10.488999 I | etcdserver: heartbeat = 100ms
      2019-11-28 20:07:10.489002 I | etcdserver: election = 1000ms
      2019-11-28 20:07:10.489006 I | etcdserver: snapshot count = 100000
      2019-11-28 20:07:10.489091 I | etcdserver: advertise client URLs = http://localhost:2379
      2019-11-28 20:07:10.489109 I | etcdserver: initial advertise peer URLs = http://localhost:2380
      2019-11-28 20:07:10.489117 I | etcdserver: initial cluster = default=http://localhost:2380
      2019-11-28 20:07:10.492157 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
      2019-11-28 20:07:10.492192 I | raft: 8e9e05c52164694d became follower at term 0
      2019-11-28 20:07:10.492213 I | raft: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
      2019-11-28 20:07:10.492227 I | raft: 8e9e05c52164694d became follower at term 1
      2019-11-28 20:07:10.494755 W | auth: simple token is not cryptographically signed
      2019-11-28 20:07:10.495375 I | etcdserver: starting server... [version: 3.3.18, cluster version: to_be_decided]
      2019-11-28 20:07:10.495816 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
      2019-11-28 20:07:10.495872 I | etcdserver: 8e9e05c52164694d as single-node; fast-forwarding 9 ticks (election ticks 10)
      2019-11-28 20:07:10.792748 I | raft: 8e9e05c52164694d is starting a new election at term 1
      2019-11-28 20:07:10.792897 I | raft: 8e9e05c52164694d became candidate at term 2
      2019-11-28 20:07:10.792928 I | raft: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
      2019-11-28 20:07:10.792974 I | raft: 8e9e05c52164694d became leader at term 2
      2019-11-28 20:07:10.792998 I | raft: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
      2019-11-28 20:07:10.793353 I | etcdserver: setting up the initial cluster version to 3.3
      2019-11-28 20:07:10.793604 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
      2019-11-28 20:07:10.793701 I | embed: ready to serve client requests
      2019-11-28 20:07:10.793831 N | etcdserver/membership: set the initial cluster version to 3.3
      2019-11-28 20:07:10.793982 I | etcdserver/api: enabled capabilities for version 3.3
      2019-11-28 20:07:10.794021 E | etcdmain: forgot to set Type=notify in systemd service file?
      2019-11-28 20:07:10.794151 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
      
      • name: 节点名称, 默认为default
      • data-dir 保存日志和快照的吗,目录
      • 2380 端口负责和集群中其他节点通信
      • 2379 端口提供HTTP API服务, 供客户端交互
      • heartbeat 默认为100ms, leader多久发送一次心跳到followers。
      • election为1000ms,该参数的作用是重新投票的超时时间,如果follow在该+ 时间间隔没有收到心跳包,会触发重新投票,默认为1000ms。
      • snapshot count 默认为100000,该参数的作用是指定有多少事务被提交时,触发+ 截取快照保存到磁盘。
      • 集群和每个节点都会生成一个uuid。
      • 启动的时候会运行raft,选举出leader。
      • 以上方法只是简单的启动一个etcd服务,但要长期运行的话,还是做成一个服务好一些。
    • 建立etcd服务 (systemd)

      • 设定etcd配置文件

        • 建立相关目录

          mkdir -p /var/lib/etcd
          mkdir -p /opt/ronnie/etcd/config
          
      • 创建etcd配置文件

        root@node01:~# cat <<EOF | sudo tee /opt/ronnie/etcd/config/etcd.conf
        # 节点名称
        > ETCD_NAME=$(hostname -s)
        # 数据存放位置
        > ETCD_DATA_DIR=/var/lib/etcd
        > EOF
        
      • 创建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=-/opt/ronnie/etcd/config/etcd.conf
        ExecStart=/opt/ronnie/etcd/etcd
        Restart=on-failure
        RestartSec=10s
        LimitNOFILE=40000
        
        [Install]
        WantedBy=multi-user.target
        EOF
        
      • 启动etcd

        systemctl daemon-reload && systemctl enable etcd && systemctl start etcd
        
      • vim ~/.bashrc 修改环境变量(PS: 我的是ubuntu, centos的 vim /etc/profile)

        # etcd
        export ETCD_HOME=/opt/ronnie/etcd
        export PATH=$ETCD_HOME:$PATH
        
      • source ~/.bashrc 并查看etcd版本

        root@node01:/opt/ronnie/etcd# etcd --version
        etcd Version: 3.3.18
        Git SHA: 3c8740a79
        Go Version: go1.12.9
        Go OS/Arch: linux/amd64
        
      • 查看帮助指令

        root@node01:~# etcd -h
        usage: etcd [flags]
               start an etcd server
        
               etcd --version
               show the version of etcd
        
               etcd -h | --help
               show the help information about etcd
        
               etcd --config-file
               path to the server configuration file
        
               etcd gateway
               run the stateless pass-through etcd TCP connection forwarding proxy
        
               etcd grpc-proxy
               run the stateless etcd v3 gRPC L7 reverse proxy
        	
        
        member flags:
        
        	--name 'default'
        		human-readable name for this member.
        	--data-dir '${name}.etcd'
        		path to the data directory.
        	--wal-dir ''
        		path to the dedicated wal directory.
        	--snapshot-count '100000'
        		number of committed transactions to trigger a snapshot to disk.
        	--heartbeat-interval '100'
        		time (in milliseconds) of a heartbeat interval.
        	--election-timeout '1000'
        		time (in milliseconds) for an election to timeout. See tuning documentation for details.
        	--initial-election-tick-advance 'true'
        		whether to fast-forward initial election ticks on boot for faster election.
        	--listen-peer-urls 'http://localhost:2380'
        		list of URLs to listen on for peer traffic.
        	--listen-client-urls 'http://localhost:2379'
        		list of URLs to listen on for client traffic.
        	--max-snapshots '5'
        		maximum number of snapshot files to retain (0 is unlimited).
        	--max-wals '5'
        		maximum number of wal files to retain (0 is unlimited).
        	--cors ''
        		comma-separated whitelist of origins for CORS (cross-origin resource sharing).
        	--quota-backend-bytes '0'
        		raise alarms when backend size exceeds the given quota (0 defaults to low space quota).
        	--max-txn-ops '128'
        		maximum number of operations permitted in a transaction.
        	--max-request-bytes '1572864'
        		maximum client request size in bytes the server will accept.
        	--grpc-keepalive-min-time '5s'
        		minimum duration interval that a client should wait before pinging server.
        	--grpc-keepalive-interval '2h'
        		frequency duration of server-to-client ping to check if a connection is alive (0 to disable).
        	--grpc-keepalive-timeout '20s'
        		additional duration of wait before closing a non-responsive connection (0 to disable).
        
        clustering flags:
        
        	--initial-advertise-peer-urls 'http://localhost:2380'
        		list of this member's peer URLs to advertise to the rest of the cluster.
        	--initial-cluster 'default=http://localhost:2380'
        		initial cluster configuration for bootstrapping.
        	--initial-cluster-state 'new'
        		initial cluster state ('new' or 'existing').
        	--initial-cluster-token 'etcd-cluster'
        		initial cluster token for the etcd cluster during bootstrap.
        		Specifying this can protect you from unintended cross-cluster interaction when running multiple clusters.
        	--advertise-client-urls 'http://localhost:2379'
        		list of this member's client URLs to advertise to the public.
        		The client URLs advertised should be accessible to machines that talk to etcd cluster. etcd client libraries parse these URLs to connect to the cluster.
        	--discovery ''
        		discovery URL used to bootstrap the cluster.
        	--discovery-fallback 'proxy'
        		expected behavior ('exit' or 'proxy') when discovery services fails.
        		"proxy" supports v2 API only.
        	--discovery-proxy ''
        		HTTP proxy to use for traffic to discovery service.
        	--discovery-srv ''
        		dns srv domain used to bootstrap the cluster.
        	--strict-reconfig-check 'true'
        		reject reconfiguration requests that would cause quorum loss.
        	--auto-compaction-retention '0'
        		auto compaction retention length. 0 means disable auto compaction.
        	--auto-compaction-mode 'periodic'
        		interpret 'auto-compaction-retention' one of: periodic|revision. 'periodic' for duration based retention, defaulting to hours if no time unit is provided (e.g. '5m'). 'revision' for revision number based retention.
        	--enable-v2 'true'
        		Accept etcd V2 client requests.
        
        proxy flags:
        	"proxy" supports v2 API only.
        
        	--proxy 'off'
        		proxy mode setting ('off', 'readonly' or 'on').
        	--proxy-failure-wait 5000
        		time (in milliseconds) an endpoint will be held in a failed state.
        	--proxy-refresh-interval 30000
        		time (in milliseconds) of the endpoints refresh interval.
        	--proxy-dial-timeout 1000
        		time (in milliseconds) for a dial to timeout.
        	--proxy-write-timeout 5000
        		time (in milliseconds) for a write to timeout.
        	--proxy-read-timeout 0
        		time (in milliseconds) for a read to timeout.
        
        
        security flags:
        
        	--ca-file '' [DEPRECATED]
        		path to the client server TLS CA file. '-ca-file ca.crt' could be replaced by '-trusted-ca-file ca.crt -client-cert-auth' and etcd will perform the same.
        	--cert-file ''
        		path to the client server TLS cert file.
        	--key-file ''
        		path to the client server TLS key file.
        	--client-cert-auth 'false'
        		enable client cert authentication.
        	--client-crl-file ''
        		path to the client certificate revocation list file.
        	--trusted-ca-file ''
        		path to the client server TLS trusted CA cert file.
        	--auto-tls 'false'
        		client TLS using generated certificates.
        	--peer-ca-file '' [DEPRECATED]
        		path to the peer server TLS CA file. '-peer-ca-file ca.crt' could be replaced by '-peer-trusted-ca-file ca.crt -peer-client-cert-auth' and etcd will perform the same.
        	--peer-cert-file ''
        		path to the peer server TLS cert file.
        	--peer-key-file ''
        		path to the peer server TLS key file.
        	--peer-client-cert-auth 'false'
        		enable peer client cert authentication.
        	--peer-trusted-ca-file ''
        		path to the peer server TLS trusted CA file.
        	--peer-cert-allowed-cn ''
        		Required CN for client certs connecting to the peer endpoint.
        	--peer-auto-tls 'false'
        		peer TLS using self-generated certificates if --peer-key-file and --peer-cert-file are not provided.
        	--peer-crl-file ''
        		path to the peer certificate revocation list file.
        	--cipher-suites ''
        		comma-separated list of supported TLS cipher suites between client/server and peers (empty will be auto-populated by Go).
        	--experimental-peer-skip-client-san-verification 'false'
        		Skip verification of SAN field in client certificate for peer connections.
        
        logging flags
        
        	--debug 'false'
        		enable debug-level logging for etcd.
        	--log-package-levels ''
        		specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').
        	--log-output 'default'
        		specify 'stdout' or 'stderr' to skip journald logging even when running under systemd.
        
        unsafe flags:
        
        Please be CAUTIOUS when using unsafe flags because it will break the guarantees
        given by the consensus protocol.
        
        	--force-new-cluster 'false'
        		force to create a new one-member cluster.
        
        profiling flags:
        	--enable-pprof 'false'
        		Enable runtime profiling data via HTTP server. Address is at client URL + "/debug/pprof/"
        	--metrics 'basic'
        		Set level of detail for exported metrics, specify 'extensive' to include histogram metrics.
        	--listen-metrics-urls ''
        		List of URLs to listen on for metrics.
        
        auth flags:
        	--auth-token 'simple'
        		Specify a v3 authentication token type and its options ('simple' or 'jwt').
        
        experimental flags:
        	--experimental-initial-corrupt-check 'false'
        		enable to check data corruption before serving any client/peer traffic.
        	--experimental-corrupt-check-time '0s'
        		duration of time between cluster corruption check passes.
        	--experimental-enable-v2v3 ''
        		serve v2 requests through the v3 backend under a given prefix.
        
        
      • 常用命令选项:

        --debug 输出CURL命令,显示执行命令的时候发起的请求
        --no-sync 发出请求之前不同步集群信息
        --output, -o 'simple' 输出内容的格式(simple 为原始信息,json 为进行json格式解码,易读性好一些)
        --peers, -C 指定集群中的同伴信息,用逗号隔开(默认为: "127.0.0.1:4001")
        --cert-file HTTPS下客户端使用的SSL证书文件
        --key-file HTTPS下客户端使用的SSL密钥文件
        --ca-file 服务端使用HTTPS时,使用CA文件进行验证
        --help, -h 显示帮助命令信息
        --version, -v 打印版本信息
        
  • 相关阅读:
    MySQL数据库“局部”乱码
    每个人都可以写博客
    命令行导入SQL文件
    数据库连接扩展
    PHP程序设计经典300例
    C# 调用外部dll
    asp.net中实现文件下载功能
    Windows10下Docker的安装
    java基础——面向对象
    java基础——第一章概述
  • 原文地址:https://www.cnblogs.com/ronnieyuan/p/11953668.html
Copyright © 2011-2022 走看看