{"id":1573,"date":"2023-03-25T19:13:20","date_gmt":"2023-03-25T11:13:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1573"},"modified":"2023-04-27T21:32:30","modified_gmt":"2023-04-27T13:32:30","slug":"prometheus-uses-consul-to-achieve-automatic-service-discovery","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/prometheus-uses-consul-to-achieve-automatic-service-discovery\/","title":{"rendered":"Prometheus\u4f7f\u7528Consul\u5b9e\u73b0\u81ea\u52a8\u670d\u52a1\u53d1\u73b0"},"content":{"rendered":"<h2>Consul \u4ecb\u7ecd<\/h2>\n<p>Consul \u662f\u57fa\u4e8e GO \u8bed\u8a00\u5f00\u53d1\u7684\u5f00\u6e90\u5de5\u5177\uff0c\u4e3b\u8981\u9762\u5411\u5206\u5e03\u5f0f\uff0c\u670d\u52a1\u5316\u7684\u7cfb\u7edf\u63d0\u4f9b\u670d\u52a1\u6ce8\u518c\u3001\u670d\u52a1\u53d1\u73b0\u548c\u914d\u7f6e\u7ba1\u7406\u7684\u529f\u80fd\u3002Consul \u63d0\u4f9b\u670d\u52a1\u6ce8\u518c\/\u53d1\u73b0\u3001\u5065\u5eb7\u68c0\u67e5\u3001<code>Key\/Value<\/code>\u5b58\u50a8\u3001\u591a\u6570\u636e\u4e2d\u5fc3\u548c\u5206\u5e03\u5f0f\u4e00\u81f4\u6027\u4fdd\u8bc1\u7b49\u529f\u80fd\u3002\u4e4b\u524d\u6211\u4eec\u901a\u8fc7 Prometheus \u5b9e\u73b0\u76d1\u63a7\uff0c\u5f53\u65b0\u589e\u4e00\u4e2a<code>Target<\/code>\u65f6\uff0c\u9700\u8981\u53d8\u66f4\u670d\u52a1\u5668\u4e0a\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5373\u4f7f\u4f7f\u7528<code>file_sd_configs<\/code>\u914d\u7f6e\uff0c\u4e5f\u9700\u8981\u767b\u5f55\u670d\u52a1\u5668\u4fee\u6539\u5bf9\u5e94<code>Json<\/code>\u6587\u4ef6\uff0c\u4f1a\u975e\u5e38\u9ebb\u70e6\u3002\u4e0d\u8fc7<code>Prometheus<\/code>\u5b98\u65b9\u652f\u6301\u591a\u79cd\u81ea\u52a8\u670d\u52a1\u53d1\u73b0\u7684\u7c7b\u578b\uff0c\u5176\u4e2d\u5c31\u652f\u6301<code>Consul<\/code>\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>Consul \u5b89\u88c5\u914d\u7f6e<\/h2>\n<p>Consul \u5b89\u88c5\u5f88\u65b9\u4fbf\uff0c<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/downloads.html\" title=\"\u5b98\u7f51\">\u5b98\u7f51<\/a> \u63d0\u4f9b\u5404\u4e2a\u7cfb\u7edf\u7248\u672c\u4e8c\u8fdb\u5236\u5b89\u88c5\u5305\uff0c\u89e3\u538b\u5b89\u88c5\u5373\u53ef\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>Docker<\/code>\u6765\u5feb\u901f\u5b89\u88c5\u3002<\/p>\n<h3>\u6e90\u7801\u5b89\u88c5<\/h3>\n<p>\u4ee5 Linux \u7cfb\u7edf\u4e3a\u4f8b\uff0c\u6e90\u7801\u5b89\u88c5\u5e76\u4ee5\u5f00\u53d1\u6a21\u5f0f\u542f\u52a8\u4e00\u4e2a\u5355\u8282\u70b9\uff0c\u4e0b\u8f7d\u6700\u65b0\u7248\u4e8c\u8fdb\u5236\u5b89\u88c5\u5305\uff0c\u89e3\u538b\u542f\u52a8\u5373\u53ef\u3002<\/p>\n<h4>\u5f00\u53d1\u73af\u5883\u5efa\u8bae<\/h4>\n<pre><code>$ wget https:\/\/releases.hashicorp.com\/consul\/1.8.3\/consul_1.8.3_linux_amd64.zip\n$ unzip -q consul_1.8.3_linux_amd64.zip\n$ .\/consul agent -dev    <\/code><\/pre>\n<h4>\u751f\u4ea7\u73af\u5883\u5efa\u8bae<\/h4>\n<pre><code class=\"language-bash\">nohup .\/consul agent -ui -server -bootstrap -data-dir=\/usr\/service\/consul_cluster\/data -pid-file=\/run\/service\/consul_cluster\/consul.pid -client=0.0.0.0 -advertise=10.10.21.15 -join=10.10.21.15 -node=consul-c1 &amp;<\/code><\/pre>\n<pre><code>nohup .\/consul agent -ui -server -bootstrap -data-dir=\/usr\/local\/consul\/data -pid-file=\/usr\/local\/consul\/consul.pid -bind=10.10.21.15 -client=0.0.0.0 -node=consul &amp;<\/code><\/pre>\n<p>\u914d\u7f6e\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/docs\/agent\/options.html\">https:\/\/www.consul.io\/docs\/agent\/options.html<\/a><\/p>\n<ul>\n<li><code>-ui<\/code> \u542f\u52a8<code>web ui<\/code>\u670d\u52a1\uff0c\u9ed8\u8ba4\u7aef\u53e38500<\/li>\n<li><code>-server<\/code> \u670d\u52a1\u5668\u6a21\u5f0f\uff08\u5f00\u53d1\u73af\u5883\u53ef\u4ee5\u4f7f\u7528<code>-dev<\/code>\uff0c\u4e0d\u5199\u5165\u78c1\u76d8\uff0c\u5185\u5b58\u5b58\u50a8\uff09<\/li>\n<li><code>-data-dir<\/code> \u6570\u636e\u5b58\u50a8\u8def\u5f84<\/li>\n<li><code>-config-dir<\/code> Service\u914d\u7f6e\u6587\u4ef6\u76ee\u5f55\uff0c\u901a\u5e38\u4e3a<code>.\/consul.d<\/code><\/li>\n<li><code>-config-file<\/code> Service\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff0c\u8be5\u9009\u9879\u53ef\u4ee5\u914d\u7f6e\u591a\u6b21<\/li>\n<li><code>-pid-file<\/code> pid\u6587\u4ef6\u8def\u5f84<\/li>\n<li><code>-client<\/code> \u6307\u5b9a\u8282\u70b9\u4e3aclient<\/li>\n<li><code>-advertise<\/code><\/li>\n<li><code>-join<\/code> \u5c06\u8282\u70b9\u52a0\u5165\u5230\u96c6\u7fa4<\/li>\n<li><code>-node<\/code> \u6307\u5b9a\u8282\u70b9\u5728\u96c6\u7fa4\u4e2d\u7684\u540d\u79f0<\/li>\n<\/ul>\n<pre><code>Usage:\n  -advertise value\n        Sets the advertise address to use.\n  -advertise-wan value\n        Sets address to advertise on WAN instead of -advertise address.\n  -allow-write-http-from value\n        Only allow write endpoint calls from given network. CIDR format, can be specified multiple times.\n  -alt-domain value\n        Alternate domain to use for DNS interface.\n  -bind value\n        Sets the bind address for cluster communication.\n  -bootstrap\n        Sets server to bootstrap mode.\n  -bootstrap-expect value\n        Sets server to expect bootstrap mode.\n  -check_output_max_size value\n        Sets the maximum output size for checks on this agent\n  -client value\n        Sets the address to bind for client access. This includes RPC, DNS, HTTP, HTTPS and gRPC (if configured).\n  -config-dir value\n        Path to a directory to read configuration files from. This will read every file ending in &#039;.json&#039; as configuration in this directory in alphabetical order. Can be specified multiple times.\n  -config-file value\n        Path to a file in JSON or HCL format with a matching file extension. Can be specified multiple times.\n  -config-format string\n        Config files are in this format irrespective of their extension. Must be &#039;hcl&#039; or &#039;json&#039;\n  -data-dir value\n        Path to a data directory to store agent state.\n  -datacenter value\n        Datacenter of the agent.\n  -default-query-time value\n        the amount of time a blocking query will wait before Consul will force a response. This value can be overridden by the &#039;wait&#039; query parameter.\n  -dev\n        Starts the agent in development mode.\n  -disable-host-node-id\n        Setting this to true will prevent Consul from using information from the host to generate a node ID, and will cause Consul to generate a random node ID instead.\n  -disable-keyring-file\n        Disables the backing up of the keyring to a file.\n  -dns-port value\n        DNS port to use.\n  -domain value\n        Domain to use for DNS interface.\n  -enable-local-script-checks\n        Enables health check scripts from configuration file.\n  -enable-script-checks\n        Enables health check scripts.\n  -encrypt value\n        Provides the gossip encryption key.\n  -grpc-port value\n        Sets the gRPC API port to listen on (currently needed for Envoy xDS only).\n  -hcl value\n        hcl config fragment. Can be specified multiple times.\n  -http-port value\n        Sets the HTTP API port to listen on.\n  -https-port value\n        Sets the HTTPS API port to listen on.\n  -join value\n        Address of an agent to join at start time. Can be specified multiple times.\n  -join-wan value\n        Address of an agent to join -wan at start time. Can be specified multiple times.\n  -log-file value\n        Path to the file the logs get written to\n  -log-json\n        Output logs in JSON format.\n  -log-level value\n        Log level of the agent.\n  -log-rotate-bytes value\n        Maximum number of bytes that should be written to a log file\n  -log-rotate-duration value\n        Time after which log rotation needs to be performed\n  -log-rotate-max-files value\n        Maximum number of log file archives to keep\n  -max-query-time value\n        the maximum amount of time a blocking query can wait before Consul will force a response. Consul applies jitter to the wait time. The jittered time will be capped to MaxQueryTime.\n  -node value\n        Name of this node. Must be unique in the cluster.\n  -node-id value\n        A unique ID for this node across space and time. Defaults to a randomly-generated ID that persists in the data-dir.\n  -node-meta key:value\n        An arbitrary metadata key\/value pair for this node, of the format key:value. Can be specified multiple times.\n  -non-voting-server\n        (Enterprise-only) This flag is used to make the server not participate in the Raft quorum, and have it only receive the data replication stream. This can be used to add read scalability to a cluster in cases where a high volume of reads to servers are needed.\n  -pid-file value\n        Path to file to store agent PID.\n  -primary-gateway value\n        Address of a mesh gateway in the primary datacenter to use to bootstrap WAN federation at start time with retries enabled. Can be specified multiple times.\n  -protocol value\n        Sets the protocol version. Defaults to latest.\n  -raft-protocol value\n        Sets the Raft protocol version. Defaults to latest.\n  -recursor value\n        Address of an upstream DNS server. Can be specified multiple times.\n  -rejoin\n        Ignores a previous leave and attempts to rejoin the cluster.\n  -retry-interval value\n        Time to wait between join attempts.\n  -retry-interval-wan value\n        Time to wait between join -wan attempts.\n  -retry-join value\n        Address of an agent to join at start time with retries enabled. Can be specified multiple times.\n  -retry-join-wan value\n        Address of an agent to join -wan at start time with retries enabled. Can be specified multiple times.\n  -retry-max value\n        Maximum number of join attempts. Defaults to 0, which will retry indefinitely.\n  -retry-max-wan value\n        Maximum number of join -wan attempts. Defaults to 0, which will retry indefinitely.\n  -segment value\n        (Enterprise-only) Sets the network segment to join.\n  -serf-lan-allowed-cidrs value\n        Networks (eg: 192.168.1.0\/24) allowed for Serf LAN. Can be specified multiple times.\n  -serf-lan-bind value\n        Address to bind Serf LAN listeners to.\n  -serf-lan-port value\n        Sets the Serf LAN port to listen on.\n  -serf-wan-allowed-cidrs value\n        Networks (eg: 192.168.1.0\/24) allowed for Serf WAN (other datacenters). Can be specified multiple times.\n  -serf-wan-bind value\n        Address to bind Serf WAN listeners to.\n  -serf-wan-port value\n        Sets the Serf WAN port to listen on.\n  -server\n        Switches agent to server mode.\n  -server-port value\n        Sets the server port to listen on.\n  -syslog\n        Enables logging to syslog.\n  -ui\n        Enables the built-in static web UI server.\n  -ui-content-path value\n        Sets the external UI path to a string. Defaults to: \/ui\/ \n  -ui-dir value\n        Path to directory containing the web UI resources.<\/code><\/pre>\n<h4>Consul Web\u7ba1\u7406\u9875\u9762<\/h4>\n<p>\u542f\u52a8\u5b8c\u6bd5\u540e\uff0c\u6d4f\u89c8\u5668\u8bbf\u95ee<code>http:\/\/127.0.0.1:8500<\/code>\u5730\u5740\uff0c\u5373\u53ef\u6253\u5f00<code>Consul Web<\/code>\u7ba1\u7406\u9875\u9762\u3002\u53ef\u4ee5\u770b\u5230\u9ed8\u8ba4\u53ea\u6709<code>consul<\/code>\u4e00\u4e2a<code>Service<\/code>\uff0c\u540e\u671f\u6211\u4eec\u6ce8\u518c\u5230<code>Consul<\/code>\u7684<code>Service<\/code>\u90fd\u53ef\u4ee5\u4ece\u9875\u9762\u4e0a\u770b\u5230\uff0c\u975e\u5e38\u76f4\u89c2\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul%20Web%20\u7ba1\u7406\u9875\u9762.png\" alt=\"Consul Web \u7ba1\u7406\u9875\u9762\" \/><\/p>\n<h3>Docker \u5b89\u88c5<\/h3>\n<p>\u4f7f\u7528<code>Docker<\/code>\u542f\u52a8<code>Consul<\/code>\u5355\u8282\u70b9\u670d\u52a1\uff0c\u76f4\u63a5\u83b7\u53d6\u6700\u65b0\u7248\u5b98\u65b9\u955c\u50cf<code>consul:latest<\/code>\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code>$ docker run --name consul -d -p 8500:8500 consul<\/code><\/pre>\n<p>\u542f\u52a8\u5b8c\u6bd5\u540e\uff0c\u540c\u4e0a\u65b9\u6cd5\u9a8c\u8bc1\u662f\u5426\u542f\u52a8\u6210\u529f\uff0c\u8fd9\u91cc\u4e3a\u4e86\u65b9\u4fbf\u6f14\u793a\uff0c\u91c7\u7528<code>Docker<\/code>\u65b9\u5f0f\u542f\u52a8<code>Consul<\/code>\uff0c\u8fd9\u91cc\u7684\u8bbf\u95ee\u5730\u5740\u4e3a\uff1a<code>http:\/\/172.30.12.167:8500<\/code>\u3002<\/p>\n<h2>API \u6ce8\u518c\u670d\u52a1\u5230 Consul<\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8981\u6ce8\u518c\u670d\u52a1\u5230<code>Consul<\/code>\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u63d0\u4f9b\u7684<code>API<\/code>\u6807\u51c6\u63a5\u53e3\u6765\u6dfb\u52a0\u3002\u90a3\u4e48\u5148\u6ce8\u518c\u4e00\u4e2a\u6d4b\u8bd5\u670d\u52a1\uff0c\u8be5\u6d4b\u8bd5\u6570\u636e\u4e3a\u672c\u673a<code>node-exporter<\/code>\u670d\u52a1\u4fe1\u606f\uff0c\u670d\u52a1\u5730\u5740\u53ca\u7aef\u53e3\u4e3a<code>node-exporter<\/code>\u9ed8\u8ba4\u63d0\u4f9b\u6307\u6807\u6570\u636e\u7684\u5730\u5740\uff0c\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>$ curl -X PUT -d &#039;{&quot;id&quot;: &quot;node-exporter&quot;, &quot;name&quot;: &quot;node-exporter-172.30.12.167&quot;, &quot;address&quot;: &quot;172.30.12.167&quot;, &quot;port&quot;: 9100, &quot;tags&quot;: [&quot;test&quot;], &quot;checks&quot;: [{&quot;http&quot;: &quot;http:\/\/172.30.12.167:9100\/metrics&quot;, &quot;interval&quot;: &quot;5s&quot;}]}&#039;  http:\/\/172.30.12.167:8500\/v1\/agent\/service\/register<\/code><\/pre>\n<p>\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5237\u65b0\u4e00\u4e0b<code>Consul Web<\/code>\u63a7\u5236\u53f0\u9875\u9762\uff0c\u53ef\u4ee5\u770b\u5230\u6210\u529f\u6ce8\u518c\u5230<code>Consul<\/code>\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul\u670d\u52a1\u6ce8\u518c.png\" alt=\"Consul\u670d\u52a1\u6ce8\u518c\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul\u670d\u52a1\u8be6\u60c5.png\" alt=\"Consul\u670d\u52a1\u8be6\u60c5\" \/><\/p>\n<p>\u63d0\u4e00\u4e0b\uff0c\u5982\u679c\u8981\u6ce8\u9500\u6389\u67d0\u4e2a\u670d\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b<code>API<\/code>\u547d\u4ee4\u64cd\u4f5c\uff0c\u4f8b\u5982\u6ce8\u9500\u4e0a\u8fb9\u6dfb\u52a0\u7684<code>node-exporter<\/code>\u670d\u52a1<\/p>\n<pre><code>$ curl -X PUT http:\/\/172.30.12.167:8500\/v1\/agent\/service\/deregister\/node-exporter<\/code><\/pre>\n<h2>\u914d\u7f6e Prometheus \u5b9e\u73b0\u81ea\u52a8\u670d\u52a1\u53d1\u73b0<\/h2>\n<p>\u73b0\u5728<code>Consul<\/code>\u670d\u52a1\u5df2\u7ecf\u542f\u52a8\u5b8c\u6bd5\uff0c\u5e76\u6210\u529f\u6ce8\u518c\u4e86\u4e00\u4e2a\u670d\u52a1\uff0c\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e<code>Prometheus<\/code>\u6765\u4f7f\u7528<code>Consul<\/code>\u81ea\u52a8\u670d\u52a1\u53d1\u73b0\uff0c\u76ee\u7684\u5c31\u662f\u80fd\u591f\u5c06\u4e0a\u8fb9\u6dfb\u52a0\u7684\u670d\u52a1\u81ea\u52a8\u53d1\u73b0\u5230<code>Prometheus<\/code>\u7684<code>Targets<\/code>\u4e2d\uff0c\u589e\u52a0<code>prometheus.yml<\/code>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">- job_name: &#039;consul-prometheus&#039;\n  consul_sd_configs:\n  - server: &#039;172.30.12.167:8500&#039;\n    services: []<\/code><\/pre>\n<p>\u8bf4\u660e\u4e00\u4e0b\uff1a\u8fd9\u91cc\u9700\u8981\u4f7f\u7528<code>consul_sd_configs<\/code>\u6765\u914d\u7f6e\u4f7f\u7528<code>Consul<\/code>\u670d\u52a1\u53d1\u73b0\u7c7b\u578b\uff0c<code>server<\/code>\u4e3a<code>Consul<\/code>\u7684\u670d\u52a1\u5730\u5740\u3002\u914d\u7f6e\u5b8c\u6bd5\u540e\uff0c\u91cd\u542f<code>Prometheus<\/code>\u670d\u52a1\uff0c\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7<code>Prometheus UI<\/code>\u9875\u9762\u7684<code>Targets<\/code>\u4e0b\u67e5\u770b\u662f\u5426\u914d\u7f6e\u6210\u529f\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Prometheus%20Targets\u914d\u7f6e.png\" alt=\"Prometheus Targets\u914d\u7f6e\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728<code>Targets<\/code>\u4e2d\u80fd\u591f\u6210\u529f\u7684\u81ea\u52a8\u53d1\u73b0<code>Consul<\/code>\u4e2d\u7684<code>Services<\/code>\u4fe1\u606f\uff0c\u540e\u671f\u9700\u8981\u6dfb\u52a0\u65b0\u7684<code>Targets<\/code>\u65f6\uff0c\u53ea\u9700\u8981\u901a\u8fc7<code>API<\/code>\u5f80<code>Consul<\/code>\u4e2d\u6ce8\u518c\u670d\u52a1\u5373\u53ef\uff0c<code>Prometheus<\/code>\u5c31\u80fd\u81ea\u52a8\u53d1\u73b0\u8be5\u670d\u52a1\uff0c\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u6709\u5982\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>\uff081\uff09\u4f1a\u53d1\u73b0<code>Prometheus<\/code>\u540c\u65f6\u52a0\u8f7d\u51fa\u4e86\u9ed8\u8ba4\u670d\u52a1<code>consul<\/code>\uff0c\u8fd9\u4e2a\u662f\u4e0d\u9700\u8981\u7684<br \/>\n\uff082\uff09\u9ed8\u8ba4\u53ea\u663e\u793a<code>job<\/code>\u53ca<code>instance<\/code>\u4e24\u4e2a\u6807\u7b7e\uff0c\u5176\u4ed6\u6807\u7b7e\u90fd\u9ed8\u8ba4\u5c5e\u4e8e<code>before relabeling<\/code>\u4e0b\uff0c\u6709\u4e9b\u5fc5\u8981\u7684\u670d\u52a1\u4fe1\u606f\uff0c\u4e5f\u60f3\u8981\u5728\u6807\u7b7e\u4e2d\u5c55\u793a\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f<br \/>\n\uff083\uff09\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u4e00\u4e9b\u6807\u7b7e\uff0c\u4f8b\u5982<code>team<\/code>\u3001<code>group<\/code>\u3001<code>project<\/code>\u7b49\u5173\u952e\u5206\u7ec4\u4fe1\u606f\uff0c\u65b9\u4fbf\u540e\u8fb9<code>alertmanager<\/code>\u8fdb\u884c\u544a\u8b66\u89c4\u5219\u5339\u914d\uff0c\u8be5\u5982\u4f55\u5904\u7406\u5462\uff1f<br \/>\n\uff084\uff09\u6240\u6709<code>Consul<\/code>\u4e2d\u6ce8\u518c\u7684<code>Service<\/code>\u90fd\u4f1a\u9ed8\u8ba4\u52a0\u8f7d\u5230<code>Prometheus<\/code>\u4e0b\u914d\u7f6e\u7684<code>consul_prometheus<\/code>\u7ec4\uff0c\u5982\u679c\u6709\u591a\u79cd\u7c7b\u578b\u7684<code>exporter<\/code>\uff0c\u5982\u4f55\u5728<code>Prometheus<\/code>\u4e2d\u914d\u7f6e\u5206\u914d\u7ed9\u6307\u5b9a\u7c7b\u578b\u7684\u7ec4\uff0c\u65b9\u4fbf\u76f4\u89c2\u7684\u533a\u522b\u5b83\u4eec\uff1f<\/p>\n<p>\u4ee5\u4e0a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>Prometheus<\/code>\u914d\u7f6e\u4e2d\u7684<code>relabel_configs<\/code>\u53c2\u6570\u6765\u89e3\u51b3\u3002<\/p>\n<h2>\u914d\u7f6e relabel_configs \u5b9e\u73b0\u81ea\u5b9a\u4e49\u6807\u7b7e\u53ca\u5206\u7c7b<\/h2>\n<h3>relabel_configs\u529f\u80fd<\/h3>\n<p>\u6211\u4eec\u5148\u6765\u666e\u53ca\u4e00\u4e0b<code>relabel_configs<\/code>\u7684\u529f\u80fd\uff0c<code>Prometheus<\/code>\u5141\u8bb8\u7528\u6237\u5728\u91c7\u96c6\u4efb\u52a1\u8bbe\u7f6e\u4e2d\uff0c\u901a\u8fc7<code>relabel_configs<\/code>\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684<code>Relabeling<\/code>\u7b49\u8fc7\u7a0b\uff0c\u6765\u5bf9\u6807\u7b7e\u8fdb\u884c\u6307\u5b9a\u89c4\u5219\u7684\u91cd\u5199\u3002<code>Prometheus<\/code>\u52a0\u8f7d<code>Targets<\/code>\u540e\uff0c\u8fd9\u4e9b<code>Targets<\/code>\u4f1a\u81ea\u52a8\u5305\u542b\u4e00\u4e9b\u9ed8\u8ba4\u7684\u6807\u7b7e\uff0c<code>Target<\/code>\u4ee5<code>__<\/code>\u4f5c\u4e3a\u524d\u7f6e\u7684\u6807\u7b7e\u662f\u5728\u7cfb\u7edf\u5185\u90e8\u4f7f\u7528\u7684\uff0c\u8fd9\u4e9b\u6807\u7b7e\u4e0d\u4f1a\u88ab\u5199\u5165\u5230\u6837\u672c\u6570\u636e\u4e2d\u3002\u773c\u5c16\u7684\u4f1a\u53d1\u73b0\uff0c\u6bcf\u6b21\u589e\u52a0<code>Target<\/code>\u65f6\u4f1a\u81ea\u52a8\u589e\u52a0\u4e00\u4e2a<code>instance<\/code>\u6807\u7b7e\uff0c\u800c<code>instance<\/code>\u6807\u7b7e\u7684\u5185\u5bb9\u521a\u597d\u5bf9\u5e94<code>Target<\/code>\u5b9e\u4f8b\u7684<code>__address__<\/code>\u503c\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b9e\u9645\u4e0a<code>Prometheus<\/code>\u5185\u90e8\u505a\u4e86\u4e00\u6b21\u6807\u7b7e\u91cd\u5199\u5904\u7406\uff0c\u9ed8\u8ba4<code>__address__<\/code>\u6807\u7b7e\u8bbe\u7f6e\u4e3a<code>&lt;host&gt;:&lt;port&gt;<\/code>\u5730\u5740\uff0c\u7ecf\u8fc7\u6807\u7b7e\u91cd\u5199\u540e\uff0c\u9ed8\u8ba4\u4f1a\u81ea\u52a8\u5c06\u8be5\u503c\u8bbe\u7f6e\u4e3a<code>instance<\/code>\u6807\u7b7e\uff0c\u6240\u4ee5\u6211\u4eec\u80fd\u591f\u5728\u9875\u9762\u770b\u5230\u8be5\u6807\u7b7e\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Prometheus%20Targets\u6807\u7b7e.png\" alt=\"Prometheus Targets\u6807\u7b7e\" \/><\/p>\n<p>\u8be6\u7ec6<code>relabel_configs<\/code>\u914d\u7f6e\u53ca\u8bf4\u660e\u53ef\u4ee5\u53c2\u8003 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/configuration\/#relabel_config\" title=\"relabel_config\">relabel_config<\/a> \u5b98\u7f51\u8bf4\u660e\uff0c\u8fd9\u91cc\u7b80\u5355\u5217\u4e3e\u4e00\u4e0b\u91cc\u9762\u6bcf\u4e2a<code>relabel_action<\/code>\u7684\u4f5c\u7528\uff0c\u65b9\u4fbf\u4e0b\u8fb9\u6f14\u793a\u3002<\/p>\n<ul>\n<li><code>replace<\/code>: \u6839\u636e<code>regex<\/code>\u7684\u914d\u7f6e\u5339\u914d<code>source_labels<\/code>\u6807\u7b7e\u7684\u503c\uff08\u6ce8\u610f\uff1a\u591a\u4e2a<code>source_label<\/code>\u7684\u503c\u4f1a\u6309\u7167<code>separator<\/code>\u8fdb\u884c\u62fc\u63a5\uff09\uff0c\u5e76\u4e14\u5c06\u5339\u914d\u5230\u7684\u503c\u5199\u5165\u5230<code>target_label<\/code>\u5f53\u4e2d\uff0c\u5982\u679c\u6709\u591a\u4e2a\u5339\u914d\u7ec4\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>${1}, ${2}<\/code>\u786e\u5b9a\u5199\u5165\u7684\u5185\u5bb9\u3002\u5982\u679c\u6ca1\u5339\u914d\u5230\u4efb\u4f55\u5185\u5bb9\u5219\u4e0d\u5bf9<code>target_label<\/code>\u8fdb\u884c\u91cd\u65b0\uff0c\u9ed8\u8ba4\u4e3a<code>replace<\/code><\/li>\n<li><code>keep<\/code>: \u4e22\u5f03<code>source_labels<\/code>\u7684\u503c\u4e2d\u6ca1\u6709\u5339\u914d\u5230<code>regex<\/code>\u6b63\u5219\u8868\u8fbe\u5f0f\u5185\u5bb9\u7684<code>Target<\/code>\u5b9e\u4f8b<\/li>\n<li><code>drop<\/code>: \u4e22\u5f03<code>source_labels<\/code>\u7684\u503c\u4e2d\u5339\u914d\u5230<code>regex<\/code>\u6b63\u5219\u8868\u8fbe\u5f0f\u5185\u5bb9\u7684<code>Target<\/code>\u5b9e\u4f8b<\/li>\n<li><code>hashmod<\/code>: \u5c06<code>target_label<\/code>\u8bbe\u7f6e\u4e3a\u5173\u8054\u7684<code>source_label<\/code>\u7684\u54c8\u5e0c\u6a21\u5757<\/li>\n<li><code>labelmap<\/code>: \u6839\u636e<code>regex<\/code>\u53bb\u5339\u914d<code>Target<\/code>\u5b9e\u4f8b\u6240\u6709\u6807\u7b7e\u7684\u540d\u79f0\uff08\u6ce8\u610f\u662f\u540d\u79f0\uff09\uff0c\u5e76\u4e14\u5c06\u6355\u83b7\u5230\u7684\u5185\u5bb9\u4f5c\u4e3a\u4e3a\u65b0\u7684\u6807\u7b7e\u540d\u79f0\uff0c<code>regex<\/code>\u5339\u914d\u5230\u6807\u7b7e\u7684\u7684\u503c\u4f5c\u4e3a\u65b0\u6807\u7b7e\u7684\u503c<\/li>\n<li><code>labeldrop<\/code>: \u5bf9<code>Target<\/code>\u6807\u7b7e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u4f1a\u79fb\u9664\u5339\u914d\u8fc7\u6ee4\u6761\u4ef6\u7684\u6240\u6709\u6807\u7b7e<\/li>\n<li><code>labelkeep<\/code>: \u5bf9<code>Target<\/code>\u6807\u7b7e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u4f1a\u79fb\u9664\u4e0d\u5339\u914d\u8fc7\u6ee4\u6761\u4ef6\u7684\u6240\u6709\u6807\u7b7e<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u6328\u4e2a\u5904\u7406\u4e0a\u8ff0\u95ee\u9898\u3002<\/p>\n<h3>\u901a\u8fc7\u6807\u7b7e\u8fc7\u6ee4\u670d\u52a1<\/h3>\n<p>\u95ee\u9898\u4e00\uff0c\u6211\u4eec\u53ef\u4ee5\u914d\u7f6e<code>relabel_configs<\/code>\u6765\u5b9e\u73b0\u6807\u7b7e\u8fc7\u6ee4\uff0c\u53ea\u52a0\u8f7d\u7b26\u5408\u89c4\u5219\u7684\u670d\u52a1\u3002\u4ee5\u4e0a\u8fb9\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fc7\u6ee4<code>__meta_consul_tags<\/code>\u6807\u7b7e\u4e3a<code>test<\/code>\u7684\u670d\u52a1\uff0c<code>relabel_config<\/code>\u5411<code>Consul<\/code>\u6ce8\u518c\u670d\u52a1\u7684\u65f6\u5019\uff0c\u53ea\u52a0\u8f7d\u5339\u914d<code>regex<\/code>\u8868\u8fbe\u5f0f\u7684\u6807\u7b7e\u7684\u670d\u52a1\u5230\u81ea\u5df1\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4fee\u6539<code>prometheus.yml<\/code>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">- job_name: &#039;consul-prometheus&#039;\n  consul_sd_configs:\n    - server: &#039;172.30.12.167:8500&#039;\n      services: []  \n  relabel_configs:\n    - source_labels: [__meta_consul_tags]\n      regex: .*test.*\n      action: keep<\/code><\/pre>\n<p>\u89e3\u91ca\u4e0b\uff0c\u8fd9\u91cc\u7684<code>relabel_configs<\/code>\u914d\u7f6e\u4f5c\u7528\u4e3a\u4e22\u5f03\u6e90\u6807\u7b7e\u4e2d<code>__meta_consul_tags<\/code>\u4e0d\u5305\u542b<code>test<\/code>\u6807\u7b7e\u7684\u670d\u52a1\uff0c<code>__meta_consul_tags<\/code>\u5bf9\u5e94\u5230<code>Consul<\/code>\u670d\u52a1\u4e2d\u7684\u503c\u4e3a<code>&quot;tags&quot;: [&quot;test&quot;]<\/code>\uff0c\u9ed8\u8ba4<code>consul<\/code>\u670d\u52a1\u662f\u4e0d\u5e26\u8be5\u6807\u7b7e\u7684\uff0c\u4ece\u800c\u5b9e\u73b0\u8fc7\u6ee4\u3002\u91cd\u542f<code>Prometheus<\/code>\u53ef\u4ee5\u770b\u5230\u73b0\u5728\u53ea\u83b7\u53d6\u4e86<code>node-exporter-172.30.12.167<\/code>\u8fd9\u4e2a\u670d\u52a1\u4e86\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Prometheus%20Targets\u6807\u7b7e\u8fc7\u6ee4.png\" alt=\"Prometheus Targets\u6807\u7b7e\u8fc7\u6ee4\" \/><\/p>\n<h3>\u81ea\u5b9a\u4e49\u53ef\u89c6\u5316\u6807\u7b7e<\/h3>\n<p>\u95ee\u9898\u4e8c\u548c\u95ee\u9898\u4e09\u53ef\u4ee5\u5f52\u4e3a\u4e00\u7c7b\uff0c\u5c31\u662f\u5c06\u7cfb\u7edf\u9ed8\u8ba4\u6807\u7b7e\u6216\u8005\u7528\u6237\u81ea\u5b9a\u4e49\u6807\u7b7e\u8f6c\u6362\u6210\u53ef\u89c6\u5316\u6807\u7b7e\uff0c\u65b9\u4fbf\u67e5\u770b\u53ca\u540e\u7eed<code>Alertmanager<\/code>\u8fdb\u884c\u544a\u8b66\u89c4\u5219\u5339\u914d\u5206\u7ec4\u3002\u4e0d\u8fc7\u8981\u5b9e\u73b0\u7ed9\u670d\u52a1\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6807\u7b7e\uff0c\u6211\u4eec\u8fd8\u5f97\u505a\u4e00\u4e0b\u4fee\u6539\uff0c\u5c31\u662f\u5728\u6ce8\u518c\u670d\u52a1\u65f6\uff0c\u5c06\u81ea\u5b9a\u4e49\u6807\u7b7e\u4fe1\u606f\u6dfb\u52a0\u5230<code>Meta Data<\/code>\u6570\u636e\u4e2d\uff0c\u5177\u4f53\u53ef\u4ee5\u53c2\u8003 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/api\/agent\/service.html\" title=\"Consul Service - Agent HTTP API\">Consul Service &#8211; Agent HTTP API<\/a> \u5b98\u7f51\u8bf4\u660e\uff0c\u4e0b\u8fb9\u6765\u6f14\u793a\u4e00\u4e0b\u5982\u4f55\u64cd\u4f5c\u3002<\/p>\n<p>\u65b0\u5efa<code>consul-0.json<\/code>\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-json\">$ vim consul-0.json\n{\n  &quot;ID&quot;: &quot;node-exporter&quot;,\n  &quot;Name&quot;: &quot;node-exporter-172.30.12.167&quot;,\n  &quot;Tags&quot;: [\n    &quot;test&quot;\n  ],\n  &quot;Address&quot;: &quot;172.30.12.167&quot;,\n  &quot;Port&quot;: 9100,\n  &quot;Meta&quot;: {\n    &quot;app&quot;: &quot;spring-boot&quot;,\n    &quot;team&quot;: &quot;appgroup&quot;,\n    &quot;project&quot;: &quot;bigdata&quot;\n  },\n  &quot;EnableTagOverride&quot;: false,\n  &quot;Check&quot;: {\n    &quot;HTTP&quot;: &quot;http:\/\/172.30.12.167:9100\/metrics&quot;,\n    &quot;Interval&quot;: &quot;10s&quot;\n  },\n  &quot;Weights&quot;: {\n    &quot;Passing&quot;: 10,\n    &quot;Warning&quot;: 1\n  }\n}<\/code><\/pre>\n<p>\u8bf4\u660e\u4e00\u4e0b\uff1a\u8be5<code>Json<\/code>\u6587\u4ef6\u4e3a\u8981\u6ce8\u518c\u7684\u670d\u52a1\u4fe1\u606f\uff0c\u540c\u65f6\u5f80<code>Meta<\/code>\u4fe1\u606f\u4e2d\u6dfb\u52a0\u4e86<code>app=spring-boot<\/code>\uff0c<code>team=appgroup<\/code>\uff0c<code>project=bigdata<\/code>\u4e09\u7ec4\u6807\u7b7e\uff0c\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u65b9\u4fbf\u544a\u8b66\u5206\u7ec4\u4f7f\u7528\u3002\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u8fdb\u884c\u6ce8\u518c\uff1a<\/p>\n<pre><code>$ curl --request PUT --data @consul-0.json http:\/\/172.30.12.167:8500\/v1\/agent\/service\/register?replace-existing-checks=1<\/code><\/pre>\n<p>\u6ce8\u518c\u5b8c\u6bd5\uff0c\u901a\u8fc7<code>Consul Web<\/code>\u7ba1\u7406\u9875\u9762\u53ef\u4ee5\u67e5\u770b\u5230\u5df2\u6ce8\u518c\u6210\u529f\uff0c\u5e76\u4e14\u5305\u542b\u4e86<code>Meta<\/code>\u4fe1\u606f\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul\u670d\u52a1\u6ce8\u518c(Meta%20Data).png\" alt=\"Consul\u670d\u52a1\u6ce8\u518c(Meta Data)\" \/><\/p>\n<p>\u7136\u540e\u4fee\u6539<code>prometheus.yml<\/code>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">- job_name: &#039;consul-prometheus&#039;\n  consul_sd_configs:\n    - server: &#039;172.30.12.167:8500&#039;\n      services: []  \n  relabel_configs:\n    - source_labels: [__meta_consul_tags]\n      regex: .*test.*\n      action: keep\n    - regex: __meta_consul_service_metadata_(.+)\n      action: labelmap<\/code><\/pre>\n<p>\u89e3\u91ca\u4e00\u4e0b\uff0c\u589e\u52a0\u7684\u914d\u7f6e\u4f5c\u7528\u4e3a\u5339\u914d<code>__meta_consul_service_metadata<\/code>\u5f00\u5934\u7684\u6807\u7b7e\uff0c\u5c06\u6355\u83b7\u5230\u7684\u5185\u5bb9\u4f5c\u4e3a\u65b0\u7684\u6807\u7b7e\u540d\u79f0\uff0c\u5339\u914d\u5230\u6807\u7b7e\u7684\u503c\u4f5c\u4e3a\u65b0\u6807\u7b7e\u7684\u503c\uff0c\u800c\u6211\u4eec\u521a\u6dfb\u52a0\u7684\u4e09\u4e2a\u81ea\u5b9a\u4e49\u6807\u7b7e\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6dfb\u52a0<code>__meta_consul_service_metadata_app=spring-boot<\/code>\u3001<code>__meta_consul_service_metadata_team=appgroup<\/code>\u3001<code>__meta_consul_service_metadata_project=bigdata<\/code>\u4e09\u4e2a\u6807\u7b7e\uff0c\u7ecf\u8fc7<code>relabel<\/code>\u540e\uff0c<code>Prometheus<\/code>\u5c06\u4f1a\u65b0\u589e<code>app=spring-boot<\/code>\u3001<code>team=appgroup<\/code>\u3001<code>project=bigdata<\/code>\u4e09\u4e2a\u6807\u7b7e\u3002\u91cd\u542f<code>Prometheus<\/code>\u670d\u52a1\uff0c\u53ef\u4ee5\u770b\u5230\u65b0\u589e\u4e86\u5bf9\u5e94\u4e86\u4e09\u4e2a\u81ea\u5b9a\u4e49\u6807\u7b7e\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Prometheus%20Targets\u81ea\u5b9a\u4e49\u6807\u7b7e.png\" alt=\"Prometheus Targets\u81ea\u5b9a\u4e49\u6807\u7b7e\" \/><\/p>\n<h3>\u670d\u52a1\u5206\u7c7b<\/h3>\n<p>\u95ee\u9898\u56db\uff0c\u5c06\u81ea\u52a8\u53d1\u73b0\u7684\u670d\u52a1\u8fdb\u884c\u5206\u7c7b\uff0c\u672c\u8d28\u4e0a\u8ddf\u4e0a\u8fb9\u7684\u5904\u7406\u65b9\u5f0f\u4e00\u81f4\uff0c\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u6807\u7b7e\u65b9\u5f0f\uff0c\u901a\u8fc7\u6807\u7b7e\u6765\u533a\u5206\uff0c\u4e8c\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1<code>Tag<\/code>\u6765\u8fdb\u884c\u5339\u914d\u6765\u521b\u5efa\u4e0d\u540c\u7684\u7c7b\u578b<code>exporter<\/code>\u5206\u7ec4\u3002\u8fd9\u91cc\u6211\u4ee5\u7b2c\u4e8c\u79cd\u4e3a\u4f8b\uff0c\u901a\u8fc7\u7ed9\u6bcf\u4e2a\u670d\u52a1\u6807\u8bb0\u4e0d\u540c\u7684<code>Tag<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>relabel_configs<\/code>\u6765\u8fdb\u884c\u5339\u914d\u533a\u5206\u3002\u6211\u4eec\u6765\u66f4\u65b0\u4e00\u4e0b\u539f<code>node-exporter-172.30.12.167<\/code>\u670d\u52a1\u6807\u7b7e\uff0c\u540c\u65f6\u6ce8\u518c\u4e00\u4e2a\u5176\u4ed6\u7c7b\u578b<code>exporter<\/code>\u7684\u670d\u52a1\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-json\">$ vim consul-1.json\n{\n  &quot;ID&quot;: &quot;node-exporter&quot;,\n  &quot;Name&quot;: &quot;node-exporter-172.30.12.167&quot;,\n  &quot;Tags&quot;: [\n    &quot;node-exporter&quot;\n  ],\n  &quot;Address&quot;: &quot;172.30.12.167&quot;,\n  &quot;Port&quot;: 9100,\n  &quot;Meta&quot;: {\n    &quot;app&quot;: &quot;spring-boot&quot;,\n    &quot;team&quot;: &quot;appgroup&quot;,\n    &quot;project&quot;: &quot;bigdata&quot;\n  },\n  &quot;EnableTagOverride&quot;: false,\n  &quot;Check&quot;: {\n    &quot;HTTP&quot;: &quot;http:\/\/172.30.12.167:9100\/metrics&quot;,\n    &quot;Interval&quot;: &quot;10s&quot;\n  },\n  &quot;Weights&quot;: {\n    &quot;Passing&quot;: 10,\n    &quot;Warning&quot;: 1\n  }\n}\n\n# \u66f4\u65b0\u6ce8\u518c\u670d\u52a1\n$ curl --request PUT --data @consul-1.json http:\/\/172.30.12.167:8500\/v1\/agent\/service\/register?replace-existing-checks=1\n\n$ vim consul-2.json\n{\n  &quot;ID&quot;: &quot;cadvisor-exporter&quot;,\n  &quot;Name&quot;: &quot;cadvisor-exporter-172.30.12.167&quot;,\n  &quot;Tags&quot;: [\n    &quot;cadvisor-exporter&quot;\n  ],\n  &quot;Address&quot;: &quot;172.30.12.167&quot;,\n  &quot;Port&quot;: 8080,\n  &quot;Meta&quot;: {\n    &quot;app&quot;: &quot;docker&quot;,\n    &quot;team&quot;: &quot;cloudgroup&quot;,\n    &quot;project&quot;: &quot;docker-service&quot;\n  },\n  &quot;EnableTagOverride&quot;: false,\n  &quot;Check&quot;: {\n    &quot;HTTP&quot;: &quot;http:\/\/172.30.12.167:8080\/metrics&quot;,\n    &quot;Interval&quot;: &quot;10s&quot;\n  },\n  &quot;Weights&quot;: {\n    &quot;Passing&quot;: 10,\n    &quot;Warning&quot;: 1\n  }\n}\n\n# \u6ce8\u518c\u670d\u52a1\n$ curl --request PUT --data @consul-2.json http:\/\/172.30.12.167:8500\/v1\/agent\/service\/register?replace-existing-checks=1<\/code><\/pre>\n<p>\u8bf4\u660e\u4e00\u4e0b\uff0c\u6211\u4eec\u66f4\u65b0\u4e86\u539f<code>node-exporter-172.30.12.167<\/code>\u670d\u52a1\u7684\u6807\u7b7e\u4e3a<code>node-exporter<\/code>\uff0c\u540c\u65f6\u6ce8\u518c\u4e00\u4e2a\u65b0\u7c7b\u578b<code>cadvisor-exporter-172.30.12.167<\/code>\u670d\u52a1\uff0c\u5e76\u8bbe\u7f6e\u6807\u7b7e\u4e3a<code>cadvisor-exporter<\/code>\uff0c\u4ee5\u793a\u533a\u522b\u3002\u6ce8\u518c\u5b8c\u6bd5\uff0c\u901a\u8fc7<code>Consul Web<\/code>\u63a7\u5236\u53f0\u53ef\u4ee5\u770b\u5230\u6210\u529f\u6ce8\u518c\u4e86\u8fd9\u4e24\u4e2a\u670d\u52a1\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul\u670d\u52a1\u66f4\u65b0.png\" alt=\"Consul\u670d\u52a1\u66f4\u65b0\" \/><\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4fee\u6539<code>prometheus.yml<\/code>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-yml\">- job_name: &#039;consul-node-exporter&#039;\n  consul_sd_configs:\n    - server: &#039;172.30.12.167:8500&#039;\n      services: []  \n  relabel_configs:\n    - source_labels: [__meta_consul_tags]\n      regex: .*node-exporter.*\n      action: keep\n    - regex: __meta_consul_service_metadata_(.+)\n      action: labelmap\n\n- job_name: &#039;consul-cadvisor-exproter&#039;\n  consul_sd_configs:\n    - server: &#039;172.30.12.167:8500&#039;\n      services: []\n  relabel_configs:\n    - source_labels: [__meta_consul_tags]\n      regex: .*cadvisor-exporter.*\n      action: keep\n    - regex: __meta_consul_service_metadata_(.+)\n      action: labelmap<\/code><\/pre>\n<p>\u8fd9\u91cc\u9700\u8981\u6839\u636e\u6bcf\u79cd\u7c7b\u578b\u7684<code>exporter<\/code>\u65b0\u589e\u4e00\u4e2a\u5173\u8054<code>job<\/code>\uff0c\u540c\u65f6<code>relabel_configs<\/code>\u4e2d\u914d\u7f6e\u4ee5<code>Tag<\/code>\u6765\u505a\u5339\u914d\u533a\u5206\u3002\u91cd\u542f<code>Prometheus<\/code>\u670d\u52a1\uff0c\u53ef\u4ee5\u770b\u5230\u670d\u52a1\u5df2\u7ecf\u6309\u7167\u7c7b\u578b\u5206\u7c7b\u4e86\uff0c\u65b9\u4fbf\u67e5\u770b\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u8fd0\u7ef4\/Consul\u670d\u52a1\u5206\u7c7b.png\" alt=\"Consul\u670d\u52a1\u5206\u7c7b\" \/><\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/docs\/index.html\" title=\"Consule Docs\">Consule Docs<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.consul.io\/api\/agent\/service.html\" title=\"Consule Service API\">Consule Service API<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/configuration\" title=\"Prometheus configurations\">Prometheus configurations<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Consul \u4ecb\u7ecd Consul \u662f\u57fa\u4e8e GO \u8bed\u8a00\u5f00\u53d1\u7684\u5f00\u6e90\u5de5\u5177\uff0c\u4e3b\u8981\u9762\u5411\u5206\u5e03\u5f0f\uff0c\u670d\u52a1\u5316\u7684\u7cfb\u7edf\u63d0\u4f9b\u670d\u52a1\u6ce8\u518c\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[386],"tags":[394,387],"class_list":["post-1573","post","type-post","status-publish","format-standard","hentry","category-monitor","tag-consul","tag-prometheus"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1573"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1573\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}