k8s (七) --- Kubernetes Service 之 Ingress
Kubernetes Service 之Ingress一、Ingress介绍二、Ingress的部署三、添加域名访问ingress单域名单服务多域名多服务单域名多服务官方文档:https://kubernetes.io/zh/docs/concepts/services-networking/ingress/一、Ingress介绍一种全局的、为了代理不同后端 Service 而设置的负载均衡...
Kubernetes Service 之Ingress
官方文档:https://kubernetes.io/zh/docs/concepts/services-networking/ingress/
一、Ingress介绍
一种全局的、为了代理不同后端 Service 而设置的负载均衡服务,就是 Kubernetes 里的Ingress 服务。
Ingress由两部分组成:Ingress controller和Ingress服务。
Ingress Controller 会根据你定义的 Ingress 对象,提供对应的代理能力。业界常用的各种反向代理项目,比如 Nginx、HAProxy、Envoy、Traefik 等,都已经为Kubernetes 专门维护了对应的 Ingress Controller。以下选择nginx进行演示。
二、Ingress的部署
下载ingress controller定义文件:
[root@server1 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml #可能需要多拉取几次才会成功
应用定义文件后到pod运行可能需要等几分钟来拉取镜像(300多M),我们可以先在其他节点上拉取所需要的镜像:
首先查看定义文件确定所需镜像:
[root@server1 ~]# vim mandatory.yaml
当然如果你有私有仓库也可以先将这个镜像拉取下来然后放更改文件种的镜像名称从私有仓库拉取。
在各节点拉取镜像:
[root@server2 ~]# docker pull quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
[root@server3 ~]# docker pull quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
应用ingress controller定义文件:
[root@server1 ~]# kubectl apply -f mandatory.yaml
namespace/ingress-nginx created
应用后会创建一个名为ingress-nginx 的namespace:
等待一下查看pod状态:
[root@server1 ~]# kubectl -n ingress-nginx get pod
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-5bb8fb4bb6-j26w5 1/1 Running 0 13s
可以看出ingress-controller已经正常运行,接下来运行ingress-service:
下载定义文件:
[root@server1 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
应用定义文件:
[root@server1 ~]# kubectl apply -f service-nodeport.yaml
service/ingress-nginx created
应用后查看svc状态:
[root@server1 ~]# kubectl -n ingress-nginx get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.109.140.206 <none> 80:31899/TCP,443:32456/TCP 3s
可以看出这个service的方式时NodePort,因此在访问在这个服务的时候需要加端口31899.
接下来进行实验的准备,创建一个myservice服务:
创建一个名为myservice的服务使用默认方式ClusterIP:
[root@server1 ~]# vim service.yaml
[root@server1 ~]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: nginx
type: ClusterIP
[root@server1 ~]# kubectl apply -f service.yaml
service/myservice created
之后创建ingress服务并将myservice添加为后端服务:
[root@server1 ~]# vim ingresss.yaml
[root@server1 ~]# cat ingresss.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-demo
spec:
backend:
serviceName: myservice #这个表示要调度的后端service的名称
servicePort: 80 #myservice的端口
[root@server1 ~]# kubectl apply -f ingresss.yaml
ingress.networking.k8s.io/ingress-demo created
现在基本的ingress集群已经部署成功,可以使用以下命令查看ingress服务:
[root@server1 ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-demo <none> * 80 17s
之后测试访问:
[root@foundation63 Desktop]# curl 172.25.63.3:31899
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@foundation63 Desktop]# curl 172.25.63.3:31899
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
访问成功。
三、添加域名访问ingress
单域名单服务
更改ingresss.yaml文件:
[root@server1 ~]# vim ingresss.yaml
[root@server1 ~]# cat ingresss.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-demo
spec:
backend:
serviceName: myservice
servicePort: 80
rules:
- host: www1.westos.org #添加域名
http:
paths:
- path: /
backend:
serviceName: myservice
servicePort: 80
[root@server1 ~]# kubectl apply -f ingresss.yaml
ingress.networking.k8s.io/ingress-demo configured
查看ingress发现已经有域名了:
[root@server1 ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-demo <none> www1.westos.org 10.109.140.206 80 47m
测试访问:
实验环境中需要提添加解析:
[root@foundation63 Desktop]# vim /etc/hosts
[root@foundation63 Desktop]# cat /etc/hosts
172.25.63.3 www1.westos.org www2.westos.org www3.westos.org
#多添加的两个以后实验要用。
访问:
[root@foundation63 Desktop]# curl www1.westos.org:31899
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@foundation63 Desktop]# curl www1.westos.org:31899
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
可以成功访问,同样也可以实现负载均衡。
多域名多服务
[root@server1 ~]# vim ingresss2.yaml
[root@server1 ~]# cat ingresss2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-myapp ##创建后端pod
labels:
app: myapp
spec:
replicas: 4
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1 #版本为v1
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service #创建service名为mynginx
metadata:
name: mynginx
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: myapp
type: ClusterIP
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-example #创建ingress,ingress服务名称
spec:
rules:
- host: www2.westos.org
http:
paths:
- path: /
backend:
serviceName: mynginx #关联名为mynginx的service
servicePort: 80
[root@server1 ~]# kubectl apply -f ingresss2.yaml
deployment.apps/deployment-myapp created
service/mynginx created
ingress.networking.k8s.io/ingress-example created
以上文件中定义的服务含义是当用户访问www2.westos.org
时调度到版本为v1的pod上,结合上个实验,当访问www1.westos.org
时调度到版本为v2的pod上,创建后查看ingress:
测试访问:
[root@foundation63 Desktop]# curl www1.westos.org:31899
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@foundation63 Desktop]# curl www2.westos.org:31899
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
可以成功访问,查看负载均衡情况:
[root@foundation63 Desktop]# curl www1.westos.org:31899/hostname.html
deployment-nginx-868855d887-q9bxz
[root@foundation63 Desktop]# curl www1.westos.org:31899/hostname.html
deployment-nginx-868855d887-k4xn6
[root@foundation63 Desktop]# curl www1.westos.org:31899/hostname.html
deployment-nginx-868855d887-q9bxz
[root@foundation63 Desktop]# curl www1.westos.org:31899/hostname.html
deployment-nginx-868855d887-k4xn6
www1.westos.org是两个主机的负载均衡。
[root@foundation63 Desktop]# curl www2.westos.org:31899/hostname.html
deployment-myapp-5dbb76d9c6-qw6dw
[root@foundation63 Desktop]# curl www2.westos.org:31899/hostname.html
deployment-myapp-5dbb76d9c6-rn4bz
[root@foundation63 Desktop]# curl www2.westos.org:31899/hostname.html
deployment-myapp-5dbb76d9c6-spt9s
[root@foundation63 Desktop]# curl www2.westos.org:31899/hostname.html
deployment-myapp-5dbb76d9c6-ljwnp
www1.westos.org是4个主机的负载均衡。
也可以更改yaml文件实现pod的拉伸,此处不在做演示。
单域名多服务
目的是访问一个域名的不同路径时,调度到不同的服务:
[root@server1 ~]# vim ingress3.yaml
[root@server1 ~]# cat ingress3.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: simple-fanout-example #ingress服务名称
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: www3.westos.org #定义域名
http:
paths:
- path: /v1
backend:
serviceName: mynginx #/v1路径对应的服务是mynginx
servicePort: 80
- path: /v2
backend:
serviceName: myservice #/v2路径对应的服务是myservice
servicePort: 80
[root@server1 ~]# kubectl apply -f ingress3.yaml
ingress.networking.k8s.io/simple-fanout-example created
测试访问:
[root@foundation63 Desktop]# curl www3.westos.org:31899/v1
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@foundation63 Desktop]# curl www3.westos.org:31899/v2
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
可以看出访问不同路径会被定位到不同的后端服务。
更多推荐
所有评论(0)