当前位置: 首页 > news >正文

k8s从入门到放弃之Service负载均衡

k8s从入门到放弃之Service负载均衡

在 Kubernetes (K8s) 中,Service 是一种抽象,它定义了一组逻辑上的 Pod 和访问它们的策略。Service 的主要目的是提供一种可靠的方式来访问一组具有相同标签(Label)的 Pod,即使这些 Pod 可能会在集群中被动态地创建或销毁。

Service的核心功能

  • 服务发现:通过 DNS 或者环境变量的方式,让其他应用可以找到并访问到这个 Service。
  • 负载均衡:将到达 Service 的请求分发给后端的多个 Pod 实例。
  • 稳定的 IP 地址和 DNS 名称:为一组 Pod 提供一个固定的 IP 地址和 DNS 名称,即使后端的 Pod 发生了变化,如重启、扩展等操作。
  • 流量代理:通过 kube-proxy 组件实现对流量的转发,支持多种代理模式,包括 userspace、iptables 和 IPVS。

Service的类型

  • ClusterIP:默认类型,通过集群内部 IP 暴露服务,只能从集群内部访问。
  • NodePort:通过每个节点的 IP 和静态端口暴露服务,允许外部流量通过节点 IP 和 NodePort 访问服务。
  • LoadBalancer:通常用于云环境中,自动创建一个外部负载均衡器,并分配一个外部 IP 来暴露服务。

ClusterIP案例

资源文档kubectl explain Service.spec

apiVersion: apps/v1
kind: Deployment
metadata:name: deploy-nginx
spec:replicas: 1strategy:type: Recreateselector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.20.0readinessProbe:tcpSocket:port: 80resources:limits:cpu: "100m"ports:- containerPort: 80
---apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:type: ClusterIPselector:app: webports:- protocol: TCPport: 80targetPort: 80

NodePort案例

资源文档kubectl explain Service.spec

apiVersion: apps/v1
kind: Deployment
metadata:name: deploy-nginx
spec:replicas: 1strategy:type: Recreateselector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.20.0readinessProbe:tcpSocket:port: 80resources:limits:cpu: "100m"ports:- containerPort: 80---apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:type: NodePortselector:app: webports:- protocol: TCPport: 80targetPort: 80

查看集群外部访问地址

[root@master /]# kubectl get svc 
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        20d
nginx-svc    NodePort    10.107.140.239   <none>        80:32017/TCP   2m16s

LoadBalancer案例

资源文档 kubectl explain Service.spec

apiVersion: apps/v1
kind: Deployment
metadata:name: deploy-nginx
spec:replicas: 1strategy:type: Recreateselector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.20.0readinessProbe:tcpSocket:port: 80resources:limits:cpu: "100m"ports:- containerPort: 80---apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:type: LoadBalancerselector:app: webports:- protocol: TCPport: 80targetPort: 80

查看集群外部访问地址

[root@master /]# kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>         443/TCP        20d
nginx-svc    LoadBalancer   10.101.116.99   10.244.2.240   80:30143/TCP   2m40s

Service 是 Kubernetes 中非常重要的概念之一,它使得应用程序可以在分布式系统中进行有效的通信和协作。通过合理配置 Service,可以确保应用即使在高动态性的容器化环境中也能保持良好的可用性和可访问性。

相关文章:

  • 【C/C++】玩转正则表达式
  • 对象回调初步研究
  • MySQL中【正则表达式】用法
  • Web中间件--tomcat学习
  • Python如何给视频添加音频和字幕
  • ui框架-文件上传组件
  • 在鸿蒙HarmonyOS 5中使用DevEco Studio实现指南针功能
  • 门静脉高压——治疗
  • 智能体革命:企业如何构建自主决策的AI代理?
  • 魔兽世界正式服插件与宏-敏锐盗贼实用宏探索(1)-宏命令制作入门与基本知识
  • C++--stack和queue的使用及其模拟实现
  • CppCon 2015 学习:Reactive Stream Processing in Industrial IoT using DDS and Rx
  • 脚本安装Doris2.10和Doris Manager
  • 什么是OLAP?OLAP具体有哪些应用?
  • 前缀和题目:寻找数组的中心下标
  • 使用VMware克隆功能快速搭建集群
  • Linux od命令
  • 火绒弹窗拦截6.0.6.1\5.0.77.1绿色独立版_WinAll
  • 6.9-QT模拟计算器
  • 封装技术生命周期 从CDIP到CSP到SiP先进封装
  • 爱 做 网站/关键词名词解释
  • wordpress文章显示标签/电脑优化大师哪个好
  • 淮安哪里有做网站的/google play应用商店
  • 网站建设客户分析调查表/百度输入法免费下载
  • 重庆奉节网站建设/腾讯企点官网
  • asp 网站源码 下载/中关村在线app