Detailed explanation of Kubernetes object Service

1. Introduction to Service

In k8s, each Pod has its own IP address. When the controller replaces a failed Pod with a new Pod, the new Pod is reassigned a new IP address. This creates a problem: if a group of Pods provide external services (such as HTTP), their IPs are likely to change, so how can the client automatically find and access this service? The solution given by k8s is Service.

The Service in k8s logically represents a group of Pods, and the specific Pods are selected by the label. Service has its own IP, and this IP is unchanged. The client only needs to access the IP of the Service, and k8s is only responsible for establishing and maintaining the mapping relationship between the Service and the Pod. No matter how the IP of the backend Pod changes, it will not have any impact on the client, because the Service has not changed.

2. Create Service

First create a Deployment-based yml file with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
   name: httpservice-deployment
spec:
  replicas: 3
  selector:
   matchLabels:
     run: httpdservice
  template:
   metadata:
     labels:
       run: httpdservice
   spec:
    containers:
    - name: httpdservice
      image: httpd
      ports:
      - containerPort: 80

Then, to execute the Deployment, execute the following command:

[root@master service]# kubectl apply -f http-se

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/132201028