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

apache:

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

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

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>

测试机添加本地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

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

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

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


  • 23
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名运维:

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值