Slide 1

Slide 1 text

使 用 Kong 與 GitOps 來管理您企業的 API 呼叫 Johnny Sung 2024 大

Slide 2

Slide 2 text

Full stack developer Johnny Sung (宋岡諺) https://fb.com/j796160836 https://blog.jks.co ff ee/ https://www.slideshare.net/j796160836 https://github.com/j796160836

Slide 3

Slide 3 text

大 綱 (1/2) •組織流程 面 •Kong 介紹 •GitOps 介紹 •API lifecycle 與 API 治理 2024 大

Slide 4

Slide 4 text

大 綱 (2/2) •Kong 的服 用 方 式 •kong.yml 設定檔上 手 •純 Docker 安裝 •搭配 Kubernetes •Kong 文 件沒寫清楚的地 方 2024 大

Slide 5

Slide 5 text

這邊使 用 Open Source 版本的 Kong 來舉例 Disclaimer 攝影師:Ihsan Adityawarman: https://www.pexels.com/zh-tw/photo/1056251/ 你也可以 用 其他類似功能的軟體來替代, 概念是類似相通的 我不是 Kong 的員 工 或協 力 廠商,我只是 一 個使 用 者做分享

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

一 切的故事 就從 一 個 水龍 頭開始講起....

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

一 個 水龍 頭的背後,當然沒有這麼簡單

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

當你服務越來越多的時候 系統越來越複雜的時候

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

https://www.baeldung.com/kong 原本 API 的世界...

Slide 14

Slide 14 text

https://medium.com/@far3ns/kong-the-microservice-api-gateway-526c4ca0cfa6

Slide 15

Slide 15 text

What is Kong? Kong API Gateway 是 一 款開源的 API 管理平台, 用 於處理 大 規模的 API 流量。它 支 援多 種微服務架構,提供負載均衡、 日 誌記錄、認證和授權等功能。Kong 可以 高 效地管理 RESTful API 和服務之間的互動,並且可以輕鬆擴展以應對 高 流量需求。它 支 援插件擴 充,允許 用戶 根據特定需求客制功能,如安全性、監控和管理。它 支 援多種通訊協議,包 括 HTTP, HTTPS, gRPC, WebSocket 等,並且能夠輕鬆擴展以應對 大 規模的流量。 https://seeklogo.com/vector-logo/394595/kong

Slide 16

Slide 16 text

https://www.baeldung.com/kong

Slide 17

Slide 17 text

https://crestsolution.com/wp-content/uploads/2021/08/Kong-GS-overview-1536x861.png

Slide 18

Slide 18 text

GitOps 介紹

Slide 19

Slide 19 text

What is GitOps? GitOps 是 一 種 用 於基礎架構和應 用 配置管理的技術 方 法,透過 Git 作為版本控 制系統來 自 動化和管理。它使得部署和運維過程變得更加透明、可追蹤及可重 現。在 GitOps 的實踐中,所有的配置 文 件都存儲在 Git 倉庫中,這樣任何的 更改都必須通過 Git 的提交和審核流程。當配置 文 件被更新後, 自 動化的 工 具 會檢測到變更並將新配置應 用 到 生 產環境中,從 而 保證系統的持續性和 一 致 性。 https://foxutech.com/lets-understand-about-gitops/

Slide 20

Slide 20 text

https://www.atlassian.com/blog/bitbucket/5-pull-request-must-haves

Slide 21

Slide 21 text

https://konghq.com/solutions/mobile-application-api-development

Slide 22

Slide 22 text

•版本控制和審計跟蹤:使 用 Git 作為單 一 資訊來源 (Single source of truth), 確保所有變更都有記錄和可追溯。 •持續部署: 自 動化的部署流程減少 手 動錯誤,加快交付速度。 • 一 致性和標準化:確保環境之間的設置 一 致性,降低複雜性。 •增強安全:透過 Pull Request 和 Code Review 強化變更的安全性和合規性。 •易於回滾和錯誤恢復:如果部署失敗或是有問題,可以輕鬆回滾 (Rollback) 到先前的穩定版本。 GitOps 的優點

Slide 23

Slide 23 text

