Grafana install for k8s

準備

k8s 集群: version >= 1.19.7

Yaml 編寫

Namespace

創(chuàng)建 grafana 運行所需的命名空間

apiVersion: v1  
kind: Namespace  
metadata:  
  name: grafana-system

存儲

創(chuàng)建 grafana 運行所需的存儲

---  
apiVersion: storage.k8s.io/v1  
kind: StorageClass  
metadata:  
  name: grafana-sc  
  namespace: grafana-system  
provisioner: kubernetes.io/no-provisioner  
volumeBindingMode: WaitForFirstConsumer  
---  
apiVersion: v1  
kind: PersistentVolume  
metadata:  
  name: grafana-pv  
  namespace: grafana-system  
spec:  
  storageClassName: grafana-sc  
  capacity:  
    storage: 1Gi  
  accessModes:  
    - ReadWriteOnce  
  hostPath:  
    path: /tmp/grafana  
---  
apiVersion: v1  
kind: PersistentVolumeClaim  
metadata:  
  name: grafana-pvc  
  namespace: grafana-system  
spec:  
  storageClassName: grafana-sc  
  accessModes:  
    - ReadWriteOnce  
  resources:  
    requests:  
      storage: 1Gi

Deployment

創(chuàng)建 grafana 運行所需的工作負載

apiVersion: apps/v1  
kind: Deployment  
metadata:  
  labels:  
    app: grafana  
  name: grafana  
  namespace: grafana-system  
spec:  
  selector:  
    matchLabels:  
      app: grafana  
  template:  
    metadata:  
      labels:  
        app: grafana  
    spec:  
      securityContext:  
        fsGroup: 472  
        supplementalGroups:  
          - 0  
      containers:  
        - name: grafana  
          image: grafana/grafana:9.1.0  
          imagePullPolicy: IfNotPresent  
          ports:  
            - containerPort: 3000  
              name: http-grafana  
              protocol: TCP  
          readinessProbe:  
            failureThreshold: 3  
            httpGet:  
              path: /robots.txt  
              port: 3000  
              scheme: HTTP  
            initialDelaySeconds: 10  
            periodSeconds: 30  
            successThreshold: 1  
            timeoutSeconds: 2  
          livenessProbe:  
            failureThreshold: 3  
            initialDelaySeconds: 30  
            periodSeconds: 10  
            successThreshold: 1  
            tcpSocket:  
              port: 3000  
            timeoutSeconds: 1  
          resources:  
            requests:  
              cpu: 250m  
              memory: 750Mi  
          volumeMounts:  
            - mountPath: /var/lib/grafana  
              name: grafana-pv  
      volumes:  
        - name: grafana-pv  
          persistentVolumeClaim:  
            claimName: grafana-pvc

修改掛載目錄權限

如果就這樣部署,會發(fā)生錯誤:

?  ~ kubectl logs -f grafana-xxx -n grafana-system
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migration-from-a-previous-version-of-the-docker-container-to-5-1-or-later
mkdir: cannot create directory '/var/lib/grafana/plugins': Permission denied

可以看出是由于沒有目錄權限引起的,我們可以通過修改目錄權限來解決這個問題,我們可以了利用一個 Job 來幫我們解決這個問題:

apiVersion: batch/v1  
kind: Job  
metadata:  
  name: grafana-chown  
  namespace: grafana-system  
spec:  
  template:  
    spec:  
      restartPolicy: Never  
      containers:  
        - name: grafana-chown  
          command: ["chown", "-R", "472:472", "/var/lib/grafana"]  
          image: busybox  
          imagePullPolicy: IfNotPresent  
          volumeMounts:  
            - name: grafana-pvc  
              mountPath: /var/lib/grafana  
      volumes:  
        - name: grafana-pvc  
          persistentVolumeClaim:  
            claimName: grafana-pvc

Service

創(chuàng)建 grafana 訪問所需的 service, 可以根據(jù)自己需求自行修改骨杂,我這里因為我的集群部署了 Ingress 控制器,所以使用 ClusterIP。

apiVersion: v1  
kind: Service  
metadata:  
  name: grafana  
  namespace: grafana-system  
spec:  
  ports:  
    - port: 3000  
      protocol: TCP  
      targetPort: http-grafana  
  selector:  
    app: grafana  
  sessionAffinity: None  
  type: ClusterIP

Ingress

創(chuàng)建 grafana 訪問所需的 ingress。

apiVersion: networking.k8s.io/v1  
kind: Ingress  
metadata:  
  name: grafana  
  namespace: grafana-system  
  annotations:  
    nginx.ingress.kubernetes.io/proxy-buffer-size: "32k"  
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-body-size: "512m"  
    nginx.ingress.kubernetes.io/ingress.class: 'nginx'  
spec:  
  ingressClassName: nginx  
  rules:  
    - host: grafana.test.com  
      http:  
        paths:  
          - path: /  
            pathType: Prefix  
            backend:  
              service:  
                name: grafana  
                port:  
                  number: 3000

完整 yaml

deploy.yaml

---  
apiVersion: v1  
kind: Namespace  
metadata:  
  name: grafana-system  
---  
apiVersion: storage.k8s.io/v1  
kind: StorageClass  
metadata:  
  name: grafana-sc  
  namespace: grafana-system  
provisioner: kubernetes.io/no-provisioner  
volumeBindingMode: WaitForFirstConsumer  
---  
apiVersion: v1  
kind: PersistentVolume  
metadata:  
  name: grafana-pv  
  namespace: grafana-system  
spec:  
  storageClassName: grafana-sc  
  capacity:  
    storage: 1Gi  
  accessModes:  
    - ReadWriteOnce  
  hostPath:  
    path: /tmp/grafana  
---  
apiVersion: v1  
kind: PersistentVolumeClaim  
metadata:  
  name: grafana-pvc  
  namespace: grafana-system  
spec:  
  storageClassName: grafana-sc  
  accessModes:  
    - ReadWriteOnce  
  resources:  
    requests:  
      storage: 1Gi  
---  
apiVersion: batch/v1  
kind: Job  
metadata:  
  name: grafana-chown  
  namespace: grafana-system  
spec:  
  template:  
    spec:  
      restartPolicy: Never  
      containers:  
        - name: grafana-chown  
          command: ["chown", "-R", "472:472", "/var/lib/grafana"]  
          image: busybox  
          imagePullPolicy: IfNotPresent  
          volumeMounts:  
            - name: grafana-pvc  
              mountPath: /var/lib/grafana  
      volumes:  
        - name: grafana-pvc  
          persistentVolumeClaim:  
            claimName: grafana-pvc  
---  
apiVersion: apps/v1  
kind: Deployment  
metadata:  
  labels:  
    app: grafana  
  name: grafana  
  namespace: grafana-system  
spec:  
  selector:  
    matchLabels:  
      app: grafana  
  template:  
    metadata:  
      labels:  
        app: grafana  
    spec:  
      securityContext:  
        fsGroup: 472  
        supplementalGroups:  
          - 0  
      containers:  
        - name: grafana  
          image: grafana/grafana:9.1.0  
          imagePullPolicy: IfNotPresent  
          ports:  
            - containerPort: 3000  
              name: http-grafana  
              protocol: TCP  
          readinessProbe:  
            failureThreshold: 3  
            httpGet:  
              path: /robots.txt  
              port: 3000  
              scheme: HTTP  
            initialDelaySeconds: 10  
            periodSeconds: 30  
            successThreshold: 1  
            timeoutSeconds: 2  
          livenessProbe:  
            failureThreshold: 3  
            initialDelaySeconds: 30  
            periodSeconds: 10  
            successThreshold: 1  
            tcpSocket:  
              port: 3000  
            timeoutSeconds: 1  
          resources:  
            requests:  
              cpu: 250m  
              memory: 750Mi  
          volumeMounts:  
            - mountPath: /var/lib/grafana  
              name: grafana-pv  
      volumes:  
        - name: grafana-pv  
          persistentVolumeClaim:  
            claimName: grafana-pvc  
---  
apiVersion: v1  
kind: Service  
metadata:  
  name: grafana  
  namespace: grafana-system  
spec:  
  ports:  
    - port: 3000  
      protocol: TCP  
      targetPort: http-grafana  
  selector:  
    app: grafana  
  sessionAffinity: None  
  type: ClusterIP  
