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

1. 安装docker-compose

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

shell 复制代码
# 下载二进制文件
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安装成功:

shell 复制代码
docker-compose --version

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

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

shell 复制代码
cd 04_prometheus_test
go mod init 04_prometheus_test

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

shell 复制代码
go get github.com/prometheus/client_golang/prometheus  
go get github.com/prometheus/client_golang/prometheus/promhttp

执行完成后写代码:

go 复制代码
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可以增加计数。

执行编译生成可执行文件:

shell 复制代码
go build .

3. docker镜像制作

写Dockerfile,用于创建Docker镜像:

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

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

shell 复制代码
docker build -t prom_custom_ne:latest .

完成后执行docker images可以看到生成新的镜像prom_custom_ne。

4. docker-compose制作

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

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文件,用于配置监控目标,如下:

yml 复制代码
scrape_configs:  
  - job_name: 'custom_exporter'  
    static_configs:  
      - targets: ['custom_ne:8080']

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

shell 复制代码
docker-compose up -d

5. 结果展示

访问宿主机的9090端口,查询自定义指标hello_world_counter_total,结果展示如下:

期间我通过宿主机8080/hello目录访问过几次,所以可以看到自定义数值在增加。

相关推荐
IT界的老黄牛10 小时前
Jenkins 监控一条龙:接入、看板 9964、11 条告警规则直接抄走
jenkins·grafana·prometheus·监控·ci-cd·告警规则
xhaxy11 小时前
docker
docker·容器·eureka
一技安身13 小时前
【信创】Docker‑Compose V2 两种离线部署(独立模式、插件模式)简易教程
java·docker·eureka
妙码生花16 小时前
使用git更新ai-go-admin框架
前端·人工智能·git·golang·typescript·php
鸿观工坊17 小时前
Docker 多阶段构建实践指南
docker
王的宝库18 小时前
GO常用标准库包
开发语言·后端·golang
平头哥AI19 小时前
Day 13 | 一个函数回两个值:Go 的 error 是从哪冒出来的
开发语言·后端·golang
大牧师19 小时前
MySQL 学习教程
数据库·sql·mysql·docker·node·全栈·后端数据
我不会起名字32221 小时前
一天一道算法题(29):单调栈
java·数据结构·python·算法·leetcode·golang·单调栈