Fabric 新特性 Operations 服务试用

作者 tinywell 日期 2019-01-17
Fabric 新特性 Operations 服务试用

写在前面

fabric 1.4 版本重点围绕生成可用性进行了更新支持。其中新增了一套 Operations 的 RESTful 服务,提供了部分运维管理功能:

  • 日志级别管理
  • 健康检查
  • 可用Prometheus消费的系统运行指标

本文基于 fabric-sample 提供的例子部署启动上述服务,试用相关功能,记录过程便于需要时翻阅。

部署

operations 服务配置

operations 服务可在 peer节点、orderer节点开启,相关配置分别在 peer 的 core.yaml 和 orderer 的 orderer.yaml 的一级配置项operations中。两者配置项名称稍有不通,但内容一。以 peer 的 core.yaml 举例:

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
###############################################################################
#
# Operations section
#
###############################################################################
operations:
# host and port for the operations server
listenAddress: 127.0.0.1:9443

# TLS configuration for the operations endpoint
tls:
# TLS enabled
enabled: false

# path to PEM encoded server certificate for the operations server
cert:
file:

# path to PEM encoded server key for the operations server
key:
file:

# require client certificate authentication to access all resources
clientAuthRequired: false

# paths to PEM encoded ca certificates to trust for client authentication
clientRootCAs:
files: []

listenAddress 配置项定义了 RESTful 服务的地址和端口;tls 配置项下定义了是否启用 TLS 以及相关证书。

第一次尝试

使用 dev_byfn.sh 启动一个 1.4 的 fabric 网络。

dev_byfn.sh 基于 fabric-sample 的 byfn.sh修改,网络结构调整为单 peer 单 orderer,方便开发及测试

进入 cli 容器尝试访问日志级别管理服务端点/logsepc

1
2
3
4
➜  first-network git:(bb39b6e) ✗ docker exec -it cli bash
root@5bdadcd7d615:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl http://peer0.org1.example.com:9443/logspec
curl: (7) Failed to connect to peer0.org1.example.com port 9443: Connection refused

结果显示连接被拒绝。
尝试直接进入 peer 节点容器访问(由于peer镜像中未添加bash,缺少 curl命令,采用wget命令代替):

1
2
3
4
5
6
7
➜  first-network git:(bb39b6e) ✗ docker exec -it peer0.org1.example.com bash
root@c18909ad41da:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl
bash: curl: command not found
root@c18909ad41da:/opt/gopath/src/github.com/hyperledger/fabric/peer# wget http://peer0.org1.example.com:9443/logspec
--2019-01-17 07:15:37-- http://peer0.org1.example.com:9443/logspec
Resolving peer0.org1.example.com (peer0.org1.example.com)... 192.168.240.3
Connecting to peer0.org1.example.com (peer0.org1.example.com)|192.168.240.3|:9443... failed: Connection refused.

连接依然拒绝,查询 peer 服务所用配置确定连接信息是否有误(peer容器内,配置文件路径为 /etc/hyperledger/fabric/core.yaml)

1
2
3
4
5
6
7
8
9
root@c18909ad41da:/opt/gopath/src/github.com/hyperledger/fabric/peer# cat /etc/hyperledger/fabric/core.yaml |grep -A 10 operations:
operations:
# host and port for the operations server
listenAddress: 127.0.0.1:9443

# TLS configuration for the operations endpoint
tls:
# TLS enabled
enabled: false

服务监听地址为127.0.0.1:9443,绑定了本地环回地址,所以外部无法访问。只能在本机使用 127.0.0.1 地址才行。验证如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@c18909ad41da:/opt/gopath/src/github.com/hyperledger/fabric/peer# wget http://127.0.0.1:9443/logspec
--2019-01-17 07:25:51-- http://127.0.0.1:9443/logspec
Connecting to 127.0.0.1:9443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16 [text/plain]
Saving to: 'logspec.2'

logspec.2 100%[============================================================================================>] 16 --.-KB/s in 0s

2019-01-17 07:25:51 (204 KB/s) - 'logspec.2' saved [16/16]

root@c18909ad41da:/opt/gopath/src/github.com/hyperledger/fabric/peer# cat logspec.2
{"spec":"info"}

