研发工程师玩转Kubernetes——通过文件创建Service

news/2024/7/4 7:46:03 标签: kubernetes, 运维, 云原生

在《研发工程师玩转Kubernetes——部署应用》一文中,我们使用kubectl expose创建了一个Service,暴露了一个Pod上的nginx服务。这篇文章我们将使用文件的形式创建Service。
为了增加有趣性,我们采用《研发工程师玩转Kubernetes——构建、推送自定义镜像》中的镜像部署两个Pod。这两个Pod有不同的Cluster IP(kubernetes内部IP),而Service将同时暴露这两个Pod上的服务。这样我们访问Service时,将通过打印出来的IP得知本次请求被分配到哪个Pod上。

创建Pod

编写Pod资源文件

我们创建两个yaml文件:simple_http_a.yaml和simple_http_b.yaml。

apiVersion: v1
kind: Pod
metadata:
  name: simple-http-a
  labels:
    name: simple-http-a
    image: simple_http
    version: v1
spec:
  containers:
  - name: simple-http-container
    image: localhost:32000/simple_http:v1
    ports:
      - containerPort: 8888
apiVersion: v1
kind: Pod
metadata:
  name: simple-http-b
  labels:
    name: simple-http-b
    image: simple_http
    version: v1
spec:
  containers:
  - name: simple-http-container
    image: localhost:32000/simple_http:v1
    ports:
      - containerPort: 8888

和《研发工程师玩转Kubernetes——通过文件创建Pod》不同的是,我们给labels增加了新的标签image:simple_http。后面我们要通过这个标签,筛选出供Service使用的Pod。

创建

在上述文件的目录执行下面的指令

kubectl create -f simple_http_a.yaml -f simple_http_b.yaml 

查看

kubectl describe pod simple-http-a simple-http-b 
Name:             simple-http-a
Namespace:        default
Priority:         0
Service Account:  default
Node:             fangliang-virtual-machine/172.30.45.36
Start Time:       Fri, 19 May 2023 20:32:50 +0800
Labels:           image=simple_http
                  name=simple-http-a
                  version=v1
Annotations:      cni.projectcalico.org/containerID: 10384e0dd24726b0e5265bcc12252bb8a9ecf917d9603f8ce62135ca93fa0573
                  cni.projectcalico.org/podIP: 10.1.62.160/32
                  cni.projectcalico.org/podIPs: 10.1.62.160/32
Status:           Running
IP:               10.1.62.160
IPs:
  IP:  10.1.62.160
Containers:
  simple-http-container:
    Container ID:   containerd://deaf2b805292288ca609095993699911c9be1cda96439a77946df20959b01bea
    Image:          localhost:32000/simple_http:v1
    Image ID:       localhost:32000/simple_http@sha256:cbee584f83426593efb95a9e2213bb40143a1c86c3d217e65d30430033f846d4
    Port:           8888/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 19 May 2023 20:32:52 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-kzscm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-kzscm:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>


Name:             simple-http-b
Namespace:        default
Priority:         0
Service Account:  default
Node:             fangliang-virtual-machine/172.30.45.36
Start Time:       Fri, 19 May 2023 20:32:50 +0800
Labels:           image=simple_http
                  name=simple-http-b
                  version=v1
Annotations:      cni.projectcalico.org/containerID: b34e51ad923f778cdba027b7bf361c534c7f4f4e40da1d5bd7c0466bbfaf9fa1
                  cni.projectcalico.org/podIP: 10.1.62.159/32
                  cni.projectcalico.org/podIPs: 10.1.62.159/32
Status:           Running
IP:               10.1.62.159
IPs:
  IP:  10.1.62.159