https://assets.prd.mktg.konghq.com/ fi les/2021/10/APIOps-Automating-API-Lifecycle-with-DevOps-and-GitOps.pdf

Slide 24

Slide 24 text

•計劃與設計:定義 API 的 目 標和功能、 目 標 用戶 是誰、以及他們將如何存取。 設計 API Endpoint點,並確定如何處理資料和認證。 •開發與測試:實際寫程式開發 API、整合與初步的功能測試。測試階段則需要 確保 API 在各種情況下都能正常運作,包括壓 力 測試、安全性測試和功能測 試。 •部署:將 API 部署到 Production 環境,這可能是 一 個雲端服務、或者地端伺 服器。在部署時,架構設計負載均衡與容錯機制,以確保 高 可 用 性 (High Availability) 和擴展性 (Scalability)。 API 的 生 命週期 (1/2)

Slide 25

Slide 25 text

•版本管理:管理 API 版本是重要的,特別是當你需要導 入 破壞性變更時。提供 版本控制可以幫助 用戶 過渡到新版本,同時也 支 援舊版本。 •淘汰:當 API 不再被需要或有更好的替代 方 案時,進 行 API 的淘汰。淘汰過程 應該提供 足 夠的過渡期給使 用 者,並清楚溝通淘汰的時間表和替代 方 案。 API 的 生 命週期 (2/2)

Slide 26

Slide 26 text

https://sandbox.cribl.io/coursedocs/gitops/docs/8-pull-request

Slide 27

Slide 27 text

https://sandbox.cribl.io/coursedocs/gitops/docs/8-pull-request

Slide 28

Slide 28 text

既然 Kong 很不賴 我要怎麼開始? 🤔

Slide 29

Slide 29 text

https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-con fi g/

Slide 30

Slide 30 text

快速導覽 kong.yml 寫法

Slide 31

Slide 31 text

Kong.yml 架構 Services Routes Upstreams Consumers Targets Plugins root username (或) custom_id

Slide 32

Slide 32 text

Kong.yml 架構 Services Routes Upstreams Targets Consumers

Slide 33

Slide 33 text

Service & Route https://docs.konghq.com/gateway/3.4.x/get-started/services-and-routes/

Slide 34

Slide 34 text

不知道 kong.yml 怎麼寫? 🥸

Slide 35

Slide 35 text

小 撇步💡 •新增 一 個帶有 DB 的 Kong,使 用 GUI 介 面 操作 •使 用 decK 指令匯出設定檔 docker run --rm kong/deck:v1.34.0 gateway dump \ --kong-addr http://192.168.1.3:8001/ > kong.yaml

Slide 36

Slide 36 text

kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true [

Slide 37

Slide 37 text

kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true [ kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true

Slide 38

Slide 38 text

kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true

Slide 39

Slide 39 text

kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true protocol: http host: my-web-service.myapp.svc.cluster.local path: / port: 80 retries: 5 connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 routes: - name: my-web-route protocols: - http - https paths: - /my-web path_handling: v0 https_redirect_status_code: 426 preserve_host: true regex_priority: 0 request_buffering: false response_buffering: false strip_path: true [ [ ]

Slide 40

Slide 40 text

Kong 運作模式 Docker Kubernetes (K8s) Database mode (via PostgreSQL) DB-less mode (Declarative config mode)

Slide 41

Slide 41 text

in

Slide 42

Slide 42 text

Created by Alice Design from the Noun Project PROD UAT https://thenounproject.com/icon/user-1994976/ https://www.svgrepo.com/svg/353929/jenkins https://en.wikiversity.org/wiki/Version_Control/Git https://en.m.wikipedia.org/wiki/File:Ansible_logo.svg Kong in Docker 架構 Commit Webhook Deploy Deploy

Slide 43

Slide 43 text

實驗環境 •Git repository — Gitea •Environment— Docker VM •CI/CD pipeline tool — Jenkins & Ansible •Image Registry — Harbor

Slide 44

Slide 44 text

version: '3.9' services: kong: image: "${KONG_DOCKER_TAG:-kong:latest}" restart: always environment: KONG_DATABASE: off KONG_ADMIN_ACCESS_LOG: /dev/stdout KONG_ADMIN_ERROR_LOG: /dev/stderr KONG_PROXY_LISTEN: "${KONG_PROXY_LISTEN:-0.0.0.0:8000}" KONG_ADMIN_LISTEN: "${KONG_ADMIN_LISTEN:-0.0.0.0:8001}" KONG_ADMIN_GUI_LISTEN: "${KONG_ADMIN_GUI_LISTEN:-0.0.0.0:8002}" KONG_PROXY_ACCESS_LOG: /dev/stdout KONG_PROXY_ERROR_LOG: /dev/stderr KONG_PREFIX: ${KONG_PREFIX:-/var/run/kong} KONG_DECLARATIVE_CONFIG: "/opt/kong/kong.yaml" KONG_ADMIN_GUI_URL: http://host.docker.internal:8012 KONG_ADMIN_GUI_API_URL: http://host.docker.internal:8011 ports: - "${KONG_INBOUND_PROXY_LISTEN:-0.0.0.0}:8000:8000/tcp" - "8001:8001/tcp" - "8002:8002/tcp" volumes: - ./kong.yaml:/opt/kong/kong.yaml security_opt: - no-new-privileges https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-con fi g/ Kong in DB-less mode (via docker compose) 設定重點 •image 位置(如果是 private registry) •環境變數設定 •設定 kong.yml 路徑 KONG_DATABASE: off docker-compose.yml

Slide 45

Slide 45 text

小 撇步💡 •在 Docker container 中,可以 用 以下內部 domain name 代表宿主本 身 host.docker.internal

Slide 46

Slide 46 text

in

Slide 47

Slide 47 text

Created by Alice Design from the Noun Project PROD UAT https://thenounproject.com/icon/user-1994976/ https://www.svgrepo.com/svg/353929/jenkins https://en.wikiversity.org/wiki/Version_Control/Git https://en.m.wikipedia.org/wiki/File:Ansible_logo.svg https://www.opsmx.com/what-is-argocd/ Kong in Kubernates 架構 Commit Monitoring Deploy Deploy

Slide 48

Slide 48 text

小 撇步💡 •在 Kubernetes 中,可以善 用 K8s 自 帶的 DNS 路徑, 快速找到對應的 Service my-web-service.myapp.svc.cluster.local (Service 名稱) (Namespace 名稱)

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

以下會有很多 Kubernetes 部署相關的設定 有很多幫助入睡的成份 如果半夜睡不著覺,可以在屋頂看看這 一 段( 大 誤) 可以準備 一 杯咖啡 ☕ https://pixabay.com/photos/cat-animal-feline-portrait-kitty-6492741/

Slide 53

Slide 53 text

如果對於新 手 ,K8s 原理極其複雜 難易度很難降下來 如果後半段聽不懂很正常,你不孤單 會 K8s 的這群 人 都是怪物(稱讚貌)

Slide 54

Slide 54 text

實驗環境 •Git repository — Gitea •Environment— 純地端 Kubernetes (K8s) (開三台 Ubuntu 22.04.1 VM 使 用 kubeadm 安裝) •CI/CD pipeline tool — Jenkins •Image Registry — Harbor •GitOps tool — ArgoCD 當然你 用 雲端也可以

Slide 55

Slide 55 text

https://mrdevops.hashnode.dev/kubernetes-architecture

Slide 56

Slide 56 text

ArgoCD 是 一 個開源的 Kubernetes 原 生 持續交付 工 具,專注於 GitOps 自 動化 部署。它利 用 Git 儲存庫作為應 用 配置的「單 一 資訊來源 (Single source of truth)」, 自 動檢測配置變更並將其同步到指定的 Kubernetes 集群中。 ArgoCD 支 援多種配置管理 工 具,包括 Helm、Kustomize 和 Jsonnet,並提 供視覺化界 面 便於監控應 用 部署狀態和健康狀況。這使得開發者和 維運團隊能夠有效地實現持續部署和管理。 ArgoCD - 在 K8s 實現 GitOps 的幕後功 臣 https://www.opsmx.com/what-is-argocd/

Slide 57

Slide 57 text

Kustomize Kustomize 是 一 個 Kubernetes 的配置管理 工 具,可以透過定制資源的配置來 簡化 Kubernetes 的部署。它專注於以聲明式 方 式修改和管理 Kubernetes manifest 檔案,不需要動態 生 成配置。使 用 者可以建 立 基礎配置的 "基底", 然後在不同環境(如開發、測試和 生 產)中進 行 客製化覆蓋。Kustomize 允許 合併或替換 YAML 檔案的部分,使得配置更加模組化和可重 用 。它現在是 Kubernetes 的 一 部分,可以直接透過 kubectl 命令 行工 具使 用 。 https://zlaval.medium.com/kustomize-template-free-kubernetes-application-management-3d70ca9d2e05

Slide 58

Slide 58 text

Kustomize 檔案架構 https://thenounproject.com/icon/ fi le-6897025/ https://thenounproject.com/icon/puzzle-6850847/ deployment.yml services.yml config-map.yml … kustomization.yaml

Slide 59

Slide 59 text

Helm Helm 是 一 個 用 於 Kubernetes 的套件管理 工 具,允許開發者和運維團隊打包、 配置和部署服務。Helm 使 用 稱為 "Charts" 的配置 文 件來描述 一 組相關的 Kubernetes 資源,這些資源可以預先配置並重複使 用 。透過 Helm, 用 戶 可以輕 鬆地安裝、升級和管理 Kubernetes 應 用 ,並 支 援版本控制和回滾功能,使得部 署和維護變得更加 方 便和有效。 https://helm.sh/

Slide 60

Slide 60 text

Created by Mas Mirza from Noun Project values.yml • deployment.yml • services.yml • rbac.yml • config-map.yml • …. Helm 檔案架構 https://thenounproject.com/icon/ fi le-6897025/ https://thenounproject.com/icon/puzzle-6850847/

Slide 61

Slide 61 text

https://artifacthub.io/packages/helm/kong/kong 小 撇步💡 •你可以好好利 用 ArtifactHUB 幫你分析 Helm Charts 裡 面 的模板內容

Slide 62

Slide 62 text

https://artifacthub.io/packages/helm/kong/kong

Slide 63

Slide 63 text

https://artifacthub.io/packages/helm/kong/kong

Slide 64

Slide 64 text

https://artifacthub.io/packages/helm/kong/kong

Slide 65

Slide 65 text

Helm 相關指令 helm show values kong/kong > kong-values.yaml helm repo add kong https://charts.konghq.com 加 入 helm repo 取得預設 values.yaml 參數 安裝 kong helm install kong kong/kong -n kong-dbless -f kong-values.yaml

Slide 66

Slide 66 text

小 撇步💡 •你可以利 用 helm template 輸出原始 yaml 設定檔做 比 對 helm template kong kong/kong -n kong-dbless -f kong-values.yaml --output-dir ./kong-yamls

Slide 67

Slide 67 text

我還是對 Helm 指令不太熟耶... 🥸

Slide 68

Slide 68 text

https://github.com/JohnnyWorks-TW/vue-helm-cli-helper 可以服 用 我寫的 Helm Chart 小 助 手 😎

Slide 69

Slide 69 text

apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: kong-dbless-config namespace: argocd spec: project: default source: repoURL: 'ssh://[email protected]/kustomize.git' path: kong-config-dbless targetRevision: HEAD destination: name: '' server: 'https://kubernetes.default.svc' namespace: kong-dbless sources: [] syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true argocd-application.yaml kong-config-dbless ├── kong.yml └── kustomization.yaml 資料夾結構 ArgoCD 設定 Kong 設定檔專案

Slide 70

Slide 70 text

kong.yml _format_version: "3.0" services: - name: my-web-service enabled: true host: my-web-service.myapp.svc.cluster.local path: / port: 80 protocol: http connect_timeout: 60000 read_timeout: 60000 write_timeout: 60000 retries: 5 routes: - name: my-web-route https_redirect_status_code: 426 path_handling: v0 paths: - /my-web preserve_host: true protocols: - http - https regex_priority: 0 request_buffering: false response_buffering: false strip_path: true apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization configMapGenerator: - name: kong-config files: - kong.yml generatorOptions: disableNameSuffixHash: true kustomization.yaml 使 用 con fi gMapGenerator 收納 kong.yml 到 Con fi gMap

Slide 71

Slide 71 text

apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: kong-dbless namespace: argocd spec: project: default source: repoURL: 'ssh://[email protected]/kustomize.git' path: helm-kong targetRevision: HEAD helm: valueFiles: - kong-values.yml destination: name: '' namespace: kong-dbless server: 'https://kubernetes.default.svc' sources: [] syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true argocd-application.yaml helm-kong ├── Chart.yaml └── kong-values.yml 資料夾結構 ArgoCD 設定 Kong helm 專案

Slide 72

Slide 72 text

這邊有 一 個重點 使 用 dependency charts 使 用一 個 自 訂的名字把 values 給包起來 https://akuity.io/blog/argo-cd-helm-values- fi les/ apiVersion: v2 name: kong description: kong type: application version: 1.0.0 appVersion: '1.0' dependencies: - name: kong repository: https://charts.konghq.com version: 2.34.0 Chart.yaml kong-values.yml kong: # Default values for Kong's Helm Chart. # Declare variables to be passed into your templates. # # Sections: # - Deployment parameters # - Kong parameters # - Ingress Controller parameters # - Postgres sub-chart parameters # - Miscellaneous parameters # - Kong Enterprise parameters # ----------------------------------------------------------------------------- # Deployment parameters # ----------------------------------------------------------------------------- deployment: kong: # Enable or disable Kong itself # Setting this to false with ingressController.enabled=true will create a # controller-only release. enabled: true ## Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, ## for it to be considered available. # minReadySeconds: 60 ## Specify the service account to create and to be assigned to the deployment / daemonset and for the migrations serviceAccount: create: true # Automount the service account token. By default, this is disabled, and the token is only mounted on the controller # container. Some sidecars require enabling this. Note that enabling this exposes Kubernetes credentials to Kong # Lua code, increasing potential attack surface. automountServiceAccountToken: false ## Optionally specify the name of the service account to create and the annotations to add. # name: # annotations: {} ## Optionally specify any extra sidecar containers to be included in the deployment ## See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#container-v1-core # sidecarContainers: # - name: sidecar # image: sidecar:latest # initContainers: # - name: initcon # image: initcon:latest # hostAliases: # - ip: "127.0.0.1" # hostnames: # - "foo.local" # - "bar.local" …(略) ⚠ 注意縮排 values.yml

Slide 73

Slide 73 text

image: repository: kong/kong tag: "3.5" pullPolicy: IfNotPresent 如果有 private registry 可以在此設定 Kong in DB-less mode (kong-values.yml) values.yml

Slide 74

Slide 74 text

# Specify Kong proxy service configuration proxy: enabled: true type: NodePort loadBalancerClass: nameOverride: "" annotations: {} labels: enable-metrics: "true" http: enabled: true servicePort: 80 containerPort: 8000 nodePort: 30080 parameters: [] tls: enabled: false servicePort: 443 containerPort: 8443 nodePort: 32443 parameters: - http2 設定 proxy 服務連接埠 (假設為 30080 NodePort) Kong in DB-less mode (kong-values.yml) values.yml

Slide 75

Slide 75 text

設定 Admin API 連接埠 (假設為 30071 NodePort) admin: enabled: true type: NodePort http: enabled: true servicePort: 8001 containerPort: 8001 nodePort: 32071 parameters: [] tls: enabled: false servicePort: 8444 containerPort: 8444 設定管理後台 (WebGUI) 連接埠 (假設為 30072 NodePort) manager: enabled: true type: NodePort loadBalancerClass: annotations: {} labels: {} http: enabled: true servicePort: 8002 containerPort: 8002 nodePort: 32072 parameters: [] tls: enabled: false servicePort: 8445 containerPort: 8445 parameters: - http2 Kong in DB-less mode (kong-values.yml) values.yml

Slide 76

Slide 76 text

## Define any volumes and mounts you want present in the Kong proxy container userDefinedVolumes: - name: "kong-config-vol" configMap: name: "kong-config" userDefinedVolumeMounts: - name: "kong-config-vol" mountPath: "/opt/kong/config" env: database: "off" declarative_config: /opt/kong/config/kong.yml apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization configMapGenerator: - name: kong-config files: - kong.yml generatorOptions: disableNameSuffixHash: true kustomization.yaml 這裏就是 文 件沒寫清楚的地 方 🧐 設定將(裡 面 有 kong.yml 的)Con fi gMap 掛載起來 設定環境變數, 設定 kong.yml 的位置 Kong in DB-less mode (kong-values.yml) values.yml

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

現在有 一 個問題 如何監控 Con fi g 變動 當 Con fi g 一 有變動,就更新部署 Kong?

Slide 80

Slide 80 text

https://github.com/stakater/Reloader

Slide 81

Slide 81 text

kind: Deployment metadata: annotations: reloader.stakater.com/auto: "true" spec: template: metadata: 照著 文 件將它 Deploy 使 用 annotations 指定 con fi gMap 名稱就可以做到監聽! 接下來就是把它塞 入 Kong 的 helm chart values 中,加 入 Custom Annotations 部署 Reloader

Slide 82

Slide 82 text

https://artifacthub.io/packages/helm/kong/kong?modal=template&template=deployment.yaml --- # Source: kong/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: kong-dbless-kong namespace: default labels: app.kubernetes.io/name: kong helm.sh/chart: kong-2.35.1 app.kubernetes.io/instance: "kong-dbless" app.kubernetes.io/managed-by: "Helm" app.kubernetes.io/version: "3.5" app.kubernetes.io/component: app annotations: configmap.reloader.stakater.com/reload: "kong-config" # Annotations to be added to Kong deployment deploymentAnnotations: configmap.reloader.stakater.com/reload: "kong-config" 加 入 reloader 的 Custom Annotations,綁定重啟條件 測試 yaml 輸出結果

Slide 83

Slide 83 text

當 Con fi g 一 有變動,就更新部署 Kong

Slide 84

Slide 84 text

Monitoring https://thenounproject.com/icon/monitoring-6880512/

Slide 85

Slide 85 text

幾乎開箱即 用 的 Prometheus plugins plugins: - instance_name: prometheus name: prometheus enabled: true protocols: - grpc - grpcs - http - https config: bandwidth_metrics: true latency_metrics: true per_consumer: true status_code_metrics: true upstream_health_metrics: true •打開全域的 Prometheus plugins

Slide 86

Slide 86 text

幾乎開箱即 用 的 Prometheus plugins •打開全域的 Prometheus plugins •在 Kong API Endpoint 就可以看到 Metrics 了 # HELP kong_bandwidth_bytes Total bandwidth (ingress/egress) throughput in bytes # TYPE kong_bandwidth_bytes counter kong_bandwidth_bytes{service="example",route="example-route",direction="egress",consumer=""} 546 kong_bandwidth_bytes{service="example",route="example-route",direction="ingress",consumer=""} 2461 kong_bandwidth_bytes{service="example.com",route="example-route",direction="egress",consumer=""} 2448 kong_bandwidth_bytes{service="example.com",route="example-route",direction="ingress",consumer=""} 7461 # HELP kong_datastore_reachable Datastore reachable from Kong, 0 is unreachable # TYPE kong_datastore_reachable gauge kong_datastore_reachable 1 # HELP kong_http_requests_total HTTP status codes per consumer/service/route in Kong # TYPE kong_http_requests_total counter kong_http_requests_total{service="example",route="example-route",code="502",source="kong",consumer=""} 1 kong_http_requests_total{service="example.com",route="example-route",code="200",source="service",consumer=""} 1 kong_http_requests_total{service="example.com",route="example-route",code="404",source="service",consumer=""} 2 # HELP kong_kong_latency_ms Latency added by Kong and enabled plugins for each service/route in Kong # TYPE kong_kong_latency_ms histogram kong_kong_latency_ms_bucket{service="example",route="example-route",le="+Inf"} 1 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="15"} 1 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="20"} 1 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="30"} 2 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="50"} 2 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="75"} 2 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="100"} 2 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="200"} 2 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="500"} 3 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="750"} 3 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="1000"} 3 kong_kong_latency_ms_bucket{service="example.com",route="example-route",le="+Inf"} 3 kong_kong_latency_ms_count{service="example",route="example-route"} 1 kong_kong_latency_ms_count{service="example.com",route="example-route"} 3 kong_kong_latency_ms_sum{service="example",route="example-route"} 11977 kong_kong_latency_ms_sum{service="example.com",route="example-route"} 532 # HELP kong_memory_lua_shared_dict_bytes Allocated slabs in bytes in a shared_dict # TYPE kong_memory_lua_shared_dict_bytes gauge kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong",kong_subsystem="http"} 45056 kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_cluster_events",kong_subsystem="h kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_core_db_cache",kong_subsystem="ht kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_core_db_cache_miss",kong_subsyste kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_db_cache",kong_subsystem="http"} kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_db_cache_miss",kong_subsystem="ht kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_healthchecks",kong_subsystem="htt kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_locks",kong_subsystem="http"} 614 kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_rate_limiting_counters",kong_subs kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_secrets",kong_subsystem="http"} 4 kong_memory_lua_shared_dict_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="prometheus_metrics",kong_subsystem="ht # HELP kong_memory_lua_shared_dict_total_bytes Total capacity in bytes of a shared_dict # TYPE kong_memory_lua_shared_dict_total_bytes gauge kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong",kong_subsystem="http"} 524 kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_cluster_events",kong_subsys kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_core_db_cache",kong_subsyst kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_core_db_cache_miss",kong_su kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_db_cache",kong_subsystem="h kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_db_cache_miss",kong_subsyst kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_healthchecks",kong_subsyste kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_locks",kong_subsystem="http kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_rate_limiting_counters",kon kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="kong_secrets",kong_subsystem="ht kong_memory_lua_shared_dict_total_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",shared_dict="prometheus_metrics",kong_subsyst # HELP kong_memory_workers_lua_vms_bytes Allocated bytes in worker Lua VM # TYPE kong_memory_workers_lua_vms_bytes gauge kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1323",kong_subsystem="http"} 48452110 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1324",kong_subsystem="http"} 47971632 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1325",kong_subsystem="http"} 47971628 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1326",kong_subsystem="http"} 47971632 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1327",kong_subsystem="http"} 47971560 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1328",kong_subsystem="http"} 84856372 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1329",kong_subsystem="http"} 47971632 kong_memory_workers_lua_vms_bytes{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",pid="1330",kong_subsystem="http"} 47971632 # HELP kong_nginx_connections_total Number of connections by subsystem # TYPE kong_nginx_connections_total gauge kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="accepted"} 258 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="active"} 9 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="handled"} 258 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="reading"} 0 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="total"} 325 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="waiting"} 0 kong_nginx_connections_total{node_id="10e43c5c-8c63-485f-84a3-4ad97e047f1e",subsystem="http",state="writing"} 9 http://192.168.1.10:8001/metrics

Slide 87

Slide 87 text

幾乎開箱即 用 的 Prometheus plugins •設定 Prometheus 收集它 •加 入 Kong 官 方 的 Grafana 面 板 https://grafana.com/grafana/dashboards/7424-kong-o ff i cial/

Slide 88

Slide 88 text

https://grafana.com/grafana/dashboards/7424-kong-o ff i cial/

Slide 89

Slide 89 text

https://grafana.com/grafana/dashboards/7424-kong-o ff i cial/

Slide 90

Slide 90 text

快速回顧 •瞭解 Kong API Gateway 的功 用 •瞭解 GitOps 重要性,應 用 在 工 作流程中 •Kong 各種花式部署 方 式 2024 大

Slide 91

Slide 91 text

小 撇步整理 •使 用 ArtifactHUB 偷看 一 下 Helm Charts 裡 面 的模板內容 •使 用 helm template 輸出原始 yaml 設定檔做 比 對 •使 用 Kong 自 帶的 decK 指令匯出設定檔 2024 大

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Q & A https://imageio.forbes.com/specials-images/imageserve/6411e96b850d823e386ebb25/Q-and-A---question-and-answer-shot-form-on-wooden-block/960x0.jpg?format=jpg&width=1440