k8s平臺:手動部署Grafana

以下是一個可用于生產環境的 Kubernetes 部署 Grafana 的 YAML 文件。該配置包括 Deployment、Service、ConfigMap 和 PersistentVolumeClaim,確保 Grafana 的高可用性和數據持久化。


Grafana 生產部署 YAML 文件

☆實操示例

cat grafana-deployment.yaml

---
# Grafana Deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: grafananamespace: monitoringlabels:app: grafana
spec:replicas: 2  # 副本數量,生產環境可根據需求調整selector:matchLabels:app: grafanatemplate:metadata:labels:app: grafanaspec:containers:- name: grafanaimage: harbor.fq.com/prometheus/grafana:9.5.3  # 建議使用固定版本,如 grafana/grafana:9.5.2ports:- containerPort: 3000  # Grafana 默認運行端口env:- name: GF_SECURITY_ADMIN_USERvalue: "admin"  # 管理員用戶名,生產環境建議修改- name: GF_SECURITY_ADMIN_PASSWORDvalue: "Abc123!"  # 管理員密碼,生產環境建議使用更強的密碼- name: GF_INSTALL_PLUGINSvalue: "grafana-clock-panel,grafana-piechart-panel"  # 需要安裝的 Grafana 插件readinessProbe:httpGet:path: /api/health  # 就緒探針,檢測 Grafana 是否健康port: 3000initialDelaySeconds: 10  # 等待 10 秒后開始探測periodSeconds: 10  # 每 10 秒進行一次探測livenessProbe:httpGet:path: /api/health  # 存活探針,檢測 Grafana 是否正常運行port: 3000initialDelaySeconds: 30  # 等待 30 秒后開始探測periodSeconds: 60  # 每 60 秒進行一次探測volumeMounts:- name: grafana-storagemountPath: /var/lib/grafana  # 掛載存儲目錄,存放 Grafana 數據- name: grafana-configmountPath: /etc/grafana/grafana.ini  # 掛載配置文件subPath: grafana.ini  # 只映射 configMap 中的 grafana.ini 文件volumes:- name: grafana-storageemptyDir: {}  # 使用 emptyDir,不持久化存儲數據,Pod 重啟后數據會丟失- name: grafana-configconfigMap:name: grafana-config  # 關聯 ConfigMap,提供 Grafana 配置文件
---
# Grafana 配置文件 ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:name: grafana-confignamespace: monitoring
data:grafana.ini: |[server]http_port = 3000  # Grafana 監聽端口root_url = %(protocol)s://%(domain)s:%(http_port)s/[database]type = sqlite3  # 默認使用 SQLite 數據庫path = /var/lib/grafana/grafana.db  # 數據庫存放路徑[security]admin_user = admin  # 管理員用戶名admin_password = Abc123!  # 管理員密碼[users]default_theme = light  # 默認 UI 主題(light / dark)[auth.anonymous]enabled = true  # 允許匿名訪問(默認 Viewer 權限)org_name = Main Org.org_role = Viewer
---
# Grafana Service (NodePort 方式暴露)
apiVersion: v1
kind: Service
metadata:name: grafananamespace: monitoringlabels:app: grafana
spec:type: NodePort  # 使用 NodePort 方式暴露 Grafanaports:- port: 3000  # Service 端口targetPort: 3000  # Grafana 容器內部端口protocol: TCPnodePort: 32000  # 指定 NodePort 端口,范圍 30000-32767selector:app: grafana  # 關聯到 app=grafana 的 Pod
---
# Grafana Deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: grafananamespace: monitoringlabels:app: grafana
spec:replicas: 2  # 根據需求調整副本數selector:matchLabels:app: grafanatemplate:metadata:labels:app: grafanaspec:containers:- name: grafanaimage: grafana/grafana:latest  # 建議使用固定版本,如 grafana/grafana:9.5.2ports:- containerPort: 3000env:- name: GF_SECURITY_ADMIN_USERvalue: "admin"  # 生產環境建議使用更安全的用戶名- name: GF_SECURITY_ADMIN_PASSWORDvalue: "StrongPassword123!"  # 生產環境建議使用強密碼- name: GF_INSTALL_PLUGINSvalue: "grafana-clock-panel,grafana-piechart-panel"  # 可選:安裝插件readinessProbe:httpGet:path: /api/healthport: 3000initialDelaySeconds: 10periodSeconds: 10livenessProbe:httpGet:path: /api/healthport: 3000initialDelaySeconds: 30periodSeconds: 60volumeMounts:- name: grafana-storagemountPath: /var/lib/grafana- name: grafana-configmountPath: /etc/grafana/grafana.inisubPath: grafana.inivolumes:- name: grafana-storagepersistentVolumeClaim:claimName: grafana-pvc- name: grafana-configconfigMap:name: grafana-config
---
# Grafana ConfigMap (配置文件)
apiVersion: v1
kind: ConfigMap
metadata:name: grafana-confignamespace: monitoring
data:grafana.ini: |[server]http_port = 3000root_url = %(protocol)s://%(domain)s:%(http_port)s/[database]type = sqlite3path = /var/lib/grafana/grafana.db[security]admin_user = adminadmin_password = StrongPassword123![users]default_theme = light[auth.anonymous]enabled = trueorg_name = Main Org.org_role = Viewer
---
# Grafana PersistentVolumeClaim (數據持久化)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: grafana-pvcnamespace: monitoring
spec:accessModes:- ReadWriteOnceresources:requests:storage: 10Gi  # 根據需求調整存儲大小storageClassName: standard  # 根據集群的 StorageClass 調整
---
# Grafana Service (暴露服務)
apiVersion: v1
kind: Service
metadata:name: grafananamespace: monitoringlabels:app: grafana
spec:type: ClusterIP  # 生產環境建議使用 ClusterIP,配合 Ingress 暴露服務ports:- port: 3000targetPort: 3000protocol: TCPselector:app: grafana
---
# Grafana Ingress (可選,用于外部訪問)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: grafana-ingressnamespace: monitoringannotations:nginx.ingress.kubernetes.io/ssl-redirect: "true"nginx.ingress.kubernetes.io/force-ssl-redirect: "true"cert-manager.io/cluster-issuer: "letsencrypt-prod"  # 使用 cert-manager 自動管理 TLS 證書
spec:tls:- hosts:- grafana.example.com  # 替換為你的域名secretName: grafana-tlsrules:- host: grafana.example.com  # 替換為你的域名http:paths:- path: /pathType: Prefixbackend:service:name: grafanaport:number: 3000

