简体   繁体   English

Kubernetes 上的 Docker nginx 反向代理

[英]Docker nginx reverse proxy on Kubernetes

I have a couple of applications, which runs in Docker containers (all on the same VM).我有几个应用程序,它们在 Docker 容器中运行(都在同一个 VM 上)。 In front of them, I have an nginx container as a reverse proxy.在他们面前,我有一个 nginx 容器作为反向代理。 Now I want to migrate that to Kubernetes.现在我想将它迁移到 Kubernetes。

When I start them by docker-composer locally it works like expected.当我在本地通过 docker-composer 启动它们时,它按预期工作。 On Kubernetes not.在 Kubernetes 上不是。

nginx.conf配置文件

http {
        server {
                location / {
                        proxy_pass http://app0:80;
                }

                location /app1/ {
                        proxy_pass http://app1:80;
                        rewrite ^/app1(.*)$ $1 break;
                }

                location /app2/ {
                        proxy_pass http://app2:80;
                        rewrite ^/app2(.*)$ $1 break;
                }
        }
}

edit: nginx.conf is not used on kubernetes.编辑: nginx.conf 不在 kubernetes 上使用。 I have to use ingress-controller for that:我必须为此使用入口控制器:

deployment.yaml 部署.yaml
 apiVersion: apps/v1beta1 kind: Deployment metadata: name: app0 spec: replicas: 1 template: metadata: labels: app: app0 spec: nodeSelector: "beta.kubernetes.io/os": linux containers: - name: app0 image: appscontainerregistry1.azurecr.io/app0:latest imagePullPolicy: Always ports: - containerPort: 80 name: nginx --- #the other apps --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-nginx annotations: # use the shared ingress-nginx kubernetes.io/ingress.class: "nginx" spec: rules: - host: apps-url.com http: paths: - path: / backend: serviceName: app0 servicePort: 80 - path: /app1 backend: serviceName: app1 servicePort: 80 - path: /app2 backend: serviceName: app2 servicePort: 80 --- apiVersion: v1 kind: Service metadata: name: loadbalancer spec: type: LoadBalancer ports: - port: 80 selector: app: ingress-nginx

I get the response on / (app0).我在 / (app0) 上得到响应。 Unfortunately, the subroutes are not working.不幸的是,子路由不起作用。 What I´m doing wrong?我做错了什么?

EDIT编辑

I figured out.我想通了。 Ich missed installing the ingress controller.我错过了安装入口控制器。 Like on this page ( https://kubernetes.io/docs/concepts/services-networking/ingress/ ) described, the ingress doesn't work if no controller is installed.就像在这个页面 ( https://kubernetes.io/docs/concepts/services-networking/ingress/ ) 上描述的那样,如果没有安装控制器,入口将不起作用。 I used ingress-nginx as a controller ( https://kubernetes.github.io/ingress-nginx/deploy/ ) because it was the best-described install guide which I was able to find and I didn´t want to use HELM.我使用 ingress-nginx 作为控制器( https://kubernetes.github.io/ingress-nginx/deploy/ ),因为它是我能找到的最好的安装指南,我不想使用 HELM . I have one more question.我还有一个问题。 How I can change my ingress that subdomains are working.如何更改子域正在工作的入口。 For example, k8url.com/app1/subroute shows me every time the start page of my app1.例如,k8url.com/app1/subroute 每次都会显示我的 app1 的起始页。 And if I use a domain name proxying, it rewrites every time the domain name by the IP.如果我使用域名代理,它每次都会通过 IP 重写域名。

you have created deployment successfully but with that service should be there.您已成功创建部署,但该服务应该在那里。 nginx ngress on kubernetes manage traffic based on the service. kubernetes 上的 nginx ngress 根据服务管理流量。

so flow goes like所以流程就像

nginx-ingress > service > deployment pod.

you are missing to create the service for both applications and add the proper route based on that in kubernetes ingress.您缺少为两个应用程序创建服务并根据 kubernetes ingress 中的路由添加正确的路由。

Add this :添加这个:

apiVersion: v1
kind: Service
metadata:
  name: loadbalancer
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: ingress-nginx

Because you didnot route for Service Load balancer to targetPort to 80因为你没有将Service Load balancer 路由到targetPort80

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

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