简体   繁体   English

Kubernetes NGINX-INGRESS 我需要运行 NGINX 服务吗?

[英]Kubernetes NGINX-INGRESS Do I need an NGINX Service running?

I am attempting to create an NGINX-INGRESS (locally at first, then to be deployed to AWS behind a load-balancer).我正在尝试创建一个 NGINX-INGRESS(首先在本地,然后部署到负载均衡器后面的 AWS)。 However I am new to Kubernetes, and I understand the Ingress model for NGINX- the configurations are confusing me as to weather I should be deploying an NGINX-INGRESS Service , Ingress or Both但是我是 Kubernetes 的新手,我了解 NGINX 的Ingress model - 配置让我感到困惑,因为我应该部署 NGINX-INGRESS服务入口两者

I am working with multiple Flask-Apps I would like to have routed by path ( /users , /content , etc.) My services are named user-service on port: 8000 (their container port is 8000 as well)我正在使用多个 Flask-Apps 我想通过路径( /users/content等)路由我的服务在端口上命名为user-service port: 8000 (它们的容器端口也是 8000 )

In this example an Ingress is defined.示例中,定义了一个Ingress However, when I apply an ingress (in the same Namespace as my Flask there is no response from http://localhost但是,当我应用入口时(在与我的 Flask 相同的命名空间中, http://localhost没有响应

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
spec:
  rules:
  - http:
      paths:
      - path: /users
        backend:
          serviceName: users-service 
          servicePort: 8000
      - path: /content
        backend:
          serviceName: content-service 
          servicePort: 8000

Furthermore, looking at the nginx-ingress "Deployment" docs, under Docker for Mac (which I assume I can use as I am using Docker on a MacOS) they define a Service like so:此外,查看nginx-ingress “部署”文档,在 Docker for Mac 下(我假设我可以使用它,因为我在 MacOS 上使用Docker )他们定义了这样的服务

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
    - name: https
      port: 443
      protocol: TCP
      targetPort: https

---

This seems to function for me (When I open "localhost" I get Nginx "not found", but it is a service in a different namespace then my apps- and there is no association between the port 80/443 and my service-ports.对我来说,这似乎是 function(当我打开“localhost”时,我得到 Nginx“未找到”,但它是与我的应用程序不同的命名空间中的服务-并且端口 80/443 和我的服务端口之间没有关联.

For reference here is one of my deployment/service definitions:这里是我的部署/服务定义之一供参考:

---
apiVersion: apps/v1
kind: Deployment
metadata:
    name: users-service
    labels:
        app: users-service
    namespace: example
spec:
    replicas: 1
    selector:
        matchLabels:
            app: users-service
    template:
        metadata:
            labels:
                app: users-service
        spec:
            containers:
                - name: users-service
                  image: users-service:latest
                  imagePullPolicy: Never
                  ports:
                  - containerPort: 8000
---
kind: Service
apiVersion: v1
metadata:
  name: users-service
spec:
  selector:
    app: users-service
  ports:
  - protocol: TCP
port: 8000

Update更新

I followed a video for setting up an NGINX-Controller+Ingress, here the results, entering "localhost/users" does not work,我跟着一个视频设置 NGINX-Controller+Ingress,这里的结果,输入“localhost/users”不起作用,

describe-ingress:描述入口:

(base) MacBook-Pro-2018-i9:microservices jordanbaucke$ kubectl describe ingress users-ingress
Name:             users-ingress
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *           
              /users   users-service:8000 (10.1.0.75:8000)
Annotations:  Events:  <none>

users-service:用户服务:

(base) MacBook-Pro-2018-i9:microservices jordanbaucke$ kubectl describe svc users-service
Name:              users-service
Namespace:         default
Labels:            <none>
Annotations:       Selector:  app=users-service
Type:              ClusterIP
IP:                10.100.213.229
Port:              <unset>  8000/TCP
TargetPort:        8000/TCP
Endpoints:         10.1.0.75:8000
Session Affinity:  None
Events:            <none>

nginx-ingress nginx入口

(base) MacBook-Pro-2018-i9:microservices jordanbaucke$ kubectl describe svc nginx-ingress
Name:                     nginx-ingress
Namespace:                default
Labels:                   <none>
Annotations:              Selector:  name=nginx-ingress
Type:                     NodePort
IP:                       10.106.167.181
LoadBalancer Ingress:     localhost
Port:                     http  80/TCP
TargetPort:               80/TCP
NodePort:                 http  32710/TCP
Endpoints:                10.1.0.74:80
Port:                     https  443/TCP
TargetPort:               443/TCP
NodePort:                 https  32240/TCP
Endpoints:                10.1.0.74:443
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Now when I try to enter the combination of NodeIP:NodePort/users , it does not connect?现在当我尝试输入NodeIP:NodePort/users的组合时,它没有连接?

From inside my nginx-ingress pod, calling:从我的nginx-ingress pod 内部,调用:

curl 10.1.0.75:8000 or curl 10.100.213.229:8000 returns results. curl 10.1.0.75:8000curl 10.100.213.229:8000返回结果。

For nginx or any other ingress to work properly:为了 nginx 或任何其他入口正常工作:

  1. Nginx ingress controller need to deployed on the cluster Nginx ingress controller 需要部署在集群上
  2. A LoadBalancer or NodePort type service need to be created to expose nginx ingress controller via port 80 and 443 in the same namespace where nginx ingress controller is deployed.LoadBalancer works in supported public cloud(AWS etc). A LoadBalancer or NodePort type service need to be created to expose nginx ingress controller via port 80 and 443 in the same namespace where nginx ingress controller is deployed.LoadBalancer works in supported public cloud(AWS etc). NodePort works if running locally.如果在本地运行,NodePort 可以工作。
  3. ClusterIP type service need to be created for workload pods in the namespace where workload pods are deployed.需要在部署工作负载 Pod 的命名空间中为工作负载 Pod 创建 ClusterIP 类型的服务。
  4. Workload Pods will be exposed via nginx ingress and you need to create ingress resource in the same namespace as of the clusterIP service of your workload Pods.工作负载 Pod 将通过 nginx 入口公开,您需要在与工作负载 Pod 的 clusterIP 服务相同的命名空间中创建入口资源。
  5. You will use either the LoadBalancer(in case nginx ingress controller was exposed via LoadBalancer) or NodeIP:NodePort(in case Nginx ingress controller was exposed via NodePort) to access your workload Pods. You will use either the LoadBalancer(in case nginx ingress controller was exposed via LoadBalancer) or NodeIP:NodePort(in case Nginx ingress controller was exposed via NodePort) to access your workload Pods.

So in this case since docker desktop is being used Loadbalancer type service(ingress-nginx) to expose nginx ingress controller will not work.所以在这种情况下,由于 docker 桌面正在使用负载均衡器类型服务(ingress-nginx)来暴露 nginx 入口 controller 将不起作用。 This needs to be of NodePort type.这需要是 NodePort 类型。 Once done workload pods can be accessed via NodeIP:NodePort/users and NodeIP:NodePort/content .完成后,可以通过NodeIP:NodePort/usersNodeIP:NodePort/content访问工作负载 pod。 NodeIP:NodePort should give nginx homepage as well. NodeIP:NodePort也应该提供 nginx 主页。

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

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