kubernetes-ingress 控制器
- [kubernetes-ingress 控制器](#kubernetes-ingress 控制器)
-
- 1、理解一下负载均衡
-
- 1.1、概念图
- 1.2、理解ingress
- 1.3、ingress的负载均衡属于7层负载均衡
- [1.4、Ingress 的关键特性包括:](#1.4、Ingress 的关键特性包括:)
- [1.5、Ingress 的工作原理:](#1.5、Ingress 的工作原理:)
- [1.6、Ingress 的配置示例:](#1.6、Ingress 的配置示例:)
- [1.7、ingress和ingress controller的关系](#1.7、ingress和ingress controller的关系)
- 2、做实验:基于域名的负载均衡
-
- [2.1、第1大步骤: 安装ingress controller](#2.1、第1大步骤: 安装ingress controller)
-
- 1.将镜像scp到所有的node节点服务器上
- 2.导入镜像,在所有的节点服务器(node-1和node-2)上进行
- [3.使用ingress-controller-deploy.yaml 文件去启动ingress controller](#3.使用ingress-controller-deploy.yaml 文件去启动ingress controller)
- [2.2、第2大步骤: 创建pod和暴露pod的服务](#2.2、第2大步骤: 创建pod和暴露pod的服务)
- [2.3、第3大步骤: 启用ingress 关联ingress controller 和service](#2.3、第3大步骤: 启用ingress 关联ingress controller 和service)
- [2.4、第四步:查看ingress controller 里的nginx.conf 文件里是否有ingress对应的规则](#2.4、第四步:查看ingress controller 里的nginx.conf 文件里是否有ingress对应的规则)
- 2.5、第五步:启动2个服务和pod,使用了pv+pvc+nfs
- 3、实验2:基于url的负载均衡
kubernetes-ingress 控制器
1、理解一下负载均衡
在本质上是一个负载均衡器
nginx和lvs都是可以做负载均衡的软件
-
lvs的背后是ipvs,支持4层负载均衡(ip+端口)
-
nginx4层和7层(http协议)都支持
1.1、概念图
基于url的负载均衡:
1.2、理解ingress
ingress的本质上是一个nginx
Ingress 是 Kubernetes 中的一个概念,它管理着集群外部到集群内部的网络流量。Ingress 可以被看作是 Kubernetes 集群的"入口",它允许您定义访问集群中服务的规则,使得外部流量可以基于这些规则被路由到正确的服务。
1.3、ingress的负载均衡属于7层负载均衡
底层使用nginx的域名需要检查http 请求报文里的host的URL
1.4、Ingress 的关键特性包括:
-
外部访问:Ingress 允许外部客户端通过定义的规则访问 Kubernetes 集群内的服务。
-
路由规则:通过 Ingress 资源,您可以定义路由规则,这些规则指定了基于域名、路径或 HTTP 方法 等条件将流量路由到特定的服务。
-
负载均衡:Ingress 控制器通常会处理负载均衡,将流量均匀地分配到后端的多个 Pod 上。
-
SSL/TLS 终端:Ingress 可以提供 SSL/TLS 加密,确保数据传输的安全性。
-
重写和重定向:Ingress 支持 URL 重写和重定向,这使得您可以根据需要将流量从一个路径重定向到另一个路径。
1.5、Ingress 的工作原理:
-
Ingress 资源:在 Kubernetes 中创建 Ingress 资源,定义访问集群内部服务的规则。
-
Ingress 控制器:Ingress 控制器是负责实现 Ingress 规则的组件。它通常作为一个 Pod 运行在 Kubernetes 集群中,并监听 Ingress 资源的变化。
-
流量路由:当外部流量到达集群时,Ingress 控制器根据 Ingress 资源中定义的规则将流量路由到正确的服务。
1.6、Ingress 的配置示例:
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: example.com
http:
paths:
- path: /path1(/|$)(.*)
pathType: Prefix
backend:
service:
name: service1
port:
name: use-annotation
- path: /path2(/|$)(.*)
pathType: Prefix
backend:
service:
name: service2
port:
number: 80
在这个示例中,Ingress 资源定义了两条规则,分别将流量基于不同的路径路由到两个不同的服务。当访问 example.com/path1
时,流量会被路由到 service1
,而访问 example.com/path2
时,流量会被路由到 service2
。
请注意,Ingress 的实现和功能可能会根据使用的 Ingress 控制器(如 NGINX、Traefik 等)的不同而有所差异。因此,具体的配置和可用的注解可能会有所不同。
1.7、ingress和ingress controller的关系
先搞明白什么是ingress和ingress controller
Ingress 是 Kubernetes 中的一个资源对象
,它定义了
访问集群内部服务的规则
。Ingress 资源本身不处理流量路由,而是作为一组声明性的配置,告诉 Kubernetes 集群应该如何处理进入集群的外部流量。Ingress 资源包括:
- 域名和路径的路由规则
- SSL/TLS 证书配置
- 负载均衡策略
- 访问控制和限制
Ingress 资源通常由 Kubernetes 用户创建和管理,以定义期望的流量路由行为。
Ingress Controller 是实际运行在 Kubernetes 集群中的一个 Pod 或服务,它的任务是实现 Ingress 资源中定义的流量路由规则。Ingress Controller 通常由第三方提供,例如 NGINX、Traefik、HAProxy 等,它们负责监听 Ingress 资源的变化,并相应地更新自己的配置,以确保外部流量根据 Ingress 规则被正确地路由到后端服务。
所以他们之间的关系:
Ingress 和 Ingress Controller 之间的关系可以类比于 Kubernetes 中的 Deployment 和 Pod 的关系
。Ingress 资源定义
了期望的状态(即流量应该如何被路由),而 Ingress Controller 负责实现
这个状态。Ingress Controller 作为集群的一部分,通过监听 API 服务器中的 Ingress 资源变化来动态更新自己的配置,从而确保流量路由的实时性和准确性。
ingress controller 本质上是一个nginx软件,用来做负载均衡
ingress 是k8s内部管理nginx配置(nginx.conf)的组件,用来给ingress controller传参
2、做实验:基于域名的负载均衡
使用旧版本ingress controller v1.1完成
准备工作:需要提前上传下面的这些镜像和yaml文件到k8s集群里的linux系统里,建议存放到master节点上,然后再scp到node节点上
shell
[root@master ingress]# ls
ingress-controller-deploy.yaml nfs-pvc.yaml sc-ingress.yaml
ingress_nginx_controller.tar nfs-pv.yaml sc-nginx-svc-1.yaml
kube-webhook-certgen-v1.1.0.tar.gz nginx-deployment-nginx-svc-2.yaml
[root@master ingress]#
- ingress-controller-deploy.yaml 是部署ingress controller使用的yaml文件
- ingress-nginx-controllerv1.1.0.tar.gz ingress-nginx-controller镜像
- kube-webhook-certgen-v1.1.0.tar.gz kube-webhook-certgen镜像
- sc-ingress.yaml 创建ingress的配置文件
- sc-nginx-svc-1.yaml 启动sc-nginx-svc服务和相关pod的yaml
- nginx-deployment-nginx-svc-2.yaml 启动sc-nginx-svc-2服务和相关pod的yaml
2.1、第1大步骤: 安装ingress controller
1.将镜像scp到所有的node节点服务器上
还要scp:ingress_nginx_controller.tar 我是直接用docker pull拉取的
shell
[root@master ingress]# scp kube-webhook-certgen-v1.1.0.tar.gz node-2:/root
kube-webhook-certgen-v1.1.0.tar.gz 100% 47MB 123.6MB/s 00:00
[root@master ingress]# scp kube-webhook-certgen-v1.1.0.tar.gz node-1:/root
kube-webhook-certgen-v1.1.0.tar.gz 100% 47MB 144.4MB/s 00:00
2.导入镜像,在所有的节点服务器(node-1和node-2)上进行
shell
[root@master ingress]# docker load -i kube-webhook-certgen-v1.1.0.tar.gz
c0d270ab7e0d: Loading layer 3.697MB/3.697MB
ce7a3c1169b6: Loading layer 45.38MB/45.38MB
Loaded image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
[root@master ingress]#
3.使用ingress-controller-deploy.yaml 文件去启动ingress controller
shell
[root@master ingress]# kubectl apply -f ingress-controller-deploy.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
configmap/ingress-nginx-controller created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
service/ingress-nginx-controller-admission created
service/ingress-nginx-controller created
deployment.apps/ingress-nginx-controller created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
serviceaccount/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
[root@master ingress]#
因为我下载的国内镜像
所以在ingress-controller-deploy.yaml 这个文件中要更改镜像来源
查看ingress controller的相关命名空间
shell
[root@master ingress]# kubectl get ns
NAME STATUS AGE
default Active 19d
default-mem-example Active 15d
gaohui-test Active 18h
halou-gh Active 14d
ingress-nginx Active 110s
kube-node-lease Active 19d
kube-public Active 19d
kube-system Active 19d
mem-example Active 15d
sc Active 15d
sc2 Active 12d
查看ingress controller的相关service
shell
[root@master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.105.40.85 <none> 80:32767/TCP,443:30105/TCP 2m4s
ingress-nginx-controller-admission ClusterIP 10.111.4.214 <none> 443/TCP 2m4s
查看ingress controller的相关pod
shell
[root@master ingress]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-fwrjt 0/1 Completed 0 2m28s
ingress-nginx-admission-patch-m7ftw 0/1 Completed 0 2m28s
ingress-nginx-controller-589dccc958-pz6s8 1/1 Running 0 2m28s
ingress-nginx-controller-589dccc958-zhrpq 1/1 Running 0 2m28s
[root@master ingress]#
2.2、第2大步骤: 创建pod和暴露pod的服务
yaml
[root@master ingress]# cat sc-nginx-svc-1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sc-nginx-deploy
labels:
app: sc-nginx-feng
spec:
replicas: 3
selector:
matchLabels:
app: sc-nginx-feng
template:
metadata:
labels:
app: sc-nginx-feng
spec:
containers:
- name: sc-nginx-feng
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: sc-nginx-svc
labels:
app: sc-nginx-svc
spec:
selector:
app: sc-nginx-feng
ports:
- name: name-of-service-port
protocol: TCP
port: 80
targetPort: 80
[root@master ingress]#
shell
[root@master ingress]# kubectl apply -f sc-nginx-svc-1.yaml
deployment.apps/sc-nginx-deploy created
service/sc-nginx-svc created
[root@master ingress]#
shell
[root@master ingress]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
sc-nginx-deploy 3/3 3 3 8m7s
[root@master ingress]#
shell
[root@master ingress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19d
mydb ClusterIP 10.111.97.213 <none> 80/TCP 14d
myservice ClusterIP 10.110.72.101 <none> 80/TCP 14d
nginx-stateful NodePort 10.109.219.37 <none> 80:30011/TCP 5d
sc-nginx-svc ClusterIP 10.96.83.44 <none> 80/TCP 8m1s
2.3、第3大步骤: 启用ingress 关联ingress controller 和service
shell
[root@master ingress]# kubectl apply -f sc-ingress.yaml
ingress.networking.k8s.io/sc-ingress created
[root@master ingress]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
sc-ingress nginx www.feng.com,www.zhang.com 80 11s
[root@master ingress]#
shell
'过几分钟后,显示ip地址'
[root@master ingress]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
sc-ingress nginx www.feng.com,www.zhang.com 192.168.182.134,192.168.182.135 80 56s
[root@master ingress]#
2.4、第四步:查看ingress controller 里的nginx.conf 文件里是否有ingress对应的规则
shell
[root@master ingress]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-fwrjt 0/1 Completed 0 152m
ingress-nginx-admission-patch-m7ftw 0/1 Completed 0 152m
ingress-nginx-controller-589dccc958-pz6s8 1/1 Running 0 152m
ingress-nginx-controller-589dccc958-zhrpq 1/1 Running 0 152m
shell
[root@master ingress]# kubectl exec -it ingress-nginx-controller-589dccc958-pz6s8 -n ingress-nginx -- bash
bash-5.1$
bash-5.1$ cat nginx.conf|grep zhang.com
## start server www.zhang.com
server_name www.zhang.com ;
## end server www.zhang.com
bash-5.1$
在其他的宿主机(nfs服务器上)或者windows机器上使用域名进行访问
shell
'先添加hosts'
[root@nfs-server ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.182.134 www.feng.com
192.168.182.135 www.zhang.com
[root@nfs-server ~]#
shell
[root@nfs-server ~]# curl www.feng.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
2.5、第五步:启动2个服务和pod,使用了pv+pvc+nfs
需要提前准备好nfs服务器+创建pv和pvc
shell
[root@master ingress]# cat nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sc-nginx-pv
labels:
type: sc-nginx-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: "/web" #nfs共享的目录
server: 192.168.182.136 #nfs服务器的ip地址
readOnly: false
[root@master ingress]#
shell
[root@master ingress]# kubectl apply -f nfs-pv.yaml
persistentvolume/sc-nginx-pv created
shell
[root@master ingress]# cat nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sc-nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: nfs #使用nfs类型的pv
[root@master ingress]# kubectl apply -f nfs-pvc.yaml
persistentvolumeclaim/sc-nginx-pvc created
[root@master ingress]#
shell
[root@master ingress]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/sc-nginx-pv 10Gi RWX Retain Bound default/sc-nginx-pvc nfs 4m18s
persistentvolume/sc-nginx-pv-2 5Gi RWX Retain Bound default/sc-nginx-pvc-2 nfs 3d3h
persistentvolume/task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 3d9h
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/sc-nginx-pvc Bound sc-nginx-pv 10Gi RWX nfs 52s
persistentvolumeclaim/sc-nginx-pvc-2 Bound sc-nginx-pv-2 5Gi RWX nfs 3d3h
persistentvolumeclaim/task-pv-claim Bound task-pv-volume 10Gi RWO manual 3d9h
[root@master ingress]#
启第二个pod和第二个服务
shell
[root@master ingress]# kubectl apply -f nginx-deployment-nginx-svc-2.yaml
deployment.apps/nginx-deployment created
service/sc-nginx-svc-2 created
[root@master ingress]#
访问宿主机暴露的端口号32767或者80都可以
shell
[root@master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.105.40.85 <none> 80:32767/TCP,443:30105/TCP 174m
ingress-nginx-controller-admission ClusterIP 10.111.4.214 <none> 443/TCP 174m
[root@master ingress]#
shell
[root@nfs-server ~]# curl www.zhang.com
welcome to sanchuang
welcome to changsha
[root@nfs-server ~]#
shell
[root@nfs-server ~]# curl www.feng.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nfs-server ~]#
3、实验2:基于url的负载均衡
1.准备yaml文件
shell
[root@master lb-url]# ls
sc-ingress-url.yaml sc-nginx-svc-3.yaml sc-nginx-svc-4.yaml
[root@master lb-url]#
sc-ingress-url.yaml --》'是创建一个基于URL做负载均衡的ingress'
sc-nginx-svc-3.yaml '创建service3 和相关pod'
sc-nginx-svc-4.yaml '创建service4 和相关pod'
查看目前已经创建的ingress
shell
[root@master lb-url]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
sc-ingress nginx www.feng.com,www.zhang.com 192.168.182.134,192.168.182.135 80 14h
[root@master lb-url]#
2.我们去创建另外一个基于url的负载均衡的ingress
yaml
[root@master lb-url]# vim sc-ingress-url.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: simple-url-lb-example
annotations:
kubernets.io/ingress.class: nginx
spec:
ingressClassName: nginx
rules:
- host: www.wen.com
http:
paths:
- path: /foo
pathType: Prefix
backend:
service:
name: sc-nginx-svc-3
port:
number: 80
- path: /bar
pathType: Prefix
backend:
service:
name: sc-nginx-svc-4
port:
number: 80
[root@master lb-url]#
去启动
shell
[root@master lb-url]# kubectl apply -f sc-ingress-url.yaml
ingress.networking.k8s.io/simple-url-lb-example created
[root@master lb-url]#
shell
[root@master lb-url]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
sc-ingress nginx www.feng.com,www.zhang.com 192.168.182.134,192.168.182.135 80 14h
simple-url-lb-example nginx www.wen.com 192.168.182.134,192.168.182.135 80 35s
[root@master lb-url]#
shell
[root@master lb-url]# kubectl apply -f sc-nginx-svc-3.yaml
deployment.apps/sc-nginx-deploy-3 created
service/sc-nginx-svc-3 created
[root@master lb-url]# kubectl apply -f sc-nginx-svc-4.yaml
deployment.apps/sc-nginx-deploy-4 created
service/sc-nginx-svc-4 created
[root@master lb-url]#
shell
[root@master lb-url]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
sc-ingress nginx www.feng.com,www.zhang.com 192.168.182.134,192.168.182.135 80 14h
simple-url-lb-example nginx www.wen.com 192.168.182.134,192.168.182.135 80 98s
[root@master lb-url]#
3.在nfs服务器上进行测试,先添加域名解析记录
shell
[root@nfs-server ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.182.134 www.feng.com
192.168.182.134 www.zhang.com
192.168.182.134 www.wen.com
192.168.182.135 www.wen.com
[root@nfs-server ~]#
4.测试发现不能找到页面
shell
[root@nfs-server ~]# curl www.wen.com/foo
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
[root@nfs-server ~]#
[root@nfs-server ~]# curl www.wen.com/bar
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
[root@nfs-server ~]#
到底是ingress controller的问题还是我们后端的pod的问题
查看ingress-nginx-controller pod的信息
shell
[root@master lb-url]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-fwrjt 0/1 Completed 0 16h
ingress-nginx-admission-patch-m7ftw 0/1 Completed 0 16h
ingress-nginx-controller-589dccc958-pz6s8 1/1 Running 1 16h
ingress-nginx-controller-589dccc958-zhrpq 1/1 Running 1 16h
[root@master lb-url]#
查看思路
1.进入ingress控制器的内部查看是否加载基于URL的负载均衡的配置
shell
[root@master lb-url]# kubectl exec -it ingress-nginx-controller-589dccc958-pz6s8 -n ingress-nginx -- bash
2.看pod日志
shell
[root@master lb-url]# kubectl logs ingress-nginx-controller-589dccc958-pz6s8 -n ingress-nginx
3.看详细信息
shell
[root@master lb-url]# kubectl describe pod ingress-nginx-controller-589dccc958-pz6s8 -n ingress-nginx
4.进入pod的内部,查看nginx的日志
shell
看不了
5.盯着末尾看日志
shell
[root@master lb-url]# kubectl logs -f ingress-nginx-controller-589dccc958-pz6s8 -n ingress-nginx
192.168.182.136 - - [23/Mar/2024:02:26:19 +0000] "GET /bar HTTP/1.1" 404 153 "-" "curl/7.29.0" 78 0.001 [default-sc-nginx-svc-4-80] [] 10.244.84.147:80 153 0.001 404 6c3fb38ab80c8a7bab9baec078001aaf
发现是pod的问题,去看容器的日志
shell
[root@master lb-url]# kubectl logs sc-nginx-deploy-4-766c99dd77-dgzq5 -f
192.168.182.134 - - [23/Mar/2024:02:30:21 +0000] "GET /bar HTTP/1.1" 404 153 "-" "curl/7.29.0" "192.168.182.136"
2024/03/23 02:30:21 [error] 30#30: *4 open() "/usr/share/nginx/html/bar" failed (2: No such file or directory), client: 192.168.182.134, server: localhost, request: "GET /bar HTTP/1.1", host: "www.wen.com"
发现缺少/usr/share/nginx/html/bar bar文件夹没有新建,导致了404错误
5.进入service4 对应的一个pod里,新建bar和foo文件夹以及index.html网页文件
shell
[root@master lb-url]# kubectl exec -it sc-nginx-deploy-4-766c99dd77-dgzq5 -- bash
root@sc-nginx-deploy-4-766c99dd77-dgzq5:/# cd /usr/share/nginx/html/
root@sc-nginx-deploy-4-766c99dd77-dgzq5:/usr/share/nginx/html# mkdir foo
root@sc-nginx-deploy-4-766c99dd77-dgzq5:/usr/share/nginx/html# mkdir bar
root@sc-nginx-deploy-4-766c99dd77-dgzq5:/usr/share/nginx/html# echo 'hello,foo' >foo/index.html
root@sc-nginx-deploy-4-766c99dd77-dgzq5:/usr/share/nginx/html# echo 'hello,bar' >bar/index.html
6.再去访问
shell
[root@nfs-server ~]# curl www.wen.com/bar/index.html
hello,bat
[root@nfs-server ~]#
在nfs服务器上测试,多测试几次,因为service 背后的ipvs的调度算法是轮询的
验证和排错
数据的流程
ingress-controller--》ingress--》service-->pod
shell
'查看配置的负载均衡策略'
kubectl describe ingress simple-url-lb-example
'在ingress-controller上查看负载均衡的情况'
[root@master url]# kubectl logs -f ingress-nginx-controller-6c8ffbbfcf-4j872 -n ingress-nginx
'在后端的nginx pod上查看访问日志'
[root@master url]# kubectl logs sc-nginx-deploy-3-7496c84fcf-6kkr5 -f
'查看ingress的负载均衡配置'
[root@master lb-url]# kubectl describe ingress simple-url-lb-example
Name: simple-url-lb-example
Namespace: default
Address: 192.168.203.129,192.168.203.130
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
www.wen.com
/foo sc-nginx-svc-3:80 (10.244.247.10:80,10.244.247.3:80,10.244.84.151:80)
/bar sc-nginx-svc-4:80 (10.244.247.13:80,10.244.84.154:80,10.244.84.160:80)
Annotations: kubernets.io/ingress.class: nginx
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 30m (x2 over 31m) nginx-ingress-controller Scheduled for sync
Normal Sync 30m (x2 over 31m) nginx-ingress-controller Scheduled for sync
[root@master lb-url]#
'查看service对应的endpoint 是否存在,就可以指定service是否有问题'
[root@master lb-url]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.203.128:6443 22d
mysql-service 10.244.84.158:3306 3d17h
nginx <none> 2d17h
nginx-stateful <none> 2d17h
php-apache <none> 19d
redis-leader <none> 3d18h
sc-nginx-svc 10.244.84.136:80,10.244.84.142:80,10.244.84.157:80 17h
sc-nginx-svc-2 10.244.247.38:80,10.244.84.138:80,10.244.84.146:80 17h
sc-nginx-svc-3 10.244.247.10:80,10.244.247.3:80,10.244.84.151:80 31m
sc-nginx-svc-4 10.244.247.13:80,10.244.84.154:80,10.244.84.160:80 31m
scnginx-service <none> 3d23h
scnginx-service-2 <none> 3d18h
wordpress-mysql 10.244.247.56:3306 41h
[root@master lb-url]#