Knative配置HTTPS

1 证书管理工具cert-manager

cert-manager是用于HTTPS连接的TLS证书管理工具。接下来,通过两个步骤来安装cert-manager。

1)安装cert-manager。

# 如果Kubernetes版本>=1.15,执行以下命令
$ kubectl apply--validate=false-f https://github.com/jetstack/cert-manager/
  releases/download/v0.16.1/cert-manager.yaml
# 如果Kubernetes版本<1.15,执行以下命令
$ kubectl apply--validate=false-f https://github.com/jetstack/cert-manager/
  releases/download/v0.16.1/cert-manager-legacy.yaml

2)配置DNS提供商来验证DNS-01 challenge请求。

默认情况下,Let’s Encrypt用于演示如何配置cert-manager。也可以使用其他被支持的CA证书,但必须使用DNS-01 challenge类型来验证请求。

需要注意的是,Let’s Encrypt颁发的证书的有效期为90天。如果选择手动获取和配置证书的方式,一定要保证在过期前更新证书。

2 手动配置TLS证书

接下来,使用cert-manager工具手动获取TLS证书,并将证书配置到Knative的入口网关。

1.获取TLS证书

1)配置DNS01 Challenge提供商,这里以Cloud DNS为例。

piVersion: cert-manager.io/v1alpha2
kind: ClusterIssuer
metadata:
  name: letsencrypt-issuer
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    # This will register an issuer with LetsEncrypt.  
    # Replace with your admin email address.
    email: myemail@gmail.com
    privateKeySecretRef:
      # Set privateKeySecretRef to any unused secret name.
      name: letsencrypt-issuer
    solvers:
   - dns01:
        clouddns:
          # Set this to your GCP project-id
          project: $PROJECT_ID
          # Set this to the secret that we publish our service account key
          # in the previous step.
          serviceAccountSecretRef:
            name: cloud-dns-key
            key: key.json

2)创建一个证书,并将其存放在istio-ingressgateway-certs Secret对象中。

# Change this value to the domain you want to use.
export DOMAIN=<your-domain.com>
kubectl apply--filename- <<EOF
apiVersion: cert-manager.io/v1alpha2
kind: Certificate
metadata:
  name: my-certificate
  namespace: istio-system
spec:
  secretName: istio-ingressgateway-certs
  issuerRef:
    name: letsencrypt-issuer
    kind: ClusterIssuer
  dnsNames:
 - "*.default.$DOMAIN"
 - "*.other-namespace.$DOMAIN"
EOF

2.添加TLS证书到入口网关

注意:如果已经拥有一个在自己的域名上已签名的证书,可以直接手动添加TLS证书。

TLS证书手动添加到Knative集群,需要首先创建一个带有证书信息的Kubernetes Secret,然后配置knative-ingress-gateway。具体命令如下。

1)运行以下命令创建一个Kubernetes Secret,以保存TLS证书cert.pem和私钥cert.pk。

kubectl create--namespace istio-system secret tls istio-ingressgateway-certs  
 --key cert.pk--cert cert.pem

2)使用新建的Secret进行HTTPS连接。

运行以下命令,以编辑模式打开Knative Gateway:

kubectl edit gateway knative-ingress-gateway--namespace knative-serving

更新gateway tls部分的配置:

tls:
  mode: SIMPLE
  privateKey: /etc/istio/ingressgateway-certs/tls.key
  serverCertificate: /etc/istio/ingressgateway-certs/tls.crt

下面是完整knative-ingress-gateway配置范例:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: knative-ingress-gateway
  namespace: knative-serving
spec:
  selector:
    istio: ingressgateway
  servers:
 - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
   - "*"
    tls:
      # Sends 301 redirect for all http requests.
      # Omit to allow http and https.
      httpsRedirect: true
 - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
   - "*"
    tls:
      mode: SIMPLE
      privateKey: /etc/istio/ingressgateway-certs/tls.key
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt

3 自动配置TLS证书

集群中安装和配置了cert-manager证书管理组件后,可以将Knative配置成自动为Knative Service获取新的TLS证书并更新现有的证书。

1.TLS自动生成模式

Knative支持下面两种自动TLS生成模式。

1)DNS-01 challenge模式

1)为每个命名空间生成证书。如果想要更快地生成证书,推荐使用该模式。在该模式下,每个命名空间将会生成一个证书,同一命名空间的不同Knative服务共用该证书。

2)为每个Knative Service生成证书。如果想要在Knative Service之间更好地实现证书隔离,建议使用该模式。在该模式下,每个Knative Service将生成一个单独的证书。因为需要为每个Knative Service生成证书,所以TLS生效时间更长。

注意:在该模式下,集群需要与DNS服务器通信,以验证你对域名的所有权。

2)HTTP-01 challenge模式

在该模式下,集群不需要与DNS服务器进行通信。你只需要将自己的域名映射到集群Ingress的IP地址。在HTTP-01 challenge模式下,将为每个Knative服务生成一个证书。该模式不支持按命名空间生成证书。

