Kubenetes Service范例与应用场景、YAML模板以及练习

[复制链接]
发表于 2025-10-3 22:39:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
什么是Kubernetes Service?


Kubernetes Service 是一种抽象,可以明白为负载平衡的端口转发服务,用于将网络流量路由到一组 Pod。它的重要作用包罗:

  • 服务发现:在 Kubernetes 中,Pod 的 IP 所在是动态分配的,大概会随着 Pod 的创建和烧毁而厘革。Service 提供了一个稳固的访问点,使客户端可以通过固定的 IP 所在或 DNS 名称访问服务。
  • 负载平衡:Service 可以将流量分发到多个后端 Pod,确保服务的高可用性和性能。
  • 网络署理:通过 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 范例及实用场景



  • ClusterIP(默认范例):


    • 形貌:仅在集群内部访问。
    • 实用场景:用于服务间通讯(如微服务架构)或集群内的内部应用。

  • NodePort:


    • 形貌:在每个节点的特定端口上公开服务。
    • 实用场景:恰当当地开发或调试,大概简单的集群外部访问。

  • LoadBalancer:


    • 形貌:通过云提供商设置一个外部负载平衡器。
    • 实用场景:生产情况中,用于外部客户端的访问。

  • ExternalName:

    • 形貌:将服务映射到外部 DNS 名称,而不是路由流量到 Pod。
    • 实用场景:必要访问外部服务(如数据库或 API)。

Service之YAML 模板

LoadBalancer 

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: lmd-api-service-lb
  5.   labels:
  6.     app: lmd-api
  7.     type: back-end
  8. spec:
  9.   selector:
  10.     app: lmd-api
  11.     type: back-end
  12.   ports:
  13.     - protocol: TCP
  14.       port: 12666
  15.       targetPort: 12666
  16.   type: LoadBalancer
复制代码
 ClusterIP

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: lmd-api-service
  5.   labels:
  6.     app: lmd-api
  7.     type: back-end
  8. spec:
  9.   selector:
  10.     app: lmd-api
  11.     type: back-end
  12.   ports:
  13.     - protocol: TCP
  14.       port: 12666
  15.       targetPort: 12666
  16.   type: ClusterIP
复制代码
Service练习

 Udemy Labs - Certified Kubernetes Administrator with Practice Tests Course | KodeKloud
  1.      Welcome to the KodeKloud Hands-On lab                                
  2.     __ ______  ____  ________ __ __    ____  __  ______
  3.    / //_/ __ \/ __ \/ ____/ //_// /   / __ \/ / / / __ \
  4.   / ,< / / / / / / / __/ / ,<  / /   / / / / / / / / / /
  5. / /| / /_/ / /_/ / /___/ /| |/ /___/ /_/ / /_/ / /_/ /
  6. /_/ |_\____/_____/_____/_/ |_/_____/\____/\____/_____/  
  7.                                                         
  8.         All rights reserved                                               
  9. controlplane ~ ➜  kubectl get service
  10. NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
  11. kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   15m
  12. controlplane ~ ➜  kubectl describe service kubernetes
  13. Name:                     kubernetes
  14. Namespace:                default
  15. Labels:                   component=apiserver
  16.                           provider=kubernetes
  17. Annotations:              <none>
  18. Selector:                 <none>
  19. Type:                     ClusterIP
  20. IP Family Policy:         SingleStack
  21. IP Families:              IPv4
  22. IP:                       10.43.0.1
  23. IPs:                      10.43.0.1
  24. Port:                     https  443/TCP
  25. TargetPort:               6443/TCP
  26. Endpoints:                192.168.243.182:6443
  27. Session Affinity:         None
  28. Internal Traffic Policy:  Cluster
  29. Events:                   <none>
  30. controlplane ~ ➜  kubectl get deployment
  31. NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
  32. simple-webapp-deployment   4/4     4            4           20s
  33. controlplane ~ ➜  kubectl describe deployment
  34. Name:                   simple-webapp-deployment
  35. Namespace:              default
  36. CreationTimestamp:      Sun, 30 Mar 2025 05:36:26 +0000
  37. Labels:                 <none>
  38. Annotations:            deployment.kubernetes.io/revision: 1
  39. Selector:               name=simple-webapp
  40. Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
  41. StrategyType:           RollingUpdate
  42. MinReadySeconds:        0
  43. RollingUpdateStrategy:  25% max unavailable, 25% max surge
  44. Pod Template:
  45.   Labels:  name=simple-webapp
  46.   Containers:
  47.    simple-webapp:
  48.     Image:         kodekloud/simple-webapp:red
  49.     Port:          8080/TCP
  50.     Host Port:     0/TCP
  51.     Environment:   <none>
  52.     Mounts:        <none>
  53.   Volumes:         <none>
  54.   Node-Selectors:  <none>
  55.   Tolerations:     <none>
  56. Conditions:
  57.   Type           Status  Reason
  58.   ----           ------  ------
  59.   Available      True    MinimumReplicasAvailable
  60.   Progressing    True    NewReplicaSetAvailable
  61. OldReplicaSets:  <none>
  62. NewReplicaSet:   simple-webapp-deployment-8555484b96 (4/4 replicas created)
  63. Events:
  64.   Type    Reason             Age   From                   Message
  65.   ----    ------             ----  ----                   -------
  66.   Normal  ScalingReplicaSet  36s   deployment-controller  Scaled up replica set simple-webapp-deployment-8555484b96 from 0 to 4
  67. controlplane ~ ➜  kubectl get deployment
  68. NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
  69. simple-webapp-deployment   4/4     4            4           2m41s
  70. controlplane ~ ✖ ls
  71. service-definition-1.yaml
  72. controlplane ~ ➜  vim service-definition-1.yaml
  73. controlplane ~ ➜  cat service-definition-1.yaml
  74. ---
  75. apiVersion: v1
  76. kind: Service
  77. metadata:
  78.   name: webapp-service
  79.   namespace: default
  80. spec:
  81.   ports:
  82.   - nodePort: 30080
  83.     port: 8080
  84.     targetPort: 8080
  85.   selector:
  86.     name: simple-webapp
  87.   type: NodePort
  88. controlplane ~ ➜  kubectl create -f service-definition-1.yaml
  89. service/webapp-service created
  90. controlplane ~ ➜  kubectl get service
  91. NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
  92. kubernetes       ClusterIP   10.43.0.1     <none>        443/TCP          23m
  93. webapp-service   NodePort    10.43.81.40   <none>        8080:30080/TCP   9s
  94. controlplane ~ ➜  kubectl describe service webapp-service
  95. Name:                     webapp-service
  96. Namespace:                default
  97. Labels:                   <none>
  98. Annotations:              <none>
  99. Selector:                 name=simple-webapp
  100. Type:                     NodePort
  101. IP Family Policy:         SingleStack
  102. IP Families:              IPv4
  103. IP:                       10.43.81.40
  104. IPs:                      10.43.81.40
  105. Port:                     <unset>  8080/TCP
  106. TargetPort:               8080/TCP
  107. NodePort:                 <unset>  30080/TCP
  108. Endpoints:                10.22.0.9:8080,10.22.0.11:8080,10.22.0.12:8080 + 1 more...
  109. Session Affinity:         None
  110. External Traffic Policy:  Cluster
  111. Internal Traffic Policy:  Cluster
  112. Events:                   <none>
  113. controlplane ~ ➜  Powered by Moshow@https://zhengkai.blog.csdn.net/
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表