---  
apiVersion: networking.k8s.io/v1  
kind: Ingress  
metadata:  
  name: grafana  
  namespace: grafana-system  
  annotations:  
    nginx.ingress.kubernetes.io/proxy-buffer-size: "32k"  
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"  
    nginx.ingress.kubernetes.io/proxy-body-size: "512m"  
    nginx.ingress.kubernetes.io/ingress.class: 'nginx'  
spec:  
  ingressClassName: nginx  
  rules:  
    - host: grafana.test.com  
      http:  
        paths:  
          - path: /  
            pathType: Prefix  
            backend:  
              service:  
                name: grafana  
                port:  
                  number: 3000

部署

# 部署 grafana
?  ~ kubectl apply -f grafana-deploy.yaml

觀察服務

查看存儲綁定狀態(tài)

?  ~ kubectl get -n grafana-system sc
NAME         PROVISIONER                    RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
grafana-sc   kubernetes.io/no-provisioner   Delete          WaitForFirstConsumer   false                  3h21m
?  ~ kubectl get -n grafana-system pv
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                        STORAGECLASS   REASON   AGE
grafana-pv   1Gi        RWO            Retain           Bound    grafana-system/grafana-pvc   grafana-sc              3h22m
?  ~ kubectl get -n grafana-system pvc
NAME          STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE
grafana-pvc   Bound    grafana-pv   1Gi        RWO            grafana-sc     3h22m

查看POD運行狀態(tài)

?  ~ kubectl get -n grafana-system pod
NAME                       READY   STATUS      RESTARTS   AGE
grafana-58445b6986-wx8tn   1/1     Running     1          3h23m
grafana-chown-x99bh        0/1     Completed   0          3h23m

查看 Serveice + Ingress

?  ~ kubectl get -n grafana-system svc
NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
grafana   ClusterIP   10.98.141.112   <none>        3000/TCP   3h24m
?  ~ kubectl get -n grafana-system ing
NAME      CLASS   HOSTS              ADDRESS       PORTS   AGE
grafana   nginx   grafana.test.com   10.99.6.241   80      3h20m

訪問 grafana

配置host

windows

文件路徑:C:\Windows\System32\drivers\etc

# 修改 hosts 文件
master ip grafana.test.com

linux & mac

sudo echo "master ip grafana.test.com" >> /etc/hosts

瀏覽器訪問: http://grafana.test.com

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市凌净,隨后出現(xiàn)的幾起案子悲龟,更是在濱河造成了極大的恐慌,老刑警劉巖冰寻,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件须教,死亡現(xiàn)場離奇詭異,居然都是意外死亡斩芭,警方通過查閱死者的電腦和手機轻腺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來划乖,“玉大人贬养,你說我怎么就攤上這事∏兮郑” “怎么了误算?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長迷殿。 經(jīng)常有香客問我儿礼,道長,這世上最難降的妖魔是什么庆寺? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任蚊夫,我火速辦了婚禮,結果婚禮上懦尝,老公的妹妹穿的比我還像新娘知纷。我一直安慰自己,他們只是感情好陵霉,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布屈扎。 她就那樣靜靜地躺著,像睡著了一般撩匕。 火紅的嫁衣襯著肌膚如雪鹰晨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天,我揣著相機與錄音模蜡,去河邊找鬼漠趁。 笑死,一個胖子當著我的面吹牛忍疾,可吹牛的內容都是我干的闯传。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼卤妒,長吁一口氣:“原來是場噩夢啊……” “哼甥绿!你這毒婦竟也來了?” 一聲冷哼從身側響起则披,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤共缕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后士复,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體图谷,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年阱洪,在試婚紗的時候發(fā)現(xiàn)自己被綠了便贵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡冗荸,死狀恐怖承璃,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情蚌本,我是刑警寧澤绸硕,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站魂毁,受9級特大地震影響玻佩,放射性物質發(fā)生泄漏。R本人自食惡果不足惜席楚,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一咬崔、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧烦秩,春花似錦垮斯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至抛寝,卻和暖如春熊杨,著一層夾襖步出監(jiān)牢的瞬間曙旭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工晶府, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留桂躏,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓川陆,卻偏偏與公主長得像剂习,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子较沪,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內容