iT邦幫忙

2021 iThome 鐵人賽

DAY 28
2
DevOps

k8s 入門學習 30天系列 第 28

IT 鐵人賽 k8s 入門30天 -- day28 k8s Service Catalog

  • 分享至 

  • xImage
  •  

前言

Service Catalog 介紹參考文件 https://kubernetes.io/docs/concepts/extend-kubernetes/service-catalog/

Service Catalog 安裝參考文件 https://kubernetes.io/docs/tasks/service-catalog/install-service-catalog-using-helm/

今天要來介紹 k8s Service Catalog 這個 Extension API

什麼是 Service Catalog

Service Catalog 是一個用來管理外部資源的 Extension API

Service Catalog 提供了一個介面與 Service Broker 互動藉此管理外部的 Service, 而不需要知道太多 Service 運行的細節

Service Broker

Service Broker 是由一組被管理的 Services 提供並且由第三方維護的 EndPoint, 需要遵守 Open service broker API spec 規範, 通常維護的第三方是雲端供應商比如 AWS, GCP, 或 AZURE.

舉例來說: Microsoft Azure Cloud Queue,Amazon Simple Queue Service, 與 Google Cloud Pub/Sub, 這些服務透能夠透過 Service Broker 讓其他應用來使用

使用 Service Catalog 的好處

透過 Service Catalog, k8s 叢集的操作者可以透過 Service Broker 來管理 Service 並且提供 Service 給其他在 k8s 的應用來使用

使用案例

當應用程式開發者想要在 k8s 叢集的應用程式中使用 message queue. 然而對應用開發者來說自行在 k8s 叢集中建立一個 message queue 太過煩雜. 幸運的是, 雲端供應商已經有藉由 Service Broker 提供可管理的 message queue Service

叢集操作者可以透過建立 Service Catalog 與 Service Broker 溝通, 透過 Service Broker 來建立 message queue service 來提供給 k8s 叢集內的應用使用

使用 Service Catalog, 應用開發者在使用 message queue service 的過程中就只需要關注應用程式的開發, 而不必要去擔心 message queue service 建制相關的事項

Service Catalog 架構

Service Catalog 使用 Open service broker API 來跟 Service Brokers 做溝通, 擔任 k8s API server 與應用程式的中介用來處理分派與提取必要的連線到 Service 的機敏資料以下是架構關係圖:

API Resources

Service Catalog 安裝 servicecatalog.k8s.io API 並且提供以下 k8s 的資源

  • ClusterServiceBroker:
    一個在叢集內代表 Service Broker 的元件, 封藏著 Server 連線細節

  • ClusterServiceClass
    一個由特定 Service Broker 提供的可管理 Service. 當一個 ClusterServiceBroker 被建立時, Service Catalog controller 會連接到 Service Broker 來取得所有可以使用的 Services, 接著 ClusterServiceClass 就會依照取得的 Service 來建立起來

  • ClusterServicePlan
    一個特製的 Service 使用計劃. 舉例來說, 一個 Service 可能有 free tier 以及 paid tier, 更據不同的使用計劃會有不同的設定選項. 而建立時機也跟 ClusterServiceClass 一樣
    在ClusterServiceBroker 被建立時, Service Catalog controller 會連接到 Service Broker 來取得所有可以使用的 Services, ClusterServicePlan 就會根據不同的 Service 建立起來

  • ServiceInstance
    一個由 ClusterServiceClass 提供的實體. ServerInstance 是由叢集操作者所建立, 用來提供服務給叢集內的應用所使用. 當一個 ServiceInstance 被建立, Service Catalog controller 會連接到對應的 Service Broker 去產生對應的實體

  • ServiceBinding
    連接 ServiceInstaance 的 credentials. ServiceBinding 由叢集操作者所建立用來讓應用程式連接到 ServiceInstacne. 當建立時, Service Catalog controller 會建立一個 k8s Secret 來存儲連接 ServiceInstance 的資訊, 並且能夠在 Pod mount 起來

驗證方式

Service Catalog 提供以下驗證方式

使用方式

一個叢集操作者使用 Service Catalog API 來提供一個服務給 k8s 叢集內的應用使用
主要步驟如下:

列出 Service Provider 內所有可用的服務還有 Service Plans

首先要建立一個 ClusterServiceBrokder 資源如下

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ClusterServiceBroker
metadata:
  name: cloud-broker
