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

news/2024/7/5 2:19:03

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

1. 起因

有小伙伴做实验想要实现以下需求:
输入www.pana.com/app1访问app1的svc
输入www.pana.com/app2访问app2的svc
在这里插入图片描述

2. 实验

2.1 Dockerfile

先准备Dockerfile

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

nginx wework-web1 v1.1.0

2.2 Deployment和SVC

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

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是不需要的,我做其他实验时候用到,和此实验无关

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

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

效果如下
可以看到,我们已经实现了预期的效果
在这里插入图片描述
小伙伴试验后也表示明白了
在这里插入图片描述


http://lihuaxi.xjx100.cn/news/2039440.html

相关文章

C# CAD交互界面-自定义面板集-comboBox选择图层

运行环境Visual Studio 2022 c# cad2016 一、代码说明 SelectLayer方法是一个自定义的AutoCAD命令方法,通过[CommandMethod("SelectLayer")]进行标记。方法首先获取当前活动文档,并检查是否有效。创建一个名为"SelectLayer"的Pale…

Windows 自带的 Linux 子系统(WSL)安装与使用

WSL官网安装教程: https://learn.microsoft.com/zh-cn/windows/wsl/install Windows 自带的Linux子系统,比用VM什么的香太多了。可以自己看官方教程,也可以以下步骤完成。 如果中间遇到我没遇到的问题百度,可以在评论区评论&#…

平台组成-资源服务

资源服务是平台一个核心服务,另一个是用户服务,用户服务下篇介绍。 资源服务包括 字典:整个平台的字典在这里维护。字典类指字典的类别,例如性别字典。字典项目指具体的项目,例如男、女。字典项目有字典ID,…

Jetpack Compose 与 Kotlin 的兼容性对应关系

点击查看:Jetpack Compose Kotlin 的兼容性 官网 声明依赖项 如需添加 Compose Compiler 的依赖项,您必须将 Google Maven 代码库添加到项目中。如需了解详情,请参阅 Google 的 Maven 代码库。 在应用或模块的 build.gradle或者build.grad…

React native更改包名后,启动app的activity包名不生效问题

这篇文章本不算记录的,因为实际开发中,类似这种小问题会有很多很多,因为导致问题的原因千奇百怪,解决方案也不尽相同,所以也都没有记录。 但今天看到我10年写的问题解决小文章,被网友收藏了, 感…

uniapp不同平台获取文件内容以及base64编码特征

前言 文件图片上传,客户端预览是很正常的需求,获取文件的md5特征码也是很正常的,那么,在uniapp中三种环境,h5, 小程序以及 app环境下,如何实现的? 参考: 如何在uniapp中读取文件Arr…

07.STL单调栈

单调栈的特点&#xff1a; 1.自顶向下一次递增&#xff0c;也就是上小下大的栈 单调栈代码&#xff1a; 算法思想&#xff1a;将不符合单调栈性质的弹出&#xff0c;符合的输入 #include<iostream> #include <stack> #include<queue> using namespace std…

k8s 基础理论

一、k8s概述 K8S 的全称为 Kubernetes&#xff0c;其作用为用于自动部署、扩展和管理“容器化&#xff08;containerized&#xff09;应用程序”的开源系统。可以理解成 K8S 是负责自动化运维管理多个容器化程序&#xff08;比如 Docker&#xff09;的集群&#xff0c;是一个生…