You need to enable JavaScript to run this app.
导航

Nginx Ingress 配置 HTTPS 协议

最近更新时间2024.04.23 11:40:45

首次发布时间2023.12.26 19:38:55

为保证公网环境下的通信安全,业务使用 Ingress 暴露到公网时,通常使用 HTTPS 协议通信。本文为您介绍如何配置 HTTPS 协议的 Nignx Ingress。

前提条件

操作步骤

步骤一:配置保密字典

  1. 已获取 SSL 证书和密钥,本文以tls.keytls.crt为例。
  2. 执行以下命令,创建名称为 ingress-secret,类型为kubernetes.io/tls的保密字典。
kubectl create secret tls ingress-secret --key tls.key --cert tls.crt

步骤二:配置路由规则

  1. 创建 Ingress 的 YAML 文件。示例文件nginx-ingress.yaml代码如下:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress # 路由规则的名称
spec:
  ingressClassName: nginx # 指定 Ingress Controller
  rules:
  - host: example.com # 转发规则域名
    http:
      paths:
      - backend:
          service:
            name: service-demo # 请求被转发到的目标服务名称
            port:
              number: 80 # 请求被转发到的目标服务开放端口号
        path: / # 访问路径
        pathType: Prefix # 路径类型:Exact(精确匹配)/Prefix(前缀匹配)
  tls:
  - hosts:
    - example.com # (可选)指定需要加密的域名。不配置表示加密所有的域名
    secretName: ingress-secret # 指定 ingress 使用的保密字典名称

说明

  • 您需要自主保障在公网环境下,自定义域名和 ingress 均衡负载 EIP 的 DNS 解析关系。
  • 当选择 HTTPS 协议时,系统默认会同时开放 80 端口和 443 端口。为保证数据安全,建议配置 80 端口重定向,即将 HTTP 重定向到 HTTPS,详情请参见 Nginx Ingress 配置重定向规则
  1. 执行以下命令,创建 Ingress。
kubectl apply -f nginx-ingress.yaml

结果验证

查看配置

  1. 执行以下命令,查看 ingress 配置。
kubectl get ingress

预期返回结果如下,表示 Ingress 创建成功。

NAME            CLASS   HOSTS           ADDRESS           PORTS   AGE
nginx-ingress   nginx   example.com     180.xxx.xxx.xxx   80,443  74s

访问服务

使用以下命令,通过域名访问服务。

curl -H "Host: example.com" https://180.xxx.xxx.xxx --insecure

注意

  • 当您使用自签名证书时,可以使用-k--insecure参数忽略证书的验证。
  • 在生产环境中使用 TLS 证书时,建议您使用官方可信证书。使用自签名证书且忽略证书验证,可能导致您受到中间人攻击。

预期输出如下,表示可以通过域名访问到后端服务。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

参考信息

如何使用 OpenSSL 创建自签名证书

  1. 在本地环境中,访问 OpenSSL 官网,下载并安装 OpenSSL。
  2. 执行以下命令,生成 2048 位的 RSA 私钥。
openssl genrsa -out tls.key 2048

预期结果如下:

Generating RSA private key, 2048 bit long modulus
........+++++
...+++++
e is 65537 (0x10001)
  1. 执行以下命令,使用 RSA 私钥生成证书。
openssl req -new -key tls.key -out tls.csr

预期结果如下,需要填写证书的相关信息,包括:国家、地区、组织名称、域名、电子邮件地址等。

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:CN # 2 位国家代码,例如 CN
State or Province Name (full name) []:Shanghai # 地区名称,例如 Shanghai
Locality Name (eg, city) []:Shanghai # 城市名称,例如 Shanghai
Organization Name (eg, company) []:vke # 组织名称
Organizational Unit Name (eg, section) []:vke # 组织中的单位名称
Common Name (eg, fully qualified host name) []:example.com # 该证书对应的域名
Email Address []:user@example.com # 电子邮件地址

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:mypassword # 密码,可以为空
  1. 执行以下命令,使用私钥对证书进行签名,有效期配置为 365 天。
openssl x509 -req -in tls.csr -out tls.crt -signkey tls.key -days 365
  1. 至此,完成创建 HTTPS 加密所需的私钥(tls.key)和证书(tls.crt)。