K8S-Service 学习

news/2025/9/28 9:46:36/文章来源:https://www.cnblogs.com/davidtian/p/19113104

image

  1. 什么是 Kubernetes Service?
    Service 是 Kubernetes 中用于为一组 Pod(通常是 Deployment/ReplicaSet 管理的)提供稳定访问入口的抽象对象

Pod 的 IP 是不固定的,Pod 重建后 IP 会变。
Service 提供一个“虚拟 IP”(ClusterIP),无论后端 Pod 如何变化,访问 Service 的方式不变。
Service 还实现了负载均衡,将流量分发到后端所有健康的 Pod。
2. Service 的工作原理
Service 会根据 selector(标签选择器)自动发现一组符合条件的 Pod,Service 发布的时候,通过label去找哪些Pod 可以部署这个service。
Service 在集群内部创建一个虚拟 IP(ClusterIP),所有请求都会自动转发到后端 Pod。
K8s 使用 kube-proxy 实现流量转发和负载均衡。
3. 常见 Service 类型
image

NodePort 模式
image
image
image
image


创建nodeport的yaml

azureuser@master-001:~$ cat Nodeport.yaml
apiVersion: v1
kind: Service
metadata:name: web-nodeport-svc
spec:type: NodePortselector:app: webports:- port: 80           # Service端口(集群内访问)targetPort: 80     # Pod容器端口(nginx)nodePort: 30080    # 可省略,K8s自动分配30000-32767之间的端口号,也可以手动指定。

执行并测试, Master 可以通过node的IP,pod IP访问, vnet 内部可以通过node IP 访问, 外网可以通过公网地址访问。

azureuser@master-001:~$ kubectl apply -f Nodeport.yaml
service/web-nodeport-svc created
azureuser@master-001:~$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
nginx-deploy-f88b65dbc-54gdx   1/1     Running   0          98m
nginx-deploy-f88b65dbc-fq7db   1/1     Running   0          98m
nginx-deploy-f88b65dbc-nxss8   1/1     Running   0          98m
azureuser@master-001:~$ kubectl get service
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP        45h
nginx-svc           ClusterIP   10.102.29.115    <none>        80/TCP         3h20m
nodeport-svc        NodePort    10.101.111.134   <none>        80:30443/TCP   44h
web-clusterip-svc   ClusterIP   10.102.249.165   <none>        80/TCP         105m
web-nodeport-svc    NodePort    10.109.102.59    <none>        80:30080/TCP   13s
web-svc             ClusterIP   10.104.249.89    <none>        80/TCP         44h
azureuser@master-001:~$ kubectl describe service web-nodeport-svc
Name:                     web-nodeport-svc
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=web
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.109.102.59
IPs:                      10.109.102.59
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.244.1.16:80,10.244.2.20:80,10.244.2.21:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
azureuser@master-001:~$ ip route
default via 10.0.0.1 dev eth0 proto dhcp src 10.0.0.8 metric 100
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.8 metric 100
10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1
10.244.1.0/24 via 10.244.1.0 dev flannel.1 onlink
10.244.2.0/24 via 10.244.2.0 dev flannel.1 onlink
168.63.129.16 via 10.0.0.1 dev eth0 proto dhcp src 10.0.0.8 metric 100
169.254.169.254 via 10.0.0.1 dev eth0 proto dhcp src 10.0.0.8 metric 100
azureuser@master-001:~$
azureuser@master-001:~$
azureuser@master-001:~$
azureuser@master-001:~$
azureuser@master-001:~$ curl http://10.0.0.5:30080
<!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>
azureuser@master-001:~$ curl http://10.244.1.16:80
<!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>

如何让vnet 内的VM 通过pod IP 访问?
kubenet 下,仅靠 UDR 无法让 VNet 其它 VM 直通 Pod IP。必须用 NodePort、LoadBalancer Service 或切换到 Azure CNI 等原生方案。

ClusterIP 模式
ClusterIP 只能在cluster内部访问,一般用于各个service 之间的相互访问。

image

创建clusterIP的yaml文件

apiVersion: v1
kind: Service
metadata:name: web-clusterip-svc
spec:type: ClusterIPselector:app: webports:- port: 80targetPort: 80

部署yaml文件

