本文主要是介绍研发工程师玩转Kubernetes——通过文件创建Service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大纲
- 创建Pod
- 编写Pod资源文件
- 创建
- 查看
- 创建Service
- 编写Service资源文件
- 创建
- 查看
- 负载均衡
- 删除
- 参考资料
在《研发工程师玩转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-alabels:name: simple-http-aimage: simple_httpversion: v1
spec:containers:- name: simple-http-containerimage: localhost:32000/simple_http:v1ports:- containerPort: 8888
# simple_http_b.yaml
apiVersion: v1
kind: Pod
metadata:name: simple-http-blabels:name: simple-http-bimage: simple_httpversion: v1
spec:containers:- name: simple-http-containerimage: localhost:32000/simple_http:v1ports:- 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_httpname=simple-http-aversion=v1
Annotations: cni.projectcalico.org/containerID: 10384e0dd24726b0e5265bcc12252bb8a9ecf917d9603f8ce62135ca93fa0573cni.projectcalico.org/podIP: 10.1.62.160/32cni.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://deaf2b805292288ca609095993699911c9be1cda96439a77946df20959b01beaImage: localhost:32000/simple_http:v1Image ID: localhost:32000/simple_http@sha256:cbee584f83426593efb95a9e2213bb40143a1c86c3d217e65d30430033f846d4Port: 8888/TCPHost Port: 0/TCPState: RunningStarted: Fri, 19 May 2023 20:32:52 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-kzscm (ro)
Conditions:Type StatusInitialized True Ready True ContainersReady True PodScheduled True
Volumes:kube-api-access-kzscm:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.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_httpname=simple-http-bversion=v1
Annotations: cni.projectcalico.org/containerID: b34e51ad923f778cdba027b7bf361c534c7f4f4e40da1d5bd7c0466bbfaf9fa1cni.projectcalico.org/podIP: 10.1.62.159/32cni.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://0b982826db40467ff7698629782e8d16d9560237027d18b3bc2305e894331c34Image: localhost:32000/simple_http:v1Image ID: localhost:32000/simple_http@sha256:cbee584f83426593efb95a9e2213bb40143a1c86c3d217e65d30430033f846d4Port: 8888/TCPHost Port: 0/TCPState: RunningStarted: Fri, 19 May 2023 20:32:52 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2vdsc (ro)
Conditions:Type StatusInitialized True Ready True ContainersReady True PodScheduled True
Volumes:kube-api-access-2vdsc:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.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: NodePortselector:image: simple_httpports:- port: 80targetPort: 8888nodePort: 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
参考资料
- https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
这篇关于研发工程师玩转Kubernetes——通过文件创建Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!