zoukankan      html  css  js  c++  java
  • Consul集群

    Consul集群
     
    官方网站:
     
     
    功能:
    Service Discovery
    Failure Detection
    Multi Datacenter
    Key/Value Storage
     
     
    环境:
    CentOS 7.0
    consul-0.6.4
     
    本实验采用3节点consul server
    consul-s1.example.com
    consul-s2.example.com
    consul-s3.example.com
    consul-c1.example.com
     
     
    一.Server端
     
    1.安装(所有节点)
    mkdir -p /opt/consul/{data,conf,web,logs,run}
    unzip consul_0.6.4_linux_amd64.zip -d /usr/local/bin/
    unzip consul_0.6.4_web_ui.zip -d /opt/consul/web
    2.启用日志(所有节点)
    cat >/etc/rsyslog.d/consul.conf <<HERE
    local0.* /opt/consul/logs/consul.log
    HERE
    systemctl restart rsyslog
    3.日志rotate(所有节点)
    cat >/etc/logrotate.d/consul <<HERE
    /opt/consul/logs/*log {
    missingok
    compress
    notifempty
    daily
    rotate 5
    create 0600 root root
    }
    HERE
    4.配置server cluster
    consul-s1.example.com
    consul agent -server -bootstrap -syslog
        -ui-dir=/opt/consul/web
        -data-dir=/opt/consul/data
        -config-dir=/opt/consul/conf
        -pid-file=/opt/consul/run/consul.pid
        -client=0.0.0.0
        -advertise=192.168.192.101
        -node=consul-s1
    提示:-bootstrap一般只在集群初始化时使用一次
    单节点测试,还可以-bootstrap-expect=1来自我选举为leader
     
    consul-s2.example.com
    consul agent -server -syslog
        -ui-dir=/opt/consul/web
        -data-dir=/opt/consul/data
        -config-dir=/opt/consul/conf
        -pid-file=/opt/consul/run/consul.pid
        -client=0.0.0.0
        -advertise=192.168.192.102
        -join=192.168.192.101
        -node=consul-s2

    consul-s3.example.com
    consul agent -server -syslog
        -ui-dir=/opt/consul/web
        -data-dir=/opt/consul/data
        -config-dir=/opt/consul/conf
        -pid-file=/opt/consul/run/consul.pid
        -client=0.0.0.0
        -advertise=192.168.192.103
        -join=192.168.192.101
        -node=consul-s3
     
    如果没有正常选出leader,可以手动触发bootstrap    consul join 192.168.192.101
    参数说明:
    -advertise:通知展现地址用来改变我们给集群中的其他节点展现的地址,一般情况下-bind地址就是展现地址
    -bootstrap:用来控制一个server是否在bootstrap模式,在一个datacenter中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为raft leader。
    -bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap公用
    -bind:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0
    -client:consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1
    -config-file:明确的指定要加载哪个配置文件
    -config-dir:配置文件目录,里面所有以.json结尾的文件都会被加载
    -data-dir:提供一个目录用来存放agent的状态,所有的agent允许都需要该目录,该目录必须是稳定的,系统重启后都继续存在
    -dc:该标记控制agent允许的datacenter的名称,默认是dc1
    -encrypt:指定secret key,使consul在通讯时进行加密,key可以通过consul keygen生成,同一个集群中的节点必须使用相同的key
    -join:加入一个已经启动的agent的ip地址,可以多次指定多个agent的地址。如果consul不能加入任何指定的地址中,则agent会启动失败,默认agent启动时不会加入任何节点。
    -retry-join:和join类似,但是允许你在第一次失败后进行尝试。
    -retry-interval:两次join之间的时间间隔,默认是30s
    -retry-max:尝试重复join的次数,默认是0,也就是无限次尝试
    -log-level:consul agent启动后显示的日志信息级别。默认是info,可选:trace、debug、info、warn、err。
    -node:节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名
    -protocol:consul使用的协议版本
    -rejoin:使consul忽略先前的离开,在再次启动后仍旧尝试加入集群中。
    -server:定义agent运行在server模式,每个集群至少有一个server,建议每个集群的server不要超过5个
    -syslog:开启系统日志功能,只在linux/osx上生效
    -ui-dir:提供存放web ui资源的路径,该目录必须是可读的
    -pid-file:提供一个路径来存放pid文件,可以使用该文件进行SIGINT/SIGHUP(关闭/更新)agent
    端口说明:
    dns - The DNS server, -1 to disable. Default 8600.
    http - The HTTP API, -1 to disable. Default 8500.
    https - The HTTPS API, -1 to disable. Default -1 (disabled).
    rpc - The CLI RPC endpoint. Default 8400.
    serf_lan - The Serf LAN port. Default 8301.
    serf_wan - The Serf WAN port. Default 8302.
    server - Server RPC address. Default 8300.
     
     
    [root@consul-s1 ~]# consul version
    Consul v0.6.4
    Consul Protocol: 3 (Understands back to: 1)
    [root@consul-s1 ~]# netstat -tunlp|grep consul
    tcp6       0      0 :::8300                 :::*                    LISTEN      2581/consul         
    tcp6       0      0 :::8301                 :::*                    LISTEN      2581/consul         
    tcp6       0      0 :::8302                 :::*                    LISTEN      2581/consul         
    tcp6       0      0 :::8400                 :::*                    LISTEN      2581/consul         
    tcp6       0      0 :::8500                 :::*                    LISTEN      2581/consul         
    tcp6       0      0 :::8600                 :::*                    LISTEN      2581/consul         
    udp6       0      0 :::8600                 :::*                                2581/consul         
    udp6       0      0 :::8301                 :::*                                2581/consul         
    udp6       0      0 :::8302                 :::*                                2581/consul 
     
    日志里有详细的选举过程
        2016/06/15 14:08:48 [ERR] agent: coordinate update error: No cluster leader
        2016/06/15 14:09:06 [ERR] agent: coordinate update error: No cluster leader
        2016/06/15 14:09:08 [ERR] agent: failed to sync remote state: No cluster leader
        2016/06/15 14:09:19 [INFO] serf: EventMemberJoin: consul-s3 192.168.192.103
        2016/06/15 14:09:19 [INFO] consul: adding LAN server consul-s3 (Addr: 192.168.192.103:8300) (DC: dc1)
        2016/06/15 14:09:21 [WARN] raft: Rejecting vote request from 192.168.192.102:8300 since our last term is greater (9, 0)
        2016/06/15 14:09:21 [WARN] raft: Clearing log suffix from 1 to 210
        2016/06/15 14:09:21 [INFO] consul: New leader elected: consul-s2
        2016/06/15 14:09:22 [INFO] agent: Synced service 'consul'
     
    [root@consul-s1 ~]# curl localhost:8500/v1/status/leader
    "192.168.192.103:8300"
    [root@consul-s1 ~]# curl localhost:8500/v1/status/peers
    ["192.168.192.101:8300","192.168.192.103:8300","192.168.192.102:8300"]
     
     
    5.web ui
    因为所有consul server都安装有web ui,因此,任意一台consul server节点都可以查看

     
    二.Client端
    1.安装
    同上
    2.启用日志
    同上
    3.日志rotate
    同上
    4.配置consul agent
    consul-c1.example.com
    consul agent -syslog
        -data-dir=/opt/consul/data
        -config-dir=/opt/consul/conf
        -pid-file=/opt/consul/run/consul.pid
        -client=0.0.0.0
        -advertise=192.168.192.20
        -join=192.168.192.101
        -node=consul-c1
    说明:客户端没有-server参数,web ui也可省略
     
    [root@consul-s1 ~]# consul members
    Node       Address               Status  Type    Build  Protocol  DC
    consul-c1  192.168.192.20:8301   alive   client  0.6.4  2         dc1
    consul-s1  192.168.192.101:8301  alive   server  0.6.4  2         dc1
    consul-s2  192.168.192.102:8301  alive   server  0.6.4  2         dc1
    consul-s3  192.168.192.103:8301  alive   server  0.6.4  2         dc1
     
  • 相关阅读:
    为什么 execute(`echo 中文`) 输出中文源码?
    使用图片跨域方式获取图片数据 使用 jsonp 方式跨域获取数据
    js 中的 sleep 方法, 阻塞式
    大数据存储单位介绍(TB、PB、EB、ZB、YB有多大)
    在 chrome 开发工具中使用终端
    作为一个代码搬运工,如何应对突如其来的灵魂拷问『你今天做了什么』?
    JVM相关
    python和C++联合调试
    Google protobuf解析消息逻辑的版本问题
    卷积转换为矩阵运算中填充数的计算-GEMM
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814049.html
Copyright © 2011-2022 走看看