forked from yoshinorim/mha4mysql-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
3. consul 配置示例
arstercz edited this page Jun 30, 2021
·
1 revision
consul 的服务发现功能支持以 DNS 接口模式对外服务, 这种方式也方便了 DNS
的更改和及时生效. 在实际的使用中, 为了高可用, 我们建议将 consul 配置为集群模式. 相关配置见: consul-agent.
下面的配置为简单的示例(json 风格配置
), 更多选项见: agent-option.
# cat /opt/consul/etc/consul.json
{
"datacenter": "chkdns",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "hostdb7",
"server": true,
"encrypt": "41MSg1hxKrPzeVMj7wDxgXozrdmv1Dmd7ckDk7C9rNM=",
"ui": false,
"bootstrap_expect": 3,
"disable_update_check": true,
"client_addr": "0.0.0.0",
"advertise_addr": "10.1.1.7",
"dns_config": {
"only_passing": true,
"recursor_timeout": "3s",
"node_ttl": "5s",
"allow_stale": true,
"max_stale": "1000s"
},
"domain": "infodb",
"ports": {
"dns" : 53
},
"recursors" : [
"1.1.1.1",
"114.114.114.114",
"8.8.8.8"
],
"performance": {
"raft_multiplier": 3
},
"primary_datacenter":"chkdns",
"acl" : {
"enabled": false,
"default_policy":"allow",
"down_policy":"extend-cache"
}
}
备注: 这里我们关闭了 acl
功能, 如果开启了 acl
, 需要设置好访问策略, 尤其是允许 service
被 dns 解析.
上述配置中, 以下选项按需修改:
选项 | 说明 |
---|---|
data_dir | consul 的数据目录, 可以按主机情况修改, 数据目录通常都很小 |
node_name | 节点名称, 建议采用当前主机的名字, 每个节点都需要按需修改 |
encrypt | consul 节点通过加密方式通信, 所有节点使用相同的 key, 可以通过 consul keygen 生成 |
advertise_addr | consul 节点对外通信的地址, 每个节点需要修改为本机的 ip 地址 |
domain | 对应 mha 的 consul_domain 选项, 默认为 consul, 最后的域名格式为 .service. |
集群中的机器都设置完成后, 可以通过以下方式检查集群的状态(比如我们的三台 consul 机器
):
# consul members
Node Address Status Type Build Protocol DC Segment
hostdb7 10.1.1.27:8301 alive server 1.10.0 2 chkdns <all>
hostdb8 10.1.1.28:8301 alive server 1.10.0 2 chkdns <all>
hostdb9 10.1.1.29:8301 alive server 1.10.0 2 chkdns <all>
可以通过以下方式手动注册服务, 并查看已注册信息:
# consul services register -name mysql-3327 -address 10.1.1.26 -port 3327 -tag mysql -kind db
# consul catalog services
consul
mysql-3327
之后, 便可以通过 dig 进行域名测试:
➜ dig +short @10.1.1.27 mysql-3327.service.infodb
10.1.1.26
➜ dig +short @10.1.1.28 mysql-3327.service.infodb
10.1.1.26
➜ dig +short @10.1.1.29 mysql-3327.service.infodb
10.1.1.26
通过 curl
命令可以看到更详细的服务信息, 注意这里的 Node
为当前的 consul
节点, 该节点异常的时候, 当前 service 就会失效, 其他节点也不能通过 dns 查询. 备注: MHA 中, 我们默认对所有 consul 节点进行了服务注册. 保证 dns 的高可用.
# curl -s localhost:8500/v1/catalog/service/mysql-3327 | jq
[
{
"ID": "c04b6f58-3069-bc5f-f323-fd7d0bbfc2b9",
"Node": "hostdb7",
"Address": "10.1.1.27",
"Datacenter": "chkdns",
"TaggedAddresses": {
"lan": "10.1.1.27",
"lan_ipv4": "10.1.1.27",
"wan": "10.1.1.27",
"wan_ipv4": "10.1.1.27"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceKind": "db",
"ServiceID": "mysql-3327",
"ServiceName": "mysql-3327",
"ServiceTags": [
"mysql"
],
"ServiceAddress": "10.1.1.26",
"ServiceTaggedAddresses": {
"lan_ipv4": {
"Address": "10.1.1.26",
"Port": 3327
},
"wan_ipv4": {
"Address": "10.1.1.26",
"Port": 3327
}
},
"ServiceWeights": {
"Passing": 1,
"Warning": 1
},
"ServiceMeta": {},
"ServicePort": 3327,
"ServiceSocketPath": "",
"ServiceEnableTagOverride": false,
"ServiceProxy": {
"Mode": "",
"MeshGateway": {},
"Expose": {}
},
"ServiceConnect": {},
"CreateIndex": 430,
"ModifyIndex": 430
}
]
不需要 service 的时候, 可以手动取消, 注意需要在相同的 Node
节点执行:
consul services deregister -id mysql-3327