星期六, 4月 22, 2023

Grafana with Helm in GKE 安裝小記

Grafana with Helm in  GKE 安裝小記


OS: openSUSE Leap 15.4

GKE: v1.25.7-gke.1000

Helm: 3.11.2


今天來寫 Grafana 的安裝, 這篇文章我們是使用 Helm 的方式來安裝 Grafana, 安裝在  GCP 的Kubernetes Engine ( GKE ) 上


Kubernetes 的部份是使用 GCP 的 GKE 服務, 目前在建立 GKE 有兩種模式

  • Autopilot

  • Standard



這次使用的是 Autopilot 的 GKE, 好處是不用去費心管理 Node, 專注在服務上, 計費的方式是以 Pod 來進行計費, 詳細資訊可以參考官方網站 https://cloud.google.com/kubernetes-engine/docs/concepts/autopilot-overview


我寫了簡單的 script 來建立 autopilot GKE 以及刪除 autopilot GKE, 可以參考以下資訊


使用上述的  gcp_create_Autopilot_gke.sh 建立 GKE cluster

  • 簡單來說就是透過 gcloud container clusters create-auto 指令來建立  cluster


建立完成之後, 接下來準備 Helm 套件, Helm 是 Kubernetes 的套件管理, 可以參考原廠網頁


首先安裝 helm 套件


# zypper  search  helm


Loading repository data...

Reading installed packages...


S | Name                                      | Summary                                                  | Type

--+-------------------------------------------+----------------------------------------------------------+-----------

  | ceph-csi-helm-charts                      | Ceph CSI helm charts                                     | package

  | helm                                      | The Kubernetes Package Manager                           | srcpackage

  | helm                                      | The Kubernetes Package Manager                           | package


於 openSUSE Leap 15.4 使用 zypper 安裝 helm


# zypper  install  helm


Loading repository data...

Reading installed packages...

Resolving package dependencies...


The following 2 NEW packages are going to be installed:

  helm helm-bash-completion


2 new packages to install.

Overall download size: 10.5 MiB. Already cached: 0 B. After the operation, additional 49.1 MiB will be used.

Continue? [y/n/v/...? shows all options] (y): y


觀察使用的版本

# helm  version


version.BuildInfo{Version:"v3.11.2", GitCommit:"d506314abfb5d21419df8c7e7e68012379db2354", GitTreeState:"clean", GoVersion:"go1.19.7"}


helm 的使用方法可以參考官方網站


Helm 的相關設定檔可以看看


觀察目前有的 helm repo

> helm  repo  list


NAME    URL                                      

kubecost https://kubecost.github.io/cost-analyzer/


接下來我們要使用 Helm 來安裝 Grafana

新增 Grafana 的 repo


> helm  repo  add  grafana  https://grafana.github.io/helm-charts


"grafana" has been added to your repositories


再次觀察

> helm  repo  list


NAME    URL                                      

kubecost https://kubecost.github.io/cost-analyzer/

grafana https://grafana.github.io/helm-charts


更新 helm repo


> helm  repo  update


Hang tight while we grab the latest from your chart repositories...

...Successfully got an update from the "kubecost" chart repository

...Successfully got an update from the "grafana" chart repository

Update Complete. ⎈Happy Helming!⎈


還沒有安裝 grafana 前觀察資訊

> helm  list


NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION


使用 helm 安裝 grafana

> helm   install  my-release  grafana/grafana


W0422 12:32:32.570130   11064 warnings.go:70] Autopilot set default resource requests for Deployment default/my-release-grafana, as resource requests were not specified. See http://g.co/gke/autopilot-defaults

NAME: my-release

LAST DEPLOYED: Sat Apr 22 12:32:30 2023

NAMESPACE: default

STATUS: deployed

REVISION: 1

NOTES:

1. Get your 'admin' user password by running:


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



2. The Grafana server can be accessed via port 80 on the following DNS name from within your cluster:


   my-release-grafana.default.svc.cluster.local


   Get the Grafana URL to visit by running these commands in the same shell:

     export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=my-release" -o jsonpath="{.items[0].metadata.name}")

     kubectl --namespace default port-forward $POD_NAME 3000


