Comfigmap:存储数据
Date:
Key:value
挂载的方式,把配置信息传给容器
生产当中的yml文件很长:
有deployment
容器的探针
资源限制
Configmap
存储卷
Service
Ingress
K8s的对外服务,ingress
Service作用体现在两个方面:
- 集群内部:不断跟踪pod的变化,更新endpoint中的pod对象,基于pod的IP地址不断变化的一种服务发现机制
- 集群外部:类似于负载均衡器,把流量IP+端口,不涉及url(http https),把请求转发到pod当中
Service:
NodePort:容器端口---service端口----NodePort,设定了nodePort,每个节点都会有一个端口被打开,30000-32767
IP+端口:节点IP+30000-32767,实现负载均衡
Loadbalancer:云平台上的一种service机制,云平台一个负载均衡IP地址
Extrenal:域名映射
Ingress:基于域名进行映射,把url(http,https)请求转发到service,再由service把请求转发到每一个pod
Ingress只要一个或者少量的公网IP或者LB,可以把多个http请求暴露到网外,七层反向代理
Service的service。是一组基于域名和url路径,把一个或者多个请求转发到service的规则
先是七层代理---四层代理-----pod
Ingress service nginx
Ingress的组成:
Ingress是一个api对象,通过yaml文件来进行配置,ingress的作用是定义请求如何转发到service的规则,配置模板
Ingress通过http和https暴露集群内部service,给service提供一个外部的URL,负载均衡,ssl/tsl(https)的能力,实现了一个基于域名的负载均衡
Ingress-controller: 具体的实现反向代理和负载均衡的程序,对ingress定义的规则进行解析,根据ingress的配置规则进行请求的转发,Ingress-controller不是k8s自带的组件功能,ingress-controller一个统称
例如nginx ingress controller,traefik都是Ingress-controller,他们都是开源的
Ingress资源的定义项:
- 定义外部流量的规则
- 定义服务的暴露方式,主机名,访问路径和其他的选择
- 负载均衡(ingress-controller)
ingress-controller的运行方式是pod方式运行在集群当中
Nginx-ingress-controller:
如何下载:
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

Ingress暴露服务的方式:
-
方式一:deployment+LoadBalance模式,ingress部署在公有云,ingress配置文件里面会有一个type,type:LoadBalancer. 公有云平台会为个Loadbalance的service创建一个负载均衡器,绑定一个公网地址。通过域名指向这个公网地址就可以实现集群对外暴露
-
方式二:daemonset+hostnetwork+nodeSelector模式:
解析:
Daemonset:在每个节点都会创建一个pod
Hostnework:pod共享节点主机的网络命名空间,容器内直接使用节点主机的IP+端口。Pod当中的容器可以直接访问网络资源
nodeSelector:根据标签来选择部署的节点,Nginx-ingress-controller部署的节点
缺点:直接利用了节点主机的网络和端口,一个node只能部署一个ingress-controller pod比价适合大并发的生产环境,性能是最好的
核心图:

- 方式三:deployment+NodePort 方式
实验演示方式一实现:
1、下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

修改一下配置文件:



创建标签:

kubectl get pod -n ingress-nginx -o wide

上传控制器镜像,也就是nginx-ingress-controller的镜像:拖入每个节点
tar -xf ingree.contro-0.30.0.tar.gz
docker load -i ingree.contro-0.30.0.tar

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

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27897/nginx: master
tcp 0 0 0.0.0.0:8181 0.0.0.0:* LISTEN 27897/nginx: master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 27897/nginx: master
解析:8081端口,nginx-controller默认配置的一个bachend,反向代理的端口
所有请求当中,只要是不符合ingress配置的请求会转发到8081,相当于error的界面
- 定义yml脚本,创建pvc、pod、service、ingress






kubectl get pod -o wide

映射
vim /etc/hosts

测试访问


负载均衡已经实现(分别查看三个容器的日志)



结论:endpointIP地址变化

如何实现第二种方式:
如何实现deployment+nodePort (常见模式)

将之前的全部删除

下载nginx ingress contoller控制器
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml


将原来的基于daemonset删除ingress删除



下载service-nodeport控制器


定义yml脚本,创建pvc、pod、service、ingress





做映射,端口可以随便写



结构图:
解析图:



三种模式:
Nginx-ingress-controller
Host--->ingress的配置找到pod----controller---请求发送到pod
NodePort----controller---ingress-----service----pod
NodePort暴露端口的方式是最简单的方法,NodePort多了一层NAT,(地址转换)
并发量大的对性能会有一定影响,内部会有NodePort
虚拟主机的方式实现http代理:
创建多个pod、service
pod1、service1

pod2、service2

配置ingress




做映射:




总结:
Ingress的核心组件----nginx-ingress-controller和traefik
他们都是开源的ingress--controller
三种方式:
Deployment+loadbalancer需要云平台提供一个负载均衡的公网地址,公有云上做(需要钱)
Daemonset+hostnetwork+nodeselector指定节点部署controller,缺点就是和宿主机共享网络,只能是一个controller的pod
Deployment+NodePort最常见,最简单的方式,其中一个nodeport端口,所有的ingress的请求都会转发到NodePort,然后service把流量转发到pod,一个nginx的NodePort,实现访问多个虚拟主机
和nginx类型:
一个端口可以访问多个
例如:80端口,可以访问www.test1.com www.test2.com