GO语言写Prometheus自定义node-exporter的Docker容器测试

1. 安装docker-compose

执行以下命令,安装docker-compose到CentOS7.9环境中:

# 下载二进制文件
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 给文件赋予执行的权限
sudo chmod +x /usr/local/bin/docker-compose
# 创建软链接到执行目录
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

安装完成后输入命令验证docker-compose安装成功:

docker-compose --version

2. 使用go语言编写node-exporter

创建文件夹04_prometheus_test,执行以下命令初始化go环境:

cd 04_prometheus_test
go mod init 04_prometheus_test

由于需要用到prometheus的go语言sdk,所以需要下载包:

go get github.com/prometheus/client_golang/prometheus  
go get github.com/prometheus/client_golang/prometheus/promhttp

执行完成后写代码:

package main  
  
import (  
	"net/http"  
	"log"  
  
	"github.com/prometheus/client_golang/prometheus"  
	"github.com/prometheus/client_golang/prometheus/promhttp"  
)  
  
// 自定义一个计数器指标  
var helloWorldCounter = prometheus.NewCounter(prometheus.CounterOpts{  
	Name: "hello_world_counter_total",  
	Help: "Total number of hello world invocations.",  
})  
  
// 定义一个简单的HTTP处理器,每次调用都会增加计数器  
func helloWorldHandler(w http.ResponseWriter, r *http.Request) {  
	helloWorldCounter.Inc()  
	w.Write([]byte("Hello, World!"))  
}  
  
func main() {  
	// 注册自定义指标  
	prometheus.MustRegister(helloWorldCounter)  
  
	// 设置HTTP服务器监听端口  
	http.Handle("/metrics", promhttp.Handler())  
	http.HandleFunc("/hello", helloWorldHandler)  
  
	// 启动HTTP服务器  
	log.Fatal(http.ListenAndServe(":8080", nil))  
}

这个程序暴露<ip>:8080/metrics给prometheus获取数据,<ip>:8080/hello可以增加计数。
执行编译生成可执行文件:

go build .

3. docker镜像制作

写Dockerfile,用于创建Docker镜像:

FROM centos
LABEL maintainer="kaijessen@gmail.com"
COPY . /app
WORKDIR /app
RUN chmod a+x /app/*
EXPOSE 8080
ENTRYPOINT ["/app/04_prometheus_test"]

执行命令生成镜像到本地库:

docker build -t prom_custom_ne:latest .

完成后执行docker images可以看到生成新的镜像prom_custom_ne。
在这里插入图片描述

4. docker-compose制作

写一个docker-compose.yml文件,用来将镜像启动起来。

version: '3.5'  
  
services:  
  prometheus:  
    image: docker.io/prom/prometheus  
    container_name: prometheus  
    networks:  
      - prom_test_net  
    ports:  
      - "9090:9090"  
    volumes:  
      - prom_test_vol:/etc/prometheus/
  
  custom_ne:  
    image: prom_custom_ne  
    container_name: custom_ne  
    networks:  
      - prom_test_net  
    ports:
      - "8080:8080"
  
networks:  
  prom_test_net:  
    driver: bridge  
  
volumes:  
  prom_test_vol:  
    driver: local  
    driver_opts:  
      type: none  
      device: /opt/prometheus/
      o: bind

这个文件启动两个服务,prometheus和custom_ne;把宿主机的/opt/prometheus/目录挂在了prometheus容器的/etc/prometheus目录下;将prometheus:9090的端口和custome_ne:8080绑定到宿主机对应端口以便访问;创建一个bridge供两个容器互相通信。其中值得注意的是/opt/prometheus目录下配置一个prometheus.yml文件,用于配置监控目标,如下:

scrape_configs:  
  - job_name: 'custom_exporter'  
    static_configs:  
      - targets: ['custom_ne:8080']

输入以下命令启动docker-compose。

docker-compose up -d

5. 结果展示

访问宿主机的9090端口,查询自定义指标hello_world_counter_total,结果展示如下:
在这里插入图片描述
期间我通过宿主机8080/hello目录访问过几次,所以可以看到自定义数值在增加。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 18:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 18:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 18:48:01       82 阅读
  4. Python语言-面向对象

    2024-04-26 18:48:01       91 阅读

热门阅读

  1. 掩码讲解,以及生成

    2024-04-26 18:48:01       35 阅读
  2. Nginx下php连接到GBase 8s数据库 - ODBC方式

    2024-04-26 18:48:01       165 阅读
  3. uniapp 页面滚动到指定位置的方法

    2024-04-26 18:48:01       28 阅读
  4. 【学习笔记】

    2024-04-26 18:48:01       29 阅读
  5. CDN引入Vue3

    2024-04-26 18:48:01       34 阅读
  6. 对象指针与对象数组(拉丁舞)

    2024-04-26 18:48:01       34 阅读
  7. Unity 数据持久化——persistentDataPath储存路径

    2024-04-26 18:48:01       34 阅读
  8. 游戏热更新进修——Lua编程

    2024-04-26 18:48:01       137 阅读
  9. Elment ui 表单上滑 加载更多数据方法

    2024-04-26 18:48:01       28 阅读
  10. CSV解析

    CSV解析

    2024-04-26 18:48:01      33 阅读
  11. Promise

    Promise

    2024-04-26 18:48:01      36 阅读