2.TLS自动生成的前提条件

1)Knative集群中需要安装的组件包括Knative Serving、Istio 1.3以上版本、cert-manager 0.12以上版本。

2)Knative集群必须配置自定义域名。

3)必须拥有自己的DNS服务器的修改权限,并配置好自定义域名。

4)如果要使用HTTP-01 challenge模式,则需要配置自定义域名以映射到Ingress的IP地址。可以通过在DNS服务器中添加A记录的方式来实现。

3.开启TLS自动配置

为了让Knative支持自动TLS配置,需要完成以下几个步骤。

1)创建cert-manager的ClusterIssuer。

创建ClusterIssuer配置文件并将其添加到Knative集群,并定义谁颁发TLS证书、如何验证请求以及哪个DNS提供商验证这些请求。

①DNS-01 challenge模式下的ClusterIssuer。

Google Cloud DNS为例的ClusterIssuer配置文件如下:

apiVersion: cert-manager.io/v1alpha2
kind: ClusterIssuer
metadata:
  name: letsencrypt-dns-issuer
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    # This will register an issuer with LetsEncrypt.  
    # Replace with your admin email address.
    email: myemail@gmail.com
    privateKeySecretRef:
      # Set privateKeySecretRef to any unused secret name.
      name: letsencrypt-dns-issuer
    solvers:
   - dns01:
        clouddns:
          # Set this to your GCP project-id
          project: $PROJECT_ID
          # Set this to the secret that we publish our service account key
          # in the previous step.
          serviceAccountSecretRef:
            name: cloud-dns-key
            key: key.json

②HTTP-01 challenge模式下的ClusterIssuer。

HTTP-02 challenge模式实现的ClusterIssuer配置文件示例如下:

apiVersion: cert-manager.io/v1alpha2
kind: ClusterIssuer
metadata:
  name: letsencrypt-http01-issuer
spec:
  acme:
    privateKeySecretRef:
      name: letsencrypt
    server: https://acme-v02.api.letsencrypt.org/directory
    solvers:
   - http01:
       ingress:
         class: istio

将以上ClusterIssuer配置文件应用到集群中:

kubectl apply-f  cluster-issuer.yaml

2)安装networking-certmanager:

kubectl apply--filename https://github.com/knative/net-certmanager/releases/download/v0.16.0/release.yaml

3)安装networking-ns-cert组件:

如果选择按每个命名空间生成证书的模式,就需要安装networking-ns-cert组件,命令如下:

kubectl apply--filename https://github.com/knative/serving/releases/download/
  v0.16.0/serving-nscert.yaml

4)配置config-certmanager ConfigMap。

knative-serving命名空间更新config-certmanager ConfigMap配置,将其引用到新的ClusterIssuer。

①使用以下命令编辑config-certmanager ConfigMap:

kubectl edit configmap config-certmanager--namespace knative-serving

②在data部分添加issuerRef:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-certmanager
  namespace: knative-serving
  labels:
    networking.knative.dev/certificate-provider: cert-manager
data:
  issuerRef: |
    kind: ClusterIssuer
    name: letsencrypt-http01-issuer

③检查更新,确保引用成功:

kubectl get configmap config-certmanager--namespace knative-serving--output yaml

5)开启TLS自动配置。

更新knative-serving命名空间的config-network ConfigMap,启用autoTLS并指定如何处理HTTP请求。

①使用以下命令编辑config-network ConfigMap:

kubectl edit configmap config-network--namespace knative-serving

②在data部分添加autoTLS:Enabled属性:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-network
  namespace: knative-serving
data:
   ...
   autoTLS: Enabled
   ...

③在httpProtocol属性中配置如何处理HTTP和HTTPS请求。

默认情况下,将Knative Ingress配置为提供HTTP流量(httpProtocol:已启用)。现在,你的群集已配置为使用TLS证书并处理HTTP流量。

httpProtocol支持的配置如下。

·Enabled:服务HTTP流量。

·Disabled:拒绝所有HTTP通信。

·Redirected:以302重定向来响应HTTP请求,使得客户端使用HTTPS连接。

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-network
  namespace: knative-serving
data:
  ...
  autoTLS: Enabled
  ...
  httpProtocol: Redirected
  ...

④检查更新已确保成功:

kubectl get configmap config-network--namespace knative-serving--output yaml

6)验证autoTLS。

①创建一个Knative Service:

kubectl apply-f https://raw.githubusercontent.com/knative/docs/master/docs/
  serving/autoscaling/autoscale-go/service.yaml

②证书生成后,你应该看到类似以下的内容:

NAME          URL                            LATESTCREATED  LATESTREADY    READY  REASON
autoscale-go  https://autoscale-go.default.  autoscale-go-  autoscale-go-  True
              {custom-domain}                6jf85          6jf85

 

posted @ 2023-01-25 16:29  muzinan110  阅读(83)  评论(0编辑  收藏  举报