使用 cert-manager 管理网关的证书
cert-manager 是一个证书生命周期管理系统,支持证书的申请、部署等功能。您可以使用 cert-manager 颁发 ASM 网关的证书,从而可以使用 HTTPS 协议通过 ASM 网关访问服务,保证数据传输的安全。本文介绍如何使用 cert-manager 管理网关的证书。
背景信息
cert-manager 支持签发自签名证书和 DNS 域名证书,从而可以使用 HTTPS 协议通过 ASM 网关访问服务。这两种证书的区别如下:
- 自签名证书:自签名证书仅具有加密功能,无身份验证功能。您可以在命令行工具中使用 HTTPS 协议访问 ASM 网关,但是自签名证书不受 Web 浏览器的信任,Web 浏览器检查 HTTPS 连接会标记为潜在风险并弹出错误消息,即无法在 Web 浏览器中使用 HTTPS 协议通过 ASM 网关访问服务。
- DNS 域名证书:DNS 域名证书由受信任的 CA 机构颁发,兼具加密和身份验证功能。相比自签名证书,DNS 域名证书具有更高的安全性,受到 Web 浏览器的信任。您可以同时在命令行工具和 Web 浏览器中使用 HTTPS 协议通过 ASM 网关访问服务。
在集群中安装 cert-manager
-
在本地安装 Helm。具体操作,请参见 Helm。
-
使用 kubectl 连接集群。具体操作,请参见 获取集群KubeConfig并通过kubectl工具连接集群。
-
执行以下命令,创建 cert-manager 命名空间。
kubectl create namespace cert-manager -
执行以下命令,添加 cert-manager Chart。
helm repo add jetstack https://charts.jetstack.io -
执行以下命令,获取 cert-manager Chart 的最新信息。
helm repo update -
执行以下命令,安装 cert-manager。
说明
cert-manager 的版本需要和 Kubernetes 版本保持兼容。关于 cert-manager 和 Kubernetes 版本的对应关系,请参见 Supported Releases。
helm install \ cert-manager jetstack/cert-manager \ --namespace cert-manager \ --version v1.12.0 \ --set installCRDs=true
Currently supported releases
| Release | Release Date | End of Life | Supported Kubernetes / OpenShift Versions | Tested Kubernetes Versions |
|---|---|---|---|---|
| 1.16 | Oct 03, 2024 | Release of 1.18 | 1.25 → 1.31 / 4.14 → 4.16 | 1.27 → 1.31 |
| 1.15 | Jun 05, 2024 | Release of 1.17 | 1.25 → 1.31 / 4.12 → 4.16 | 1.25 → 1.31 |
| 1.12 LTS | May 19, 2023 | May 19, 2025 | 1.22 → 1.31 / 4.9 → 4.16 | 1.22 → 1.29 |
使用 cert-manager 签发自签名证书
步骤一:在集群中生成自签证书
-
使用以下内容,创建 issuer.yaml。
展开查看 issuer.yaml
-
执行以下命令,创建自签名 CA 来颁发工作负载证书。
kubectl apply -f issuer.yaml -n istio-system -
执行以下命令,查看证书。
kubectl get secret -n istio-system预期输出:
NAME TYPE DATA AGE istio-ingressgateway-certs kubernetes.io/tls 3 68m
步骤二:添加证书和私钥挂载卷到入口网关
-
登录 ASM控制台,在左侧导航栏,选择**** 服务网格** > *网格管理***。
-
在网格管理页面,单击目标实例名称,然后在左侧导航栏,选择****ASM 网关** > *入口网关***。
-
在入口网关页面的目标网关右侧,单击查看 YAML。
-
在编辑对话框,添加如下信息到
spec字段。secretVolumes: - mountPath: /etc/istio/ingressgateway-certs name: ingressgateway-certs secretName: istio-ingressgateway-certs添加之后的 YAML 文件示例如下:
展开查看 YAML
步骤三:在集群中检查入口网关配置
添加证书和私钥挂载卷到入口网关后,入口网关会加载证书与密钥。
-
执行以下命令,验证 tls.crt 与 tls.key 已被挂载到入口网关 Pod 中。
kubectl exec -it -n istio-system $(kubectl -n istio-system get pods -l istio=ingressgateway -o jsonpath='{.items[0].metadata.name}') -- ls -al /etc/istio/ingressgateway-certs预期输出:
lrwxrwxrwx 1 root root 14 May 9 01:14 tls.crt -> ..data/tls.crt lrwxrwxrwx 1 root root 14 May 9 01:14 tls.key -> ..data/tls.key -
执行以下命令,检查 Ingress gateway 证书中的
Subject字段的正确性。kubectl exec -it -n istio-system $(kubectl -n istio-system get pods -l istio=ingressgateway -o jsonpath='{.items[0].metadata.name}') -- cat /etc/istio/ingressgateway-certs/tls.crt | openssl x509 -text -noout | grep 'Subject:'预期输出:
Subject: O = cert-manager + O = cluster.local, CN = istio-ingressgateway-certs -
执行以下命令,检查 Ingress gateway 的代理能够正确访问证书。
kubectl exec -it -n istio-system $(kubectl -n istio-system get pods -l istio=ingressgateway -o jsonpath='{.items[0].metadata.name}') -- curl 127.0.0.1:15000/certs预期输出:
{ "certificates": [ { "ca_cert": [ { "path": "\u003cinline\u003e", "serial_number": "4edcf3", "subject_alt_names": [], "days_until_expiration": "7251", "valid_from": "2022-01-06T06:17:00Z", "expiration_time": "2042-01-01T06:22:03Z" } ], ...........
步骤四:验证使用 HTTPS 协议访问服务是否成功
-
在集群中创建服务。
-
使用以下内容,创建 myexample-nginx.conf。
本文示例服务基于 Nginx 实现,您需要为 Nginx 服务器创建配置文件,以域名 aliyun.com 的服务为例,以下内容定义请求根路径直接返回字样
Welcome to aliyun.com!及状态码200。events { } http { log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log; server { listen 80; location / { return 200 'Welcome to aliyun.com!'; add_header Content-Type text/plain; } } } -
执行以下命令,创建 Nginx 服务器的配置项。
kubectl create configmap myexample-nginx-configmap --from-file=nginx.conf=./myexample-nginx.conf -
执行以下命令,为命名空间 default 启用 Sidecar 自动注入。
kubectl label namespace default istio-injection=enabled -
使用以下内容,创建 myexampleapp.yaml。
展开查看 myexampleapp.yaml
-
执行以下命令,创建域名为 aliyun.com 的内部服务。
kubectl apply -f myexampleapp.yaml
-
-
通过 kubectl 连接 ASM。具体操作,请参见 通过控制面kubectl访问Istio资源。
-
在 ASM 中创建网关规则。
-
使用以下内容,创建 istio-myexample-customingressgateway.yaml。
展开查看 YAML
-
执行以下命令,创建网关规则。
kubectl apply -f istio-myexample-customingressgateway.yaml
-
-
在 ASM 中创建虚拟服务。
-
使用以下内容,创建 istio-myexample-customvirtualservice.yaml。
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: istio-myexample-customvirtualservice spec: hosts: - "www.aliyun.com" gateways: - istio-myexample-customingressgateway http: - route: - destination: host: myexampleapp.default.svc.cluster.local port: number: 80 -
执行以下命令,创建虚拟服务。
kubectl apply -f istio-myexample-customvirtualservice.yaml
-
-
执行以下命令,在集群中获取入口网关服务的地址。
kubectl get svc -n istio-system -l istio=ingressgateway -
执行以下命令,通过 HTTPS 协议访问 aliyun.com 服务。
curl -k -H Host:www.aliyun.com --resolve www.aliyun.com:443:{替换成真实的入口网关IP地址} https://www.aliyun.com预期输出:
Welcome to aliyun.com!