一、 service
1、service作用
****①集群内部:****不断跟踪pod的变化,不断更新endpoint中的pod对象,基于pod的IP地址不断变化的一种服务发现机制(endpoint存储最终对外提供服务的IP地址和端口)
****②集群外部:****类似负载均衡器,不涉及转发url(不涉及http和https),把流量(IP地址+端口)转发到pod中
2 、 service类型
****(1)nodeport:****容器端口和service端口做映射,设定nodeport后,每个节点都会有一个端口被打开(30000-32767),通过IP+端口实现负载均衡
****(2)loadbalance:****云平台上的service服务,由云平台提供负载均衡的IP地址
****(3)extrenalname:****基于域名映射
ingress基于域名进行映射,将url转发到service,再由service把请求转发到每一个pod
二、ingress
1、定义
ingress只要一个或少量的公网IP或者loadbalance,可以把多个http请求暴露到外网。ingress是七层反向代理,可以理解为service的service,是一组基于域名和URL路径把一个或多个请求转发到service的规则
七层代理ingress------四层代理service------pod
客户端发出请求,先由ingress七层代理转发到service,再由service四层代理转发到pod 【面试】
2、 ingress组成
****(1)内部结构:****ingress是一个api对象,通过yaml文件来进行配置。ingress的作用是定义请求如何转发到service的规则,相当于一个配置模板,ingress通过http和https暴露集群内部的service,给service提供一个外部的url、负载均衡、ssl/tls(https)功能,实现一个基于域名的负载均衡
****(2)ingress-controller:****由这个组件来具体实现反向代理和负载均衡,以及对ingress定义的规则进行解析,根据ingress配置来进行请求的转发。ingress-controller不是k8s自带的组件,只是一个统称,凡是能实现上述功能的都称为ingress-controller
1)实现ingress-controller功能的开源工具
①nginx ingress contoller
②traefik
2) ingress-controller资源定义项
①定义外部流量的路由规则
②定义服务的暴露方式、主机名、访问路径和其他选项
③负载均衡(由ingress-contrller实现)
3、 下载nginx ingress contoller
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
nginx ingress contoller以pod方式运行在集群中
4、 ingress暴露服务的方式
****(1)deployment+LoadBalancer模式:****把ingress部署在公有云,在ingress配置文件里有一个type,type:LoadBalancer。公有云平台会为loadbalancer的service自动创建一个负载均衡器,且会绑定一个公网地址,通过域名指向这个公网地址可以实现集群对外暴露
****(2)DaemonSet+hostnetwork+nodeSelector模式:****七层代理。DaemonSet每个节点都会创建一个pod,hostnetwork表示pod共享节点主机的网络命名空间,容器内直接使用节点主机的IP地址+端口,pod中的容器可以直接访问主机上的网络资源;nodeSelector根据标签选择nginx ingress contoller部署的节点
缺点:直接利用节点主机的网络和端口,一个node只能部署一个ingress controller pod,比较是个大并发的生产环境,性能最好
(3) ****deployment+NodePort模式:****七层+四层代理


ingress标签匹配ingress-nodeport标签,ingress-nodeport匹配service标签,service匹配pod标签
5、 traefik ingress controller(有可视化界面)

(1)定义: traefik是一个为了让部署微服务更加快捷而诞生的http反向代理、负载均衡(工作在应用层,是七层代理)。traefik可以实时与k8s的API交互,自动感知后端service以及pod的变化,自动更新配置和重载
(2)部署方式
①daemonset
• ****优点:****每个节点都会部署一个trafik,可以自动发现、更新容器的配置,不需要手动重载
• ****缺点:****大型集群中daemonset可能会运行多个traefik的实例,尤其是在节点上不需要大量容器运行的情况下,资源利用率低;daemonset一般部署在对外集群,对外的业务会经常变更,daemonset可以自动发现服务配置变更;也没有办法扩缩容
②deployment
• ****优点:****集中控制,可以使用少量的实例来运行处理整个集群的流量,更容器升级和维护
• ****缺点:****deployment的负载均衡不会均分到每个节点;且无法感知容器内部配置的变化,需手动更新,一般部署在对内集群,对内的业务相对稳定,更新和变化比较少,适用deployment方式
设置标签:
对内服务:traffic-type:interal
对外服务:traffic-type:exteral
(3)nginx-ingress和traefik-ingress的区别
****①相同点:****都是七层代理,都可以动态的更新配置、自动发现服务
②不同点
• traefik-ingress自动更新的重载更快,更方便;nginx-ingress相对较慢
• traefik-ingress的并发能力只有nginx-ingress的60%
ingress之DaemonSet+hostnetwork+nodeSelector模式暴露服务实验
1、下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

nginx ingress contoller以pod方式运行在集群中
2、设置节点标签3、每个节点上传控制器镜像并解压

4、在controller控制器创建的pod的节点上查看端口
netstat -antp|grep nginx

|--------|--------------------------------------------------------------------------------------|
| 8181端口 | nginx-controller默认配置的一个bachend,反向代理的端口,所有请求中,只要是不符合ingress配置的请求会转发到8181,相当于一个error页面 |
5、、创建pvc、pod、service、ingress


kubectl apply -f service-nginx.yaml
6、域名映射


7、测试访问




8、测试删除一个pod查看endpoind是否发生变化

结论:endpointIP地址变化
ingress之deployment+NodePort模式暴露服务实验
1、下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

nginx ingress contoller以pod方式运行在集群中




2、下载service-nodeport控制器




nodeport会在每个节点上创建一个端口
3、创建pv、pod、service、ingress4、域名映射
5、访问测试


基于deployment+NodePort模式的一个ingress可以访问多个主机实验
1、下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
nginx ingress contoller以pod方式运行在集群中
2、下载service-nodeport控制器
3、创建多个pod、service
pod1、service1
pod2、service2

4、创建多个ingress
5、主机映射






基于DaemonSet+hostnetwork+nodeSelector模式的一个ingress可以访问多个主机实验
1、下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

nginx ingress contoller以pod方式运行在集群中
2、设置节点标签
3、每个节点上传控制器镜像并解压

4、在controller控制器创建的pod的节点上查看端口
netstat -antp|grep nginx

|--------|--------------------------------------------------------------------------------------|
| 8181端口 | nginx-controller默认配置的一个bachend,反向代理的端口,所有请求中,只要是不符合ingress配置的请求会转发到8181,相当于一个error页面 |
5、创建多个pod、service
pod1、service1
pod2、service2
6、创建多个ingress


7、主机映射


8、测试

ingress通过域名实现https代理访问实验
第一步:创建证书和密钥
第二步:secrets保存密钥信息,部署pod时将secret信息挂载到pod
1、生成证书和密钥文件
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/0=nginxsvc"

|---------------------------------|--------------------------------------------|
| req | 请求生成证书文件 |
| x509 | 自动生成x.509的自签名的证书 |
| -sha256 | 一种散列算法,用于加密密钥 |
| -nodes | 生成的密钥不加密 |
| -days 365 | 证书有效期365天 |
| -newkey rsa:2048 | rsa的密钥对,长度2048位 |
| -keyout tls.key -out tls.crt | 密钥文件key,证书文件crt |
| -subj "/CN=nginxsvc/0=nginxsvc" | subj主题,CN(common name)名称,O(organization)组织 |
2、创建secret保存证书和密钥(将证书和密钥都加密)
kubectl create secret tls tls-secret --key tls.key --cert tls.crt

3、创建pod、service(创建ingress时导入secret)


注:spec下出现tls字段代表使用的是https协议进行流量转发。表明对于域名www.123ccc.com来说,使用了https协议


4、主机映射


5、测试访问
curl -k https://www.123ccc.com:30048


6、测试轮询



nginx的账号认证实验
第一步:借助http的加密工具,生成认证文件,只能叫auth
第二步:创建ingress
①声明认证类型
②导入认证的密钥文件,以secret的方式存储在集群中
1、下载httpd
yum -y install httpd

2、创建用户、密码

3、创建secret保存用户信息(将用户信息加密)

4、生成新的ingress

使用上一个实验的service。虽然这个信息加密了,但可以把流量转发到上一个实验service对应的pod


5、域名映射


nginx的重写实验
nginx的重定向(用ingress代理实现)
在ingress文件中声明的路径都会跳转到指定页面
1、创建ingress


重点:容器内页面跳转(一定要用Ingress来实现代理)


2、域名映射


3、访问测试

输入www.shu.com:31520跳转到https://www.123ccc.com:30048/
nginx-ingress-controller以上所有均介绍完毕
traefik-ingress的deployment模式实验
1、下载3个yaml文件
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml3、访问ui界面

8080是提供可视化界面的端口


4、创建pod、service、ingress

7、域名映射
8、测试





扩容



缩容
traefik-ingress的daemonset模式实验
1、下载3个文件
①wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
②wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml
③wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml
2、修改nginx的配置文件


3、创建pod、service、ingress
4、测试daemonset自动发现功能

5、域名映射


6、热更新(将配置文件传入容器中)
kubectl patch deployments.apps nginx-traefik --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20240118"}}}}}'

7、更改配置文件,看ui界面是否自动发现