果然,得到服务正确的返回结果{"spec":"info"}

配置修改

生产应用时,这个配置肯定需要修改,以提供外部访问。
fabric 配置模块使用 viper 包进行配置文件解析,支持通过环境变量进行配置项的覆盖替换。阅读相关源码可知,peer 对 core.yaml 的配置解析,设置了 CORE 作为环境变量配置项的前缀,需要用环境修改某个配置项时,设置一个以CORE为前缀然后具体配置项路径用短下划线连接起来环境变量并赋新值即可。我们需要修改listenAddress配置项,因此需要设置环境变量CORE_OPERATIONS_LISTENADDRESS,设置其值为peer0.org1.example.com:9443。(修改启动容器的 docker-compose 文件)

1
2
3
4
environment:
...
- CORE_OPERATIONS_LISTENADDRESS=peer0.org1.example.com:9443
...

如果网络外需要访问,则还需要为 peer/orderer 容器设置端口映射

1
2
ports:
- 9443:9443

再次尝试

修改配置,重新启动网络,进入 cli 容器进行访问:

1
2
3
➜  first-network git:(bb39b6e) ✗ docker exec -it cli bash
root@8abe1cf5cb9d:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl http://peer0.org1.example.com:9443/logspec
{"spec":"info"}

成功得到日志级别信息。
logspec 端点支持 GET 请求获取日志信息,同时支持PUT 设置新的日志级别:

1
2
3
4
root@8abe1cf5cb9d:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl --request PUT http://peer0.org1.example.com:9443/logspec -d '{"spec":"chaincode=debug:info"}'
root@8abe1cf5cb9d:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl http://peer0.org1.example.com:9443/logspec
{"spec":"chaincode=debug:info"}

这对于生产问题排查来说,应该很有用

健康检查

同样访问健康检查端点/healthz:

1
2
root@8abe1cf5cb9d:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl http://peer0.org1.example.com:9443/healthz
{"status":"OK","time":"2019-01-17T07:44:46.7200463Z"}

返回statusOK表明系统健康。(状态码为200)
根据官方文档介绍,如果检查失败,则会返回503,并有相关的 JSON 信息:

1
2
3
4
5
6
7
8
9
10
11
{
"status": "Service Unavailable",
"time": "2009-11-10T23:00:00Z",
"failed_checks": [
{
"component": "docker",
"reason": "failed to connect to Docker daemon: invalid endpoint"
}
]
}

目前仅支持对 docker 是否可用的状态进行检测。(摊手.jpg)

运行指标

关于运行指标,其相关配置如下:

1
2
3
4
5
6
7
8
###############################################################################
#
# Metrics section
#
###############################################################################
metrics:
# metrics provider is one of statsd, prometheus, or disabled
provider: disabled

通过 RESTful 端点服务提供指标信息的用于 prometheus,所以我们需要先将metrics.provider设置为prometheus。根据之前的方法,我们设置环境变量CORE_METRICS_PROVIDER:

1
2
3
4
environment:
...
- CORE_METRICS_PROVIDER=prometheus
...

进入 cli 容器访问指标端点/metrics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  first-network git:(bb39b6e) ✗ docker exec -it cli bash  
root@8abe1cf5cb9d:/opt/gopath/src/github.com/hyperledger/fabric/peer# curl http://peer0.org1.example.com:9443/metrics
# HELP chaincode_launch_duration The time to launch a chaincode.
# TYPE chaincode_launch_duration histogram
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.005"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.01"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.025"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.05"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.1"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.25"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="0.5"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="1"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="2.5"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="5"} 1
chaincode_launch_duration_bucket{chaincode="cscc:1.4.0",success="true",le="10"} 1
......

metric 提供的指标很多,包括账本、交易、cc等各方面的运行数据,非常丰富,具体可见官网Metrics Reference

总结

  • 日志级别信息的动态获取及设置,对生产问题排查来说很有用,可以在运行中修改日志级别;
  • 健康检查目前很鸡肋,只能用于检查 docker 可用性,期待后续的其他检查点的支持;
  • 运行指标很有用。很多链上的信息不需要第三方平台或者客户端自己去存储、记录获取了,直接开启运行指标,然后用prometheus或者StatsD进行抓取展示。