简体   繁体   English

eks http https 使用入口重定向

[英]eks http https redirect using ingress

This is my ingress file , what I need is how to add https redirection settings here in ingress file , I did it using service file and it works but after to reduce costs I decided to use SINGLE ingress file which manage multiple services with SINGLE AWS CLASSIC load balancer.这是我的入口文件,我需要的是如何在入口文件中添加 https 重定向设置,我是使用服务文件完成的,它可以工作,但为了降低成本,我决定使用单一入口文件,该文件使用单一 AWS CLASSIC 管理多项服务负载均衡器。

   apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      generation: 4
      name: brain-xx
      namespace: xx
    spec:
      rules:
      - host: app.xx.com
        http:
          paths:
          - backend:
              serviceName: xx-frontend-service
              servicePort: 443
            path: /
    status:
      loadBalancer:
        ingress:
        - ip: xx.xx.xx.xx

I have managed to create http to https redirection on GKE.我已经成功地在 GKE 上创建了httphttps重定向。 Let me know if this solution will work for your case on AWS:让我知道此解决方案是否适用于您在 AWS 上的案例:

Steps to reproduce重现步骤

  • Apply Ingress definitions应用入口定义
  • Configure basic HTTP ingress resource配置基本的 HTTP 入口资源
  • Create SSL certificate创建 SSL 证书
  • Replace old Ingress resource with HTTPS enabled one.用启用了 HTTPS 的资源替换旧的 Ingress 资源。

Apply Ingress definitions应用入口定义

Follow this Ingress link to check if there are any needed prerequisites before installing NGINX Ingress controller on your AWS infrastructure and install it.在您的 AWS 基础设施上安装 NGINX Ingress 控制器并安装之前,请按照此Ingress 链接检查是否有任何所需的先决条件。

Configure basic HTTP ingress resource and test it配置基本的 HTTP 入口资源并测试它

Example below is Ingress configuration with HTTP traffic only.下面的示例是仅包含 HTTP 流量的 Ingress 配置。 It will act as starting point:它将作为起点:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: xx.yy.zz
    http:
      paths:
      - path: /
        backend:
          serviceName: hello-service
          servicePort: hello-port
      - path: /v2/ 
        backend: 
          serviceName: goodbye-service 
          servicePort: goodbye-port

Please change this file to reflect configuration appropriate to your case.请更改此文件以反映适合您情况的配置。

Create SSL certificate创建 SSL 证书

For this to work without browser's security warnings you will need valid SSL certificate and a domain name.要使其在没有浏览器安全警告的情况下工作,您需要有效的 SSL 证书和域名。

To create this certificate you can use for example: Linode create Let's Encrypt SSL certificates .要创建此证书,您可以使用例如: Linode create Let's Encrypt SSL证书

Let's Encrypt will create files which will be used later. Let's Encrypt 将创建稍后使用的文件。

Configure HTTPS ingress resource and test it配置HTTPS入口资源并测试

By default Nginx Ingress will create a self-signed certificate if he's not provided one.默认情况下,如果未提供自签名证书,Nginx Ingress 将创建一个自签名证书。 To provide him one you will need to add it as a secret to your Kubernetes cluster.为了给他提供一个,你需要将它作为秘密添加到你的 Kubernetes 集群中。

As I said earlier the files ( cert.pem privkey.pem ) that Let's Encrypt created will be added to Kubernetes to configure HTTPS.正如我之前所说,Let's Encrypt 创建的文件( cert.pem privkey.pem )将被添加到 Kubernetes 以配置 HTTPS。

Below command will use this files to create secret for Ingress:以下命令将使用此文件为 Ingress 创建密钥:

$ kubectl create secret tls ssl-certificate --cert cert.pem --key privkey.pem

This Ingress configuration support HTTPS as well as redirects all the traffic to it:此 Ingress 配置支持 HTTPS 并将所有流量重定向到它:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-https
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
  tls:
  - secretName: ssl-certificate
  rules:
  - host: xx.yy.zz 
    http:
      paths:
      - path: /
        backend:
          serviceName: hello-service
          servicePort: hello-port
      - path: /v2/ 
        backend: 
          serviceName: goodbye-service 
          servicePort: goodbye-port 

Please change this file to reflect configuration appropriate to your case.请更改此文件以反映适合您情况的配置。

Take a look at this fragment which will enable HTTPS and redirect all the traffic to it:看看这个片段,它将启用 HTTPS 并将所有流量重定向到它:

    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
  tls:
  - secretName: ssl-certificate

Apply this configuration and check if it worked for you.应用此配置并检查它是否适合您。

Below is part of curl output which shows that connecting to http://xx.yy.zz gives redirection to https://xx.yy.zz下面是 curl 输出的一部分,它显示连接到http://xx.yy.zz会重定向到https://xx.yy.zz

< HTTP/1.1 308 Permanent Redirect
< Server: openresty/1.15.8.2
< Date: Fri, 20 Dec 2019 15:06:57 GMT
< Content-Type: text/html
< Content-Length: 177
< Connection: keep-alive
< Location: https://xx.yy.zz/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM