Apache APISIX Ingress Controller 是一款以 Apache APISIX 作为数据面的 Kubernetes Ingress Controller 开源工具,目前曾经更新到 v1.3 版本,实现了如证书治理、负载平衡、金丝雀公布等性能。

长久以来,证书治理都不是一件简略的事件,尽管 Apache APISIX Ingress Controller 反对从 Kubernetes Secrets 资源中提取证书和私钥,并转换为 Apache APISIX 可辨认的 SSL 对象,但这只是整个证书治理链中的一部分,证书的颁发、轮转、撤消逻辑仍然须要管理员执行,尤其当证书数量比拟多时,工作量往往并不小,因此会占用管理员不少的工夫。

Cert Manager 是一款致力于在 Kubernetes 平台上简化证书治理的软件,它反对对接许多不同的证书源,如 Let's Encrypt 和 HashiCorp Vault。

如果你在应用 Apache APISIX Ingress Controller 时,遇到了证书治理的麻烦,那么应用 Cert Manager 将会是一个不错的抉择,本文将介绍如何通过 Cert Manager 来创立证书并对接到 Apache APISIX Ingress Controller。

步骤一:环境筹备

如果你心愿依照本文的领导进行理论的操作,请确保以下环境和工具已准备就绪:

  1. 筹备一个可用的 Kubernetes 集群,开发环境中,你能够应用 Kind 和 Minikube
  2. 装置 kubectl
  3. 装置 Helm v3
请留神,下文所有的操作都将在 ingress-apisix 命名空间中执行,因而须要先创立该命名空间:kubectl create namespace ingress-apisix

步骤二:装置 Apache APISIX Ingress Controller

咱们能够通过 Helm 来装置 Apache APISIX Ingress Controller,包含数据面的 Apache APISIX 和 etcd 集群。

helm repo add apisix https://charts.apiseven.comhelm repo updatehelm install apisix apisix/apisix --set gateway.tls.enabled=true --set ingress-controller.enabled=true --namespace ingress-apisix

点击查看具体装置介绍。

步骤三:装置 Cert Manager

通过 Helm 来装置 Cert Manager,点击可查看具体装置介绍。

helm install cert-manager jetstack/cert-manager --namespace ingress-apisix  --set prometheus.enabled=false --set installCRDs=true

装置结束后请期待一会后查看组件的运行状态,确保所有组件都已失常运行,你能够通过如下命令进行查看。

kubectl get all -n ingress-apisix

返回后果如下所示,示意所有组件都已失常运行。

NAME                                             READY   STATUS        RESTARTS   AGEpod/apisix-5d99956d88-j68sj                      1/1     Running       0          63spod/apisix-69459554d4-btnwn                      0/1     Terminating   0          57mpod/apisix-etcd-0                                1/1     Running       0          57mpod/apisix-etcd-1                                1/1     Running       0          57mpod/apisix-etcd-2                                0/1     Running       0          50spod/apisix-ingress-controller-7b5c767cc7-j62hb   1/1     Running       0          55mpod/cert-manager-5ffd4f6c89-q9f7m                1/1     Running       0          45mpod/cert-manager-cainjector-748dc889c5-nrvkh     1/1     Running       0          45mpod/cert-manager-startupapicheck-kmgxf           0/1     Completed     0          45mpod/cert-manager-webhook-bc964d98b-mkjj7         1/1     Running       0          45mNAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGEservice/apisix-admin                ClusterIP   10.96.16.25     <none>        9180/TCP                     57mservice/apisix-etcd                 ClusterIP   10.96.232.251   <none>        2379/TCP,2380/TCP            57mservice/apisix-etcd-headless        ClusterIP   None            <none>        2379/TCP,2380/TCP            57mservice/apisix-gateway              NodePort    10.96.118.75    <none>        80:32039/TCP,443:30107/TCP   57mservice/apisix-ingress-controller   ClusterIP   10.96.13.76     <none>        80/TCP                       57mservice/cert-manager-webhook        ClusterIP   10.96.182.188   <none>        443/TCP                      45mNAME                                        READY   UP-TO-DATE   AVAILABLE   AGEdeployment.apps/apisix                      1/1     1            1           57mdeployment.apps/apisix-ingress-controller   1/1     1            1           57mdeployment.apps/cert-manager                1/1     1            1           45mdeployment.apps/cert-manager-cainjector     1/1     1            1           45mdeployment.apps/cert-manager-webhook        1/1     1            1           45mNAME                                                   DESIRED   CURRENT   READY   AGEreplicaset.apps/apisix-5d99956d88                      1         1         1       63sreplicaset.apps/apisix-69459554d4                      0         0         0       57mreplicaset.apps/apisix-ingress-controller-74c6b5fbdd   0         0         0       57mreplicaset.apps/apisix-ingress-controller-7b5c767cc7   1         1         1       55mreplicaset.apps/apisix-ingress-controller-7d58db957c   0         0         0       55mreplicaset.apps/cert-manager-5ffd4f6c89                1         1         1       45mreplicaset.apps/cert-manager-cainjector-748dc889c5     1         1         1       45mreplicaset.apps/cert-manager-webhook-bc964d98b         1         1         1       45mNAME                           READY   AGEstatefulset.apps/apisix-etcd   2/3     57mNAME                                     COMPLETIONS   DURATION   AGEjob.batch/cert-manager-startupapicheck   1/1           6m24s      45m
Kubernetes Controller Manager 的机制决定了 Pod 名称会有所不同。

