目录
1:Service:将一组Pods公开为网络服务的抽象方法。
2: Ingress:service统一的网关入口,总网关入口
1:Service:将一组Pods公开为网络服务的抽象方法。
#暴露Deploy,集群内部能够访问8080,来进入pod的80达到负载均衡
kubectl expose deploy my-dep --port=8000 --target-port=80
#暴露Deploy,集群内外部能够访问8080,来进入pod的80达到负载均衡
kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePort
#使用标签检索Pod
kubectl get pod -l app=my-dep
#删除暴露端口
kubectl delete service my-dep
# 查看pod标签,相同的deployment 有同一个标签
kubectl get pod --show-labels
配置文件创建
apiVersion: v1
kind: Service
metadata:
labels:
app: my-dep
name: my-dep
spec:
selector:
app: my-dep
ports:
- port: 8000
protocol: TCP
targetPort: 80
2: Ingress:service统一的网关入口,总网关入口
# 下载ingress
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/baremetal/deploy.yaml
#修改镜像
vi deploy.yaml
#将image的值改为如下值:
registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller:v0.46.0
# 检查安装的结果
kubectl get pod,svc -n ingress-nginx
# 最后别忘记把svc暴露的端口要放行
80:31378/TCP,443:31782/TCP http访问31378端口 http访问31782端口
测试:
1:环境准备
部署两个Deployment,每个Deployment的pod有两个副本 ,部署两个网格分别管理两组pod
[root@k8s-master ~]# vim test.yaml
添加如下内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-server
spec:
replicas: 2
selector:
matchLabels:
app: hello-server
template:
metadata:
labels:
app: hello-server
spec:
containers:
- name: hello-server
image: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/hello-server
ports:
- containerPort: 9000
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-demo
name: nginx-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-demo
template:
metadata:
labels:
app: nginx-demo
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-demo
name: nginx-demo
spec:
selector:
app: nginx-demo
ports:
- port: 8000
protocol: TCP
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
labels:
app: hello-server
name: hello-server
spec:
selector:
app: hello-server
ports:
- port: 8000
protocol: TCP
targetPort: 9000
然后执行 [root@k8s-master ~]# kubectl apply -f test.yaml
2:域名访问
[root@k8s-master ~]# vim ingress-rule.yaml
添加如下内容
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: hello-server
port:
number: 8000
- host: "demo.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/nginx" # 把请求会转给下面的服务,下面的服务一定要能处理这个路径,不能处理就是404
backend:
service:
name: nginx-demo ## java,比如使用路径重写,去掉前缀nginx
port:
number: 8000
注:修改电脑的hosts域名映射关系,集群任意一台主机ip即可
47.104.182.107 hello.atguigu.com
47.104.182.107 demo.atguigu.com
path: "/nginx" 与 path: "/" 虽然都为404,但是path: "/" 是ingress直接返回不处理,path: "/nginx" 是ingress把请求转给了nginx-demo服务处理,但nginx-demo没有对该路径处理
3.路径重写
[root@k8s-master ~]# rm -rf ingress-rule.yaml
[root@k8s-master ~]# vim ingress-rule.yaml
[root@k8s-master ~]# kubectl apply -f ingress-rule.yaml
ingress.networking.k8s.io/ingress-host-bar configured
[root@k8s-master ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-host-bar nginx hello.atguigu.com,demo.atguigu.com 192.168.0.137 80 22m
# vim ingress-rule.yaml的内容
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: hello-server
port:
number: 8000
- host: "demo.atguigu.com"
http:
paths:
- pathType: Prefix
path: "/nginx(/|$)(.*)" # 把请求会转给下面的服务,下面的服务一定要能处理这个路径,不能处理就是404
backend:
service:
name: nginx-demo ## java,比如使用路径重写,去掉前缀nginx
port:
number: 8000
4.流量限制
47.104.182.107 haha.atguigu.com
[root@k8s-master ~]# vim ingress-rule2.yaml
[root@k8s-master ~]# kubectl apply -f ingress-rule2.yaml
ingress.networking.k8s.io/ingress-limit-rate created
[root@k8s-master ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-host-bar nginx hello.atguigu.com,demo.atguigu.com 192.168.0.137 80 27m
ingress-limit-rate nginx haha.atguigu.com 80 3s
# vim ingress-rule2.yaml的内容
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-limit-rate
annotations:
nginx.ingress.kubernetes.io/limit-rps: "1" #每秒放行一个请求
spec:
ingressClassName: nginx
rules:
- host: "haha.atguigu.com"
http:
paths:
- pathType: Exact
path: "/"
backend:
service:
name: nginx-demo
port:
number: 8000
记得疯狂刷新哦