简体   繁体   English

Nginx 托管在 windows 上的 Kube.netes 集群的入口配置

[英]Nginx ingress configuration for Kubernetes cluster hosted on windows

I am running Kube.netes cluster on my windows PC via Docker desktop.我通过 Docker 桌面在我的 windows PC 上运行 Kube.netes 集群。 I am trying to create a very basic pod with a simple ingress configuration, but it doesn't seem to work.我正在尝试使用简单的入口配置创建一个非常基本的 pod,但它似乎不起作用。 I thought the backend pod + service + ingress is a very basic setup, however I don't find a lot of help online.我认为后端 pod + 服务 + ingress 是一个非常基本的设置,但是我在网上找不到很多帮助。 Please advise what I am doing wrong here.请告知我在这里做错了什么。

My deployment.yaml file我的deployment.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: test-cluster-ip
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 1234
      targetPort: 80

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Exact
        backend:
          service:
            name: test-cluster-ip
            port:
              number: 1234

This is what I see when I access localhost from the browser这是我从浏览器访问本地主机时看到的

在此处输入图像描述

Also, I would like to ask if it is uncommon to run Kube.netes on windows even for testing (especially with ingress).另外,我想问一下在 windows 上运行 Kube.netes 是否不常见,即使是为了测试(尤其是入口)。 I don't seem to find a lot of examples in the inte.net.我似乎没有在 inte.net 中找到很多示例。

I thought the backend pod + service + ingress is a very basic setup, however I don't find a lot of help online.我认为后端 pod + 服务 + ingress 是一个非常基本的设置,但是我在网上找不到很多帮助。 Please advise what I am doing wrong here.请告知我在这里做错了什么。

It is indeed a very basic setup.这确实是一个非常基本的设置。 And your k8s deployment/service/ingress yaml files are correct.并且您的 k8s deployment/service/ingress yaml 文件是正确的。

First, check if you installed NGINX ingress controller .首先,检查你是否安装了NGINX ingress controller If not, run:如果没有,运行:

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml

After that, you will be able to reach the k8s cluster using the following URL:之后,您将能够使用以下 URL 访问 k8s 集群:

http://kubernetes.docker.internal/

But deploying ingress like this :但是像这样部署入口

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Exact
        backend:
          service:
            name: test-cluster-ip
            port:
              number: 1234

you are configuring the ingress to rewrite /testpath to the / .您正在配置入口以将/testpath重写为/ And requesting url without /testpath will return 404 status code.在没有 /testpath 的情况下请求 url 将返回 404 状态代码。

See more rewrite examples here .在此处查看更多rewrite示例。

So, if you use the following URL, you will get the Nginx webpage from k8s deployment.因此,如果您使用以下 URL,您将从 k8s 部署中获得 Nginx 网页。

http://kubernetes.docker.internal/testpath

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

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