Containers:
  simple-http-container:
    Container ID:   containerd://0b982826db40467ff7698629782e8d16d9560237027d18b3bc2305e894331c34
    Image:          localhost:32000/simple_http:v1
    Image ID:       localhost:32000/simple_http@sha256:cbee584f83426593efb95a9e2213bb40143a1c86c3d217e65d30430033f846d4
    Port:           8888/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 19 May 2023 20:32:52 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2vdsc (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-2vdsc:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

可以看到Pod simple-http-a的内部IP是10.1.62.160,simple-http-b是10.1.62.159。

创建Service

编写Service资源文件

apiVersion: v1
kind: Service
metadata:
  name: simple-http-service
spec:
  type: NodePort
  selector:
    image: simple_http
  ports:
  - port: 80
    targetPort: 8888
    nodePort: 30000

因为我们的Service需要对外提供服务,即通过物理机器IP访问,于是要把type设置为NodePort。
selector表示该Service将包装什么样的Pod,它是通过资源的Labels检索的。image:simple_http和Pod资源文件中的Labels强匹配了。
targetPort: 8888,表示容器开放的端口是8888。
port: 80,表示这个服务在内部使用80端口提供服务。
nodePort: 30000表示我们将物理机的30000端口映射到这个服务上。

创建

在上述文件所在目录执行下面的指令

kubectl create -f simple_http_service.yaml 

查看

kubectl describe service simple-http-service
Name:                     simple-http-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 image=simple_http
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.152.183.88
IPs:                      10.152.183.88
Port:                     <unset>  80/TCP
TargetPort:               8888/TCP
NodePort:                 <unset>  30000/TCP
Endpoints:                10.1.62.159:8888,10.1.62.160:8888
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

该Service在kubernetes内部的IP是10.152.183.88,port是80。
在这里插入图片描述
通过物理机IP 172.30.45.36访问的port是30000。
在这里插入图片描述

负载均衡

我们多访问几次该Service,可以看到10.1.62.160和10.1.62.159两个Pod都会响应请求。
在这里插入图片描述在这里插入图片描述
它有两个Endpoints,分别是simple-http-a和simple-http-b两个Pod的IP:TargetPort。

参考资料

  • https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

http://www.niftyadmin.cn/n/339925.html

相关文章

open cv 4.6.0 导入maven库以及依赖包 安装教程

windows&#xff1a; 1.官网下载windows安装包 2.安装完成后&#xff0c;复制opencv\build\java\x64\opencv_java451.dll 到 C:\Windows 下 centos&#xff1a; 参考https://www.cnblogs.com/huizhipeng/p/12732019.html 1.先安装cmake3 参考https://www.jianshu.com/p/20…

体验联网版 ChatGPT:优点和缺点同样明显,还藏着无限可能

ChatGPT 有点像古希腊的阿喀琉斯&#xff1a;它很强大&#xff0c;却有个致命的弱点——无法联网&#xff0c;这注定了它只能是一个停留在 2021 年的超人。 但很快&#xff0c;我们将等到一个「鱼和熊掌兼得」的时刻。 通过插件集的 Web browsing 功能&#xff0c;ChatGPT 就…

【wpf】列表类,用相对源时,如何绑定到子项

前言 在之前的一篇文章 &#xff1a;《【wpf】深度解析&#xff0c;Binding是如何寻找数据源的》https://blog.csdn.net/songhuangong123/article/details/126195727#:~:text%E3%80%90wpf%E3%80%91%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90%EF%BC%8CBinding%E6%98%AF%E5%A6%82%E4…

d3d(Direct X)中的com技术详解

本文不会对Com进行非常详细的分析 因为这个技术分析起来难度还是非常大的 要想真正弄懂还是非常困难的 我只会针对d3d中使用到的com技术和comptr技术进行说明 所以看完本文后 可以熟练使用d3d中使用到的相应技术 comptr类似于c11中的智能指针,用于管理com对象的生命周期,所以我…

小白量化《穿云箭集群量化》(9)用指标公式实现miniQMT全自动交易

小白量化《穿云箭集群量化》&#xff08;9&#xff09;用指标公式实现miniQMT全自动交易 在穿云箭量化平台中&#xff0c;支持3中公式源码运行模式&#xff0c;还支持在Python策略中使用仿指标公式源码运行&#xff0c;编写策略。 我们先看如何使用指标公式源码。 #编程_直接使…

Handler、Looper、Message 和 Thread 的合作机制——安卓 Handler 机制、跨线程机制详解

我们都知道平时在子线程要刷新UI线程的时候一般会用 runOnUiThread(new Runnable() {Overridepublic void run() {} }); 或者 handler.post(new Runnable(){ }); 那么Handler机制是怎么实现跨线程通信的呢&#xff1f; 首先 Looper 像是一条流水线&#xff0c;一直在不停的…

《Amazon DynamoDB》 论文笔记 1

文章目录 1. 写在最前面2. 核心观点2.1 作为服务提供要考虑的问题2.1.1.1 部署方案2.1.1.2 多租户的问题2.1.1.3 容量上限2.1.1.4 容量扩展2.1.1.5 可用性指标评估 3. 碎碎念4. 参考资料 1. 写在最前面 最近读到一句话&#xff0c;「所谓云原生并不是简单的将一个云下的数据库…

Promise理解

做一道题,理解一下: function getPrinterList() {let res 初始setTimeout(() > {res 1},1000)return res }let res getPrinterList() console.log(res); //输出初始 在getPrinterList函数中,先分清同步异步. JS执行语句时,会区分同步异步,把所有的同步放在同步队列中,把…