欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

K8s service catalog

程序员文章站 2024-03-11 16:40:25
...

Service catalog

Service catalog: https://github.com/kubernetes-incubator/service-catalog, 是 K8s 的一个孵化项目,目标是通过其 K8s 上部署的应用可以通过 service broker 连接外部服务;service broker 也是又一个标准化组织推动:open service broker - https://www.openservicebrokerapi.org/,简单说 service broker 会提供:

  • List catalog
  • Provision instance
  • Service bind
  • Service unbind
  • Deprovision instance

Service catalog 可以创建下列资源:

  • clusterservicebroker: 连接 broker
  • service instance: 创建的 instance
  • service binding: 绑定 instance,生成 secret

下文提供了更多的信息:
https://medium.com/bitnami-perspectives/service-catalog-in-kubernetes-78c0736e3910

其中提到的 service broker 的 Go 的示例代码:

安装

使用 helm 安装:

  • 启动 minikube,并配置 RBAC: minikube start --extra-config=apiserver.Authorization.Mode=RBAC,同时配置 Tiller。
minikube start --extra-config=apiserver.Authorization.Mode=RBAC
kubectl config current-context
kubectl create clusterrolebinding add-on-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default
  • 配置 repo:helm repo add svc-cat https://svc-catalog-charts.storage.googleapis.com
  • 运行 search 确保 repo 正常显示
  • 更新 repo:helm repo update
  • 安装 service catalog:helm install svc-cat/catalog --name catalog --namespace catalog
➜  ~ helm repo add svc-cat https://svc-catalog-charts.storage.googleapis.com
"svc-cat" has been added to your repositories
➜  ~ helm search service-catalog
NAME            VERSION DESCRIPTION
svc-cat/catalog 0.1.3   service-catalog API server and controller-manag...
  • 查看所有的 API: kubectl api-versions,确认 servicecatalog.k8s.io 已经存在。
➜  service-catalog git:(master) ✗ kubectl api-versions
admissionregistration.k8s.io/v1alpha1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1beta1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
batch/v1
batch/v1beta1
batch/v2alpha1
certificates.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1alpha1
rbac.authorization.k8s.io/v1beta1
servicecatalog.k8s.io/v1beta1
settings.k8s.io/v1alpha1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

使用

参照 https://github.com/gyliu513/mariadb-broker

  • 部署 maria db server: kubectl create -f xxx.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mariadb-server
  labels:
    app: mariadb-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mariadb-server
  template:
    metadata:
      labels:
        app: mariadb-server
    spec:
      containers:
      - name: mariadb-server
        image: mariadb:10.3
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: passw0rd
        ports:
        - containerPort: 3306
{"services":[{"name":"mariadb","id":"3533e2f0-6335-4a4e-9d15-d7c0b90b75b5","description":"MariaDB database","bindable":true,"plan_updateable":false,"tags":null,"requires":null,"metadata":null,"plans":[{"name":"default","id":"b9600ecb-d511-4621-b450-a0fa1738e632","description":"MariaDB database","metadata":null,"free":true,"schemas":null}],"dashboard_client":null}]}
  • 创建 cluster service broker:kubectl apply -f examples/1.mariadb-broker.yaml,注意需要修改 broker url 根据上面一部的起动完成的 pod 的 ip;运行 kubectl get clusterservicebroker 查看资源已经创建完成;
➜  service-catalog git:(master) ✗ kubectl get clusterservicebroker
NAME             AGE
mariadb-broker   19m
  • 运行 workpress 例子:
➜  service-catalog git:(master) ✗ kubectl apply -f examples/3.wodpress/3.1.wordpress-instance.yaml
serviceinstance "wordpress-mariadb-instance" created
➜  service-catalog git:(master) ✗ kubectl get serviceinstance
NAME                         AGE
wordpress-mariadb-instance   19m

➜  service-catalog git:(master) ✗ kubectl apply -f examples/3.wodpress/3.2.wordpress-binding.yaml
servicebinding "wordpress-mariadb-binding" created
➜  service-catalog git:(master) ✗ kubectl get servicebinding
NAME                        AGE
wordpress-mariadb-binding   19m

➜  service-catalog git:(master) ✗ kubectl apply -f examples/3.wodpress/3.3.wordpress-blog-system.yaml
service "wordpress" created
deployment "wordpress" created
  • 查看 wordpress 相应的 service,找到 node port,访问 workdpress。

helm 删除的命令:

  • helm del --purge mariadb-broker

Tips

安装 helm,init 完成后由于网的原因,tiller 的 pod 会起不来,解决办法:通过 ssh 登入minikube,手动拉取镜像和 打 tag。

docker pull sapcc/tiller:v2.7.2
docker tag sapcc/tiller:v2.7.2 gcr.io/kubernetes-helm/tiller:v2.7.2

运行 kubectl create clusterrolebinding add-on-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default 命令配置 Tiller ,使其具有 cluster-admin 访问权限时,可能报错:Error: unknown flag: --clusterrole,这是由于 kubectl 版本太导致的,要升级 kubectl 。