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

Kubenetes Service类型与应用场景、YAML模板以及练习

什么是Kubernetes Service?

Kubernetes Service 是一种抽象,可以理解为负载均衡的端口转发服务用于将网络流量路由到一组 Pod。它的主要作用包括:

  1. 服务发现:在 Kubernetes 中,Pod 的 IP 地址是动态分配的,可能会随着 Pod 的创建和销毁而变化。Service 提供了一个稳定的访问点,使客户端可以通过固定的 IP 地址或 DNS 名称访问服务。

  2. 负载均衡:Service 可以将流量分发到多个后端 Pod,确保服务的高可用性和性能。

  3. 网络代理:通过 kube-proxy 实现流量的转发和路由,支持多种代理模式(如 iptables 和 IPVS)。

Kubernetes Service 与 API Gateway 有什么区别和联系

Kubernetes Service 与 API Gateway 有相似之处,但它们的核心功能和使用场景有所不同。

  • 相似点

    • 它们都用于流量管理。

    • 都可以作为客户端访问后端服务的入口点。

  • 不同点

    • Kubernetes Service:更侧重于在 Kubernetes 集群内的网络流量路由和负载均衡,解决服务发现问题。它是一个集群内的概念,主要负责将流量分发到后端的 Pod,并确保服务之间的通信。

    • API Gateway:通常用于微服务架构,具备更多高级功能,比如身份验证、安全控制、请求/响应转换、限流等。API Gateway 适用于更复杂的流量管理场景,尤其是在对外暴露 API 或需要统一入口时。

简单来说,Kubernetes Service 是一种基础网络功能组件,而 API Gateway 是一种高层次的应用组件。

Kubernetes Service 类型及适用场景

  1. ClusterIP(默认类型):

    • 描述:仅在集群内部访问。

    • 适用场景:用于服务间通信(如微服务架构)或集群内的内部应用。

  2. NodePort

    • 描述:在每个节点的特定端口上公开服务。

    • 适用场景:适合本地开发或调试,或者简单的集群外部访问。

  3. LoadBalancer

    • 描述:通过云提供商配置一个外部负载均衡器。

    • 适用场景:生产环境中,用于外部客户端的访问。

  4. ExternalName

    • 描述:将服务映射到外部 DNS 名称,而不是路由流量到 Pod。

    • 适用场景:需要访问外部服务(如数据库或 API)。

Service之YAML 模板

LoadBalancer 

apiVersion: v1
kind: Service
metadata:
  name: lmd-api-service-lb
  labels:
    app: lmd-api
    type: back-end
spec:
  selector:
    app: lmd-api
    type: back-end
  ports:
    - protocol: TCP
      port: 12666
      targetPort: 12666
  type: LoadBalancer

 ClusterIP

apiVersion: v1
kind: Service
metadata:
  name: lmd-api-service
  labels:
    app: lmd-api
    type: back-end
spec:
  selector:
    app: lmd-api
    type: back-end
  ports:
    - protocol: TCP
      port: 12666
      targetPort: 12666
  type: ClusterIP

Service练习

 Udemy Labs - Certified Kubernetes Administrator with Practice Tests Course | KodeKloud

     Welcome to the KodeKloud Hands-On lab                                
    __ ______  ____  ________ __ __    ____  __  ______ 
   / //_/ __ \/ __ \/ ____/ //_// /   / __ \/ / / / __ \
  / ,< / / / / / / / __/ / ,<  / /   / / / / / / / / / /
 / /| / /_/ / /_/ / /___/ /| |/ /___/ /_/ / /_/ / /_/ / 
/_/ |_\____/_____/_____/_/ |_/_____/\____/\____/_____/  
                                                        
        All rights reserved                                               

controlplane ~ ➜  kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   15m

controlplane ~ ➜  kubectl describe service kubernetes
Name:                     kubernetes
Namespace:                default
Labels:                   component=apiserver
                          provider=kubernetes
