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. 效果

效果如下

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

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

相关推荐
孔令飞3 分钟前
Go:终于有了处理未定义字段的实用方案
人工智能·云原生·go
玄明Hanko22 分钟前
Quarkus+Docker最全面完整教程:手把手搞定Java云原生
后端·docker·云原生
hoho不爱喝酒13 小时前
微服务Nacos组件的介绍、安装、使用
微服务·云原生·架构
樽酒ﻬق15 小时前
Kubernetes 常用运维命令整理
运维·容器·kubernetes
樽酒ﻬق16 小时前
深度解析 Kubernetes 配置管理:如何安全使用 ConfigMap 和 Secret
安全·贪心算法·kubernetes
阿里云云原生17 小时前
API 即 MCP|Higress 发布 MCP Marketplace,加速存量 API 跨入 MCP 时代
云原生
爱吃龙利鱼20 小时前
rocky9.4部署k8s群集v1.28.2版本(containerd)(纯命令)
云原生·容器·kubernetes
Serverless社区1 天前
MCP云托管最优解,揭秘国内最大MCP中文社区背后的运行时
阿里云·云原生·serverless·函数计算
掘金-我是哪吒1 天前
分布式微服务系统架构第120集:专业WebSocket鉴权
分布式·websocket·微服务·云原生·架构
哈哈幸运1 天前
Linux Awk 深度解析:10个生产级自动化与云原生场景
linux·云原生·自动化·awk·三剑客