<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          使用 Prometheus 監(jiān)控 WireGuard

          共 14075字,需瀏覽 29分鐘

           ·

          2021-03-15 08:38


          云原生是一種信仰,是一種全新的技術(shù)模式,它不局限于你腦海中固有的那一畝三分地。人有多大膽,地有多大產(chǎn),只要你敢想,萬物皆可云原生。作為一個云原生狂熱信徒,給大家看看我的狂熱程度:

          我的所有服務(wù)(包括博客、鏡像加速、評論服務(wù))都部署在云上 k3s 集群中,同時(shí)本地和家中設(shè)備均和云上集群 Pod 網(wǎng)絡(luò)通過 WireGuard 打通,家中網(wǎng)關(guān) DNS 用的是 CoreDNS 對國內(nèi)外解析進(jìn)行分流,網(wǎng)關(guān)使用 Envoy 來代理家中的各種服務(wù),等等。

          家中的所有設(shè)備和服務(wù),包括云上的服務(wù),全部使用 kube-prometheus 進(jìn)行監(jiān)控,具體我就不細(xì)說了,截幾張圖給大家看看:

          現(xiàn)在還剩下個 WireGuard 沒有監(jiān)控,下面就來看看如何使用 Prometheus 來監(jiān)控 WireGuard。

          如果看到這篇文章的你仍然是個 WireGuard 新手,請務(wù)必按照以下順序閱讀每一篇文章:

          如果遇到不明白的,可以參考這篇文章的注解:

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

          WireGuard 本身是不暴露任何指標(biāo)的,需要通過第三方的 exporter 來暴露指標(biāo)。目前有兩個版本的 exporter,單純使用其中一個都不太完美,所以我干脆都用。

          1. 鏡像構(gòu)建

          這兩個 exporter 都沒有提供 Docker 鏡像,所以我只好自己動手了,Rust 版本 exporter 的 Dockerfile 如下:

          FROM rust as builder

          LABEL description="Docker container for building prometheus exporter for wireguard."
          LABEL maintainer="Ryan Yang <[email protected]>"

          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 <[email protected]>"

          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 倉庫[1]。

          2. prometheus_wireguard_exporter 部署

          prometheus_wireguard_exporter[2] 直接利用 wg 的配置文件來獲取指標(biāo),它自己不需要單獨(dú)準(zhǔn)備配置文件,所以只需將 /etc/wireguard 目錄映射到容器中。如果你的 wg 組網(wǎng)模式是中心輻射型,建議只需監(jiān)控 wg 網(wǎng)關(guān),如果是全互聯(lián)模式,也可以只監(jiān)控其中一個用來生成配置的節(jié)點(diǎn),當(dāng)然你也可以監(jiān)控所有節(jié)點(diǎn)。

          我這里只監(jiān)控了其中一個用來生成配置的節(jié)點(diǎn),以下是部署清單:

          # 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[3] 需要單獨(dú)準(zhǔn)備配置文件,格式如下:

          # /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-h45x5   1/1     Running   0          52s

          4. 加入 Prometheus 監(jiān)控

          kube-prometheus 的部署方式這里略過,新手請自己查閱文檔部署,我只講關(guān)鍵的步驟。要想讓 kube-prometheus 能獲取到 WireGuard 的指標(biāo),需要創(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)控儀表盤。

          至于儀表盤的語法細(xì)節(jié),我就不展開講了,感興趣的可以先導(dǎo)入我的儀表盤,后面遇到不懂的再來問我。想獲取儀表盤 json 文件的可以關(guān)注公眾號:

          然后在公眾號后臺回復(fù) wg-grafana 即可獲取。

          參考資料

          [1]

          GitHub 倉庫: https://github.com/yangchuansheng/docker-image/tree/master/wireguard_exporter

          [2]

          prometheus_wireguard_exporter: https://github.com/MindFlavor/prometheus_wireguard_exporter.git

          [3]

          wireguard_exporter: https://github.com/mdlayher/wireguard_exporter


          你可能還喜歡

          點(diǎn)擊下方圖片即可閱讀

          Jenkins 大叔與 kubernetes 船長手牽手

          云原生是一種信仰 ??


          關(guān)注公眾號

          后臺回復(fù)?k8s?獲取史上最方便快捷的 Kubernetes 高可用部署工具,只需一條命令,連 ssh 都不需要!



          點(diǎn)擊 "閱讀原文" 獲取更好的閱讀體驗(yàn)!


          發(fā)現(xiàn)朋友圈變“安靜”了嗎?

          瀏覽 94
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  香蕉电影网 | 日本色情 电影在线播放 | 日韩黄色一级网站 | 午夜天堂精品久久久久 | 午夜噜噜|