步骤四:申请证书并测试

首先咱们须要配置证书颁发对象。

# issuer.yamlapiVersion: cert-manager.io/v1kind: Issuermetadata:  name: issuer  namespace: ingress-apisixspec:  selfSigned: {}

并创立自签名证书颁发者。

kubectl apply -f issuer.yaml
请留神,自签名颁发对象不举荐应用在生产环境中!更多证书颁发对象的配置请参考这里。
而后为域名 httpbin.org 创立一张证书。
# httpbin-cert.yamlapiVersion: cert-manager.io/v1kind: Certificatemetadata:  name: httpbin  namespace: ingress-apisixspec:  secretName: httpbin  duration: 2160h # 90d  renewBefore: 360h # 15d  subject:    organizations:      - foo  commonName: httpbin.org  isCA: false  privateKey:    algorithm: RSA    encoding: PKCS1    size: 2048  usages:    - server auth  dnsNames:    - "httpbin.org"    - "*.httpbin.org"  issuerRef:    name: issuer    kind: Issuer    group: cert-manager.iokubectl apply -f httpbin-cert.yaml

此时须要查看对应 Secrets 是否曾经被创立。

kubectl get secrets -n ingress-apisix httpbinNAME      TYPE                DATA   AGEhttpbin   kubernetes.io/tls   3      2m5s

通过上述验证,该 Secrets 对象的创立事件曾经被 Apache APISIX Ingress Controller 捕捉到,咱们尝试拜访 Apache APISIX Ingress Controller 来验证证书是否失效,首先咱们须要创立额定的路由对象。

# 创立后端kubectl run httpbin --image kennethreitz/httpbin --namespace ingress-apisixkubectl expose pod httpbin -n ingress-apisix --port 80# 定义 ApisixTls 对象apiVersion: apisix.apache.org/v1kind: ApisixTlsmetadata:  name: httpbin  namespace: ingress-apisixspec:  hosts:  - httpbin.org  secret:    name: httpbin    namespace: ingress-apisix---# 定义拜访后端的路由apiVersion: apisix.apache.org/v2beta1kind: ApisixRoutemetadata:  name: httpbin  namespace: ingress-apisixspec:  http:  - name: httpbin    match:      paths:      - /*      hosts:      - httpbin.org    backends:    - serviceName: httpbin      servicePort: 80

接下来拜访服务 apisix-gateway。留神,默认状况下该服务的类型为 NodePort,你能够依据须要批改其类型,比方你的 Kubernetes 集群是云厂商托管的,则能够思考将其批改为 LoadBalancer 类型,以获取一个内部可达的 IP。

这里咱们通过端口转发的形式将服务映射到本地。

kubectl port-forward -n ingress-apisix svc/apisix-gateway 8443:443

而后开始配置拜访。

curl https://httpbin.org:8443/json --resolve 'httpbin.org:8443:127.0.0.1' -sk{  "slideshow": {    "author": "Yours Truly",    "date": "date of publication",    "slides": [      {        "title": "Wake up to WonderWidgets!",        "type": "all"      },      {        "items": [          "Why <em>WonderWidgets</em> are great",          "Who <em>buys</em> WonderWidgets"        ],        "title": "Overview",        "type": "all"      }    ],    "title": "Sample Slide Show"  }}

通过上述操作,能够看到拜访胜利,阐明证书曾经失效。留神,因为证书是自签名的,这里须要加上 -k 选项来疏忽证书的校验。

此外,如果你想要轮转证书,删除 httpbin 这一 Secret 对象即可,Cert Manager 会立即创立一个新的 httpbin Secret 对象,并且蕴含新的证书。

总结

本文次要解说了如何利用 Cert Manager 在 Apache APISIX Ingress Controller 中进行证书的创立和治理。想理解更多对于 Apache APISIX Ingress 的介绍与内容,可参考本篇文章 或者参加 Apache APISIX Ingress 我的项目每两周举办的线上探讨,分享当下我的项目进度、最佳实际及设计思路等多个话题,可查看具体 issue 理解更多。