跳到主内容

使用 Helm 在 Kubernetes 上运行 Casdoor

本指南介绍如何使用官方 Helm chart 在 Kubernetes 集群上安装 Casdoor,将其暴露在集群外部,以及升级或卸载发布版本。如果要运行多个实例,或者将组织和应用声明为 Kubernetes 资源,请参阅 Kubernetes operator。


学习目标​

  • 安装 Casdoor Helm chart。
  • 覆盖 chart 值,包括数据库连接。
  • 使用 Ingress 或 Gateway API 暴露 Casdoor。
  • 在 values 文件中保留组织、应用和用户信息。
  • 升级和卸载发布版本。

所需条件​

  • 一个 Kubernetes 集群,版本 1.19 或更高
  • Helm 3.8 或更高版本
  • 对于 Gateway API 选项:集群中的 Gateway API CRDs 和一个 Gateway 控制器

不想自己运行?

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.

安装 chart​

该 chart 作为 OCI artifact 发布在 GitHub Container Registry 上。它在 Artifact Hub 上有列表,其源代码位于 Casdoor 仓库中。

  1. 安装 chart。将 <version> 替换为 Artifact Hub 上的一个 chart 版本。

    helm install casdoor oci://ghcr.io/casdoor/helm-charts/casdoor --version <version>

    要覆盖值,请传递你自己的 values 文件:

    helm install casdoor oci://ghcr.io/casdoor/helm-charts/casdoor      --version <version>      -f my-values.yaml
  2. 在集群中 casdoor 服务的 URL 处打开 Casdoor。使用默认值时,服务类型为 ClusterIP,端口为 8000,因此在暴露它之前,仅可在集群内部访问。

  3. 首次访问时,Casdoor 会打开 Welcome to Casdoor 页面。设置 admin 密码并点击 Set Password。请参阅 登录管理控制台。

    若要在首次启动前设置密码,请从 Secret 中读取:

    kubectl create secret generic casdoor-admin --from-literal=initAdminPassword='<admin-password>'
    initAdminPassword:
    existingSecret: casdoor-admin

    initAdminPassword.existingSecretKey 用于选择 Secret 中的另一个键。对于本地测试,--set initAdminPassword.value=<admin-password> 也可以正常工作,但这会将密码放入 values 文件和 Helm 发布版本中。该设置仅当 Casdoor 创建新数据库时生效。

自定义部署​

在你自己的 values 文件中覆盖 values.yaml 的值。主要值有:

参数描述默认值
replicaCount运行 Casdoor 应用的副本数量。1
image.repositoryCasdoor Docker 图像的仓库。casbin
image.nameCasdoor Docker 图像的名称。casdoor
image.pullPolicyCasdoor Docker 图像的拉取策略。IfNotPresent
image.tagCasdoor Docker 图像的标签。""
configCasdoor 应用的配置设置。See values.yaml
initAdminPassword.value新数据库中 built-in/admin 的密码;使用 existingSecret 更安全。""
initAdminPassword.existingSecret包含初始密码的现有 Secret。""
initAdminPassword.existingSecretKeyexistingSecret 中初始密码的键。initAdminPassword
database.driver要使用的数据库驱动程序(mysql、postgres、cockroachdb、sqlite)。sqlite
database.user数据库用户名。""
database.password数据库密码。""
database.host数据库主机。""
database.port数据库端口。""
database.databaseNameCasdoor 使用的数据库名称。casdoor
database.sslMode数据库连接的 SSL 模式。disable
service.typeKubernetes服务类型(ClusterIP、NodePort、LoadBalancer)。ClusterIP
service.portCasdoor 服务的端口号。8000
service.annotationsAnnotations for the Kubernetes Service, for example the annotations of a cloud load balancer when service.type is LoadBalancer.{}
ingress.enabled是否启用 Casdoor 的 Ingress。false
ingress.annotationsIngress 资源的注解。{}
ingress.hostsIngress 资源的主机名。[]
resourcesCasdoor 容器的资源请求和限制。{}
autoscaling.enabled是否启用 Casdoor 的水平 Pod 自动扩展。false
autoscaling.minReplicasHPA 的最小副本数。1
autoscaling.maxReplicasHPA 的最大副本数。100
autoscaling.targetCPUUtilizationPercentageHPA 的目标 CPU 利用率百分比。80
nodeSelectorPod 分配的节点标签。{}
tolerationsPod 分配的容忍标签。[]
affinityPod 分配的亲和性设置。{}
extraContainersEnabled是否启用额外的边车容器。false
extraContainers额外的边车容器。""
extraVolumeMountsCasdoor 容器的额外卷挂载。[]
extraVolumesCasdoor 容器的额外卷。[]
envFromSecret来自单个Secret密钥的环境变量。[]
envFromConfigmap来自单个ConfigMap密钥的环境变量。[]
envFrom来自整个 Secret 或 ConfigMap 的环境变量。[]

暴露 Casdoor​

选择 Ingress 或 Gateway API。

使用 Ingress 暴露 Casdoor​

在您的 values 文件中启用 Ingress:

ingress:
enabled: true
className: nginx
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
hosts:
- host: casdoor.example.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: casdoor-tls
hosts:
- casdoor.example.com

使用 Gateway API 暴露 Casdoor​

Kubernetes Gateway API 是 Ingress 的后继者。Istio、Envoy Gateway、Cilium、Kong、NGINX Gateway Fabric 和其他控制器都支持它。

在启用此选项之前,请安装 Gateway API CRDs,并确保集群中运行着兼容的 Gateway 控制器:

kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.0/standard-install.yaml

然后使用以下配置之一。

附加到现有网关​

如果集群已存在 Gateway,请将 HTTPRoute 指向它:

gatewayApi:
enabled: true
parentRefs:
- name: my-gateway
namespace: gateway-system
sectionName: https
hostnames:
- casdoor.example.com

创建 Gateway​

让 Chart 同时创建 Gateway 和 HTTPRoute。此示例使用 Istio:

gatewayApi:
enabled: true
createGateway: true
hostnames:
- casdoor.example.com
gateway:
gatewayClassName: istio
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same

创建将 HTTP 重定向到 HTTPS 的 Gateway​

在 Gateway 处终止 TLS 并将 HTTP 请求重定向到 HTTPS:

gatewayApi:
enabled: true
createGateway: true
hostnames:
- casdoor.example.com
gateway:
gatewayClassName: istio
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
- name: https
protocol: HTTPS
port: 443
tls:
certificateRefs:
- name: casdoor-tls
kind: Secret
allowedRoutes:
namespaces:
from: Same
httpsRedirect:
enabled: true

Gateway API 值​

参数描述Default
gatewayApi.enabled启用HTTPRoute创建false
gatewayApi.createGateway同时创建一个 Gateway 资源false
gatewayApi.annotationsHTTPRoute 的注解{}
gatewayApi.labelsHTTPRoute 的额外标签{}
gatewayApi.parentRefs父网关引用[]
gatewayApi.hostnames要匹配的主机名(Host标头)[]
gatewayApi.rules路由规则(匹配、过滤器、后端引用)路径前缀 /
gatewayApi.gateway.name网关名称(默认为图表全名)""
gatewayApi.gateway.gatewayClassName网关类名称(当createGateway=true时必填)""
gatewayApi.gateway.listeners网关监听器HTTP:80
gatewayApi.httpsRedirect.enabled启用HTTP→HTTPS重定向HTTPRoutefalse
gatewayApi.httpsRedirect.statusCode重定向响应码301
gatewayApi.httpsRedirect.hostnames重定向路由的主机名[]
gatewayApi.httpsRedirect.parentRefs覆盖重定向路由的 parentRefs[]

在 values 文件中管理 Casdoor 对象​

您可以将组织、应用、用户、提供商、角色和权限保留在 values 文件中,与部署的其他部分放在一起。Casdoor 会在启动时应用它们,并每 30 秒检查一次更改。执行 helm upgrade 修改这些内容后,无需重启 Pod 即可生效。

initData:
enabled: true
data:
organizations:
- owner: admin
name: acme
displayName: Acme
passwordType: bcrypt
applications:
- owner: admin
name: app-acme
organization: acme
displayName: Acme Portal
redirectUris:
- https://portal.acme.example.com/callback

现有对象仅接收您在此处编写的字段。其其他字段保持管理员控制台中设置的值。

Chart 将数据存储在 Secret 中。若要将数据从 values 文件中移出,请自行创建 Secret 并设置 initData.existingSecret。

ParameterDescriptionDefault
initData.enabledApply initData.data (or initData.existingSecret)false
initData.mergeUpdate existing objects with the given fields only; when false, they are deleted and re-created on every applytrue
initData.watchIntervalSeconds between the checks for changes, 0 applies the data only at startup30
initData.existingSecretExisting Secret holding the data, instead of initData.data""
initData.existingSecretKeyKey of the file in existingSecret, .yaml/.yml keys are read as YAMLinit_data.yaml
initData.dataThe objects to apply, in the init data format{}

升级发布版本​

helm upgrade casdoor oci://ghcr.io/casdoor/helm-charts/casdoor --version <version>
备注

直到版本 4.15.0 为止,Charts 均以 oci://registry-1.docker.io/casbin/casdoor-helm-charts 形式发布。该位置仍接收所有发布版本。要将现有发布版本迁移到新位置,请运行上述 helm upgrade 命令。资源名称保持不变。

卸载发布版本​

helm uninstall casdoor

另请参阅​