consul安装过程与一些指令

1.下载consul_0.7.5_linux_amd64.zip

wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

2.解压consul_0.7.5_linux_amd64.zip

unzip consul_0.7.5_linux_amd64.zip
解决办法-bash: unzip: 未找到命令如果解压出现该错误
yum -y install unzip

3.检查

./consul 看到如下界面说明安装成功

4.启动

我的linux Ip地址192.168.128.149
./consul agent -dev -ui -node=consul-dev -client=192.168.128.149

5.访问

192.168.128.149:8500
如果访问不了,临时关闭防火墙
systemctl stop firewalld

6.使用postMan注册服务(Post请求)

http://192.168.128.149:8500/v1/catalog/register
参数
{
“Datacenter”: “dc1”,
“Node”: “tomcat”,
“Address”: “192.168.1.100”,
“Service”: {
“Id”: “192.168.1.100:8080”,
“Service”: “itmayiedu”,
“tags”: [
“dev”
],
“Port”: 8080
}
}
参数
{
“Datacenter”: “dc1”,
“Node”: “tomcat”,
“Address”: “192.168.1.100”,
“Service”: {
“Id”: “192.168.1.100:8081”,
“Service”: “itmayeidu”,
“tags”: [
“dev”
],
“Port”: 8081
}
}
Datacenter指定数据中心,Address指定注册服务的IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、发布环境等),Service.Port指定服务端口

7.存储key/value

使用postmen 发送put请求
http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8080
http://192.168.128.149:8500/v1/kv/upstreams/itmayiedu/192.168.1.100:8081

Consul的安装与启动

cd /usr/local/bin/
wget https://releases.hashicorp.com/consul/1.0.0/consul_1.0.0_linux_amd64.zip?_ga=2.86515800.1546778999.1510551854-179199314.1510551854
unzip consul_1.0.0_linux_amd64.zip
得到consul 文件,这样就完成了安装。
启动consul./consul agent -dev # -dev表示开发模式运行,另外还有-server表示服务模式运行
-dev(该节点的启动不能用于生产环境,因为该模式下不会持久化任何状态),该启动模式仅仅是为了快速便捷的启动单节点consul
该节点处于server模式
该节点是leader
该节点是一个健康节点

输入http://127.0.0.1:8500/ui/ 访问Consul,可查看到默认的ui界面:
尝试访问http://192.168.11.143/ui/ ,会发现无法访问。说明Consul还不能被远程访问.
Consul常用命令
Consul常用命令

Consul常用命令详解
输入consul agent --help ,可以看到consul agent 的选项,如下:

1 -advertise=addr Sets the advertise address to use 2 -advertise-wan=addr Sets address to advertise on wan instead of advertise addr 3 -atlas=org/name Sets the Atlas infrastructure name, enables SCADA. 4 -atlas-join Enables auto-joining the Atlas cluster 5 -atlas-token=token Provides the Atlas API token 6 -atlas-endpoint=1.2.3.4 The address of the endpoint for Atlas integration. 7 -bootstrap Sets server to bootstrap mode 8 -bind=0.0.0.0 Sets the bind address for cluster communication 9 -http-port=8500 Sets the HTTP API port to listen on 10 -bootstrap-expect=0 Sets server to expect bootstrap mode. 11 -client=127.0.0.1 Sets the address to bind for client access. 12 This includes RPC, DNS, HTTP and HTTPS (if configured) 13 -config-file=foo Path to a JSON file to read configuration from. 14 This can be specified multiple times. 15 -config-dir=foo Path to a directory to read configuration files 16 from. This will read every file ending in ".json" 17 as configuration in this directory in alphabetical 18 order. This can be specified multiple times. 19 -data-dir=path Path to a data directory to store agent state 20 -dev Starts the agent in development mode. 21 -recursor=1.2.3.4 Address of an upstream DNS server. 22 Can be specified multiple times. 23 -dc=east-aws Datacenter of the agent (deprecated: use 'datacenter' instead). 24 -datacenter=east-aws Datacenter of the agent. 25 -encrypt=key Provides the gossip encryption key 26 -join=1.2.3.4 Address of an agent to join at start time. 27 Can be specified multiple times. 28 -join-wan=1.2.3.4 Address of an agent to join -wan at start time. 29 Can be specified multiple times. 30 -retry-join=1.2.3.4 Address of an agent to join at start time with 31 retries enabled. Can be specified multiple times. 32 -retry-interval=30s Time to wait between join attempts. 33 -retry-max=0 Maximum number of join attempts. Defaults to 0, which 34 will retry indefinitely. 35 -retry-join-wan=1.2.3.4 Address of an agent to join -wan at start time with 36 retries enabled. Can be specified multiple times. 37 -retry-interval-wan=30s Time to wait between join -wan attempts. 38 -retry-max-wan=0 Maximum number of join -wan attempts. Defaults to 0, which 39 will retry indefinitely. 40 -log-level=info Log level of the agent. 41 -node=hostname Name of this node. Must be unique in the cluster 42 -protocol=N Sets the protocol version. Defaults to latest. 43 -rejoin Ignores a previous leave and attempts to rejoin the cluster. 44 -server Switches agent to server mode. 45 -syslog Enables logging to syslog 46 -ui Enables the built-in static web UI server 47 -ui-dir=path Path to directory containing the Web UI resources 48 -pid-file=path Path to file to store agent PID 49 50

consul agent 命令的常用选项,如下:

-data-dir作用:指定agent储存状态的数据目录这是所有agent都必须的对于server尤其重要,因为他们必须持久化集群的状态
-config-dir作用:指定service的配置文件和检查定义所在的位置通常会指定为"某一个路径/consul.d"(通常情况下,.d表示一系列配置文件存放的目录)
-config-file作用:指定一个要装载的配置文件该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)
-dev作用:创建一个开发环境下的server节点该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘这种模式不能用于生产环境(因为第二条)
-bootstrap-expect作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。
-node作用:指定节点在集群中的名称该名称在集群中必须是唯一的(默认采用机器的host)推荐:直接采用机器的IP
-bind作用:指明节点的IP地址有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常
-server作用:指定节点为server每个数据中心(DC)的server数推荐至少为1,至多为5所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性server也是与其他DC交互的门面(gateway)
-client作用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC默认是127.0.0.1,只允许回环接口访问若不指定为-server,其实就是-client
-join作用:将节点加入到集群
-datacenter作用:指定机器加入到哪一个数据中心
尝试一下:
consul agent -dev -client 10.128.223.231
发现果然可以使用http://10.128.223.231:8500/ui 访问了。

代码交流 2021