文章目录
- 前言
- 一、基于请求地址转发不同应用的pod
- 1.创建一个nginx的pod和一个apache的pod及其各自的service
- 2.创建ingress实现一个地址两个path分别访问nginx和apache
- 3.验证根据域名web2.study.com的两个路径/foo和/bar来访问到不同的pod
- 4.分别在nginx和apache的pod里创建网站目录以及index.html
- 5.再次访问web2.study.com/foo和web2.study.com/bar来进行验证
前言
从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