# 使用 cert-manager 管理网关的证书
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/03.%20%E8%BF%90%E7%BB%B4%E6%89%8B%E5%86%8C/02.%20K8S%E8%BF%90%E7%BB%B4/10.%20K8S%20%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6/05.%20cert-manager


# 使用 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

1. 在本地安装 Helm。具体操作，请参见 [Helm](https://github.com/helm/helm?spm=a2c4g.11186623.0.0.589e7b7dpNmXXE)。

2. 使用 kubectl 连接集群。具体操作，请参见 [获取集群KubeConfig并通过kubectl工具连接集群](https://help.aliyun.com/zh/ack/ack-managed-and-ack-dedicated/user-guide/obtain-the-kubeconfig-file-of-a-cluster-and-use-kubectl-to-connect-to-the-cluster#task-2076136)。

3. 执行以下命令，创建 cert-manager 命名空间。

   ```bash
   kubectl create namespace cert-manager
   ```

4. 执行以下命令，添加 cert-manager Chart。

   ```bash
   helm repo add jetstack https://charts.jetstack.io
   ```

5. 执行以下命令，获取 cert-manager Chart 的最新信息。

   ```bash
   helm repo update
   ```

6. 执行以下命令，安装 cert-manager。

   **说明**

   cert-manager 的版本需要和 Kubernetes 版本保持兼容。关于 cert-manager 和 Kubernetes 版本的对应关系，请参见 [Supported Releases](https://cert-manager.io/docs/installation/supported-releases/)。

   ```bash
   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](https://cert-manager.io/docs/releases/#kubernetes-supported-versions) | [Tested Kubernetes Versions](https://cert-manager.io/docs/releases/#supported-vs-tested) |
| ----------------------------------------------------------------------------------- | ------------ | --------------- | ----------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| [1.16](https://cert-manager.io/docs/releases/release-notes/release-notes-1.16/)     | Oct 03, 2024 | Release of 1.18 | 1.25 → 1.31 / 4.14 → 4.16                                                                                         | 1.27 → 1.31                                                                              |
| [1.15](https://cert-manager.io/docs/releases/release-notes/release-notes-1.15/)     | Jun 05, 2024 | Release of 1.17 | 1.25 → 1.31 / 4.12 → 4.16                                                                                         | 1.25 → 1.31                                                                              |
| [1.12 LTS](https://cert-manager.io/docs/releases/release-notes/release-notes-1.12/) | May 19, 2023 | May 19, 2025    | 1.22 → 1.31 / 4.9 → 4.16                                                                                          | 1.22 → 1.29                                                                              |

## 使用 cert-manager 签发自签名证书

### 步骤一：在集群中生成自签证书

1. 使用以下内容，创建 issuer.yaml。

   **展开查看 issuer.yaml**

2. 执行以下命令，创建自签名 CA 来颁发工作负载证书。

   ```bash
   kubectl apply -f issuer.yaml -n istio-system
   ```

3. 执行以下命令，查看证书。

   ```bash
   kubectl get secret -n istio-system 
   ```

   预期输出：

   ```bash
   NAME                                        TYPE                             DATA         AGE
   istio-ingressgateway-certs                  kubernetes.io/tls                3            68m
   ```

### 步骤二：添加证书和私钥挂载卷到入口网关

1. 登录 [ASM控制台](https://servicemesh.console.aliyun.com/)，在左侧导航栏，选择***\* 服务网格\** > \**网格管理\****。

2. 在**网格管理**页面，单击目标实例名称，然后在左侧导航栏，选择***\*ASM 网关\** > \**入口网关\****。

3. 在**入口网关**页面的目标网关右侧，单击**查看 YAML**。

4. 在**编辑**对话框，添加如下信息到 `spec` 字段。

   ```yaml
     secretVolumes:
       - mountPath: /etc/istio/ingressgateway-certs
         name: ingressgateway-certs
         secretName: istio-ingressgateway-certs
   ```

   添加之后的 YAML 文件示例如下：

   **展开查看 YAML**

### 步骤三：在集群中检查入口网关配置

添加证书和私钥挂载卷到入口网关后，入口网关会加载证书与密钥。

1. 执行以下命令，验证 tls.crt 与 tls.key 已被挂载到入口网关 Pod 中。

   ```bash
   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
   ```

   预期输出：

   ```bash
   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
   ```

2. 执行以下命令，检查 Ingress gateway 证书中的 `Subject` 字段的正确性。

   ```bash
   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:'
   ```

   预期输出：

   ```bash
   Subject: O = cert-manager + O = cluster.local, CN = istio-ingressgateway-certs
   ```

3. 执行以下命令，检查 Ingress gateway 的代理能够正确访问证书。

   ```bash
   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
   ```

   预期输出：

   ```bash
   {
    "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 协议访问服务是否成功

1. 在集群中创建服务。

   1. 使用以下内容，创建 myexample-nginx.conf。

      本文示例服务基于 Nginx 实现，您需要为 Nginx 服务器创建配置文件，以域名 aliyun.com 的服务为例，以下内容定义请求根路径直接返回字样 `Welcome to aliyun.com!` 及状态码 `200`。

      ```python
      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;
          }
        }
      }
      ```

   2. 执行以下命令，创建 Nginx 服务器的配置项。

      ```bash
      kubectl create configmap myexample-nginx-configmap --from-file=nginx.conf=./myexample-nginx.conf
      ```

   3. 执行以下命令，为命名空间 default 启用 Sidecar 自动注入。

      ```bash
      kubectl label namespace default istio-injection=enabled
      ```

   4. 使用以下内容，创建 myexampleapp.yaml。

      **展开查看 myexampleapp.yaml**

   5. 执行以下命令，创建域名为 aliyun.com 的内部服务。

      ```bash
      kubectl apply -f myexampleapp.yaml
      ```

2. 通过 kubectl 连接 ASM。具体操作，请参见 [通过控制面kubectl访问Istio资源](https://help.aliyun.com/zh/asm/user-guide/use-kubectl-on-the-control-plane-to-access-istio-resources#task-2390744)。

3. 在 ASM 中创建网关规则。

   1. 使用以下内容，创建 istio-myexample-customingressgateway.yaml。

      **展开查看 YAML**

   2. 执行以下命令，创建网关规则。

      ```bash
      kubectl apply -f istio-myexample-customingressgateway.yaml
      ```

4. 在 ASM 中创建虚拟服务。

   1. 使用以下内容，创建 istio-myexample-customvirtualservice.yaml。

      ```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
      ```

   2. 执行以下命令，创建虚拟服务。

      ```bash
      kubectl apply -f istio-myexample-customvirtualservice.yaml
      ```

5. 执行以下命令，在集群中获取入口网关服务的地址。

   ```bash
   kubectl get svc -n istio-system -l istio=ingressgateway
   ```

6. 执行以下命令，通过 HTTPS 协议访问 aliyun.com 服务。

   ```bash
   curl -k -H Host:www.aliyun.com --resolve www.aliyun.com:443:{替换成真实的入口网关IP地址}  https://www.aliyun.com
   ```

   预期输出：

   ```bash
   Welcome to aliyun.com!
   ```