Annotations:              <none>
Selector:                 <none>
Type:                     ClusterIP
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.0.1
IPs:                      10.43.0.1
Port:                     https  443/TCP
TargetPort:               6443/TCP
Endpoints:                192.168.243.182:6443
Session Affinity:         None
Internal Traffic Policy:  Cluster
Events:                   <none>

controlplane ~ ➜  kubectl get deployment
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
simple-webapp-deployment   4/4     4            4           20s

controlplane ~ ➜  kubectl describe deployment
Name:                   simple-webapp-deployment
Namespace:              default
CreationTimestamp:      Sun, 30 Mar 2025 05:36:26 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               name=simple-webapp
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  name=simple-webapp
  Containers:
   simple-webapp:
    Image:         kodekloud/simple-webapp:red
    Port:          8080/TCP
    Host Port:     0/TCP
    Environment:   <none>
    Mounts:        <none>
  Volumes:         <none>
  Node-Selectors:  <none>
  Tolerations:     <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   simple-webapp-deployment-8555484b96 (4/4 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  36s   deployment-controller  Scaled up replica set simple-webapp-deployment-8555484b96 from 0 to 4

controlplane ~ ➜  kubectl get deployment
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
simple-webapp-deployment   4/4     4            4           2m41s

controlplane ~ ✖ ls
service-definition-1.yaml

controlplane ~ ➜  vim service-definition-1.yaml 

controlplane ~ ➜  cat service-definition-1.yaml 
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
  namespace: default
spec:
  ports:
  - nodePort: 30080
    port: 8080
    targetPort: 8080 
  selector:
    name: simple-webapp
  type: NodePort

controlplane ~ ➜  kubectl create -f service-definition-1.yaml 
service/webapp-service created

controlplane ~ ➜  kubectl get service
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.43.0.1     <none>        443/TCP          23m
webapp-service   NodePort    10.43.81.40   <none>        8080:30080/TCP   9s

controlplane ~ ➜  kubectl describe service webapp-service 
Name:                     webapp-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 name=simple-webapp
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.81.40
IPs:                      10.43.81.40
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.22.0.9:8080,10.22.0.11:8080,10.22.0.12:8080 + 1 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Internal Traffic Policy:  Cluster
Events:                   <none>

controlplane ~ ➜  Powered by Moshow@https://zhengkai.blog.csdn.net/

相关文章:

  • 计算机网络面经(一)
  • Redis-02.Redis入门
  • python 原型链污染学习
  • 深度求索:开源革命下的AI普惠之路
  • JavaScript创建时间对象、数字、字符串方法
  • Python-八股总结
  • RabbitMQ、RocketMQ 和 Kafka 的消息特性对比
  • 用mkdocs写文档#自动更新github-page
  • 浙江大学公开课|第二季|智能金融:AI 驱动的金融变革
  • Vue3中的Parent-Child通信全解析
  • flink 基站与服务器长连接,每次连接和断开都会上报数据,统计过去一小时每个基站断开次数和时长
  • 如何保证云服务器的可靠性与可用性
  • Playwright页面对象模型POM + 常见断言 + playwright接口断言 + 参数化
  • 深入理解 DNS:互联网的“电话簿”
  • 【DPI】精讲深度数据包检测技术基础
  • Oracle数据库数据编程SQL<3.3 PL/SQL 游标>
  • 关于中文编程的一些思考
  • 使用 Cheerio 和 Node.js 进行网络抓取
  • 数据结构初阶:单链表
  • golang strings包常用方法
  • 建网站做商城个体户资质可以/合肥网站制作推广
  • 网站建设后运维合同/友链交易平台源码
  • 用微信小程序怎么做网站/网络推广的平台
  • 设计软件网站推荐/seo优化sem推广
  • 河南郑州网站建设/seo的工具有哪些
  • iis做的网站模板/seo怎么去优化