Consul
Consul是什么
特性
- 服务发现 Consul的客户端可用提供一个服务,比如 api 或者mysql ,另外一些客户端可用使用Consul去发现一个指定服务的提供者.通过DNS或者HTTP应用程序可用很容易的找到他所依赖的服务.
- 健康检查 Consul客户端可用提供任意数量的健康检查,指定一个服务(比如:webserver是否返回了200 OK 状态码)或者使用本地节点(比如:内存使用是否大于90%). 这个信息可由operator用来监视集群的健康.被服务发现组件用来避免将流量发送到不健康的主机.
- Key/Value存储 应用程序可用根据自己的需要使用Consul的层级的Key/Value存储.比如动态配置,功能标记,协调,领袖选举等等,简单的HTTP API让他更易于使用.
- 多数据中心: Consul支持开箱即用的多数据中心.这意味着用户不需要担心需要建立额外的抽象层让业务扩展到多个区域.
进程启动
consul agent -server -bootstrap -syslog \
-ui \
-data-dir=/opt/consul/data \
-config-dir=/opt/consul/conf \
-pid-file=/opt/consul/run/consul.pid \
-client=192.168.232.128 \
-bind=192.168.232.128 \
-node=consul-server01 \
-disable-host-node-id
consul agent -syslog \
-data-dir=/opt/consul/data \
-config-dir=/opt/consul/conf \
-pid-file=/opt/consul/run/consul.pid \
-client=192.168.232.129 \
-bind=192.168.232.129 \
-join=192.168.232.128 \
-node=consul-client01 \
-disable-host-node-id
Service 启动
useradd -s /sbin/nologin consul
mkdir /opt/consul/data -p
mkdir /opt/consul/conf
mkdir /opt/consul/run
chown -R consul.consul /opt/consul
Server
$ consul keygen
cg8StVXbQJ0gPvMd9o7yrg==
$ cat encrypt.json
{"encrypt": "cg8StVXbQJ0gPvMd9o7yrg=="}
服务参数
cat /opt/consul/conf/config.json
{
"datacenter":"good",
"data_dir":"/opt/consul/data",
"server":true,
"log_level": "INFO",
"client_addr":"192.168.232.128",
"bind_addr":"192.168.232.128",
"advertise_addr":"192.168.232.128",
"node_name":"consul-server01"
}
cat /etc/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/opt/consul/conf/config.json
[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -bootstrap -ui -config-dir=/opt/consul/conf -pid-file=/opt/consul/run/consul.pid -config-file=/opt/consul/encrypt.json
ExecReload=/usr/local/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
client
cat /opt/consul/conf/config.json
{
"client_addr": "127.0.0.1",
"datacenter": "good",
"bind_addr":"192.168.232.129",
"advertise_addr":"192.168.232.129",
"data_dir": "/opt/consul/data",
"domain": "consul",
"enable_script_checks": true,
"dns_config": {
"enable_truncate": true,
"only_passing": true
},
"enable_syslog": true,
"encrypt": "RPrXvkiNltfYCEZ5dpSHtw==", //服务器生成encrypt
"leave_on_terminate": true,
"log_level": "ERR",
"rejoin_after_leave": true,
"server": false,
"start_join": [
"192.168.232.128"
]
}
vim /etc/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/opt/consul/conf/config.json
[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/opt/consul/conf -pid-file=/opt/consul/run/consul.pid
ExecReload=/usr/local/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
启动
systemctl daemon-reload
systemctl start consul
客户端注册
[root@k8s-node1 conf]# pwd
/opt/consul/conf
[root@k8s-node1 conf]# cat spring-boot.json
{"service":
{"name": "spring-boot",
"tags": ["itsm"],
"port": 8080,
"check":
{"http": "http://192.168.232.129:8080", "interval": "10s"}
}
}
起一个端口
python -m SimpleHTTPServer 8080