關鍵配置說明

  1. Deployment

    • 使用 replicas: 2 確保高可用性。
    • 通過環境變量設置管理員用戶名和密碼。
    • 配置了 readinessProbelivenessProbe 以確保 Grafana 的健康狀態。
    • 掛載了持久化存儲卷 (PersistentVolumeClaim) 和配置文件 (ConfigMap)。
  2. ConfigMap

    • 包含 grafana.ini 配置文件,用于自定義 Grafana 的行為。
    • 配置了匿名訪問、數據庫路徑等。
  3. PersistentVolumeClaim

    • 使用持久化存儲確保 Grafana 的數據(如儀表盤、用戶配置)不會丟失。
    • 可以根據需求調整存儲大小和 StorageClass
  4. Service

    • 使用 ClusterIP 類型,僅在集群內部暴露服務。
    • 如果需要外部訪問,可以通過 Ingress 暴露。
  5. Ingress(可選):

    • 使用 Ingresscert-manager 自動管理 TLS 證書。
    • 配置了 HTTPS 強制跳轉以提高安全性。

部署步驟

  1. 將上述 YAML 文件保存為 grafana-deployment.yaml

  2. 使用 kubectl 部署 Grafana:

    kubectl apply -f grafana-deployment.yaml
  3. 檢查部署狀態:

    kubectl get pods -n monitoring
    kubectl get svc -n monitoring
    kubectl get ingress -n monitoring
  4. 訪問 Grafana:

    • 如果使用了 Ingress,可以通過 https://grafana.example.com 訪問。

    • 如果未使用 Ingress,可以通過 kubectl port-forward 臨時訪問:

      kubectl port-forward svc/grafana -n monitoring 3000:3000

      然后訪問 http://localhost:3000