azureuser@master-001:~$ vi svc-clusterip.yaml
azureuser@master-001:~$ vi deployment1.yaml
#可以创建成功
azureuser@master-001:~$ kubectl apply -f svc-clusterip.yaml
service/web-clusterip-svc created
azureuser@master-001:~$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-8556777689-2dd92   1/1     Running   0          95m
nginx-deploy-8556777689-9r8w2   1/1     Running   0          95m
nginx-deploy-8556777689-b9hwl   1/1     Running   0          95m
nginx-deploy-8556777689-h5fcm   1/1     Running   0          95m
nginx-deploy-8556777689-sm89m   1/1     Running   0          95m
azureuser@master-001:~$ kubectl get service
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP        43h
nginx-svc           ClusterIP   10.102.29.115    <none>        80/TCP         94m
nodeport-svc        NodePort    10.101.111.134   <none>        80:30443/TCP   42h
web-clusterip-svc   ClusterIP   10.102.249.165   <none>        80/TCP         13s
web-svc             ClusterIP   10.104.249.89    <none>        80/TCP         42h# 但访问测试失败azureuser@master-001:~$ kubectl run testbox --image=busybox --restart=Never --rm -it -- /bin/sh-O- http://web-clusterip-svc.default.svc.cluster.local/
If you don't see a command prompt, try pressing enter.
/ # # ? busybox ??????
/ # wget -O- http://web-clusterip-svc.default.svc.cluster.local/
Connecting to web-clusterip-svc.default.svc.cluster.local (10.102.249.165:80)
wget: can't connect to remote host (10.102.249.165): Connection refused
/ #
/ #
/ # wget -O- http://web-svc.default.svc.cluster.local/
Connecting to web-svc.default.svc.cluster.local (10.104.249.89:80)
wget: can't connect to remote host (10.104.249.89): Connection refused/ # wget -O- http://10.102.249.165
Connecting to 10.102.249.165 (10.102.249.165:80)
wget: can't connect to remote host (10.102.249.165): Connection refused^C
/ #
/ #
/ #
/ # exit
pod "testbox" deleted
pod default/testbox terminated (Error)# 查看Deployment的yaml 文件,发现label 和service的不一致,导致service 找不到pod去部署,修改label 从nginx 到appapiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploy
spec:replicas: 3selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.25.3ports:- containerPort: 80# 更新Deployment失败,
azureuser@master-001:~$ kubectl apply -f deployment1.yaml
The Deployment "nginx-deploy" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"web"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable# 原因是selector的label 不能修改,只能删除重建azureuser@master-001:~$ kubectl delete deployment nginx-deploy
deployment.apps "nginx-deploy" deleted#删除后,原service还在,但访问不通了azureuser@master-001:~$ kubectl get service
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP        43h
nginx-svc           ClusterIP   10.102.29.115    <none>        80/TCP         101m
nodeport-svc        NodePort    10.101.111.134   <none>        80:30443/TCP   43h
web-clusterip-svc   ClusterIP   10.102.249.165   <none>        80/TCP         6m44s
web-svc             ClusterIP   10.104.249.89    <none>        80/TCP         43hazureuser@master-001:~$ kubectl get deployment
No resources found in default namespace.
azureuser@master-001:~$ kubectl apply -f deployment1.yaml
deployment.apps/nginx-deploy created
azureuser@master-001:~$ kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           3sazureuser@master-001:~$ kubectl get service
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP        43h
nginx-svc           ClusterIP   10.102.29.115    <none>        80/TCP         101m
nodeport-svc        NodePort    10.101.111.134   <none>        80:30443/TCP   43h
web-clusterip-svc   ClusterIP   10.102.249.165   <none>        80/TCP         7m25s
web-svc             ClusterIP   10.104.249.89    <none>        80/TCP         43h#重新测试,可以访问了azureuser@master-001:~$ kubectl run testbox --image=busybox --restart=Never --rm -it -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # wget -O- http://10.102.249.165
Connecting to 10.102.249.165 (10.102.249.165:80)
writing to stdout
<!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>
-                    100% |*****************************************************************************************************************************************************************|   615  0:00:00 ETA
written to stdout
/ #
/ #
/ # wget -O- http://10.102.249.165
Connecting to 10.102.249.165 (10.102.249.165:80)
writing to stdout
<!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>
-                    100% |*****************************************************************************************************************************************************************|   615  0:00:00 ETA
written to stdout
/ # wget -O- http://10.102.29.115
Connecting to 10.102.29.115 (10.102.29.115:80)
wget: can't connect to remote host (10.102.29.115): Connection refused
/ #
/ #
/ # exit
pod "testbox" deleted
pod default/testbox terminated (Error)
azureuser@master-001:~$
azureuser@master-001:~$

