ApisixTls CRD 配置 SSL 证书
根据代码分析 (config/crd/bases/apisix.apache.org_apisixtlses.yaml:41-130),ApisixTls 用于管理 TLS 和 mTLS 证书。
- 基础 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
- 多域名 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
- 跨命名空间引用 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 # 证书存储在不同命名空间
- 配置 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/.*"
- 完整示例(包含 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 | []string | ✅ | SNI 主机名列表,至少一个 |
| secret | object | ✅ | Kubernetes TLS Secret 引用 |
| secret.name | string | ✅ | Secret 名称 |
| secret.namespace | string | ✅ | Secret 所在命名空间 |
| ingressClassName | string | ❌ | 关联的 IngressClass |
| client | object | ❌ | mTLS 客户端配置 |
| client.caSecret | object | ❌ | CA 证书 Secret 引用 |
| client.depth | int | ❌ | 客户端证书链验证深度 |
| 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 路由解耦
实际使用建议
- 统一管理证书:在专门的命名空间(如 cert-manager 或 apisix-cert)存储所有证书
- 使用 cert-manager:自动更新证书
- 通配符证书:对于多子域名场景,使用通配符证书减少配置数量
应用配置:
kubectl apply -f apisix-tls-config.yaml