大连网站建设吗,小程序登录入口软件,房地产公司排名,深圳住建设局官方网站一、概述
consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案#xff0c;不再需要依赖其他工具#xff08;比如ZooKeeper等#xff09;。服务部署简单#x…一、概述
consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案不再需要依赖其他工具比如ZooKeeper等。服务部署简单只有一个可运行的二进制的包。每个节点都需要运行agent他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全同时保证server-leader的选举能够正确的进行。
client
CLIENT表示consul的client模式就是客户端模式。是consul节点的一种模式这种模式下所有注册到当前节点的服务会被转发到SERVER本身是不持久化这些信息。
server
SERVER表示consul的server模式表明这个consul是个server这种模式下功能和CLIENT都一样唯一不同的是它会把所有的信息持久化的本地这样遇到故障信息是可以被保留的。
server-leader
中间那个SERVER下面有LEADER的字眼表明这个SERVER是它们的老大它和其它SERVER不一样的一点是它需要负责同步注册的信息给其它的SERVER同时也要负责各个节点的健康监测。
raft
server节点之间的数据一致性保证一致性协议使用的是raft而zookeeper用的paxosetcd采用的也是raft。
服务发现协议
consul采用http和dns协议etcd只支持http
服务注册
consul支持两种方式实现服务注册一种是通过consul的服务注册http API由服务自己调用API实现注册另一种方式是通过json个是的配置文件实现注册将需要注册的服务以json格式的配置文件给出。consul官方建议使用第二种方式。
服务发现
consul支持两种方式实现服务发现一种是通过http API来查询有哪些服务另外一种是通过consul agent 自带的DNS8600端口域名是以NAME.service.consul的形式给出NAME即在定义的服务配置文件中服务的名称。DNS方式可以通过check的方式检查服务。
服务间的通信协议
Consul使用gossip协议管理成员关系、广播消息到整个集群他有两个gossip poolLAN pool和WAN poolLAN pool是同一个数据中心内部通信的WAN pool是多个数据中心通信的LAN pool有多个WAN pool只有一个。 二、consul集群搭建
1安装
首先去官网现在合适的consul包https://www.consul.io/downloads.html
安装直接下载zip包解压后只有一个可执行的文件consul将consul添加到系统的环境变量里面。
#unzip consul_1.2.3_linux_amd64.zip
#cp -a consul /usr/bin
#consul Usage: consul [--version] [--help] command [args]Available commands are:agent Runs a Consul agentcatalog Interact with the catalogconnect Interact with Consul Connectevent Fire a new eventexec Executes a command on Consul nodesforce-leave Forces a member of the cluster to enter the left stateinfo Provides debugging information for operators.intention Interact with Connect service intentionsjoin Tell Consul agent to join clusterkeygen Generates a new encryption keykeyring Manages gossip layer encryption keyskv Interact with the key-value storeleave Gracefully leaves the Consul cluster and shuts downlock Execute a command holding a lockmaint Controls node or service maintenance modemembers Lists the members of a Consul clustermonitor Stream logs from a Consul agentoperator Provides cluster-level tools for Consul operatorsreload Triggers the agent to reload configuration filesrtt Estimates network round trip time between nodessnapshot Saves, restores and inspects snapshots of Consul server statevalidate Validate config files/directoriesversion Prints the Consul versionwatch Watch for changes in Consul 输入consul出现上面的内容证明安装成功。 2启动
consul必须启动agent才能使用有两种启动模式server和client还有一个官方自带的ui。server用与持久化服务信息集群官方建议3或5个节点。client只用与于server交互。ui可以查看集群情况的。
server
cn1
#consul agent -bootstrap-expect 2 -server -data-dir /data/consul0 -nodecn1 -bind192.168.1.202 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1
cn2:
#consul agent -server -data-dir /data/consul0 -nodecn2 -bind192.168.1.201 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -join 192.168.1.202
cn3:
#consul agent -server -data-dir /data/consul0 -nodecn3 -bind192.168.1.200 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -join 192.168.1.202
参数解释
-bootstrap-expect:集群期望的节点数只有节点数量达到这个值才会选举leader。
-server 运行在server模式
-data-dir指定数据目录其他的节点对于这个目录必须有读的权限
-node指定节点的名称
-bind为该节点绑定一个地址
-config-dir指定配置文件定义服务的默认所有一.json结尾的文件都会读
-enable-script-checkstrue设置检查服务为可用
-datacenter: 数据中心没名称
-join加入到已有的集群中 client
#consul agent -data-dir /data/consul0 -nodecn4 -bind192.168.1.199 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -join 192.168.1.202
client节点可以有多个自己根据服务指定即可。
ui:
#consul agent -ui -data-dir /data/consul0 -nodecn4 -bind192.168.1.198 -client 192.168.1.198 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -join 192.168.1.202 -ui:使用自带的ui
-ui-dir指定ui的目录使用自己定义的ui
-client指定web ui、的监听地址默认127.0.0.1只能本机访问。
集群创建完成后
使用一些常用的命令检查集群的状态
#consul info
可以在raftstat看到此节点的状态是Fllower或者leader
#consul members
Node Address Status Type Build Protocol DC Segment cn1 192.168.1.202:8301 alive server 1.0.2 2 dc1 all cn2 192.168.1.201:8301 alive server 1.0.2 2 dc1 all cn3 192.168.1.200:8301 alive client 1.0.2 2 dc1 default
新加入一个节点有几种方式
1、这种方式重启后不会自动加入集群
#consul join 192.168.1.202
2、#在启动的时候使用-join指定一个集群
#consul agent -ui -data-dir /data/consul0 -nodecn4 -bind192.168.1.198 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -join 192.168.1.202
3、使用-startjoin或-rejoin
#consul agent -ui -data-dir /data/consul0 -nodecn4 -bind192.168.1.198 -config-dir /etc/consul.d -enable-script-checkstrue -datacenterdc1 -rejoin 访问ui
http://192.168.1.198:8500/ui
端口
8300consul agent服务relplaction、rpcclient-server
8301lan gossip
8302wan gossip
8500http api端口
8600DNS服务端口 3服务注册
采用的是配置文件的方式官方推荐首先创建一个目录用于存放定义服务的配置文件
#mkdir /etc/consul.d/
启动服务的时候要使用-config-dir 参数指定。
下面给出一个服务定义
#cat web.json {service:{name:web,tags:[rails],port:80,check:{name:ping,script:curl -s localhost:80,interval:3s}}
} 如果这样启动consul后会发现consul的日志里面一直报错因为我们没有启动80端口的服务下面给出我写的一个go程序
#cat web.go package main
import (iolognet/httpstrconvfmt
)
var iCnt int 0;func helloHandler(w http.ResponseWriter, r*http.request) {iCnt;str :Hell eorld ! friend( strconv.Itoa(iCnt))io.WriteString(w,str)fmt.Println(str)
}func main(){ht :http.HanderFunc(helloHandler)if ht ! nil {http.Handle(/hello,ht)}err : http.ListenAndServe(:80,nil)if err ! nil{log.Fatal(ListenAndserve:,err.Error())}
} #需要一个goalong的环境
#go build -o web web.go
#./web
此时就可以在没有运行web服务的机器上面执行DNS查询
# dig 127.0.0.1 -p 8600 web.service.consul SRV
;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 cn2.node.dc1.consul. web.service.consul. 0 IN SRV 1 1 80 cn3.node.dc1.consul.
;; ADDITIONAL SECTION: cn2.node.dc1.consul. 0 IN A 192.168.1.201 cn2.node.dc1.consul. 0 IN TXT consul-network-segment cn3.node.dc1.consul. 0 IN A 192.168.1.200 cn3.node.dc1.consul. 0 IN TXT consul-network-segment
;; Query time: 17 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: 四 1月 04 14:39:32 CST 2018 ;; MSG SIZE rcvd: 229
可以看到服务已经注册到集群里面了。
使用dns查询默认域名格式NAME.service.consulNAME就是web.json里面定义的service的name。可以自己指定域和端口-domain、-dns-port 53
为了方便使用consul集群的注册使用所以写了一个三节点client的注册脚本方便统一注册服务和管理。还利用到了nfs将服务文件共享到集群。 #!/usr/bin/env python
#encoding: utf8
#decription: registered a service to consul
from subprocess import call
import syshosts {b1:10.10.1.01:8500,b2:10.10.7.1:8500,b3:10.10.8.21:8500}def consul_relaod():if (len(sys.argv) ! 2) or sys.argv[1] -h:print((Usage: {0} [option] :{1} ,if you wang update all of them,you must use all).format(sys.argv[0],hosts.keys()))sys.exit()elif(sys.argv[1] all):for i in hosts.keys():call((consul reload -http-addr {}).format(hosts[i]),shellTrue)else:call((consul reload -http-addr {}).format(hosts[sys.argv[1]]),shellTrue)if __name__ __main__:consul_relaod() hosts是client节点列表。可以只注册其中的一个节点输入hosts中对应的key也可以输入all注册到所有节点nfs共享的是/etc/consul.d目录。
4健康检查
check使用来做服务的健康检查的可以拥有多个也可以不使用支持多种方式检查。check必须是script或者TTL类型如果是TTL类型则ttl变量必须被提供。script是consul主动去检查服务的健康状况ttl是服务主动向consul报告自己的状况。新版本的consul不在使用script来表示使用args如果是https服务的健康检查可以使用args这种脚本的方式实现因为consul默认不支持https的健康检查。
script check
check: { args: [/data/scripts/kubeadm-ha-0.sh,], interval: 10s
} http check:
{ check: { id: api, name: HTTP API 500, http: http://loclhost:500/health, interval: 10s, timeout: 1s
}
} tcp check:
{ check: { id: ssh, name: ssh TCP 26622, tcp: localhost:26622, interval: 10s, timeout: 1s }
} ttl check:
{ check: { id: web-app, name: Web APP status, notes: Web APP does a curl internally every 10 seconds, ttl: 30s
}
} 三、更新consul版本为最新版本1.2.3.
版本更新特性
https://github.com/hashicorp/consul/blob/v1.2.3/CHANGELOG.md FEATURES:agent: New Cloud Auto-join provider: Kubernetes (K8S) [GH-4635]
http: Added support for Authorization: Bearer head in addition to the X-Consul-Token header. [GH-4483]
dns: Added a way to specify SRV weights for each service instance to allow weighted DNS load-balancing. [GH-4198]
dns: Include EDNS-ECS options in EDNS responses where appropriate: see RFC 7871 [GH-4647]
IMPROVEMENTS:ui: Switch to fullscreen layout for lists and detail, left aligned forms [GH-4435]
connect: TLS certificate readiness now performs x509 certificate verification to determine whether the cert is usable. [GH-4540]
ui: The syntax highlighting/code editor is now on by default [GH-4651]
ui: Fallback to showing Node.Address if Service.Address is not set [GH-4579] ui较之前有很大改变