21

Prometheus学习笔记–Prometheus监控之elasticsearch集群 |坐而言不如起而行! 二丫讲...

 3 years ago
source link: http://www.eryajf.net/4738.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
本文预计阅读时间 12 分钟

prometheus监控es,同样采用exporter的方案。

  • 项目地址:
    • elasticsearch_exporter:https://github.com/justwatchcom/elasticsearch_exporter

1、安装部署

现有es三节点的集群,环境大概如下:

主机 组件 10.3.6.30–es-node1 es,nginx 10.3.6.125–es-node2 es 10.3.6.124–es-node3 es,kibana

接着分别在如上三台主机上进行如下配置:

  1. wget https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.1.0/elasticsearch_exporter-1.1.0.linux-amd64.tar.gz
  2. tar xf elasticsearch_exporter-1.1.0.linux-amd64.tar.gz
  3. mv elasticsearch_exporter-1.1.0.linux-amd64 /usr/local/elasticsearch_exporter

启动监控客户端:

  1. nohup ./elasticsearch_exporter --web.listen-address ":9109" --es.uri http://10.3.6.30:9200 &

使用systemd管理:

  1. cat /lib/systemd/system/es_exporter.service
  2. [Unit]
  3. Description=The es_exporter
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. User=prometheus
  8. ExecStart=/usr/local/elasticsearch_exporter/elasticsearch_exporter --web.listen-address ":9308" --es.uri http://127.0.0.1:9200
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  1. systemctl daemon-reload
  2. systemctl start es_exporter

查看metrics:

  1. curl 127.0.0.1:9109/metrics

2,配置 prometheus.yml 添加监控目标

  1. $ vim /usr/local/prometheus/prometheus.yml
  2. - job_name: 'elasticsearch'
  3. scrape_interval: 60s
  4. scrape_timeout: 30s
  5. metrics_path: "/metrics"
  6. static_configs:
  7. - targets:
  8. - '10.3.0.41:9109'
  9. labels:
  10. service: elasticsearch

重启服务。

  1. $ systemctl restart prometheus

或者通过命令热加载:

  1. curl -XPOST localhost:9090/-/reload

5,配置 Grafana 的模板

模板通过json文件进行导入,文件就在解压的包内。

参考地址:https://shenshengkun.github.io/posts/550bdf86.html

或者通过如下ID进行导入:2322以及其他。

6,开启认证的启动方式

如果es开启了认证,那么启动的时候需要将用户名密码加载进去:

  1. lasticsearch_exporter --web.listen-address ":9308" --es.uri http://username:[email protected]:9200 &

其中使用的是monitoring的用户密码。

当然,除去这种命令行的启动方式之外,还可以像上边一样,基于systemd进行管理,只需将认证的参数信息写入到如下内容当中:

  1. $ cat /etc/default/elasticsearch_exporter
  2. EXPORTER_ARGS="--es.uri=http://username:[email protected]:9200"

接着将启动配置文件封装成如下脚本:

  1. $ cat /etc/init.d/elasticsearch_exporter
  2. #!/bin/sh
  3. # chkconfig: 2345 60 20
  4. # description: elasticsearch_exporter
  5. NAME=elasticsearch_exporter
  6. SCRIPT="/usr/bin/${NAME}"
  7. PIDFILE="/var/run/${NAME}.pid"
  8. LOGFILE="/var/log/${NAME}.log"
  9. ENVFILE="/etc/default/${NAME}"
  10. USER="root"
  11. URL='http://192.10.10.1'
  12. EXPORTER_NAME=$NAME
  13. EXPORTER_PORT="9114"
  14. #获取本机ip
  15. IP=$(grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}")
  16. register_exporter() {
  17. json_data='{"service_id":"'${EXPORTER_NAME}${IP//./}'","job":"'${EXPORTER_NAME}'","ip":"'${IP}'","port":"'$EXPORTER_PORT'","tags":"","meta": {"hostname": "'$(hostname)'"}}'
  18. curl --connect-timeout 2 -s -X POST -H "Content-type: application/json" -d "${json_data}" $URL 2>&1 > /dev/null
  19. }
  20. start() {
  21. if [ -f "${PIDFILE}" ] && kill -0 $(cat "${PIDFILE}") &> /dev/null; then
  22. echo "${NAME} already running with PID $(cat ${PIDFILE})" >&2
  23. return 1
  24. fi
  25. echo "Starting ${NAME}" >&2
  26. . "${ENVFILE}"
  27. CMD="${SCRIPT} --web.listen-address=${IP}:${EXPORTER_PORT} --log.level=error ${EXPORTER_ARGS}"
  28. su - "${USER}" -c "${CMD} &> ${LOGFILE} & echo \$! > ${PIDFILE}"
  29. # echo "${NAME} started with PID $(cat ${PIDFILE})" >&2
  30. sleep 1
  31. if [ -f "${PIDFILE}" ] && kill -0 $(cat "${PIDFILE}") &> /dev/null; then
  32. echo "${NAME} started successfully." >&2
  33. register_exporter
  34. else
  35. echo "${NAME} was not started OK"
  36. return 1
  37. fi
  38. }
  39. stop() {
  40. if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE") &> /dev/null; then
  41. echo "${NAME} not running" >&2
  42. return 1
  43. fi
  44. echo "Stopping ${NAME}..." >&2
  45. kill -15 $(cat "$PIDFILE")
  46. rm -f "$PIDFILE"
  47. echo "${NAME} stopped" >&2
  48. }
  49. status() {
  50. if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE") &> /dev/null; then
  51. echo "${NAME} is not running" >&2
  52. else
  53. echo "${NAME} is running" >&2
  54. fi
  55. }
  56. uninstall() {
  57. echo -n "Are you really sure you want to uninstall ${NAME}? That cannot be undone. [yes|No] "
  58. local SURE
  59. read SURE
  60. if [ "$SURE" = "yes" ]; then
  61. stop
  62. rm -f "$PIDFILE"
  63. echo "Notice: log file is not be removed: '$LOGFILE'" >&2
  64. update-rc.d -f <NAME> remove
  65. rm -fv "$0"
  66. fi
  67. }
  68. case "$1" in
  69. start)
  70. start
  71. ;;
  72. stop)
  73. stop
  74. ;;
  75. uninstall)
  76. uninstall
  77. ;;
  78. restart)
  79. stop
  80. start
  81. ;;
  82. status)
  83. status
  84. ;;
  85. register)
  86. register_exporter
  87. ;;
  88. *)
  89. echo "Usage: $0 {start|stop|restart|status|register|uninstall}"
  90. esac

此处服务启动之后将会自动注册到统一的注册中心去,而不必再手动添加配置。


weinxin


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK