APISIX tsl CRD

APISIX tsl CRD

_

ApisixTls CRD 配置 SSL 证书

根据代码分析 (config/crd/bases/apisix.apache.org_apisixtlses.yaml:41-130),ApisixTls 用于管理 TLS 和 mTLS 证书。

  1. 基础 TLS 配置(单域名)

首先创建 Kubernetes TLS Secret:

创建 TLS Secret

kubectl create secret tls my-tls-secret \
  --cert=tls.crt \
  --key=tls.key \
  -n default

然后创建 ApisixTls CRD:

apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: my-tls
  namespace: default
spec:
  ingressClassName: apisix
  hosts:
  - api.example.com
  secret:
    name: my-tls-secret
    namespace: default
  1. 多域名 TLS 配置(使用通配符证书)
  apiVersion: apisix.apache.org/v2
  kind: ApisixTls
  metadata:
    name: wildcard-tls
    namespace: default
  spec:
    ingressClassName: apisix
    hosts:
    - "*.example.com"
    - example.com
    secret:
      name: wildcard-tls-secret
      namespace: default
  1. 跨命名空间引用 Secret

ApisixTls 可以引用其他命名空间的 Secret:

  apiVersion: apisix.apache.org/v2
  kind: ApisixTls
  metadata:
    name: cross-ns-tls
    namespace: app-namespace
  spec:
    ingressClassName: apisix
    hosts:
    - api.example.com
    secret:
      name: tls-cert
      namespace: cert-storage  # 证书存储在不同命名空间
  1. 配置 mTLS(双向认证)

根据代码 (config/crd/bases/apisix.apache.org_apisixtlses.yaml:63-94),支持 mTLS 配置:

  apiVersion: apisix.apache.org/v2
  kind: ApisixTls
  metadata:
    name: mtls-config
    namespace: default
  spec:
    ingressClassName: apisix
    hosts:
    - secure-api.example.com
    secret:
      name: server-tls-secret
      namespace: default
    client:
      caSecret:
        name: client-ca-secret
        namespace: default
      depth: 3  # 客户端证书链验证深度
      skip_mtls_uri_regex:
      - "/health"  # 健康检查端点跳过 mTLS
      - "/public/.*"
  1. 完整示例(包含 Secret 和 ApisixTls)

1. 创建服务器证书 Secret

apiVersion: v1
kind: Secret
metadata:
  name: api-tls-secret
  namespace: default
type: kubernetes.io/tls
data:
  tls.crt: LS0tLS1CRUdJTi... # base64 编码的证书
  tls.key: LS0tLS1CRUdJTi... # base64 编码的私钥

---
# 2. 创建 CA 证书 Secret(用于 mTLS)
apiVersion: v1
kind: Secret
metadata:
  name: client-ca-secret
  namespace: default
type: kubernetes.io/tls
data:
  tls.crt: LS0tLS1CRUdJTi... # CA 证书

---
# 3. 创建 ApisixTls 配置
apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: api-tls
  namespace: default
spec:
  ingressClassName: apisix
  hosts:
  - api.example.com
  - "*.api.example.com"
  secret:
    name: api-tls-secret
    namespace: default
  client:
    caSecret:
      name: client-ca-secret
      namespace: default
    depth: 1

配置字段说明

根据 CRD 定义 (config/crd/bases/apisix.apache.org_apisixtlses.yaml:95-130):

字段类型必填说明
hosts[]stringSNI 主机名列表,至少一个
secretobjectKubernetes TLS Secret 引用
secret.namestringSecret 名称
secret.namespacestringSecret 所在命名空间
ingressClassNamestring关联的 IngressClass
clientobjectmTLS 客户端配置
client.caSecretobjectCA 证书 Secret 引用
client.depthint客户端证书链验证深度
client.skip_mtls_uri_regex[]string跳过 mTLS 的 URI 正则列表

验证配置

查看所有 TLS 配置

kubectl get apisixtls -A

查看特定 TLS 配置详情

kubectl get apisixtls my-tls -n default -o yaml

查看状态

kubectl get apisixtls my-tls -n default -o jsonpath='{.status.conditions}'

与 Ingress 的 TLS 配置对比

传统 Ingress 方式:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  tls:
  - hosts:
    - api.example.com
    secretName: my-tls-secret

ApisixTls 方式:
apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: my-tls
spec:
  hosts:
  - api.example.com
  secret:
    name: my-tls-secret
    namespace: default

优势:

  • 支持跨命名空间引用 Secret
  • 支持 mTLS 配置
  • 更灵活的证书管理
  • 与 APISIX 路由解耦

实际使用建议

  1. 统一管理证书:在专门的命名空间(如 cert-manager 或 apisix-cert)存储所有证书
  2. 使用 cert-manager:自动更新证书
  3. 通配符证书:对于多子域名场景,使用通配符证书减少配置数量

应用配置:
kubectl apply -f apisix-tls-config.yaml

(转载)公有云是不是杀猪盘 2024-04-11
k8s集群内部分Docker仓库故障,导致k8s dockerd进程镜像拉取资源耗尽,影响全局镜像拉取 2026-01-04

评论区