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

本文介绍了如何在Kubernetes中通过资源文件创建Pod和Service,特别是创建一个Service来负载均衡两个Pod的流量。首先,创建了两个带有特定标签的Pod,然后编写Service资源文件,设置为NodePort类型并选择相应的Pod。Service的配置允许外部通过节点IP和指定端口访问,实现了对两个Pod的负载均衡。

《研发工程师玩转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。

# simple_http_a.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
# simple_http_b.yaml
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资源文件

# simple_http_service.yaml
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。

删除

可以先通过下面指令删除该service

kubectl delete service simple-http-service

service “simple-http-service” deleted

然后再删除上述创建的Pod

kubectl delete pod simple-http-a simple-http-b 

pod “simple-http-a” deleted
pod “simple-http-b” deleted

参考资料

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

breaksoftware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值