WireGuard 教程:使用 Prometheus 監控 WireGuard

雲原生是一種信仰,是一種全新的技術模式,它不侷限於你腦海中固有的那一畝三分地。人有多大膽,地有多大產,只要你敢想,萬物皆可雲原生。做爲一個雲原生狂熱信徒,給你們看看個人狂熱程度:node

個人全部服務(包括博客、鏡像加速、評論服務)都部署在雲上 k3s 集羣中,同時本地和家中設備均和雲上集羣 Pod 網絡經過 WireGuard 打通,家中網關 DNS 用的是 CoreDNS 對國內外解析進行分流,網關使用 Envoy 來代理家中的各類服務,等等。git

家中的全部設備和服務,包括雲上的服務,所有使用 kube-prometheus 進行監控,具體我就不細說了,截幾張圖給你們看看:github

如今還剩下個 WireGuard 沒有監控,下面就來看看如何使用 Prometheus 來監控 WireGuardgolang

若是看到這篇文章的你仍然是個 WireGuard 新手,請務必按照如下順序閱讀每一篇文章:web

若是遇到不明白的,能夠參考這篇文章的註解:docker

剩下這幾篇文章是可選的,有興趣就看看:json

WireGuard 自己是不暴露任何指標的,須要經過第三方的 exporter 來暴露指標。目前有兩個版本的 exporter,單純使用其中一個都不太完美,因此我乾脆都用。api

1. 鏡像構建

這兩個 exporter 都沒有提供 Docker 鏡像,因此我只好本身動手了,Rust 版本 exporter 的 Dockerfile 以下:bash

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 以下:markdown

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"]
複製代碼

鏡像的構建我就不贅述了,你們能夠看個人 GitHub 倉庫

2. prometheus_wireguard_exporter 部署

prometheus_wireguard_exporter 直接利用 wg 的配置文件來獲取指標,它本身不須要單獨準備配置文件,因此只需將 /etc/wireguard 目錄映射到容器中。若是你的 wg 組網模式是中心輻射型,建議只需監控 wg 網關,若是是全互聯模式,也能夠只監控其中一個用來生成配置的節點,固然你也能夠監控全部節點。

我這裏只監控了其中一個用來生成配置的節點,如下是部署清單:

# 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
複製代碼

3. wireguard_exporter 部署

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 中的配置內容轉化爲上面的格式保存到 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-h45x5   1/1     Running   0          52s
複製代碼

4. 加入 Prometheus 監控

kube-prometheus 的部署方式這裏略過,新手請本身查閱文檔部署,我只講關鍵的步驟。要想讓 kube-prometheus 能獲取到 WireGuard 的指標,須要建立相應的 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
複製代碼

使用資源清單建立 ServiceMonitor

$ kubectl apply -f prometheus-serviceMonitorWireguard.yaml
複製代碼

查看 Prometheus 中對應的 Target 是否已經獲取成功:

最後在 Grafana 中添加儀表盤,經過環境變量來切換不一樣 wg 接口的監控儀表盤。

至於儀表盤的語法細節,我就不展開講了,感興趣的能夠先導入個人儀表盤,後面遇到不懂的再來問我。儀表盤 json 文件連接:

相關文章
相關標籤/搜索