生產環境建議

  1. 使用固定版本的鏡像

    • 避免使用 latest 標簽,改為固定版本(如 grafana/grafana:9.5.2)。
  2. 啟用身份驗證

    • 配置 OAuth、LDAP 或 SAML 集成,避免使用默認的管理員賬號。
  3. 備份數據

    • 定期備份 Grafana 的持久化數據(如 /var/lib/grafana)。
  4. 監控 Grafana

    • 使用 Prometheus 監控 Grafana 的性能和健康狀態。

希望這個配置能幫助你順利部署 Grafana!如果有其他問題,請隨時告訴我。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/81110.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/81110.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/81110.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

農產品園區展示系統——仙盟創夢IDE開發

<!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>農業大數據平臺</title><style>* {margi…

每日Bug:(2)共享內存

對于整個系統而言&#xff0c;主存與CPU的資源都是有限的&#xff0c;隨著打開進程數量的增加&#xff0c;若是將所有進程運行所需的代碼/數據/棧/共享庫都存放在主存中&#xff0c;那么開啟一部分進程就可以將主存占用完。 虛擬內存就是解決以上問題的方法&#xff0c;使用虛…

C語言Makefile編寫與使用指南

Makefile 詳細指南&#xff1a;編寫與使用 Makefile 是 C/C 項目中常用的自動化構建工具&#xff0c;它定義了項目的編譯規則和依賴關系。下面我將詳細介紹 Makefile 的編寫和使用方法。 一、Makefile 基礎 1. 基本結構 一個典型的 Makefile 包含以下部分&#xff1a; mak…

Centos離線安裝Docker(無坑版)

1、下載并上傳docker離線安裝包 官方地址&#xff1a;安裝包下載 2、上傳到離線安裝的服務器解壓 tar -zxvf docker-28.1.1.tgz#拷貝解壓二進制文件到相關目錄 cp docker/* /usr/bin/ 3、創建docker啟動文件 cat << EOF > /usr/lib/systemd/system/docker.servic…

OceanBase數據庫-學習筆記4-租戶

租戶 租戶偏向于資源層面的邏輯概念&#xff0c;是在物理節點上劃分的資源單元&#xff0c;可以指定其資源規格&#xff0c;包括 CPU、內存、日志盤空間、IOPS 等。 租戶類似于傳統數據庫的數據庫實例&#xff0c;租戶通過資源池與資源關聯&#xff0c;從而獨占一定的資源配額…

UNIAPP項目記錄

一、通過 vue-cli 創建 uni-app 項目 創建 vue3 項目 創建以 javascript 開發的工程&#xff08;如命令行創建失敗&#xff0c;請直接訪問 gitee 下載模板&#xff09; npx degit dcloudio/uni-preset-vue#vite my-vue3-project復制代碼 npx degit dcloudio/uni-preset-vue#vit…

華為發布全球首個L3商用智駕ADS4.0

2024年10月2024世界智能網聯汽車大會上&#xff0c;余承東講到&#xff1a;“華為ADS 4.0將于2025年推出高速L3級自動駕駛商用及城區L3級自動駕駛試點&#xff0c;希望加快L3級自動駕駛標準的進程&#xff0c;推動L3級自動駕駛技術的普及。” 世界智能網聯汽車大會演講PPT 所以…

【Python學習路線】零基礎到項目實戰

目錄 &#x1f31f; 前言技術背景與價值當前技術痛點解決方案概述目標讀者說明 &#x1f9e0; 一、技術原理剖析核心概念圖解核心作用講解關鍵技術模塊說明技術選型對比 &#x1f4bb; 二、實戰演示環境配置要求核心代碼實現運行結果驗證 ? 三、性能對比測試方法論量化數據對比…

解決redis序列號和反序列化問題

最近遇到了一個問題,將 List<Map<String, Object>> 類型數據以list形式存入到redis之后,發現取出來時數據格式完全不對,根據報錯信息發現是反序列化問題,遇到類似問題,主要有兩種解決方案1.使用序列號工具 例如&#xff0c;Java中常用的序列化工具有Jackson、Gso…

Android學習總結之設計場景題

設計圖片請求框架的緩存模塊 核心目標是通過分層緩存策略&#xff08;內存緩存 磁盤緩存&#xff09;提升圖片加載效率&#xff0c;同時兼顧內存占用和存儲性能。以下是針對 Android 面試官的回答思路&#xff0c;結合代碼注釋說明關鍵設計點&#xff1a; 一、緩存架構設計&…

Webug3.0通關筆記14 第十四關:存儲型XSS

目錄 第十四關:存儲型XSS 1.打開靶場 2.源碼分析 3.滲透實戰 第十四關:存儲型XSS 本文通過《webug3靶場第十四關 存儲型XSS》來進行存儲型XSS關卡的滲透實戰。 存儲型 XSS&#xff08;Stored Cross - Site Scripting&#xff09;&#xff0c;也被稱為持久型 XSS&#xff…

Java父類、子類實例初始化順序詳解

1、完整的初始化順序&#xff08;含繼承&#xff09; 1、父類的靜態初始化 父類靜態變量默認值 → 父類靜態變量顯式賦值 父類靜態代碼塊&#xff08;按代碼順序執行&#xff09;。 2、子類的靜態初始化 子類靜態變量默認值 → 子類靜態變量顯式賦值 子類靜態代碼塊&…

13.組合模式:思考與解讀

原文地址:組合模式&#xff1a;思考與解讀 更多內容請關注&#xff1a;7.深入思考與解讀設計模式 引言 在軟件開發中&#xff0c;是否曾經遇到過這樣一種情況&#xff1a;你有一個對象&#xff0c;它本身很簡單&#xff0c;但是它包含了其他類似的對象。隨著系統變得越來越復…

OpenCV實戰教程 第一部分:基礎入門

第一部分&#xff1a;基礎入門 1. OpenCV簡介 什么是OpenCV及其應用領域 OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一個開源的計算機視覺和機器學習軟件庫&#xff0c;于1999年由Intel公司發起&#xff0c;現在由非營利組織OpenCV.org維護。Ope…

虛幻商城 Quixel 免費資產自動化入庫(2025年版)

文章目錄 一、背景二、問題講解1. Quixel 免費資產是否還能一鍵入庫?2. 是不是使用了一鍵入庫功能 Quixel 的所有資產就能入庫了?3. 一鍵入庫會入庫哪些資產?三、實現效果展示四、實現自動化入庫五、常見問題1. 出現401報錯2. 出現429報錯3. 入庫過于緩慢4. 入庫 0 個資產一…

uni-app - 小程序使用高德地圖完整版

文章目錄 ??功能描述??效果??開發環境??代碼部分??功能描述 頁面自動通過定位獲取用戶位置并展示周邊POI數據,同時支持關鍵詞輸入實時聯想推薦關聯地點信息, 實現精準智能的地點發現與檢索功能。 ??效果 ??開發環境 unibest2.5.4nodev18.20.5pnpm9.14.2wot-des…

牛客:AB4 逆波蘭表達式求值

鏈接&#xff1a;逆波蘭表達式求值_牛客題霸_牛客網 題解&#xff1a; 利用棧&#xff0c;遍歷字符串數組&#xff0c;遇到運算數則入棧&#xff0c;遇到運算符則取出棧頂兩個運算數進行運算&#xff0c;并將運算結果入棧。 class Solution { public:/*** 代碼中的類名、方法…

Ant(Ubuntu 18.04.6 LTS)安裝筆記

一、前言 本文與【MySQL 8&#xff08;Ubuntu 18.04.6 LTS&#xff09;安裝筆記】同批次&#xff1a;先搭建數據庫&#xff0c;再安裝JDK&#xff0c;后面肯定就是部署Web應用。其中Web應用的部署使用 Ant 方式&#xff0c;善始善終&#xff0c;特以筆記。 二、準備 &#xf…

ultralytics 目標檢測 混淆矩陣 背景圖像 沒被記錄

修改 utils/metrics.py ConfusionMatrix def process_batch(self, detections, gt_bboxes, gt_cls):"""Update confusion matrix for object detection task.Args:detections (Array[N, 6] | Array[N, 7]): Detected bounding boxes and their associated inf…

iview 如何設置sider寬度

iview layout組件中&#xff0c;sider設置了默認寬度和最大寬度&#xff0c;在css樣式文件中修改無效&#xff0c;原因是iview默認樣式設置在了element.style中&#xff0c;只能通過行內樣式修改 樣式如下&#xff1a; image.png image.png 修改方式&#xff1a; 1.官方文檔中寫…