星期六, 5月 20, 2023

yq with openSUSE Leap 15.2 使用小記

yq with openSUSE Leap 15.2 使用小記


openSUSE Leap 15.4

yq 4.18


最近在練習 GKE 的時候接觸到 yq 這個軟體

現在的設定檔愈來愈多 YAML 格式的檔案, 之前會用 jq 來處理 YAML 檔的內容

今天來試試看 yq :) 


yq 於 Github 介紹如下

  • a lightweight and portable command-line YAML, JSON and XML processor. yq uses jq like syntax but works with yaml files as well as json, xml, properties, csv and tsv. It doesn't yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously.


Github  https://github.com/mikefarah/yq

文件  https://mikefarah.gitbook.io/yq/


首先透過 zypper 來安裝 yq


# zypper  install  yq


Loading repository data...

Reading installed packages...

Resolving package dependencies...


The following 2 NEW packages are going to be installed:

  yq yq-bash-completion


2 new packages to install.

Overall download size: 2.3 MiB. Already cached: 0 B. After the operation,

additional 8.2 MiB will be used.

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


確認安裝的版本, 目前裝的是 4.18.1 的版本

  • 網路上有些教學文件, 可能是 3.x 的版本, 所以用法可能會不一樣


# rpm  -q  yq


yq-4.18.1-bp154.1.17.x86_64


測試的 yaml 內容如下


> cat  test.yml 

apples:

  collection:

  - name: Green

  - name: Blue

  favourite: Pink Lady


可以用 yq 來接 STDIN


> cat  test.yml  |  yq


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: Pink Lady


也可以直接用 yq


> yq  test.yml 


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: Pink Lady


我目前其實用的相對簡單


讀取某個特定的 value


> yq  '.apples.collection'  test.yml 


- name: Green

- name: Blue


> yq  '.apples.favourite'  test.yml 

Pink Lady


實務上使用的時候是要在 yaml 裏面找出一個欄位然後更新裡面的 value


假設我們要把 favourite 的 value 換成 sakana

現看看置換的結果


> yq  '.apples.favourite = "sakana" '  test.yml 


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: sakana


這個時候還沒有實際置換掉 favourtie 的 value


> cat  test.yml 


apples:

  collection:

  - name: Green

  - name: Blue

  favourite: Pink Lady


使用 -i ( --inplace ) 實際置換

  • -i, --inplace                       update the file inplace of first file given.


> yq  -i  '.apples.favourite = "sakana" '  test.yml

  • 要替代的 value 用雙引號 " " 括起來


> cat  test.yml 


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: sakana


也可以使用變數的方式來處理


> NAME=max  yq  -i  '.apples.favourite = strenv(NAME) '  test.yml


確認檔案內容


> cat  test.yml 


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: max


  • 這邊要注意的是, 因為是使用 strenv(NAME) 如果你的變數沒有跟 yq 在同一行, 可能會取不到正確的值, 如果是之前就宣告的, 要確認 > env | grep NAME 有看到, 或是透過 > export NAME 來處理


另外一種方式


> NAME=ines


> echo $NAME


ines


> yq  -i  '.apples.favourite = " '$NAME' " ' test.yml


  • 這邊要注意的是要替代的 value 用雙引號 " " 括起來, 然後變數使用單引號 ' ' 括起來


> cat  test.yml 


apples:

  collection:

    - name: Green

    - name: Blue

  favourite: ines


取代多欄位


> yq  -i   '.apples.favourite = "max"  |  .apples.collection[0].name = "red" '  test.yml

  • 使用 Pipe 符號分隔, 這邊多指定 collection 的第1個, name 的key 內容為 red


> cat test.yml 


apples:

  collection:

    - name: red

    - name: Blue

  favourite: max

 

最後來嘗試轉檔功能



> yq  -o  json  test.yml 


{

  "apples": {

    "collection": [

      {

        "name": "red"

      },

      {

        "name": "Blue"

      }

    ],

    "favourite": "max"

  }

}


其中我真的很喜歡 props 的輸出, 可以很清楚的列出架構


> yq  -o  props  test.yml 


apples.collection.0.name = red

apples.collection.1.name = Blue

apples.favourite = max



又多學了些東西


~ enjoy it




References


星期六, 5月 06, 2023

Kube No Trouble (kubent) with GKE 測試小記

Kube No Trouble (kubent) with GKE 測試小記


