在Nginx反向代理后面运行的Kubernetes Ingress [英] Kubernetes Ingress running behind nginx reverse proxy

查看:800
本文介绍了在Nginx反向代理后面运行的Kubernetes Ingress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在可以从Internet访问的服务器上安装了minikube.

I have installed minikube on a server which I can access from the internet.

我创建了一个可用的kubernetes服务:

I have created a kubernetes service which is available:

>kubectl get service myservice
NAME        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
myservice   10.0.0.246   <nodes>       80:31988/TCP   14h

minikube的IP地址是:

The IP address of minikube is:

>minikube ip
192.168.42.135

我希望URL http://myservice.myhost.com(即端口80)映射到minikube中的服务.

I would like the URL http://myservice.myhost.com (i.e. port 80) to map to the service in minikube.

我在主机上运行了Nginx(与kubernetes完全无关).我可以设置一个虚拟主机,将URL映射到192.168.42.135:31988(节点端口),并且可以正常工作.

I have nginx running on the host (totally unrelated to kubernetes). I can set up a virtual host, mapping the URL to 192.168.42.135:31988 (the node port) and it works fine.

我想使用一个入口.我已添加并启用了入口.但是我不确定:

I would like to use an ingress. I've added and enabled ingress. But I am unsure of:

a)yaml文件应包含什么

a) what the yaml file should contain

b)如何将来自浏览器的端口80上的传入流量重定向到入口和minikube.

b) how incoming traffic on port 80, from the browser, gets redirected to the ingress and minikube.

c)我还需要使用nginx作为反向代理吗?

c) do I still need to use nginx as a reverse proxy?

d)如果是,那么ingress-nginx在哪个地址上运行(以便我可以将流量映射到该地址)?

d) if so, what address is the ingress-nginx running on (so that I can map traffic to it)?

推荐答案

如@silverfox所述,您需要一个入口控制器.您可以在minikube中启用入口控制器,如下所示:

As stated by @silverfox, you need an ingress controller. You can enable the ingress controller in minikube like this:

minikube addons enable ingress

根据minikube ip,Minikube在IP 192.168.42.135上运行.启用入口插件后,它也会监听端口80.但这意味着主机上需要像nginx这样的反向代理,以将对端口80的调用代理到minikube.

Minikube runs on IP 192.168.42.135, according to minikube ip. And after enabling the ingress addon it listens to port 80 too. But that means a reverse proxy like nginx is required on the host, to proxy calls to port 80 through to minikube.

在minikube上启用入口后,我创建了一个入口文件(myservice-ingress.yaml):

After enabling ingress on minikube, I created an ingress file (myservice-ingress.yaml):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myservice-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: myservice.myhost.com
      http:
        paths:
        - path: /
          backend:
            serviceName: myservice
            servicePort: 80

请注意,这与@silverfox给出的答案不同,因为它必须包含应匹配的主机".

Note that this is different to the answer given by @silverfox because it must contain the "host" which should match.

使用此文件,我创建了入口:

Using this file, I created the ingress:

kubectl create -f myservice-ingress.yaml

最后,我向nginx(在minikube外部运行)添加了虚拟主机,以将来自外部的流量代理到minikube:

Finally, I added a virtual host to nginx (running outside of minikube) to proxy traffic from outside into minikube:

server {
  listen 80;
  server_name myservice.myhost.com;
  location / {
    proxy_set_header Host            $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://192.168.42.135;
  }
}

Host标头必须通过,因为入口使用它来匹配服务.如果未通过,则minikube无法将请求与服务匹配.

The Host header must be passed through because the ingress uses it to match the service. If it is not passed through, minikube cannot match the request to the service.

请记住在添加了上面的虚拟主机后重新启动nginx.

Remember to restart nginx after adding the virtual host above.

这篇关于在Nginx反向代理后面运行的Kubernetes Ingress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