consul单台机器部署,注册外网服务健康检查原创
@TOC
# 下载环境
1.consul版本: v1.16.2 2.系统:centos7 其他系统及consul版本安装 (opens new window)
# centos7系统
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul
1
2
3
4
2
3
4
# 命令行运行consul
sudo consul agent -dev -client 0.0.0.0 -enable-script-checks -node=web -ui
1
# 使用api方式注册外部服务进行健康检查
test.json文件
{
"ID": "test",
"Name": "My Service",
"Address": "s1.nsloop.com",
"Port": 16271,
"Check": {
"HTTP": "https://s1.nsloop.com:16271/",
"Interval": "10s"
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
虚拟机内网地址:192.168.0.195,需要开放8500端口
# json文件存放位置:D:/test.json 假设虚拟机内网为:
curl --request PUT --data @D:/test2.json 192.168.0.195:8500/v1/agent/service/register
1
2
2
# 第三方 SaaS 服务以及在无法直接运行 Consul 代理的其他环境中运行的服务
# Consul ESM (外部服务监视器)注册和运行状况检查内部和外部服务的过程
- 安装ESM扩展 下载地址 (opens new window) gitGub地址 (opens new window)
- 命令行运行consul-esm
consul-esm
1
- 启动esm后,使用json文件注册服务到consul进行外部服务健康检查
test2.json文件
{
"Datacenter": "dc1",
"ID": "40e4a748-2192-161a-0510-9bf59fe950b5",
"Node": "foo",
"Address": "s1.nsloop.com",
"TaggedAddresses": {
"lan": "s1.nsloop.com",
"wan": "s1.nsloop.com"
},
"NodeMeta": {
"external-node": "true",
"external-probe": "true"
},
"Service": {
"ID": "web1",
"Service": "web",
"Tags": [
"v1"
],
"Address": "s1.nsloop.com",
"Port": 16271
},
"Checks": [{
"Node": "foo",
"CheckID": "service:web1",
"Name": "Web HTTP check",
"Notes": "",
"Status": "passing",
"ServiceID": "web1",
"Definition": {
"HTTP": "https://s1.nsloop.com:16271/",
"Interval": "10s",
"Timeout": "5s"
}
},{
"Node": "foo",
"CheckID": "service:web2",
"Name": "Web HTTP check",
"Notes": "",
"Status": "passing",
"ServiceID": "web1",
"Definition": {
"HTTP": "https://s1.nsloop.com:8285/",
"Interval": "10s",
"Timeout": "5s"
}
}]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
使用api的方式 注意地址为:/v1/catalog/register
curl --request PUT --data @D:/consul/test2.json 192.168.0.195:8500/v1/catalog/register
1
完成了对外部服务进行健康检查
# 外部服务检查的坑
健康检查现象:context deadline exceeded (Client.Timeout exceeded while awaiting headers) 超时
- 检查指令
curl www.baidu.com
- 消息阻塞,并没有返回百度的消息
- 排查思路: 查看本地env环境,是否做了代理:
env | grep http
- 发现莫名其妙的会代理到8118端口
- 需要取消8118端口代理
sudo vi /etc/profile
- 注释或删除代理命令,更新文件
source /etc/profile
再次检查是否还有代理 - 完成对外部服务的健康检查,没有超时错误
- 检查指令
使用该方式,进程结束,注册的历史服务会消失
上次更新: 2024/07/30, 22:53:10