IT 铁人赛 k8s 入门30天 -- day28 k8s Service Catalog

前言

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

<<:  Day30 阿里云30後结语

>>:  图的最短路径 - 佛洛伊德演算法 - 表格算法 - DAY 28

Day 24 利用transformer自己实作一个翻译程序(六) Masking

Masking 需要把填充的部分标记为0,其余部分标记为1,才不会导致填充的部分被误认为是输入 de...

以Ardiuno控制CPU散热器呼吸灯

▲ AR12-TUF采用导热管与CPU直接接触的热导管直触技术(HDC),4根Ø 6mm全铜热导管...

#20 数据中的机率性(1)

tags: tags: 2021IT 了解随机性 从整体上观察矩阵(集合)中的物件分布与矩阵整体的关...

Microsoft Azure Machine Learning - Day 3

Chap.I Practical drill 实战演练 以下内容来自这里 Prat4. Run Ex...