LoadBalancer 模式

什么是 LoadBalancer Service?
LoadBalancer 类型的 Service 是 Kubernetes 提供的最直接的“对外流量入口”方式。
当你在云环境(如 Azure、AWS、GCP)创建 type=LoadBalancer 的 Service 时,Kubernetes 会自动调用云平台 API,创建一个云负载均衡器(Azure LB),并将其公网/内网 IP 作为服务入口。
云 LB 会将流量分发到后端 K8s 节点,再由 kube-proxy 转发到具体 Pod。

在 Azure 上 LoadBalancer 的工作机制
K8s Master 检测到 type=LoadBalancer 的 Service 后,调用 Azure Cloud Provider。
Azure 自动分配一个公网IP(默认)或内网IP,并创建 LB 资源。
LB 后端池自动包含所有 K8s 节点。
LB 的健康探针和转发规则自动配置好,将流量转发到各节点上对应的 NodePort,然后再由 kube-proxy 转发到 Pod。
Service 的 EXTERNAL-IP 字段变成 LB 分配的 IP,你可以直接 curl 这个地址访问服务。

测试发现,通过load balancer 去调用Azure API,要修改很多配置文件,填写很多azure认证信息,cluster还要重新部署, 整体配置起来很复杂。 测试阶段,建议手动部署Azure lb,并关联到node(VM)即可。

总结与注意事项
ClusterIP:只能在集群内部访问。
NodePort:可通过任意节点 IP+端口访问(记得放行防火墙)。
LoadBalancer:云厂商自动分配公网 IP,可直接外网访问。
Selector 必须与 Deployment 的 labels 匹配,否则无后端 Pod。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/920465.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第05周 预习、实验与作业:继承与多态

第05周 预习、实验与作业:继承与多态第05周 预习、实验与作业:继承与多态 目录第05周 预习、实验与作业:继承与多态0.任务完成说明1.预习1.1 学习目标1.2 预习任务2.实验3.课后任务(作业)3.1 在线学习平台3.2 PTA…

深入解析:ShardingSphere 与分库分表:分布式数据库中间件实战指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

纸浆2511

30分钟和60分钟 跌势 每个波段疑似都是三波。 当前在走4浪调整

哪个网站看电影做便宜资源分享论坛wordpress

Redis 是由 C 语言开发的开源内存数据存储器&#xff0c;经常被用作数据库、缓存以及消息队列等。 Redis 因为其强大的功能和简洁的设计&#xff0c;深受广大开发者和公司的喜爱&#xff0c;几乎占领了内存数据库市场的所有份额。 1 Redis 特性 Redis 有很多优秀的特性&#…

四川建设网官方网站青岛外贸网站建站公司

1. 文章说明 说明&#xff1a;目前讲的是第一部分nginx核心技术篇&#xff0c;后需篇章会以第一部分为核心技术篇为基础来展开深度讲解&#xff0c;详情关注后续课程的发布。 2. 介绍和准备环境 2.1 介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器&#xf…

专门做淘宝代运营的网站成都网站排名公司

来自 | 逐梦erhttps://zhumenger.blog.csdn.net/article/details/106530281本文仅作技术交流&#xff0c;如有侵权&#xff0c;请联系后台删除。数据可视化非常重要&#xff0c;因为错误或不充分的数据表示方法可能会毁掉原本很出色的数据分析工作。matplotlib 库是专门用于开发…

electron38-admin桌面端后台|Electron38+Vue3+ElementPlus管理系统

最新款vite7+electron38+pinia3电脑端通用后台管理系统ElectronVue3Admin。 electron38-vite7-admin最新版跨平台框架 Electron38 整合 Vite7+Vue3+ElementPlus 搭建高颜值轻量级客户端中后台管理系统解决方案。包含4种…

中国十大品牌网站建设银行怎么加入信用网站

