consul自动发现原理就不多说了。估计大家都是看了之后的过来的
开始把,我的是基于K8s部署的
官方安装文档
https://www.consul.io/docs/k8s/installation/install
配置文件
cat<<END> node.json
{
"services": [{
"id": "node_win10",
"name": "node01",
"tags": ["nodes"],
"address": "192.168.8.88",
"port": 9182,
"checks": [{
"http": "http://192.168.8.88:9182/metrics",
"interval": "8s"
}]
},
{
"id": "node_export02",
"name": "node02",
"tags": ["nodes"],
"address": "192.168.8.171",
"port": 9100,
"checks": [{
"http": "http://192.168.8.171:9100/metrics",
"interval": "8s"
}]
}]
}
END
kubectl create configmap consuls -n istio-system --from-file=./node.json
cat<<END> consul.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: consul
namespace: istio-system
spec:
replicas: 1
selector:
matchLabels:
app: consul-example
template:
metadata:
labels:
app: consul-example
spec:
containers:
- name: example
image: consul:latest
command:
- '/bin/sh'
- '-c'
- |
consul agent -data-dir=/consul/data -config-dir=/consul/config -dev -ui -client 0.0.0.0
securityContext:
privileged: true
runAsUser: 0
volumeMounts:
- name: conf
mountPath: "/consul/config"
readOnly: true
volumes:
- name: conf
configMap:
name: consuls
---
apiVersion: v1
kind: Service
metadata:
name: consul
namespace: istio-system
spec:
ports:
- port: 8500
targetPort: 8500
nodePort: 8500
type: NodePort
selector:
app: consul-example
END
kubectl apply -f consul.yaml
访问web得到以下两个节点
prometheus 修改配置
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
consul_sd_configs:
- server: "consul:8500"
tags:
- "prometheus"
refresh_interval: 10s
- job_name: nodes
consul_sd_configs:
- server: "consul:8500"
tags:
- "nodes"
refresh_interval: 10s
修改完重置生效最后加入grafana
curl -X POST http://192.168.8.169:19090/-/reload
以下是redis
{
"services": [{
"id":"redis1",
"name":"redis",
"tags":["redis1"],
"address": "127.0.0.1",
"port":6378,
"checks": [
{
"Tcp": "127.0.0.1:6378",
"interval": "3s"
}
]
},{
"id":"redis2",
"name":"redis",
"tags":["redis2"],
"address": "127.0.0.1",
"port":6379,
"checks": [
{
"Tcp": "127.0.0.1:6379",
"interval": "3s"
}
]
}
]
}