GKE

  • 1.25.7-gke.1000

  • 1.23.16-gke.1400


OS: openSUSE Leap 15.4

Helm: 3.11.2

kubent: 0.7.0


今天來測試 kubent ( Kube No Trouble )


這個在社群上看到朋友分享的, 會想測試這個是因為 Kubernetes 版本畢竟不像一般的 Linux OS, 也許會有比較長的維護期間, 版本的更替速度相對的快, 所以在升級之前就要檢查有沒有棄用停止的 API.


首先到 kubent 的 Github 頁面看說明


他上面的安裝是使用  sh -c "$(curl -sSL https://git.io/install-kubent)"  

但是我實際使用的時候有錯誤, 所以我是直接去抓程式下來執行


下載 kubent-0.7.0-linux-amd64.tar.gz


解壓縮檔案

> tar  zxvf  kubent-0.7.0-linux-amd64.tar.gz 


kubent


觀察檔案

>  ls


kubent  kubent-0.7.0-linux-amd64.tar.gz


使用方法很簡單, 如果你現在已經有 Kubernetes 的 cluster, 直接執行就可以


我先以之前測試的 GKE 1.23.16-gke.1400 版本為例

>  ./kubent 


11:16AM INF >>> Kube No Trouble `kubent` <<<

11:16AM INF version 0.7.0 (git sha d1bb4e5fd6550b533b2013671aa8419d923ee042)

11:16AM INF Initializing collectors and retrieving data

11:16AM INF Target K8s version is 1.23.16-gke.1400

11:16AM INF Retrieved 117 resources from collector name=Cluster

11:16AM INF Retrieved 0 resources from collector name="Helm v3"

11:16AM INF Loaded ruleset name=custom.rego.tmpl

11:16AM INF Loaded ruleset name=deprecated-1-16.rego

11:16AM INF Loaded ruleset name=deprecated-1-22.rego

11:16AM INF Loaded ruleset name=deprecated-1-25.rego

11:16AM INF Loaded ruleset name=deprecated-1-26.rego

11:16AM INF Loaded ruleset name=deprecated-future.rego

__________________________________________________________________________________________

>>> Deprecated APIs removed in 1.22 <<<

------------------------------------------------------------------------------------------

KIND                       NAMESPACE     NAME                                           API_VERSION                    REPLACE_WITH (SINCE)

CustomResourceDefinition   <undefined>   capacityrequests.internal.autoscaling.k8s.io   apiextensions.k8s.io/v1beta1   apiextensions.k8s.io/v1 (1.16.0)

CustomResourceDefinition   <undefined>   scalingpolicies.scalingpolicy.kope.io          apiextensions.k8s.io/v1beta1   apiextensions.k8s.io/v1 (1.16.0)

__________________________________________________________________________________________

>>> Deprecated APIs removed in 1.25 <<<

------------------------------------------------------------------------------------------

KIND                NAMESPACE     NAME                    API_VERSION      REPLACE_WITH (SINCE)

PodSecurityPolicy   <undefined>   gce.gke-metrics-agent   policy/v1beta1   <removed> (1.21.0)



再來測試現在測試的 GKE 1.25.7-gke.1000


> ./kubent 


11:14AM INF >>> Kube No Trouble `kubent` <<<

11:14AM INF version 0.7.0 (git sha d1bb4e5fd6550b533b2013671aa8419d923ee042)

11:14AM INF Initializing collectors and retrieving data

11:14AM INF Target K8s version is 1.25.7-gke.1000

11:15AM INF Retrieved 0 resources from collector name=Cluster

11:15AM INF Retrieved 0 resources from collector name="Helm v3"

11:15AM INF Loaded ruleset name=custom.rego.tmpl

11:15AM INF Loaded ruleset name=deprecated-1-16.rego

11:15AM INF Loaded ruleset name=deprecated-1-22.rego

11:15AM INF Loaded ruleset name=deprecated-1-25.rego

11:15AM INF Loaded ruleset name=deprecated-1-26.rego

11:15AM INF Loaded ruleset name=deprecated-future.rego


這邊就沒有出現 Deprecated API :)


他也有容器的版本, 但是我覺得不需要, 實際上測試容器版本的 kubent 也有權限的問題要調整, 所以直接下載執行應該就可以了


紀錄一下


~ enjoy it



References


星期六, 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