目录 1算法最优解 2.时间复杂度排序 3.对数器 1算法最优解 1.首先&#xff0c;保证时间复杂度最低 2.其次&#xff0c;保证空间复杂度最低 3.常数项低不低&#xff0c;一般没人管 2.时间复杂度排序 3.对数器 import java.util.Arrays;public class Test {public static …

长江中游干流河道崩岸特征与机理研究综述

长江中游干流河道崩岸特征与机理研究综述参考:http://ckyyb.crsri.cn/CN/rich_html/10.11988/ckyyb.20240625 1 长江中游河道基本情况长江干流从湖北宜昌南津关以下,经湖北、湖南至江西鄱阳湖口为中游,长约955 km(图1…

漫谈《数字图像处理》之最大稳定极值区域(MSER) - 实践

漫谈《数字图像处理》之最大稳定极值区域(MSER) - 实践2025-09-28 09:38 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important;…

基于 Python Keras 建立 猫狗图像的精准分类

基于 Python Keras 建立 猫狗图像的精准分类pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &quo…

成都网站建设专家网站建设需什么软件

本篇解释了STM32中断原理 MCU为什么需要中断 中断&#xff0c;是嵌入式系统中很重要的一个功能&#xff0c;在系统运行过程中&#xff0c;当出现需要立刻处理的情况时&#xff0c;暂停当前任务&#xff0c;转而处理紧急任务&#xff0c;处理完毕后&#xff0c;恢复之前的任务…

【变量与数据类型】让自动化拥有“记忆”

【变量与数据类型】让自动化拥有“记忆”在上一课中,我们学会了使用 Get-Process 这样的命令来获取信息。但它的结果只是显示在屏幕上,然后便消失了。如果我们想对这个结果进行进一步的处理——比如只显示某些特定进…

《ESP32-S3使用指南—IDF版 V1.6》第四十章 图片显示实验

第四十章图片显示实验 1)实验平台:正点原子DNESP32S3开发板 2)章节摘自【正点原子】ESP32-S3使用指南—IDF版 V1.6 3)购买链接:https://detail.tmall.com/item.htm?&id=768499342659 4)全套实验源码+手册+视…

QCOW2: A Virtual Disk Format Designed for Modern Virtualization

QCOW2: A Virtual Disk Format Designed for Modern VirtualizationQCOW2: A Virtual Disk Format Designed for Modern Virtualization by Karine Huang | May 28, 2025 | Blog, QCOW2 | 0 commentsIn the world of v…

鹏达建设集团有限公司网站网页设计版权信息代码

本节将向读者介绍如何使用键盘鼠标操控模拟技术&#xff0c;键盘鼠标操控模拟技术是一种非常实用的技术&#xff0c;可以自动化执行一些重复性的任务&#xff0c;提高工作效率&#xff0c;在Windows系统下&#xff0c;通过使用各种键盘鼠标控制函数实现动态捕捉和模拟特定功能的…

学习网站开发体会与感想定制型网站建设服务器

高兴的是有博友mark了我的文章。我知道mark之后&#xff0c;很少会再来继续关注的。但是从侧面说明了在博友点开博客的同时&#xff0c;他感觉这篇博客是有价值的&#xff0c;是能够弥补他的知识欠缺。一篇博客最重要的是对自己有用&#xff0c;如果再对别人有用&#xff0c;那…

做外贸有哪些好的网站桓台新城建设有限公司网站

区间预测 | Matlab实现GRU-Adaboost-ABKDE的集成门控循环单元自适应带宽核密度估计多变量回归区间预测 目录 区间预测 | Matlab实现GRU-Adaboost-ABKDE的集成门控循环单元自适应带宽核密度估计多变量回归区间预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实…

鸿蒙应用开发从入门到实战(十六):线性布局案例

ArkUI提供了丰富的系统组件,用于制作鸿蒙原生应用APP的UI,本文通过简单案例演示如何使用Column和Row组件实现线性布局。线性布局案例:商品列表 大家好,我是潘Sir,持续分享IT技术,帮你少走弯路。《鸿蒙应用开发从…

SQL注入流程

SQL注入流程 1.寻找注入点 得到目标网站的一般信息和技术分析信息之后,就要寻找注入点和测试注入点,这些点位都是与数据库有交互的地方 常用注入点在哪里? 我们一般在三个地方,寻找注入点表单中的输入参数 URL中的…