3. Login with the password from step 1 and the username: admin

#################################################################################

######   WARNING: Persistence is disabled!!! You will lose your data when   #####

######            the Grafana pod is terminated.                            #####

#################################################################################




觀察資訊

> helm list


NAME      NAMESPACE REVISION UPDATED                                STATUS  CHART         APP VERSION

my-release default  1       2023-04-22 12:32:30.125892779 +0800 CST deployed grafana-6.54.0 9.4.7


pod 準備要一點時間, 可以透過 kubectl 觀察

  • 因為我們是採取 Autopilot  GKE, 所以是有相關需求的時候, GKE 會進行資源的調度, 相對於如果使用 Standard Cluster 如果沒有在 autoscaler 設定完善, 可以節省相關的費用


可以透過 kubectl get nodes 來觀察

> kubectl  get  nodes


NAME                                          STATUS   ROLES    AGE    VERSION

gk3-test-cluster-default-pool-8aebbb37-0xms   Ready    <none>   55m    v1.25.7-gke.1000

gk3-test-cluster-default-pool-ded2be35-sxb7   Ready    <none>   55m    v1.25.7-gke.1000

gk3-test-cluster-nap-179b5mkg-77a5d3c0-rlc7   Ready    <none>   114s   v1.25.7-gke.1000


參考剛剛安裝的輸出說明

admin 密碼取得

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


取得 Grafana URL 來連接


> export  POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=my-release" -o jsonpath="{.items[0].metadata.name}")


觀察資訊

> echo  $POD_NAME


my-release-grafana-855869984c-g82pb


但是如果有  kubectl 指令可以直接使用  kubectl  get  pod  觀察

> kubectl get pod


NAME                                  READY   STATUS    RESTARTS   AGE

my-release-grafana-855869984c-g82pb   1/1     Running   0          16m


進行 Port Forward

> kubectl --namespace default port-forward $POD_NAME 3000


Forwarding from 127.0.0.1:3000 -> 3000

Forwarding from [::1]:3000 -> 3000


連接 Grafana 介面

開啟瀏覽器 http://localhost:3000






這個時候會看到登入畫面


請使用 admin 使用者登入, 密碼為剛剛提示的 kubectl get secret --namespace default my-release-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo 來取得


登入畫面如下



當然還是要記得先換密碼



使用 helm 安裝 grafana  的同時, 他也設定了 service


可以透過 kubectl 指令來觀察

> kubectl  get  services


NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE

kubernetes           ClusterIP   10.93.0.1     <none>        443/TCP   153m

my-release-grafana   ClusterIP   10.93.1.112   <none>        80/TCP    109m


  • 這邊是使用 ClusterIP 的方式來建立服務


所以我們剛剛連接的方式是使用 Port Forward 的方式, 來進行連接, 那如果我們想要直接從網路上來接該如何呢?

這個部份, 在 GKE 的 console 上面就可以簡單按幾個按鈕就可以設定 Ingress

  • 也可以簡單的從 GKE console 上面把 workload expose 為 LB 類型的 Service, 也是一種方式


來嘗試建立 Ingress


登入 GCP 切換到 Kubernetes Engine 的  Services & Ingress 頁面

勾選剛剛我們建立的 my-release-grafana 服務

點選 CREATE INGRESS



輸入我們要設定的 ingress 名稱  (例如 my-release-grafana)

點選 Host and path rules


右方的視窗 Backends 點選下拉式選單

點選 剛剛的服務


這個時候可以觀察到 Host and path rules 就有設定一個 rule

點選 CREATE

接下來就會開始建立 Ingress

建立完成後就可以嘗試, 直接使用 IP 的方式來進行連接

  • 如果沒有對外服務, 記得設定 firewall




這樣有沒有感受到 GKE 方便的地方 :)


實驗完成記得刪除所有的服務與資源, 以達到節費的目的

  • > helm delete my-release

    • 剛剛的 ingress 不是 helm 建立的所以不會刪除, 要手動刪除( 如果只用這個指令的話 )

  • 使用 gcp_delete_gke.sh 刪除 cluster


~ enjoy it




References