平台 HTTPS 配置
平台默认通过 istio ingressgateway 以 HTTP(80 端口)对外提供服务。要启用 HTTPS,有两条路线:
- istio 侧:直接给
kubeflow-gateway配 TLS(当前不可用,见下)。 - nginx 侧(推荐 / 实际可用):在 istio 前面加一层 nginx 做 TLS 终止,nginx 处理证书并把明文流量转发给 istio ingressgateway。
最后还需要生成(或替换)证书,并在前端做 scheme 改写避免 https 页面跳回 http。下面分「istio 侧 / nginx 侧 / 证书生成」三部分。
一、istio 侧:直接给 gateway 配 TLS(方式一,目前不行)
说明:此方式来自原文档,标注「目前不行」,仅作背景了解,请勿据此作为生产方案。
在对应命名空间创建 TLS secret:
kubectl create secret tls tls-secret --cert=server.crt --key=server.key -n kubeflow
修改 kubeflow 命名空间的 kubeflow-gateway,新增 443/HTTPS server 并引用该 secret:
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: kubeflow-gateway
namespace: kubeflow
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- '*'
port:
name: http-port
number: 80
protocol: HTTP
# 使用 https 添加这部分,并让 istio-ingressgateway 开放 443 端口
# 注意不要和 rancher 等已占用 443 的服务端口冲突
- hosts:
- '*'
port:
name: https-port
number: 443
protocol: HTTPS
tls:
credentialName: tls-secret
mode: SIMPLE
经过该 gateway 的所有 VirtualService 理论上即可用 https 访问。
二、nginx 侧:nginx 代理 + 前端 scheme 改写(方式二,推荐)
思路:新增一层 nginx 做 TLS 终止,把流量代理到 istio ingressgateway;istio 本身不做任何 TLS 配置。相关清单在 install/kubernetes/nginx-https/。
2.1 部署 https nginx(接管 TLS)
nginx 的 default-https.conf 监听 443(k8s 形态)做 TLS 终止,转发到 http://istio-ingressgateway.istio-system:80/,并已带上 proxy_set_header X-Forwarded-Proto https;(install/kubernetes/nginx-https/default-https.conf)。
k8s 部署(把证书与配置打成 configmap ssl-config,再起一个 nginx Deployment + NodePort Service,对外 443):
# 先确认 default-https.conf 中转发地址为 http://istio-ingressgateway.istio-system:80/
kubectl delete configmap ssl-config --namespace=istio-system
kubectl create configmap ssl-config \
--from-file=server.crt --from-file=server.csr --from-file=server.key --from-file=default-https.conf \
--namespace=istio-system
kubectl apply -f ingress-nginx.yaml
ingress-nginx.yaml创建:istio-system下名为ingress-nginx的 NodePort Service(443)和同名 Deployment,挂载ssl-config到/etc/nginx/conf.d/default-https.conf与/etc/nginx/ssl/。依据install/kubernetes/nginx-https/ingress-nginx.yaml。
docker 部署(docker-compose / 单机形态):
# 修改 default-https.conf 的监听端口(作为 https 暴露端口)与转发到 istio ingressgateway 的内网地址
docker run --name https-nginx -d -p 8443:8443 \
-v $PWD:/etc/nginx/ssl/ \
-v $PWD/default-https.conf:/etc/nginx/conf.d/default-https.conf \
nginx
如只需 http 代理(不加密,用于调试):
docker run --name nginx -it --network=host \
-v $PWD/default-http.conf:/etc/nginx/conf.d/default-http.conf nginx
2.2 修改平台外网地址与 labelstudio host
- 把 CubeStudio 的「外网 ip/域名」配置改为该 nginx 的 ip:端口(
config.py中CLUSTERS的HOST等,见 配置文件管理.md)。 - 把数据标注 labelstudio 的 host 环境变量改为该 nginx 的 ip:端口 + https 协议。该环境变量为
LABEL_STUDIO_HOST,正常部署时形如http://$ip/labelstudio(同时设置在 labelstudio Deployment 的 frontend 与 backend 两个容器上),https 场景应改为https://$ip/labelstudio。依据install/kubernetes/start.sh:169-170(patch frontend/backend 两个容器的LABEL_STUDIO_HOST)、install/kubernetes/labelstudio/labelstudio.yaml:81,135。详见 03-平台使用 / 数据标注。
2.3 前端 nginx 改 scheme(关键,避免 https 跳回 http)
CubeStudio 前端 Pod 的 nginx 配置里有一条把根路径重定向到 /frontend/ 的规则,默认用 $scheme(会回填成 http)。https 下需写死成 https,并打开 X-Forwarded-Proto https:
# 原:rewrite ^(.*) $scheme://$http_host/frontend/ permanent;
# 改为:
rewrite ^(.*) https://$http_host/frontend/ permanent;
# 同时取消注释:
proxy_set_header X-Forwarded-Proto https;
改完重启前端 Pod。
该 nginx 配置在前端 Deployment 的内嵌 configmap 中:install/kubernetes/cube/base/deploy-frontend.yaml:19(重写规则)与 deploy-frontend-more.yaml:18,66;本地形态对应 install/docker/dockerFrontend/nginx.80.conf:9,53。
三、证书生成(openssl 自签)
使用 openssl 生成自签证书(install/kubernetes/nginx-https/readme.md,参考腾讯云文档 https://cloud.tencent.com/developer/article/1611144):
# 1. 生成 server.key
openssl genrsa -aes256 -out server.key 2048
# 2. 去掉私钥密码(生成无密码的 server.key)
openssl rsa -in server.key -out server.key
# 3. 创建证书申请文件 server.csr
openssl req -new -key server.key -out server.csr
# 交互式填写示例:
# Country Name (2 letter code): CN
# State or Province Name: Zhejiang
# Locality Name: HangZhou
# Organization Name: kaiqiao
# Organizational Unit Name: kaiqiao
# Common Name: * # 填你的域名或访问 ip;通配符域名似乎不行,可直接用 *
# Email Address: xxx@xx.com
# 4. 生成 crt 证书文件 server.crt(自签,有效期 10 年)
openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
生成的 server.crt / server.key / server.csr 即上文 nginx 部署中引用的证书文件(install/kubernetes/nginx-https/ 目录下已带一套示例)。生产环境应替换为受信任 CA 签发的正式证书。
相关
- 平台外网地址、前端/配置文件位置:配置文件管理.md。
- labelstudio host 改 https 属数据标注功能配置:03-平台使用 / 数据标注。