iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
DevOps

關於我幫新公司建立整套部屬流程那檔事系列 第 26

EP26 - EKS 日誌蒐集使用 Loki 和 Grafana(二)

  • 分享至 

  • xImage
  •  

因為昨天發現我們在有個設定似乎有些問題
造成 Grafana 無法啟動
所以先將 values.yaml 的 pvc 註解起來

grafana:
  enabled: true
  sidecar:
    datasources:
      enabled: true
  # persistence:
  #   enabled: true
  #   type: pvc
  #   existingClaim: efs-grafana-claim
  image:
    tag: 7.5.0

然後再下一次更新

helm upgrade -f values.yaml grafana . --namespace=grafana

為 Grafana 配置 Ingress

這裡我們不使用 helm 來建立 ingress
主要原因還是 enable 後設定完
會是 apiversion 是 apiVersion: networking.k8s.io/v1
這會讓網頁的設定怪怪的
關於已經將程式碼 pull 下來
但這裡我還不知道要怎麼修改比較好QQ
所以 ingress 的部分就還是另外建立 yaml 檔來 apply

ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: grafana
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/wafv2-acl-arn:  "arn:aws:wafv2:ap-northeast-1:776212102166:regional/webacl/fundamental-acl/e571dcd7-ea0b-41fa-90db-f853d884e951"
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/healthcheck-path: '/'
    alb.ingress.kubernetes.io/subnets: 'subnet-00280de34952ba84c, subnet-058594e4949f7e8de, subnet-053a83439089c2886'
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/certificate-arn: "arn:aws:acm:ap-northeast-1:776212102166:certificate/28b6cabd-85d6-498c-aa91-1e4cd239dd3d"
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'

spec:
  rules:
    - http:
        paths:
          - path: /*
            backend:
              serviceName: ssl-redirect
              servicePort: use-annotation
          - path: /*
            backend:
              serviceName: grafana
              servicePort: 80
kubectl apply -f ingress.yaml

拿到登入密碼

kubectl get secret --namespace grafana grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

https://ithelp.ithome.com.tw/upload/images/20211008/20141518VvK6fsgjme.png

設定 Grafana

登入

https://ithelp.ithome.com.tw/upload/images/20211008/20141518HNdAb78YHh.png

確認 datasource

齒輪圖案的第一個選項
就是 datasource

https://ithelp.ithome.com.tw/upload/images/20211008/20141518W9spu5ErTu.png

Grafana 支援各種資料來源
而 Loki 只是其中一種
可以看到預設已經幫我們加進去了
https://ithelp.ithome.com.tw/upload/images/20211008/20141518mRXAhbgWqJ.png

新增 Dashboard

左側+可新增 Dashboard
https://ithelp.ithome.com.tw/upload/images/20211008/201415180DjXf3I5V4.png

選擇 Add an empty panel
https://ithelp.ithome.com.tw/upload/images/20211008/20141518AjMnVlmf9K.png

如果要顯示 Log 的話
右邊的 Visualization 選擇 Logs
並在下方查詢方法中輸入 json {namespace="ithome-ironman"}
可根據 namespace、pod、service 去查找 Log
https://ithelp.ithome.com.tw/upload/images/20211008/20141518fMlI3OXbPg.png

按下 Save 儲存
https://ithelp.ithome.com.tw/upload/images/20211008/20141518x2zqhoGYXx.png

最後再按下磁碟片圖案 Save Dashboard 即可儲存
https://ithelp.ithome.com.tw/upload/images/20211008/20141518zHYoJ65jte.png

因為當初部署的專案只是個簡單的 initial 專案
尚未配置任何的 Log 可供顯示
因此目前查無資料是正常狀況

添加 DNS 紀錄

剛剛我們建立完 Ingress 資訊後
在 EC2 的負載平衡器就會出現 load balance
此時我們就可以在自己的 DNS 添加一筆 CName Record 指向負載平衡器

關聯服務

寫在這裡其實有點奇怪
但,昨天就是忘了寫只好寫在這裡

簡單來說
我們用了 Grafana、Promtail 和 Loki 三個服務
只是透過 Helm Repository
幫我們把這些包好
功能開關及設定都參數化
讓我們可以很輕易的就將整個服務進行更版

Grafana

一個儀表板服務
整合各種 datasource
從自家的 Loki、各大廠的資料庫
到 Azure Monitor 和 AWS CloudWatch 都有支援
覺得資料庫權限不好卡
或是雲端供應商的 IAM 不好設定
不妨建立一個 Grafana 來接這些 Datasource
這樣可以只讓相關人士看到需要的資訊

Loki

就...會自動幫你把 Log 建立起來做索引
最後當 Datasource 來讓 Grafana 撈取資料

Promtail

會採集 K8S 環境中的 Log
並轉發到 Loki 裏面

其他設定

在部分情境的使用上
我們可能不見得會仰賴帳密
可能會使用 O365 或是 ldap 登入
我們如果將原始碼 pull 下來
就可以直接修改這設定了

/loki-stack/charts/grafana/values.yaml
搜尋 grafana.ini
在這區塊以下可以設定 Azure AD OAuth 登入
你沒看錯是 OAuth
不是 SAML Fedaration
所以實際上它在登入時會建立新的使用者


雖然我們不如預期
沒辦法將 EFS 掛載在 EKS 上
但所幸我們還是將 Grafana 架起來
並建立 Dashboard
明天老實說我還不知道要寫什麼
目前最後一哩路
大概就差 CloudWatch 和之前未填的坑

參考資料:

  1. Azure AD OAuth2 authentication
  2. LDAP Authentication
  3. 日誌系統 Loki - Promtail 詳解

上一篇
EP25 - EKS 日誌蒐集使用 Loki 和 Grafana(一)
下一篇
EP27 - 建立 VPN 連線,直接連線到 AWS
系列文
關於我幫新公司建立整套部屬流程那檔事30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言