Kubernetes入口:SSL(HTTP-> HTTPS)重定向不起作用(Nginx Docker)

时间:2019-04-21 13:06:42

标签: ssl https kubernetes google-kubernetes-engine kubernetes-ingress

我正在Google Cloud Kubernetes Engine中使用Kubernetes,并进行了以下设置: -Nginx Docker映像(nginx:latest),用于托管Web应用程序 -Kubernetes部署(yaml文件) -Kubernetes服务(yaml文件) -具有现有密钥和证书的Kubernetes Secret(通配卡PositiveSSL) -Kubernetes入口

目前,我同时使用HTTP和HTTPS。但是,我想将所有HTTP调用自动重定向到HTTPS,但似乎无法正常工作。

我已经尝试了下面的conf和脚本文件的许多变体,但它似乎无法将HTTP重定向到HTTPS。

你知道我在这里做错什么吗?

请参阅下面的conf,yaml和docker文件。

Nginx Conf:

server {
  listen 80;
  charset utf-8;
  root /usr/share/nginx/html;

  location / {
    proxy_set_header        Host $host:$server_port;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_redirect  http:// https://;
    proxy_pass              http://portal.domain.com;
    proxy_http_version 1.1;
    proxy_request_buffering off;
  }
}

server {
  listen 443 ssl;

  charset utf-8;
  root /usr/share/nginx/html;

  ssl_certificate       /etc/nginx/ssl/domain_com_full.crt;
  ssl_certificate_key   /etc/nginx/ssl/domain_com.key;

  location / {
    proxy_set_header        Host $host:$server_port;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_redirect  http:// https://;
    proxy_pass              http://portal.domain.com;
    proxy_http_version 1.1;
    proxy_request_buffering off;
  }
}

Docker文件:

FROM nginx:latest
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY domain_com_full.crt /etc/nginx/ssl/domain_com_full.crt
COPY domain_com.key /etc/nginx/ssl/domain_com.key
COPY dist /usr/share/nginx/html
EXPOSE 443 80

部署YAML(我使用脚本来填充映像的修订部分):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: domain-frontend-prd
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
        maxSurge: 1
        maxUnavailable: 0
  selector:
    matchLabels:
      run: domain-frontend-prd
  template:
    metadata:
      labels:
        run: domain-frontend-prd
    spec:
      containers:
      - name: domain-frontend-image
        image: eu.gcr.io/domain-service/domain-frontend-image:{{REVISION_ID}}
        ports:
        - containerPort: 80
        - containerPort: 443
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5
          successThreshold: 1

YAML服务:

apiVersion: v1
kind: Service
metadata:
  name: domain-frontend-service-prd
spec:
  type: NodePort
  selector:
    run: domain-frontend-prd
  ports:
  - protocol: TCP
    port: 443
    targetPort: 443
    name: https-port
  - protocol: TCP
    port: 80
    targetPort: 80
    name: http-port

入口YAML(秘密正在起作用,因为HTTPS调用也起作用+静态IP也在那里并且正在起作用):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: domain-frontend-ingress-prd
  annotations:
    kubernetes.io/ingress.global-static-ip-name: kubernetes-ingress
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
  tls:
  - hosts:
    - portal.domain.com
    secretName: domain-tls
  backend:
    serviceName: domain-frontend-service-prd
    servicePort: 80
  rules:
  - host: portal.domain.com
    http:
      paths:
        - path: /
          backend:
            serviceName: domain-frontend-service-prd
            servicePort: 80

1 个答案:

答案 0 :(得分:1)

通过广泛的搜索,我发现Google Cloud Kubernetes引擎中的标准Ingress控制器显然不支持重定向到HTTPS。

为了能够将流量重新发送到HTTPS(从HTTP),您需要根据以下教程/文档安装NGINX Ingress控制器:

https://cloud.google.com/community/tutorials/nginx-ingress-gke

这解决了我的问题。