cd /usr/local/prometheus wget https://releases.hashicorp.com/consul/1.12.3/consul_1.12.3_linux_amd64.zip
unzip consul_1.12.3_linux_amd64.zip
解压出来的就是一个可执行文件
nohup /usr/local/prometheus/consul agent -dev -ui -node=consul-dev -client=0.0.0.0 >/usr/local/prometheus/consul.log 2>&1 &
systemctl status firewalld firewall-cmd --zone=public --add-port=8500/tcp --permanent firewall-cmd --reload firewall-cmd --zone=public --query-port=8500/tcp
浏览器访问
新建一个JSON文件
touch prometheus.json vim prometheus.json
JSON文件需要指定服务ID、名称、标签,服务地址,端口,轮询周期等。
{ "ID": "prometheus", "Name": "prometheus", "Tags": [ "primary", "v1" ], "address": "localhost", "port": 9090, "check": { "http": "http://localhost:9090/metrics", "interval": "10s" } }
注册服务(重启失效)
curl --request PUT --data @prometheus.json http://localhost:8500/v1/agent/service/register
删除服务(url最后面为服务ID)
curl --request PUT http://localhost:8500/v1/agent/service/deregister/prometheus
注册服务,不使用JSON文件(重启失效)
curl --request PUT --data '{"ID":"prometheus","Name":"prometheus","Tags":["primary","v1"],"address":"localhost","port":9090,"check":{"http":"http://localhost:9090/metrics","interval":"10s"}}' http://localhost:8500/v1/agent/service/register
touch consul.sh && chmod +x consul.sh
#!/bin/bash #用于安装Consul #版本号 CONSUL_VER=1.12.3 #安装位置 CONSUL_DIR=/app/consul #安装包存放路径 INSTALL_PACKAGE=/app/install #日志文件 LOG_FILE=/tmp/consul.log #节点名称 NODE_NAME=wxhntmy # consul的用户名 CONSUL_USER=prometheus # CONSUL监听端口 CONSUL_PORT=8500 check_err() { if [ $? -ne 0 ]; then echo "$1" exit 1 else echo "$2" fi } start() { [ ! -d /app ] && mkdir -p /app #检查是否存在wget命令,没有则安装 type wget &> /dev/null if [ $? -ne 0 ]; then yum install wget -y check_err "\033[31myum安装依赖包 wget 失败,请检查\033[0m" "\033[36myum安装依赖包 wget 成功\033[0m" fi #检查是否存在netstat命令,没有则安装 type netstat &> /dev/null if [ $? -ne 0 ]; then yum install net-tools -y check_err "\033[31myum安装依赖包 net-tools 失败,请检查\033[0m" "\033[36myum安装依赖包 net-tools 成功\033[0m" fi netstat -tln | grep ":$CONSUL_PORT" CHECK_RESULT=$? if [ $CHECK_RESULT -eq 0 ];then echo "CONSUL 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 CONSUL....." [ ! -d $INSTALL_PACKAGE ] && mkdir -p $INSTALL_PACKAGE [ ! -f $INSTALL_PACKAGE/consul_${CONSUL_VER}_linux_amd64.zip ] && wget -O $INSTALL_PACKAGE/consul_${CONSUL_VER}_linux_amd64.zip https://releases.hashicorp.com/consul/${CONSUL_VER}/consul_${CONSUL_VER}_linux_amd64.zip [ ! -d $CONSUL_DIR ] && mkdir -p $CONSUL_DIR count=`ls $CONSUL_DIR | wc -l` if [ $count -gt 0 ]; then echo "目录 $CONSUL_DIR 不为空,不再重复解压压缩包" else unzip $INSTALL_PACKAGE/consul_${CONSUL_VER}_linux_amd64.zip -d $CONSUL_DIR fi cat /etc/group | grep $CONSUL_USER if [ $? -ne 0 ]; then groupadd -g 10002 $CONSUL_USER fi cat /etc/passwd | grep $CONSUL_USER if [ $? -ne 0 ]; then useradd -u 10002 -g $CONSUL_USER -d /home/$CONSUL_USER -m $CONSUL_USER fi chown -R $CONSUL_USER:$CONSUL_USER $CONSUL_DIR [ ! -f $LOG_FILE ] && touch $LOG_FILE chown -R $CONSUL_USER:$CONSUL_USER $LOG_FILE # 切换用户 su - $CONSUL_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $CONSUL_DIR/consul agent -dev -ui -node=$NODE_NAME -client=0.0.0.0 >> $LOG_FILE 2>&1 & #每2s检查一次监听端口是否启动,总共检查60次共2分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=60 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 2 netstat -tln | grep ":$CONSUL_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$CONSUL_PORT端口超时,请检查!" exit 1 fi done echo "CONSUL 启动成功!" EOF #检查防火墙是否已启动,如果启动则开放端口 systemctl status firewalld > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=$CONSUL_PORT/tcp --permanent firewall-cmd --reload fi } stop(){ netstat -tln | grep ":$CONSUL_PORT" check_err "CONSUL 未启动!" "CONSUL 正在运行,开始停止应用....." su - $CONSUL_USER << EOF PROCESS=`ps aux | grep $CONSUL_DIR/consul | grep -v "grep" | awk '{print $2}'` for i in \${PROCESS} do echo "Kill the CONSUL process [ \$i ]" kill -9 \$i done EOF CHECK_RESULT=0 CHECK_COUNT=30 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 2 netstat -tln | grep ":$CONSUL_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$CONSUL_PORT端口超时,请检查!" exit 1 fi done echo "关闭 CONSUL 成功!" } restart(){ stop start } status(){ su $CONSUL_USER -c "ps aux | grep $CONSUL_DIR/consul | grep -v \"grep\" | grep -v \"su $CONSUL_USER\"" } remove(){ rm -rf $LOG_FILE rm -rf $CONSUL_DIR userdel -r $CONSUL_USER } register(){ JSON_FILE=$1 if [[ ! -n "$JSON_FILE" ]] || [[ ! -f $JSON_FILE ]]; then echo "$JSON_FILE 文件不存在!" echo "请在 register 后面指定注册服务的 JSON 文件,JSON文件需要指定服务ID、名称、标签,服务地址,端口,轮询周期等。" echo "JSON文件示例:{ "ID": "prometheus", "Name": "prometheus", "Tags": [ "primary", "v1" ], "address": "localhost", "port": 9090, "check": { "http": "http://localhost:9090/metrics", "interval": "10s" } }" exit 1 fi ip=`ifconfig ens33 | grep 'inet ' | sed 's/^.*inet //g' | sed 's/netmask.*$//g' | sed 's/ //g'` echo "当前服务器 IP 为:$ip,默认以该 IP 地址进行服务注册!" curl --request PUT -T $JSON_FILE http://${ip}:${CONSUL_PORT}/v1/agent/service/register SERVICE_ID=`cat $JSON_FILE | grep 'ID' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/://g' | sed 's/ID//g'` echo "SERVICE_ID: $SERVICE_ID" if [[ ! -n "$SERVICE_ID" ]] ; then echo "SERVICE_ID不能为空!" exit 1 fi CNT=`curl --request GET http://${ip}:${CONSUL_PORT}/v1/agent/services | grep 'ID' | grep $SERVICE_ID | wc -l` if [ $CNT -gt 0 ]; then echo "服务注册成功!" else echo "服务注册失败!" fi } deregister(){ ip=`ifconfig ens33 | grep 'inet ' | sed 's/^.*inet //g' | sed 's/netmask.*$//g' | sed 's/ //g'` echo "当前服务器 IP 为:$ip,默认以该 IP 地址删除 Consul 已注册的服务!" curl --request PUT http://${ip}:${CONSUL_PORT}/v1/agent/service/deregister/$1 CNT=`curl --request GET http://${ip}:${CONSUL_PORT}/v1/agent/services | grep 'ID' | grep $1 | wc -l` if [ $CNT -eq 0 ]; then echo "服务删除成功!" else echo "服务删除失败!" fi } service(){ ip=`ifconfig ens33 | grep 'inet ' | sed 's/^.*inet //g' | sed 's/netmask.*$//g' | sed 's/ //g'` echo "当前服务器 IP 为:$ip,默认以该 IP 地址获取 Consul 已注册的服务!" SERVICE_STATUS=`curl http://${ip}:${CONSUL_PORT}/v1/agent/service/$1` echo "$SERVICE_STATUS" } usage () { echo " " echo "请输入 start 启动 CONSUL。" echo "请输入 stop 停止 CONSUL。" echo "请输入 status 获取 CONSUL 状态。" echo "请输入 restart 重启 CONSUL。" echo "请输入 remove 删除 CONSUL。" echo "请输入 service 获取已注册的服务状态。在 service 后面指定服务ID(示例:./consul.sh service prometheus)。" echo "请输入 register 进行服务注册(重启失效)。在 register 后面指定注册服务的 JSON 文件(示例:./consul.sh register /root/prometheus.json)。" echo "请输入 deregister 删除服务。在 deregister 后面指定要删除的服务ID(示例:./consul.sh deregister prometheus)。" echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=start} case ${ACTIVE} in start) start ;; stop) stop ;; status) status ;; restart) restart ;; remove) remove ;; register) register $2 ;; deregister) deregister $2 ;; service) service $2 ;; *) usage ;; esac