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

效果如下

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

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

相关推荐
Linux运维老纪7 小时前
K8s之Service详解(Detailed Explanation of K8s Service)
服务器·网络·云原生·容器·kubernetes·云计算·运维开发
Charlie__ZS9 小时前
微服务-配置管理
微服务·云原生·架构
A ?Charis11 小时前
ExternalName Service 针对的是k8s集群外部有api服务的场景?
kubernetes
Dusk_橙子11 小时前
在K8S中,pending状态一般由什么原因导致的?
云原生·容器·kubernetes
喝醉酒的小白14 小时前
几种K8s运维管理平台对比说明
运维·容器·kubernetes
Linux运维老纪1 天前
DNS缓存详解(DNS Cache Detailed Explanation)
计算机网络·缓存·云原生·容器·kubernetes·云计算·运维开发
元气满满的热码式1 天前
K8S部署DevOps自动化运维平台
运维·kubernetes·devops
IT艺术家-rookie2 天前
k8s--部署k8s集群--控制平面节点
容器·kubernetes
Elastic 中国社区官方博客2 天前
使用 Ollama 和 Kibana 在本地为 RAG 测试 DeepSeek R1
大数据·数据库·人工智能·elasticsearch·ai·云原生·全文检索
Linux运维老纪2 天前
windows部署deepseek之方法(The Method of Deploying DeepSeek on Windows)
linux·人工智能·分布式·云原生·运维开发·devops