K8s ingress-nginx根据请求目录不同将请求转发到不同应用

K8s ingress-nginx根据请求目录不同将请求转发到不同应用

1. 起因

有小伙伴做实验想要实现以下需求:

输入www.pana.com/app1访问app1的svc

输入www.pana.com/app2访问app2的svc

2. 实验

2.1 Dockerfile

先准备Dockerfile

bash 复制代码
FROM nginx:1.20

ADD index.html /usr/share/nginx/html/index.html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

再准备一个index.html

当app1时就把它改为web1 v1.1.0

当app2时就把它改为web2 v1.2.0

html 复制代码
nginx wework-web1 v1.1.0

2.2 Deployment和SVC

将镜像分别上传至harbor后,通过yaml分别部署app1和app2

app1:

yaml 复制代码
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: nginx-app1
  name: nginx-app1
  namespace: test-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app1
  template:
    metadata:
      labels:
        app: nginx-app1
    spec:
      containers:
      - name: nginx
        image: harbor.panasonic.cn/test-nginx/nginx-web:v1.1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 1
            memory: "512Mi"
          requests:
            cpu: 500m
            memory: "512Mi"
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app1-svc
  namespace: test-nginx
  labels:
    app: nginx-app1
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx-app1
  type: ClusterIP

app2:

app2的nodeport是不需要的,我做其他实验时候用到,和此实验无关

yaml 复制代码
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: nginx-app2
  name: nginx-app2
  namespace: test-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app2
  template:
    metadata:
      labels:
        app: nginx-app2
    spec:
      containers:
      - name: nginx
        image: harbor.panasonic.cn/test-nginx/nginx-web:v1.2.0
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 1
            memory: "512Mi"
          requests:
            cpu: 500m
            memory: "512Mi"
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app2-svc
  namespace: test-nginx
  labels:
    app: nginx-app2
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
      nodePort: 30080
  selector:
    app: nginx-app2
  type: NodePort

测试是否可以正常访问2个应用

2.3 Ingress

创建Ingress的yaml

注意的是小伙伴因为看了之前另外个tomcat的文档发现安装那个配置就一直404报错.

原因也很简单,另外个实验的目录是放在不通的uri下:

app1: www.pana.com/app1

app2: www.pana.com/app2

那么就不需要再对地址重写,而我们这里2个index都是在/下面

那么在匹配了path后就需要将它重写到app的/,于是就用到了nginx.ingress.kubernetes.io/rewrite-target

yaml 复制代码
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-app1-ing
  namespace: test-nginx
  # 以下两行是必须的,小伙伴就卡在这里一直报404错误
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: www.pana.com
    http:
      paths:
      - pathType: Prefix
        path: /app1
        backend:
          service:
            name: nginx-app1-svc
            port:
              number: 80
      - pathType: Prefix
        path: /app2
        backend:
          service:
            name: nginx-app2-svc
            port:
              number: 80

3. 效果

效果如下

可以看到,我们已经实现了预期的效果

小伙伴试验后也表示明白了

相关推荐
数据智能老司机4 小时前
Kubernetes 上的生成式 AI——模型数据
kubernetes·llm·agent
数据智能老司机4 小时前
Kubernetes 上的生成式 AI——部署模型
kubernetes·aigc
AI攻城狮1 天前
OpenFang 给我的一个提醒:AI Agent 真正难的不是自主,而是治理
人工智能·云原生·aigc
Java陈序员1 天前
轻量强大!一款现代化的 Kubernetes 集群管理与监控工具!
云原生·容器·kubernetes
Johny_Zhao2 天前
OpenClaw中级到高级教程
linux·人工智能·信息安全·kubernetes·云计算·yum源·系统运维·openclaw
AI攻城狮3 天前
OpenClaw 里 TAVILY_API_KEY 明明写在 ~/.bashrc,为什么还是失效?一次完整排查与修复
人工智能·云原生·aigc
阿里云云原生4 天前
零配置部署顶级模型!函数计算一键解锁 Qwen3.5
云原生
AI攻城狮4 天前
Kimi Bot + OpenClaw 完整配置指南:5 步实现本地 AI Agent 集成
人工智能·云原生·aigc
AI攻城狮5 天前
RAG Chunking 为什么这么难?5 大挑战 + 最佳实践指南
人工智能·云原生·aigc
可观测性用观测云6 天前
云原生网关 Ingress-Nginx 链路追踪实战:OpenTelemetry 采集与观测云集成方案
nginx·kubernetes