您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用 Prometheus 監(jiān)控 WireGuard”,在日常操作中,相信很多人在如何使用 Prometheus 監(jiān)控 WireGuard問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用 Prometheus 監(jiān)控 WireGuard”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
云原生是一種信仰,是一種全新的技術(shù)模式,它不局限于你腦海中固有的那一畝三分地。人有多大膽,地有多大產(chǎn),只要你敢想,萬物皆可云原生。作為一個云原生狂熱信徒,給大家看看我的狂熱程度:
我的所有服務(wù)(包括博客、鏡像加速、評論服務(wù))都部署在云上 k3s
集群中,同時本地和家中設(shè)備均和云上集群 Pod 網(wǎng)絡(luò)通過 WireGuard
打通,家中網(wǎng)關(guān) DNS 用的是 CoreDNS 對國內(nèi)外解析進行分流,網(wǎng)關(guān)使用 Envoy
來代理家中的各種服務(wù),等等。
家中的所有設(shè)備和服務(wù),包括云上的服務(wù),全部使用 kube-prometheus
進行監(jiān)控,具體我就不細說了,截幾張圖給大家看看:
現(xiàn)在還剩下個 WireGuard
沒有監(jiān)控,下面就來看看如何使用 Prometheus
來監(jiān)控 WireGuard
。
如果看到這篇文章的你仍然是個 WireGuard
新手,請務(wù)必按照以下順序閱讀每一篇文章:
WireGuard 教程:WireGuard 的工作原理
WireGuard 快速安裝教程
WireGuard 配置教程:使用 wg-gen-web 來管理 WireGuard 的配置
Wireguard 全互聯(lián)模式(full mesh)配置指南
如果遇到不明白的,可以參考這篇文章的注解:
WireGuard 教程:WireGuard 的搭建使用與配置詳解
剩下這幾篇文章是可選的,有興趣就看看:
我為什么不鼓吹 WireGuard
Why not "Why not WireGuard?"
WireGuard 教程:使用 DNS-SD 進行 NAT-to-NAT 穿透
WireGuard 本身是不暴露任何指標的,需要通過第三方的 exporter
來暴露指標。目前有兩個版本的 exporter,單純使用其中一個都不太完美,所以我干脆都用。
這兩個 exporter
都沒有提供 Docker 鏡像,所以我只好自己動手了,Rust
版本 exporter 的 Dockerfile
如下:
FROM rust as builder LABEL description="Docker container for building prometheus exporter for wireguard." LABEL maintainer="Ryan Yang <yangchuansheng33@gmail.com>" WORKDIR /usr/src/ RUN git clone https://github.com/MindFlavor/prometheus_wireguard_exporter.git; \ cd prometheus_wireguard_exporter; \ cargo install --path . FROM debian:buster-slim RUN sh -c "echo 'deb http://deb.debian.org/debian buster-backports main contrib non-free' > /etc/apt/sources.list.d/buster-backports.list"; \ apt update; \ apt install -y wireguard; \ rm -rf /var/lib/apt/lists/* COPY --from=builder /usr/local/cargo/bin/prometheus_wireguard_exporter /usr/local/bin/prometheus_wireguard_exporter CMD ["prometheus_wireguard_exporter"]
Go
版本 exporter 的 Dockerfile
如下:
FROM golang AS build LABEL description="Docker container for building prometheus exporter for wireguard." LABEL maintainer="Ryan Yang <yangchuansheng33@gmail.com>" WORKDIR /src RUN git clone https://github.com/mdlayher/wireguard_exporter; \ cd wireguard_exporter/cmd/wireguard_exporter/; \ go build . FROM busybox:glibc COPY --from=build /src/wireguard_exporter/cmd/wireguard_exporter/wireguard_exporter . CMD ["./wireguard_exporter"]
鏡像的構(gòu)建我就不贅述了,大家可以看我的 GitHub 倉庫。
prometheus_wireguard_exporter 直接利用 wg
的配置文件來獲取指標,它自己不需要單獨準備配置文件,所以只需將 /etc/wireguard
目錄映射到容器中。如果你的 wg 組網(wǎng)模式是中心輻射型,建議只需監(jiān)控 wg 網(wǎng)關(guān),如果是全互聯(lián)模式,也可以只監(jiān)控其中一個用來生成配置的節(jié)點,當(dāng)然你也可以監(jiān)控所有節(jié)點。
我這里只監(jiān)控了其中一個用來生成配置的節(jié)點,以下是部署清單:
# wireguard_exporter.yaml apiVersion: apps/v1 kind: Deployment metadata: name: wireguard-exporter labels: app: wireguard-exporter spec: replicas: 1 selector: matchLabels: app: wireguard-exporter strategy: rollingUpdate: maxSurge: 0 maxUnavailable: 1 type: RollingUpdate template: metadata: labels: app: wireguard-exporter spec: nodeSelector: kubernetes.io/hostname: blog-k3s03 tolerations: - key: node-role.kubernetes.io/ingress operator: Exists effect: NoSchedule hostNetwork: true containers: - name: wireguard-exporter image: yangchuansheng/wireguard_exporter command: ["/usr/local/bin/prometheus_wireguard_exporter"] args: ["-n", "/etc/wireguard/wg0.conf", "-r"] securityContext: capabilities: add: ["NET_ADMIN"] ports: - containerPort: 9586 protocol: TCP name: http-metrics volumeMounts: - mountPath: /etc/localtime name: localtime - mountPath: /etc/wireguard name: config volumes: - name: localtime hostPath: path: /etc/localtime - name: config hostPath: path: /etc/wireguard --- apiVersion: v1 kind: Service metadata: name: wireguard-exporter labels: app: wireguard-exporter spec: sessionAffinity: ClientIP selector: app: wireguard-exporter ports: - protocol: TCP name: http-metrics port: 9586 targetPort: 9586
使用部署清單部署 prometheus_wireguard_exporter
:
$ kubectl apply -f wireguard_exporter.yaml
查看是否部署成功:
$ kubectl get pod -l app=wireguard-exporter NAME READY STATUS RESTARTS AGE wireguard-exporter-78d44b8bd9-ppm9t 1/1 Running 0 41s
wireguard_exporter 需要單獨準備配置文件,格式如下:
# /etc/wireguard/wg0.toml [[Peer]] public_key = "cGsHfwmPEiLJj6Fv3GU5xFvdyQByn50PC5keVGJEe0w=" name = "RouterOS" [[Peer]] public_key = "izv5L8Kn48+SVwE3D498mdi7YfSrn6aKDNIRxIAHDkU=" name = "macOS" [[Peer]] public_key = "EOM0eLVxsj9jGKWamuIn65T3Wmqw36uLOg2ss7yJ2gw=" name = "blog-k3s02" [[Peer]] public_key = "1RxEokE41ypnIMsbE5OVHFVx199V71MOYzpzQ8bbsFY=" name = "blog-k3s01" [[Peer]] public_key = "b3JiuvdOUV7cFpXyJzLbO2Ea4V4c4AoyugIC/ufGZ18=" name = "Openwrt" [[Peer]] public_key = "FIbzqNv10cdCDO/Ka2GIN9rpxNVV2tO2f00R71EHeSg=" name = "Oneplus"
你需要將 wg0.conf
中的配置內(nèi)容轉(zhuǎn)化為上面的格式保存到 wg0.toml
文件中,再將其映射到容器中。部署清單如下:
# wireguard_exporter_go.yaml apiVersion: apps/v1 kind: Deployment metadata: name: wireguard-exporter-go labels: app: wireguard-exporter-go spec: replicas: 1 selector: matchLabels: app: wireguard-exporter-go strategy: rollingUpdate: maxSurge: 0 maxUnavailable: 1 type: RollingUpdate template: metadata: labels: app: wireguard-exporter-go spec: nodeSelector: kubernetes.io/hostname: blog-k3s03 tolerations: - key: node-role.kubernetes.io/ingress operator: Exists effect: NoSchedule hostNetwork: true containers: - name: wireguard-exporter-go image: docker.io/yangchuansheng/wireguard_exporter:golang command: ["/wireguard_exporter"] args: ["-wireguard.peer-file", "/etc/wireguard/wg0.toml", "-metrics.addr", ":9587"] securityContext: capabilities: add: ["NET_ADMIN"] ports: - containerPort: 9587 protocol: TCP name: http-metrics volumeMounts: - mountPath: /etc/localtime name: localtime - mountPath: /etc/wireguard name: config volumes: - name: localtime hostPath: path: /etc/localtime - name: config hostPath: path: /etc/wireguard --- apiVersion: v1 kind: Service metadata: name: wireguard-exporter-go labels: app: wireguard-exporter-go spec: sessionAffinity: ClientIP selector: app: wireguard-exporter-go ports: - protocol: TCP name: http-metrics port: 9587 targetPort: 9587
使用部署清單部署 wireguard_exporter
:
$ kubectl apply -f wireguard_exporter_go.yaml
查看是否部署成功:
$ kubectl get pod -l app=wireguard-exporter-go NAME READY STATUS RESTARTS AGE wireguard-exporter-go-7f5c88fc68-h55x5 1/1 Running 0 52s
kube-prometheus
的部署方式這里略過,新手請自己查閱文檔部署,我只講關(guān)鍵的步驟。要想讓 kube-prometheus
能獲取到 WireGuard 的指標,需要創(chuàng)建相應(yīng)的 ServiceMonitor
資源,資源清單如下:
# prometheus-serviceMonitorWireguard.yaml apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: labels: app: wireguard-exporter name: wireguard-exporter namespace: monitoring spec: endpoints: - interval: 15s port: http-metrics namespaceSelector: matchNames: - default selector: matchLabels: app: wireguard-exporter --- apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: labels: app: wireguard-exporter-go name: wireguard-exporter-go namespace: monitoring spec: endpoints: - interval: 15s port: http-metrics namespaceSelector: matchNames: - default selector: matchLabels: app: wireguard-exporter-go
使用資源清單創(chuàng)建 ServiceMonitor
:
$ kubectl apply -f prometheus-serviceMonitorWireguard.yaml
查看 Prometheus 中對應(yīng)的 Target
是否已經(jīng)獲取成功:
最后在 Grafana
中添加儀表盤,通過環(huán)境變量來切換不同 wg 接口的監(jiān)控儀表盤。
至于儀表盤的語法細節(jié),我就不展開講了,感興趣的可以先導(dǎo)入我的儀表盤,后面遇到不懂的再來問我。儀表盤 json 文件鏈接:
https://cdn.jsdelivr.net/gh/yangchuansheng/docker-image@master/wireguard_exporter/dashboard.json
到此,關(guān)于“如何使用 Prometheus 監(jiān)控 WireGuard”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。