第8章 Kubernetes网络
• Service 对外暴露应用
• Ingress 对外暴露应用最佳方式
Kubernetes网络之Service
• Service存在的意义
• Pod与Service的关系
• Service三种类型
• Service代理模式
• Service DNS名称
8.1Service存在的意义
Service引入主要是解决pod的动态变化,提供统一访问入口:
• 防止pod失联,准备找到提供同一服务的Pod(服务发现)
• 定义一组pod的访问策略(负载均衡)
client -> service -> pod
pod/node -> service(clusterip) -> pod
域名 -> 公网负载均衡器(IP)内网和外网-> (内网机器)NodePort -> 分布在各个节点的Pod
8.2Pod与Service的关系
1.service通过标签关联一组pod
2.service使用iptables或ipvs为一组pod提供负载均衡能力
kubectl expose deployment nginx --port=80 --target-port=80 --dry-run=client -o yaml >service.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx namespace: default spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx # image: lizhenliang/java-demo image: nginx:1.16 ########################### apiVersion: v1 kind: Service metadata: creationTimestamp: null name: nginx spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx status: loadBalancer: {}
8.3Service定义与创建
创建service:
kubectl apply -f service.yaml
查看service:
kubectl get service
#定义service apiVersion: v1 kind: Service metadata: name: web spec: type: ClusterIP # 服务类型 ports: - port: 80 # Service端口 protocol: TCP # 协议 targetPort: 80 # 容器端口(应用程序监听端口) selector: app: web # 指定关联Pod的标签
8.4多端口service定义:
对于某些服务,需要公开多个端口,Service也需要配置多个端口定义,通过端口名称区分。
apiVersion: v1 kind: Service metadata: name: web spec: type: ClusterIP ports: - name: http port: 80 protocol: TCP targetPort: 80 - name: https port: 443 protocol: TCP targetPort: 443 selector: app: web
8.5Service三种常用类型
- ClusterIP:集群内部使用
- NodePort:对外暴露应用
- LoadBalancer:对外暴露应用,适用公有云
8.5.1Service 的ClusterIP类型
ClusterIP:默认,分配一个稳定的IP地址,即VIP,只能在集群内部访问。
spec: type: ClusterIP ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web
8.5.2 Service的NoePort类型
NodePort:在每个节点上启用一个端口来暴露服务,可以在集群外部访问。也会分配一个稳定内部集群IP地址。 访问地址:<任意NodeIP>:<NodePort> 端口范围:30000-32767
spec: type: NodePort ports: - port: 80 protocol: TCP targetPort: 80 nodePort: 30009 selector: app: web
8.5.3Service的LoadBalancer类型:
与nodePort类似,在每个节点上启用一个端口来暴露服务。除此之外,Kubenetes会请求底层云平台(例如:阿里云/腾讯云/AWS等)上的负载均衡器,将每个Node(NodeIP:NodePort)作为后端添加进去。
8.6Service代理模式(iptables和IPVS)
Service的底层实现主要有iptables和ipvs二种网络模式,决定了如何转发流量。
8.7Service方式代理模式修改
Kubectl方式修改ipvs模式: Kubectl edit configmap kube-proxy -n kube-system … mode: “ipvs” … Kubectl delete pod kube-proxy-btz4p -n kube-system 注: 1. kube-proxy配置文件configmap方式存储 2. 如果让所有节点生效,需要重建所有节点kube-proxy pod 二进制方式修改ipvs模式: vi kube-proxy-config.yml mode: ipvs ipvs: scheduler: ”rr” systemctl restart kube-proxy 注:配置文件路径根据实际安装目录为准。
8.8流程包流程
客户端—>NodePort/clusterIP(iptables/ipvs负载均衡规则)—>分布在各节点pod 查看负载均衡规则: Iptables模式: Iptables-save |grep <SERVICE-NAME> Ipvs模式: Ipvsadm -L -n
8.9Iptables VS ipvs
Iptables:
- 灵活,功能强大
- 规则遍历匹配和更新,呈线性时延
IPVS:
- 工作在内核态,有更好的性能
- 调度算法丰富:rr,wrr,lc,wlc,ip hash…
8.10Service DNS 名称
CoreDNS YAML文件: https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns ClusterIP A记录格式:<service-name>.<namespace-name>.svc.cluster.local 示例:my-svc.my-namespace.svc.cluster.local
8.10.1CoreDNS工作流程图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!