spec:
  # Points to the endpoint of a service broker. (This example is not a working URL.)
  url:  https://servicebroker.somecloudprovider.com/v1alpha1/projects/service-catalog/brokers/default
  #####
  # Additional values can be added here, which may be used to communicate
  # with the service broker, such as bearer token info or a caBundle for TLS.
  #####

注意以上只是範例並無法透過上面 yaml 去發佈

處理流程如下圖:

Provisioning a new instance of the managed service.

接著透過回傳回來的 ClusterServiceClass 與 ClusterServicePlan

叢集操作者可以建制對應的 ServiceInstance 如下

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceInstance
metadata:
  name: cloud-queue-instance
  namespace: cloud-apps
spec:
  # References one of the previously returned services
  clusterServiceClassExternalName: cloud-provider-service
  clusterServicePlanExternalName: service-plan-name
  #####
  # Additional parameters can be added here,
  # which may be used by the service broker.
  #####

注意以上只是範例並無法透過上面 yaml 去發佈

處理流程圖如下

Binding to the managed service, which returns the connection credentials

建立完 ServiceInstance 後, 叢集操作者需要建立一個 ServiceBind 來取得 Service 的連接資訊

建立 ServiceBing 如下:

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceBinding
metadata:
  name: cloud-queue-binding
  namespace: cloud-apps
spec:
  instanceRef:
    name: cloud-queue-instance
  #####
  # Additional information can be added here, such as a secretName or
  # service account parameters, which may be used by the service broker.
  #####

注意以上只是範例並無法透過上面 yaml 去發佈

處理流程圖如下

Mapping the connection credentials into the application.

建立完 ServiceBinding 後, 最後一件事就是要把 credential 跟應用程式對應起來

這些資訊會被儲存在 Secret 讓在叢集內的應用可以去存取

所以建立用者只要指定對應的 Secret 就可以使用 Service 了

互動關係如下

以下範例是在 Pod 指定 PROVIDER_APPILICATIOM_CREDENTIAL 環境變數

...
    spec:
      volumes:
        - name: provider-cloud-key
          secret:
            secretName: sa-key
      containers:
...
          volumeMounts:
          - name: provider-cloud-key
            mountPath: /var/secrets/provider
          env:
          - name: PROVIDER_APPLICATION_CREDENTIALS
            value: "/var/secrets/provider/key.json"

注意以上只是範例並無法透過上面 yaml 去發佈

以下範例則是使用 Secret 來做對應

          env:
          - name: "TOPIC"
            valueFrom:
                secretKeyRef:
                   name: provider-queue-credentials
                   key: topic

注意以上只是範例並無法透過上面 yaml 去發佈

安裝 Service Catalog

以下將會使用 Helm 來安裝 Service Catalog

把 service-catalog 加入 Helm repository

使用以下指令把 service-catalog 加入 Helm repository

helm repo add svc-cat https://kubernetes-sigs.github.io/service-catalog

用以下指令檢查是否加入 repository 成功

helm search repo service-catalog

Enable RBAC

使用以下指令讓 minikube 開啟 RBAC

如果使用 minikube v0.25 以下, 必須跑以下指令

minikube start --extra-config=apiserver.Authorization.Mode=RBAC

如果使用 minikube v0.26 以上, 則跑下指令

minikube start

在 v0.26 的版本, RBAC 已經預設開啟了

設定 Tiller 具有 cluster-admin 權限指令如下

kubectl create clusterrolebinding tiller-cluster-admin \
    --clusterrole=cluster-admin \
    --serviceaccount=kube-system:default

在 k8s 叢集安裝 Service Catalog

先建立一個 catalog 的 namespace

kubectl create namespace catalog

如果是 helm version 3 使用以下指令

helm install catalog svc-cat/catalog --namespace catalog

如果是 helm version 2 使用以下指令

helm install svc-cat/catalog --name catalog --namespace catalog

上一篇
IT 鐵人賽 k8s 入門30天 -- day27 Communicate Between Containers in the Same Pod Using a Shared Volume
下一篇
IT 鐵人賽 k8s 入門30天 -- day29 Adding entries to Pod /etc/hosts with HostAliases
系列文
k8s 入門學習 30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
juck30808
iT邦研究生 1 級 ‧ 2021-10-12 18:30:10

第28天了! 恭喜即將完賽 (拍手!!!

json_liang iT邦研究生 5 級 ‧ 2021-10-12 21:44:48 檢舉

感謝鼓勵

我要留言

立即登入留言