gpt4 book ai didi

nginx - 路径上的应用程序而不是root不适用于Kubernetes Ingress

转载 作者:行者123 更新时间:2023-12-02 11:46:31 27 4
gpt4 key购买 nike

我在使用K8s Ingress时遇到问题,在这里我将使用伪造的示例来说明我的观点。
假设我有一个名为Tweeta的应用程序,而我的公司名为ABC。我的应用程序当前位于tweeta.abc.com。
但是我们想将我们的应用程序迁移到app.abc.com/tweeta。
我当前在K8s中的入口如下:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: tweeta-ingress
spec:
rules:
- host: tweeta.abc.com
http:
paths:
- path: /
backend:
serviceName: tweeta-frontend
servicePort: 80
- path: /api
backend:
serviceName: tweeta-backend
servicePort: 80
对于迁移,我添加了第二个入口:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: tweeta-ingress-v2
spec:
rules:
- host: app.abc.com
http:
paths:
- path: /tweeta
backend:
serviceName: tweeta-frontend
servicePort: 80
- path: /tweeta/api
backend:
serviceName: tweeta-backend
servicePort: 80

为了连续性,我想同时有两个入口指向我的服务。当新域准备就绪并可以正常工作时,我只需要拆除旧的入口即可。
但是,通过此入口,我对新域并没有任何运气。是因为它托管在路径上,而k8s入口需要托管在根目录上吗?还是我需要在Nginx端进行配置?

最佳答案

据我尝试,我无法重现您的问题。因此,我决定描述如何尝试重现它,以便您可以按照相同的步骤进行操作,并根据失败的位置/地点,找出导致问题的原因。
首先,请确保您使用的NGINX Ingress功能更强大。
我按照以下步骤使用Helm安装了NGINX Ingress:

$ curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com
$ helm repo update
$ helm install nginx-ingress stable/nginx-ingress
对于部署,我们将使用 here中的示例。
部署您好世界应用
  • 使用以下命令创建部署:
    kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0
    输出:
    deployment.apps/web created
  • 公开部署:
    kubectl expose deployment web --type=NodePort --port=8080
    输出:
    service/web exposed

  • 创建第二个部署
  • 使用以下命令创建v2部署:
    kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0
    输出:
    deployment.apps/web2 created
  • 公开部署:
    kubectl expose deployment web2 --port=8080 --type=NodePort
    输出:
    service/web2 exposed

  • 至此,我们已在运行“部署和服务”:
    $ kubectl get deployments.apps 
    NAME READY UP-TO-DATE AVAILABLE AGE
    web 1/1 1 1 24m
    web2 1/1 1 1 22m
    $ kubectl get service
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d5h
    nginx-ingress-controller LoadBalancer 10.111.183.151 <pending> 80:31974/TCP,443:32396/TCP 54m
    nginx-ingress-default-backend ClusterIP 10.104.30.84 <none> 80/TCP 54m
    web NodePort 10.102.38.233 <none> 8080:31887/TCP 24m
    web2 NodePort 10.108.203.191 <none> 8080:32405/TCP 23m
    对于入口,我们将使用问题中提供的入口,但是我们必须更改后端:
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
    name: tweeta-ingress
    spec:
    rules:
    - host: tweeta.abc.com
    http:
    paths:
    - path: /
    backend:
    serviceName: web
    servicePort: 8080
    - path: /api
    backend:
    serviceName: web2
    servicePort: 8080
    ---
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
    name: tweeta-ingress-v2
    spec:
    rules:
    - host: app.abc.com
    http:
    paths:
    - path: /tweeta
    backend:
    serviceName: web
    servicePort: 8080
    - path: /tweeta/api
    backend:
    serviceName: web2
    servicePort: 8080
    现在让我们测试一下入口:
    $ curl tweeta.abc.com
    Hello, world!
    Version: 1.0.0
    Hostname: web-6785d44d5-j8bgk

    $ curl tweeta.abc.com/api
    Hello, world!
    Version: 2.0.0
    Hostname: web2-8474c56fd-lx55n

    $ curl app.abc.com/tweeta
    Hello, world!
    Version: 1.0.0
    Hostname: web-6785d44d5-j8bgk

    $ curl app.abc.com/tweeta/api
    Hello, world!
    Version: 2.0.0
    Hostname: web2-8474c56fd-lx55n
    可以看出,一切正常,并且在入口中没有任何mod。

    关于nginx - 路径上的应用程序而不是root不适用于Kubernetes Ingress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62496338/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com