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

kubernetes学习-Service

kubernetes学习-Service

  • 1. Service说明
  • 2. 功能
  • 3.Service类型
    • 3.1 NodePort
      • 3.1.1 创建web-service.yaml
      • 3.1.2 创建web-pod.yaml
      • 3.1.3 部署
      • 3.1.4 验证
    • 3.2 ClusterIP
      • 3.2.1 创建web-clusterIp-service.yaml
      • 3.2.2 创建web-clusterIp-pod.yaml
      • 3.2.3 部署
      • 3.2.4 验证
    • 3.3 LoadBalancer
  • 4. Service代理模式
    • 4.1 iptables
    • 4.2 ipvs
  • 5. DNS
    • 5.1 创建busybox-service.yaml
    • 5.2 创建busybox-pod.yaml
    • 5.3 部署
    • 5.4 验证

1. Service说明

Service是Kubernetes中的一种资源对象,用于定义一组Pod的网络访问规则,它为Pod提供了一个稳定的统一访问入口,使得客户端可以始终使用同一个IP地址进行访问,避免了直接使用Pod IP地址导致的不稳定性。
在这里插入图片描述在这里插入图片描述

2. 功能

  • 负载均衡:当多个Pod提供服务时,Service通过负载均衡算法将请求分发到这些Pod,从而实现应用程序的负载均衡。

  • 服务发现:Service提供了一种服务发现机制,自动维护后端Pod IP的变化,确保客户端访问地址保持不变。

3.Service类型

3.1 NodePort

NodePort,在每个节点上启用一个端口来暴露服务,可以在集群外部访问。也会分配一个稳定内部集群IP地址。
访问地址:<任意NodeIP>:<NodePort>
端口范围:30000-32767
在这里插入图片描述

3.1.1 创建web-service.yaml

web-service.yaml

apiVersion: v1
kind: Service
metadata:name: web
spec:type: NodePort # 服务类型ports:- port: 80 # Service端口protocol: TCP # 协议targetPort: 80 # 容器端口(应用程序监听端口)nodePort: 32301selector:app: web # 指定关联Pod的标签

多端口Service定义,对于某些服务,需要公开多个端口,Service也需要配置多个端口定义,通过端口名称区分。
web-service.yaml

apiVersion: v1
kind: Service
metadata:name: web
spec:type: NodePort # 服务类型ports:- port: 80 # Service端口protocol: TCP # 协议targetPort: 80 # 容器端口(应用程序监听端口)nodePort: 32301- port: 443 # Service端口protocol: TCP # 协议targetPort: 443 # 容器端口(应用程序监听端口)nodePort: 32302selector:app: web # 指定关联Pod的标签

3.1.2 创建web-pod.yaml

web-pod.yaml

apiVersion: apps/v1
kind: Deployment
metadata:labels:app: webname: web
spec:replicas: 1selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- image: nginx:1.16name: nginx

3.1.3 部署

~]# kubectl apply -f web-pod.yaml 
deployment.apps/web created
~]# kubectl apply -f web-service.yaml 
service/web created   
~]# kubectl get services -o wide
NAME           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes     ClusterIP      10.96.0.1        <none>        443/TCP        27d   <none>
web            NodePort       10.104.219.112   <none>        80:32301/TCP   13s   app=web
~]# kubectl get pods
NAME                         READY   STATUS             RESTARTS       AGE
web-65655cd78-hsprm          1/1     Running            0              27m

3.1.4 验证

访问集群节点IP的映射端口
在这里插入图片描述

3.2 ClusterIP

ClusterIP,默认,分配一个稳定的IP地址,即VIP,只能在集群内部访问。
在这里插入图片描述

3.2.1 创建web-clusterIp-service.yaml

apiVersion: v1
kind: Service
metadata:name: web-clusterip
spec:type: ClusterIP # 服务类型ports: - port: 80 # Service端口protocol: TCP # 协议targetPort: 80 # 容器端口(应用程序监听端口)selector:app: web-clusterip # 指定关联Pod的标签

3.2.2 创建web-clusterIp-pod.yaml

apiVersion: apps/v1
kind: Deployment
metadata:labels:app: web-clusteripname: web-nginx
spec:replicas: 1selector:matchLabels:app: web-clusteriptemplate:metadata:labels:app: web-clusteripspec:containers:- image: nginx:1.16name: nginx

3.2.3 部署

~]# kubectl apply -f web-clusterIp-service.yaml 
service/web-clusterip created
~]# kubectl apply -f web-clusterIp-pod.yaml 
deployment.apps/web-nginx created
~]# kubectl get pods
NAME                         READY   STATUS             RESTARTS       AGE
web-65655cd78-hsprm          1/1     Running            0              27m
web-nginx-6b59757964-9rd6v   1/1     Running            0              18s
~]# kubectl get service -o wide
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE     SELECTOR
web             NodePort       10.104.219.112   <none>        80:32301/TCP   28m     app=web
web-clusterip   ClusterIP      10.97.209.118    <none>        80/TCP         6m28s   app=web-clusterip

3.2.4 验证

访问集群IP(10.97.209.118)的80端口,集群外无法访问。

~]# curl http://10.97.209.118
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>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>

3.3 LoadBalancer

LoadBalancer,外部,对外暴露应用,适用公有云。与NodePort类似,在每个节点上启用一个端口来暴露服务。除此之外,Kubernetes会请求底层云平台(例如阿里云、腾讯云、AWS等)上的负载均衡器,将每个Node([NodeIP]:[NodePort])作为后端添加进去。
在这里插入图片描述

4. Service代理模式

4.1 iptables

特点

  • 灵活,功能强大
  • 规则遍历匹配和更新,呈线性时延

在这里插入图片描述

4.2 ipvs

  • 工作在内核态,有更好的性能
  • 调度算法丰富:rr,wrr,lc,wlc,ip hash…
    在这里插入图片描述

5. DNS

CoreDNS:是一个DNS服务器,Kubernetes默认采用,以Pod部署在集群中,CoreDNS服务监视Kubernetes API,为每一个Service创建DNS记录用于域名解析。
ClusterIP A记录格式:<service-name>.<namespacename>.svc.cluster.local
示例:my-svc.my-namespace.svc.cluster.local
在这里插入图片描述

5.1 创建busybox-service.yaml

busybox-service.yaml

apiVersion: v1
kind: Service
metadata:name: busybox-service
spec:type: NodePort # 服务类型ports: - port: 80 # Service端口protocol: TCP # 协议targetPort: 80 # 容器端口(应用程序监听端口)nodePort: 32302selector:app: busybox # 指定关联Pod的标签

5.2 创建busybox-pod.yaml

busybox-pod.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: busybox-sleep-deploymentlabels:app: busybox
spec:replicas: 1selector:matchLabels:app: busyboxtemplate:metadata:labels:app: busyboxspec:containers:- name: busyboximage: busyboxcommand: ["/bin/sh", "-c", "sleep 300s"]

5.3 部署

~]# kubectl apply -f busybox-pod.yaml 
deployment.apps/busybox-sleep-deployment unchanged
]# kubectl apply -f busybox-service.yaml 
service/busybox-service created
~]# kubectl get pods -o wide
NAME                                        READY   STATUS         RESTARTS       AGE     IP              NODE      NOMINATED NODE   READINESS GATES
busybox-sleep-deployment-5bddd5fcfb-9tfql   1/1     Running        0              4m51s   10.244.184.58   node-02   <none>           <none>
~]# kubectl get services
NAME              TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
busybox-service   NodePort       10.104.240.39    <none>        80:32302/TCP   67s

5.4 验证

验证域名busybox-service.default.svc.cluster.local

~]# kubectl exec -it busybox-sleep-deployment-5bddd5fcfb-9tfql -- sh
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # nslookup busybox-service.default.svc.cluster.local
Server:         10.96.0.10
Address:        10.96.0.10:53
Name:   busybox-service.default.svc.cluster.local
Address: 10.104.240.39
/ # 
http://www.lryc.cn/news/514428.html

相关文章:

  • Springcloud项目-前后端联调(一)
  • 洛谷P1525 [NOIP2010 提高组] 关押罪犯(种子并查集基础)
  • 【算法刷题指南】模拟
  • 学习笔记078——Java Properties类使用详解
  • 若依使用 Undertow 替代 Tomcat 容器
  • 多输入多输出 | Matlab实现WOA-CNN鲸鱼算法优化卷积神经网络多输入多输出预测
  • Elasticsearch:基础概念
  • Spring MVC的@ResponseBody与@RequestBody
  • 智能商业分析 Quick BI
  • LUA基础语法
  • SpringBoot的pom.xml文件中,scope标签有几种配置?
  • Leetcode729: 我的日程安排表 I
  • 青少年编程与数学 02-006 前端开发框架VUE 02课题、创建工程
  • Redis的生态系统和社区支持
  • Tomcat解析
  • UML之组合与聚合
  • 数据结构理论篇(期末突击)
  • 《一文读懂PyTorch核心模块:开启深度学习之旅》
  • 摆脱Zotero存储限制:WebDAV结合内网穿透打造个人文献管理云平台
  • Flutter封装一个三方ViewPager学习
  • 服务器数据恢复—离线盘数超过热备盘数导致raidz阵列崩溃的数据恢复
  • nginx-nginx的缓存集成
  • 【Vim Masterclass 笔记01】Section 1:Course Overview + Section 2:Vim Quickstart
  • 【数据库系列】Spring Boot 中使用 MyBatis 详细指南
  • Azure Airflow 中配置错误可能会使整个集群受到攻击
  • Python跨年烟花
  • 【代码】Python|Windows 批量尝试密码去打开加密的 Word 文档(docx和doc)
  • java开发中注解汇总​​
  • C# 设计模式(结构型模式):外观模式
  • PowerShell 常见问题解答