• Ingress 基于URL路由多个服务



    前言

    Kubernetes Ingress暴露应用的工作流程帖子我们可以得知ingress 与 nignx配置对应关系如下图所示:
    在这里插入图片描述
    接下来我们来学习下ingress怎么配置根据一个地址多个路径来转发至不同应用的pod(如果在ngx里是直接添加多个location就可以实现了)


    一、基于请求地址转发不同应用的pod

    在这里插入图片描述

    1.创建一个nginx的pod和一个apache的pod及其各自的service

    nginx

    # vim web.yaml
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web
      labels:
        app: web
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: web
      template:
        metadata:
          labels:
            app: web
        spec:
          containers:
          - name: nginx-application
            image: nginx:1.14.2
            ports:
            - containerPort: 80
            
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      selector:
        app: web
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
          
    # kubectl apply -f web.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    apache:

    # kubectl create deployment httpd --image=httpd
    # kubectl expose deployment httpd --port=80 --target-port=80
    
    • 1
    • 2

    2.创建ingress实现一个地址两个path分别访问nginx和apache

    # vim web-ingress-path.yaml
    
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: web2
    spec:
      ingressClassName: nginx
      rules:
      - host: web2.study.com
        http:
          paths:
          #请求的路径,例如localhost/foo
          - path: /foo
            pathType: Prefix
            #关联的后端service
            backend:
              service:
                name: web
                port:
                  number: 80
          - path: /bar
            pathType: Prefix
            backend:
              service:
                name: httpd
                port:
                  number: 80
                  
    # kubectl apply -f web-ingress-path.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3.验证根据域名web2.study.com的两个路径/foo和/bar来访问到不同的pod

    [root@k8s-master ~]# kubectl get pod,svc,ingress 
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/httpd-757fb56c8d-6st7x   1/1     Running   0          92m
    pod/web-6cdb47d94-htnnm      1/1     Running   0          56d
    
    NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    service/httpd        ClusterIP   10.98.156.70    <none>        80/TCP    90m
    service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   68d
    service/web          ClusterIP   10.110.44.127   <none>        80/TCP    56d
    
    NAME                             CLASS   HOSTS            ADDRESS       PORTS   AGE
    ingress.networking.k8s.io/web2   nginx   web2.study.com   192.168.1.1   80      65m
    
    [root@k8s-master ~]# kubectl describe ingress/web2 
    Name:             web2
    Namespace:        default
    Address:          192.168.1.1
    Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
    #如下可以看到域名path的信息
    Rules:
      Host            Path  Backends
      ----            ----  --------
      web2.study.com  
                      /foo   web:80 (10.244.107.195:80)
                      /bar   httpd:80 (10.244.36.66:80)
    Annotations:      <none>
    Events:           <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    测试机添加本地dns解析,并进行对两个path测试
    /foo:
    在这里插入图片描述
    /bar
    在这里插入图片描述
    可以看到以上两个的访问来自不同应用返回的结果,web2.study.com/foo是nignx返回的,web2.study.com/bar是 apache 返回的,但是返回的结果都是404,这是因为
    web2.study.com/foo --> nginx:80/foo (404)
    在访问web2.study.com/foo的时候,这个请求就类似于去访问nginx:80/foo,我们大家都知道如果去访问nginx:80/foo的话,nginx必须有foo这个网站目录以及index.html页面,同理,下面的apache也是一样
    web2.study.com/bar --> httpd:80/bar (404)


    4.分别在nginx和apache的pod里创建网站目录以及index.html

    nginx 创建网站目录foo并创建页面

    [root@k8s-master ~]# kubectl exec -it pod/web-6cdb47d94-htnnm -- bash 
    root@web-6cdb47d94-htnnm:/# cd /usr/share/nginx/html/
    root@web-6cdb47d94-htnnm:/usr/share/nginx/html# mkdir foo
    root@web-6cdb47d94-htnnm:/usr/share/nginx/html# echo "hello ngx--foo" > foo/index.html
    root@web-6cdb47d94-htnnm:/usr/share/nginx/html# cat foo/index.html 
    hello ngx--foo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    nginx 创建网站目录bar并创建页面

    [root@k8s-master ~]# kubectl exec -it pod/httpd-757fb56c8d-6st7x -- bash
    root@httpd-757fb56c8d-6st7x:/usr/local/apache2# pwd 
    /usr/local/apache2
    root@httpd-757fb56c8d-6st7x:/usr/local/apache2# cd htdocs/
    root@httpd-757fb56c8d-6st7x:/usr/local/apache2/htdocs# mkdir bar
    root@httpd-757fb56c8d-6st7x:/usr/local/apache2/htdocs# echo "hello apache666--bar" > bar/index.html
    root@httpd-757fb56c8d-6st7x:/usr/local/apache2/htdocs# cat bar/index.html 
    hello apache666--bar
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.再次访问web2.study.com/foo和web2.study.com/bar来进行验证

    foo
    在这里插入图片描述
    bar
    在这里插入图片描述


  • 相关阅读:
    C++基础
    ubuntu基本操作
    Java核心篇,二十三种设计模式(十五),行为型——解析器模式
    Python 程序员过中秋Python+pygame 制作拼图小游戏(附源码:5源码)
    【软考 系统架构设计师】系统可靠性分析与设计② 可靠性设计
    C++的重大特性之一:继承、菱形继承
    java计算机毕业设计-学生宿舍故障报修管理信息系统-源码+数据库+系统+lw文档+mybatis+运行部署
    RDD编程_第五章笔记
    竞赛python区块链实现 - proof of work工作量证明共识算法
    交换机与路由技术-12-单臂路由
  • 原文地址:https://blog.csdn.net/qq_44930876/article/details/136207817