使用HTTPS监听转发来自HTTPS协议的请求时,需要使用证书建立TLS连接。ALB Ingress支持自动发现证书、Secret证书等多种形式为HTTPS监听配置证书。本文介绍如何使用ALB Ingres为HTTPS监听配置证书。
前提条件
- 已安装2.5.0及以上版本的ALB Ingress Controller组件。具体操作,请参见管理组件。
- 已获取到可信的数字证书。您可以从以下三个途径获取数字证书:
- 从阿里云数字证书中心购买证书。具体操作,请参见购买SSL证书。
- 从其他CA购买或自签名的证书。
- 通过K8s Secret资源管理的证书。
背景信息
ALB Ingress支持配置自动发现证书、Secret证书,和AlbConfig指定证书。三种配置方式的区别如下。比较项 | 自动发现证书 | Secret证书 | AlbConfig指定证书 |
---|
适用场景 | 适用于已在阿里云数字证书中心购买证书,或已上传证书到阿里云数字证书中心。 | 适用于集群内部管理的证书,例如使用cert-manager类的管理工具时,ALB实例证书会跟随证书Secret更新而更新。 | 依赖证书上传到证书中心,适用于同域名多个证书情况下,指定确定证书的场景。 |
自动更新ALB关联证书 | 不支持,需要手动更改证书。 | 支持。 | 不支持,需手动更换证书。 |
优先级 | 低。 | 中。 | 高。 |
跨命名空间使用 | 支持。 | 不支持,仅支持在Secret命名空间使用。 | 支持,AlbConfig是集群粒度类型的资源,会在整个集群生效。 |
更新证书方式 | 在数字证书中心上传新证书或续费证书后。需手动修改Ingress内容(例如修改Ingress名称、注解等),以实现自动证书发现。 | 更新Ingress关联的Secret资源。 | 在数字证书中心上传新证书或续费证书后,需手动更新AlbConfig资源,指定新的证书ID。 |
相关文档 | 配置自动发现证书 | 配置Secret证书 | 使用AlbConfig指定证书 |
配置自动发现证书
以下以自签名证书为例,介绍如何配置自动发现证书。
说明 同一ALB支持的扩展证书上限为25个,包含同域名的证书总数不能超过这个阈值。
- 执行以下命令,通过OpenSSL创建自签名证书。
openssl genrsa -out albtop-key.pem 4096
openssl req -subj "/CN=demo.alb.ingress.top" -sha256 -new -key albtop-key.pem -out albtop.csr
echo subjectAltName = DNS:demo.alb.ingress.top > extfile.cnf
openssl x509 -req -days 3650 -sha256 -in albtop.csr -signkey albtop-key.pem -out albtop-cert.pem -extfile extfile.cnf
- 上传自签名证书至阿里云数字证书中心。具体操作,请参见上传
和共享SSL
证书。
- 创建Ingress、Service和应用。
- 使用以下内容,创建demo.yaml。
在Ingress的YAML中添加证书对应的域名。
tls:
- hosts:
- demo.alb.ingress.top
展开查看完整示例apiVersion: v1
kind: Service
metadata:
name: demo-service-https
namespace: default
spec:
ports:
- name: port1
port: 443
protocol: TCP
targetPort: 8080
selector:
app: demo-cafe
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-cafe
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo-cafe
template:
metadata:
labels:
app: demo-cafe
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/alb-sample/cafe:v1
imagePullPolicy: IfNotPresent
name: demo-cafe
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-https
namespace: default
spec:
ingressClassName: alb
rules:
- host: demo.alb.ingress.top
http:
paths:
- backend:
service:
name: demo-service-https
port:
number: 443
path: /
pathType: Prefix
tls:
- hosts:
- demo.alb.ingress.top
- 执行以下命令,创建Ingress、Service和应用。
kubectl apply -f demo.yaml
- 执行以下命令,验证证书是否配置成功。
curl https://demo.alb.ingress.top/tea
预期输出:
{"hello":"tee"}
返回以上结果,说明证书配置成功。
配置Secret证书
以下以自签名证书为例,介绍如何配置Secret证书。
说明 同一证书支持创建不同的Secret,ALB也支持关联多个证书,但同一ALB支持的扩展证书上限为25个。
- 执行以下命令,通过OpenSSL创建自签名证书。
openssl genrsa -out albtop-key.pem 4096
openssl req -subj "/CN=demo.alb.ingress.top" -sha256 -new -key albtop-key.pem -out albtop.csr
echo subjectAltName = DNS:demo.alb.ingress.top > extfile.cnf
openssl x509 -req -days 3650 -sha256 -in albtop.csr -signkey albtop-key.pem -out albtop-cert.pem -extfile extfile.cnf
- 在Base64中base64编码步骤1的albtop-key.pem和albtop-cert.pem。
echo -n <albtop-key.pem> | base64
echo -n <albtop-cert.pem> | base64
- 创建Secret。
- 使用以下内容,创建secret.yaml。
apiVersion: v1
kind: Secret
metadata:
name: secret-tls
type: kubernetes.io/tls
data:
# the data is abbreviated in this example
tls.crt: |
{base64 albtop-cert.pem} # Base64编码后的albtop-cert.pem。
tls.key: |
{base64 albtop-key.pem} # Base64编码后的albtop-key.pem。
- 执行以下命令,创建Secret。
kubectl apply -f secret.yaml
- 创建Ingress、Service和应用。
- 使用以下内容,创建demo.yaml。
在Ingress的YAML中添加证书对应的域名。
tls:
- hosts:
- demo.alb.ingress.top
secretName: secret-tls
展开查看完整示例apiVersion: v1
kind: Service
metadata:
name: demo-service-https
namespace: default
spec:
ports:
- name: port1
port: 443
protocol: TCP
targetPort: 8080
selector:
app: demo-cafe
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-cafe
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo-cafe
template:
metadata:
labels:
app: demo-cafe
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/alb-sample/cafe:v1
imagePullPolicy: IfNotPresent
name: demo-cafe
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-https
namespace: default
spec:
ingressClassName: alb
rules:
- host: demo.alb.ingress.top
http:
paths:
- backend:
service:
name: demo-service-https
port:
number: 443
path: /
pathType: Prefix
tls:
- hosts:
- demo.alb.ingress.top
secretName: secret-tls
- 执行以下命令,创建Ingress、Service和应用。
kubectl apply -f demo.yaml
- 执行以下命令,验证证书是否配置成功。
curl https://demo.alb.ingress.top/tea
预期输出:
{"hello":"tee"}
返回以上结果,说明证书配置成功。
使用AlbConfig指定证书
以下以自签名证书为例,介绍如何使用AlbConfig指定证书。
说明 如果监听配置了证书ID,那么相同监听下的域名不会再使用Secret证书和自动证书发现。
- 执行以下命令,通过OpenSSL创建自签名证书。
openssl genrsa -out albtop-key.pem 4096
openssl req -subj "/CN=demo.alb.ingress.top" -sha256 -new -key albtop-key.pem -out albtop.csr
echo subjectAltName = DNS:demo.alb.ingress.top > extfile.cnf
openssl x509 -req -days 3650 -sha256 -in albtop.csr -signkey albtop-key.pem -out albtop-cert.pem -extfile extfile.cnf
- 上传自签名证书至阿里云数字证书中心。具体操作,请参见上传
和共享SSL
证书。
- 获取证书ID。
- 登录数字证书管理服务控制台。
- 在控制台左侧导航栏,单击SSL证书。
- 在SSL证书页面,单击上传证书页签,在目标证书操作列下选择。
在证书详情面板中获取证书ID。
- 创建AlbConfig。
- 使用以下内容,创建albconfig.yaml。
apiVersion: alibabacloud.com/v1
kind: AlbConfig
metadata:
name: alb-demo
spec:
config:
#...
listeners:
- caEnabled: false
certificates:
- CertificateId: 756****-cn-hangzhou # 证书ID
IsDefault: true
port: 443
protocol: HTTPS
#...
参数 | 描述 |
---|
CertificateId | 证书ID。本文配置为756****-cn-hangzhou 。CertificateId 格式示例及说明如下:
- 中国地域:
756****-cn-hangzhou 。-cn-hangzhou 为固定内容,不受地域影响,配置时您只需替换756**** 即可。 - 海外地域:
756****-ap-southeast-1 。-ap-southeast-1 为固定内容,不受地域影响,配置时您只需替换756**** 即可。
|
IsDefault | 是否为默认证书。本文配置为true ,表示是默认证书。 |
protocol | 支持监听的协议类型。本文配置为HTTPS ,表示支持HTTPS协议的监听。 |
- 执行以下命令,创建AlbConfig。
kubectl apply -f albconfig.yaml
- 创建Ingress、Service和应用。
- 使用以下内容,创建demo.yaml。
说明 本例Ingress中的tls
字段只作为挂载到443监听标识,并不是自动发现证书的配置。
展开查看详细内容apiVersion: v1
kind: Service
metadata:
name: demo-service-https
namespace: default
spec:
ports:
- name: port1
port: 443
protocol: TCP
targetPort: 8080
selector:
app: demo-cafe
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-cafe
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo-cafe
template:
metadata:
labels:
app: demo-cafe
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/alb-sample/cafe:v1
imagePullPolicy: IfNotPresent
name: demo-cafe
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-https
namespace: default
spec:
ingressClassName: alb
rules:
- host: demo.alb.ingress.top
http:
paths:
- backend:
service:
name: demo-service-https
port:
number: 443
path: /
pathType: Prefix
tls:
- hosts:
- demo.alb.ingress.top
- 执行以下命令,创建Ingress、Service和应用。
kubectl apply -f demo.yaml
- 执行以下命令,验证证书是否配置成功。
curl https://demo.alb.ingress.top/tea
预期输出:
{"hello":"tee"}
返回以上结果,说明证书配置成功。