使用 Kubernetes Operator 运行 Casdoor
本指南介绍如何使用 Casdoor operator 在 Kubernetes 上运行 Casdoor,以及如何将组织和应用声明为 Kubernetes 资源。
学习目标
- 安装 operator。
- 通过
Casdoor资源运行一个 Casdoor 实例,试用时使用 SQLite,生产环境使用 PostgreSQL、Redis 和 Ingress。 - 使用
CasdoorOrganization和CasdoorApplication创建组织(Organization)和应用(Application),并从 Secret 中挂载应用的客户端凭证。 - 升级和移除实例。
前提条件
- 一个 Kubernetes 集群,版本为 1.29 或更高
kubectl- 对于生产环境:一个 MySQL 或 PostgreSQL 数据库以及一个集群可以访问的 Redis 服务器
Casdoor Cloud gives you a dedicated Casdoor instance that we host and keep upgraded for you, from $9/month with no per-user fees. New accounts get $4 in free credit to try it.
Operator 还是 Helm chart?
两者都运行相同的 Casdoor 镜像。
- Helm chart 安装单个 Casdoor 发布版本,并通过 values 文件进行配置。当您只运行一个实例并使用 Helm 管理它时,请选择此方式。
- operator 为集群添加了三种资源类型。
Casdoor用于运行实例;CasdoorOrganization和CasdoorApplication用于管理实例内部的内容,每个应用都会获得一个包含其 Client ID、Client Secret 和证书的 Secret。当您运行多个实例、使用 Casdoor 登录的应用程序部署在同一集群中,或者使用 Argo CD 或 Flux 等 GitOps 工具管理集群时,请选择此方式。
安装 operator
kubectl apply --server-side -k https://github.com/casdoor/casdoor-operator/config/default
该命令安装三个自定义资源定义(CRD)和一个集群角色(ClusterRole),并在 casdoor-operator-system 命名空间中运行 operator:
kubectl -n casdoor-operator-system get pods
operator 会监视所有命名空间。若要将其限制在某个特定命名空间,请在 casdoor-operator-controller-manager 部署的参数中添加 --watch-namespace=<namespace>。
若要固定某个发布版本,请在 URL 中添加 ?ref=<tag>,并在您自己的 kustomization 文件中设置镜像标签:
resources:
- https://github.com/casdoor/casdoor-operator/config/default?ref=v2.2.0
images:
- name: casbin/casdoor-operator
newTag: "2.2.0"
发布版本列在 GitHub 上,镜像位于 Docker Hub。
创建 Casdoor 实例
-
创建一个 Secret,其中包含
built-in/admin账户的密码:kubectl create secret generic casdoor-admin --from-literal=password='<password>' -
创建一个
Casdoor资源。以下示例运行单副本,并将数据存储在 1Gi 卷上的 SQLite 文件中:casdoor.yamlapiVersion: operator.casdoor.org/v1alpha1
kind: Casdoor
metadata:
name: casdoor
spec:
database:
driver: sqlite
storage:
size: 1Gi
initAdminPasswordSecretRef:
name: casdoor-admin
key: passwordkubectl apply -f casdoor.yaml
kubectl get casdoorNAME READY REPLICAS IMAGE URL AGE
casdoor True 1 casbin/casdoor:4.19.0 http://casdoor.default.svc:8000 2m -
打开 Casdoor:
kubectl port-forward svc/casdoor 8000:8000访问
http://localhost:8000,以admin身份登录,密码来自上述 Secret。
operator 会创建 Deployment、Service 和卷。当 Casdoor 初始化空数据库时会应用该密码,且需要 Casdoor 4.19.0 或更高版本。如果没有 initAdminPasswordSecretRef,Casdoor 会在首次访问时询问 built-in/admin 的密码。
在生产环境中运行 Casdoor
以下实例运行三个副本,使用 PostgreSQL 存储数据,Redis 存储会话,并配置了带 TLS 的 Ingress:
apiVersion: v1
kind: Secret
metadata:
name: casdoor-credentials
stringData:
db-password: <database password>
admin-password: <admin password>
---
apiVersion: operator.casdoor.org/v1alpha1
kind: Casdoor
metadata:
name: casdoor
spec:
replicas: 3
database:
driver: postgres
host: postgres.default.svc
user: casdoor
name: casdoor
passwordSecretRef:
name: casdoor-credentials
key: db-password
redis:
endpoint: redis.default.svc:6379
initAdminPasswordSecretRef:
name: casdoor-credentials
key: admin-password
ingress:
className: nginx
host: door.example.com
tlsSecretName: door-example-com-tls
resources:
requests:
cpu: 100m
memory: 256Mi
operator 对其执行的操作:
- Origin:
origin默认设置为 Ingress 主机名,因此 OIDC issuer 为https://door.example.com。当 Casdoor 通过其他方式暴露时,请设置spec.origin。 - Replicas:多于一个副本时需要
redis,因为 Casdoor 将会话存储在 Redis 中;否则,当请求到达其他 Pod 时,用户会被注销。SQLite 仅支持单副本。如果违反任一规则,API 服务器将拒绝该Casdoor资源。 - Database:如果不存在,Casdoor 会自动创建 MySQL 或 PostgreSQL 数据库。当数据库用户无权创建数据库时,请设置
database.create: false。 - Secrets:密码从 Secret 中读取,绝不会写入 ConfigMap。更改 Secret 中的数据库或 Redis 密码会导致 Pod 重启并应用新值。
- Availability:当副本数大于等于 2 时,operator 会添加 PodDisruptionBudget。
Casdoor资源具有scale子资源,因此kubectl scale casdoor casdoor --replicas=5和 HorizontalPodAutoscaler 均可对其生效。 - Health:探针使用
/api/health。当当前版本的每个副本都可用时,Ready条件变为True。
配置实例
| Field | Description |
|---|---|
image | Casdoor image. Defaults to the version the operator was released with. |
replicas | Number of pods, 1 by default. |
origin | Public URL, used as the OIDC issuer. |
database.driver | mysql, postgres or sqlite. |
database.host, port, name, user, sslMode | Connection settings. name defaults to casdoor. |
database.passwordSecretRef | Secret key holding the database password. |
database.dataSourceNameSecretRef | Secret key holding a complete dataSourceName, which replaces the fields above. |
database.create | Create the MySQL or PostgreSQL database if it is missing. true by default. |
database.storage | Volume for the SQLite file. Without it, the file lives in an emptyDir and is lost with the pod. The volume is kept when the Casdoor resource is deleted. |
redis.endpoint, passwordSecretRef, db | Session store. Several addresses separated by ; use Redis Cluster. |
initAdminPasswordSecretRef | Password of built-in/admin for a new database. |
apiCredentialsSecretRef | Credentials the operator uses to call the Casdoor API. See API credentials. |
initData.secretRef, newOnly | Secret key holding an init data file. With newOnly, the default, objects that already exist are left alone. |
config | Other configuration items, such as defaultLanguage: zh. Items covered by the fields above are ignored. |
env | Extra environment variables for the Casdoor container, applied last. |
service.type, port, annotations | Service settings. Port 8000 by default. |
ingress.host, className, tlsSecretName, annotations | Creates an Ingress when set. |
resources, nodeSelector, tolerations, affinity, topologySpreadConstraints, priorityClassName, serviceAccountName, imagePullSecrets, podLabels, podAnnotations | Passed to the pod. |
运行 kubectl explain casdoor.spec 可查看完整架构。
管理组织和应用
API 凭证
To create objects in an instance, the operator calls the Casdoor API with the client ID and client secret of app-built-in, the application of the built-in organization, which can manage every organization.
- 配置
initAdminPasswordSecretRef后,operator 会在首次启动后以built-in/admin身份登录一次,读取app-built-in的凭证并将其保存在 Secret<name>-api-credentials中。此后不再使用该密码。 - 如果密码在 operator 登录前已更改,或者您不想将密码提供给 operator,请从管理控制台中复制
app-built-in的 client ID 和 client secret,将其存入一个包含键clientId和clientSecret的 Secret,并将spec.apiCredentialsSecretRef指向该 Secret。
Casdoor 资源的 APIReady 条件显示凭据是否可用:
kubectl get casdoor casdoor -o jsonpath='{.status.conditions[?(@.type=="APIReady")].message}'
如果密码错误,operator 每 5 分钟重试一次,这低于触发账户锁定的失败登录次数。
创建组织和应用
apiVersion: operator.casdoor.org/v1alpha1
kind: CasdoorOrganization
metadata:
name: acme
spec:
casdoorRef:
name: casdoor
displayName: ACME Corp
websiteUrl: https://acme.example.com
passwordType: bcrypt
languages: [en, zh]
---
apiVersion: operator.casdoor.org/v1alpha1
kind: CasdoorApplication
metadata:
name: acme-portal
spec:
casdoorRef:
name: casdoor
organization: acme
displayName: ACME Portal
redirectUris:
- https://portal.acme.example.com/callback
grantTypes: [authorization_code, refresh_token]
tokenFormat: JWT-Standard
expireInHours: 24
cert: cert-built-in
kubectl get casdoororg,casdoorapp
NAME CASDOOR READY AGE
casdoororganization.operator.casdoor.org/acme casdoor True 10s
NAME CASDOOR ORGANIZATION CLIENT ID READY AGE
casdoorapplication.operator.casdoor.org/acme-portal casdoor acme 44f1aa1fbe1afc436140 True 10s
casdoorRef.name 是同一命名空间中 Casdoor 资源的名称。当 Kubernetes 资源名称与 Casdoor 中的对象名称不同时,使用 name 设置 Casdoor 中的对象名称。
使用应用的凭据
对于每个应用,operator 会写入一个名为 <name>-casdoor 的 Secret(或使用 credentialsSecretName 指定的名称),其中包含键 endpoint、clientId、clientSecret、organizationName、applicationName 和 certificate。这些键与 Casdoor SDKs 的设置相匹配,因此集群中的应用可以从环境变量中读取它们:
containers:
- name: portal
image: example/portal
envFrom:
- secretRef:
name: acme-portal-casdoor
prefix: CASDOOR_
除非您设置了 clientId 或 clientSecretSecretRef,否则 Casdoor 会生成 client ID 和 client secret。endpoint 是实例的来源地址;如果没有来源地址,则是其 Service 地址。
了解 operator 写入哪些字段
-
Spec 中的字段:它们保留 Casdoor API 的名称。operator 将它们写入 Casdoor,如果在管理控制台中编辑了这些字段,operator 会在 10 分钟内将其改回。
-
Spec 未设置的字段:它们保留在 Casdoor 中的现有值,因此您可以在管理控制台中编辑它们。在
extraFields中设置任何没有专用 spec 字段的字段,例如signinMethods、providers或mfaItems:spec:
extraFields:
enableSigninSession: true
signinMethods:
- name: Password
displayName: Password
rule: All -
已存在的对象:具有现有对象名称的资源会接管该对象,并且只写入其 spec 中的字段。
-
删除:删除
CasdoorApplication会删除 Casdoor 中的应用。删除CasdoorOrganization会保留 Casdoor 中的组织及其用户,除非资源设置了deletionPolicy: Delete。
使用不由 operator 运行的 Casdoor 服务器
CasdoorOrganization 和 CasdoorApplication 也适用于集群外的 Casdoor 服务器,例如 Casdoor Cloud 实例。将该实例 built-in 组织中某个应用的 client ID 和 client secret 放入 Secret,并使用 casdoorRef.external:
apiVersion: v1
kind: Secret
metadata:
name: door-api
stringData:
clientId: <client ID of app-built-in>
clientSecret: <client secret of app-built-in>
---
apiVersion: operator.casdoor.org/v1alpha1
kind: CasdoorApplication
metadata:
name: grafana
spec:
casdoorRef:
external:
endpoint: https://door.example.com
credentialsSecretRef:
name: door-api
organization: acme
redirectUris:
- https://grafana.example.com/login/generic_oauth
LDAP登录
Service 在端口 389 上暴露 Casdoor 的 LDAP server。在 Pod 内部,Casdoor 监听端口 10389,因为 Pod 以用户 1000 运行,且某些容器运行时不允许绑定低于 1024 的端口。若要在 Pod 内使用其他端口,请在 config 中设置 ldapServerPort。
升级 Casdoor
修改 spec.image,或升级 operator 以获取更新的默认镜像。operator 逐个滚动更新 Pod;如果使用 SQLite,它会先停止旧 Pod 再启动新 Pod,因为两个进程无法打开同一个数据库文件。Casdoor 在启动时更新数据库架构。在从版本 3 迁移到版本 4 之前,请阅读 Upgrade from v3 to v4。
卸载
-
在 operator 仍在运行时删除
CasdoorApplication和CasdoorOrganization资源,以便它移除它们的 finalizers:kubectl delete casdoorapplications,casdoororganizations --all -A -
删除实例,如果不需要数据,则删除 SQLite 卷:
kubectl delete casdoor casdoor
kubectl delete pvc casdoor-data -
移除 operator:
kubectl delete -k https://github.com/casdoor/casdoor-operator/config/default