关于后端:APISIX-Ingress-如何使用-Cert-Manager-管理证书

3次阅读

共计 5273 个字符,预计需要花费 14 分钟才能阅读完成。

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.com
helm repo update
helm 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   AGE
pod/apisix-5d99956d88-j68sj                      1/1     Running       0          63s
pod/apisix-69459554d4-btnwn                      0/1     Terminating   0          57m
pod/apisix-etcd-0                                1/1     Running       0          57m
pod/apisix-etcd-1                                1/1     Running       0          57m
pod/apisix-etcd-2                                0/1     Running       0          50s
pod/apisix-ingress-controller-7b5c767cc7-j62hb   1/1     Running       0          55m
pod/cert-manager-5ffd4f6c89-q9f7m                1/1     Running       0          45m
pod/cert-manager-cainjector-748dc889c5-nrvkh     1/1     Running       0          45m
pod/cert-manager-startupapicheck-kmgxf           0/1     Completed     0          45m
pod/cert-manager-webhook-bc964d98b-mkjj7         1/1     Running       0          45m

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/apisix-admin                ClusterIP   10.96.16.25     <none>        9180/TCP                     57m
service/apisix-etcd                 ClusterIP   10.96.232.251   <none>        2379/TCP,2380/TCP            57m
service/apisix-etcd-headless        ClusterIP   None            <none>        2379/TCP,2380/TCP            57m
service/apisix-gateway              NodePort    10.96.118.75    <none>        80:32039/TCP,443:30107/TCP   57m
service/apisix-ingress-controller   ClusterIP   10.96.13.76     <none>        80/TCP                       57m
service/cert-manager-webhook        ClusterIP   10.96.182.188   <none>        443/TCP                      45m

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/apisix                      1/1     1            1           57m
deployment.apps/apisix-ingress-controller   1/1     1            1           57m
deployment.apps/cert-manager                1/1     1            1           45m
deployment.apps/cert-manager-cainjector     1/1     1            1           45m
deployment.apps/cert-manager-webhook        1/1     1            1           45m

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/apisix-5d99956d88                      1         1         1       63s
replicaset.apps/apisix-69459554d4                      0         0         0       57m
replicaset.apps/apisix-ingress-controller-74c6b5fbdd   0         0         0       57m
replicaset.apps/apisix-ingress-controller-7b5c767cc7   1         1         1       55m
replicaset.apps/apisix-ingress-controller-7d58db957c   0         0         0       55m
replicaset.apps/cert-manager-5ffd4f6c89                1         1         1       45m
replicaset.apps/cert-manager-cainjector-748dc889c5     1         1         1       45m
replicaset.apps/cert-manager-webhook-bc964d98b         1         1         1       45m

NAME                           READY   AGE
statefulset.apps/apisix-etcd   2/3     57m

NAME                                     COMPLETIONS   DURATION   AGE
job.batch/cert-manager-startupapicheck   1/1           6m24s      45m

Kubernetes Controller Manager 的机制决定了 Pod 名称会有所不同。

步骤四:申请证书并测试

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

# issuer.yaml
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: issuer
  namespace: ingress-apisix
spec:
  selfSigned: {}

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

kubectl apply -f issuer.yaml

请留神,自签名颁发对象不举荐应用在生产环境中!更多证书颁发对象的配置请参考这里。
而后为域名 httpbin.org 创立一张证书。

# httpbin-cert.yaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: httpbin
  namespace: ingress-apisix
spec:
  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.io
kubectl apply -f httpbin-cert.yaml

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

kubectl get secrets -n ingress-apisix httpbin
NAME      TYPE                DATA   AGE
httpbin   kubernetes.io/tls   3      2m5s

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

# 创立后端
kubectl run httpbin --image kennethreitz/httpbin --namespace ingress-apisix
kubectl expose pod httpbin -n ingress-apisix --port 80
# 定义 ApisixTls 对象
apiVersion: apisix.apache.org/v1
kind: ApisixTls
metadata:
  name: httpbin
  namespace: ingress-apisix
spec:
  hosts:
  - httpbin.org
  secret:
    name: httpbin
    namespace: ingress-apisix
---
# 定义拜访后端的路由
apiVersion: apisix.apache.org/v2beta1
kind: ApisixRoute
metadata:
  name: httpbin
  namespace: ingress-apisix
spec:
  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 理解更多。

正文完
 0