Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

43 of kubernetes practice: a detailed explanation of Service

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

One: brief introduction

Through Service, you can provide a unified entry address for a group of container applications with the same function, and distribute the request load to each back-end application container to achieve Pod load balancing.

Two: Service definition template important attribute description

The type of 1.spec.type Service, which specifies the access method of Service. The default value is ClusterIP.

A.ClusterIP: the virtual IP address of the service, which is used for Pod access within the Kubernetes cluster. On Node, kube-proxy forwards through the set Iptables rules.

B.NodePort: use the port of the host to enable clients that can access each Node to access the service through the IP address and port number of the Node.

C.LoadBalancer: load distribution to the service using an external load balancer

2.spec.sessionAffinity: whether Session is supported. The optional value is ClientIP. The default value is empty. ClientIP means that all access requests from the same client are forwarded to the same backend Pod.

Three: the basic usage of Service

1. Commonly used, the service acts as a load balancer for Pod. The current load distribution strategies are:

A. RoundRobin: polling mode, in which polling forwards requests to each Pod at the back end.

B.SessionAffinity: a mode of call hold based on the client IP address.

two。 Multi-port Service, sometimes a container application may also provide services with multiple ports, so the Service definition can also be set to correspond to multiple ports to multiple application services.

Click (here) to collapse or open

ApiVersion: v1

Kind: Service

Metadata:

Name: webapp

Spec:

Ports:

-port: 8080

TargetPort: 8080

Name: web

-port: 8005

TargetPort: 8005

Name: management3. External Service Service

In some environments, the application system needs to connect an external database as a back-end service, or a service in another cluster or Namespace as the back-end of the service, which can be achieved by creating a Label Selector-free Service (in this case, you need to create an Endpoint with the same name as the Service).

Click (here) to collapse or open

ApiVersion: v1

Kind: Service

Metadata:

Name: mysql-test

Spec:

Ports:

-port: 3306

Click (here) to collapse or open

ApiVersion: v1

Kind: Endpoints

Metadata:

Name: mysql-test

Namespace: default

Subsets:

-addresses:

-ip: 120.25.154.90

Ports:

-port: 33064.Headless Service. In some application scenarios, open people want to control the policy of load balancer by themselves, without using the default load balancer feature provided by Service, or the application wants to know other instances that belong to the same group. This service does not set ClusterIP for Service, but only finds the backend Pod list through Label Selector and returns it to the calling client.

Click (here) to collapse or open

ApiVersion: v1

Kind: Service

Metadata:

Name: nginx

Labels:

App: nginx

Spec:

Ports:

-port: 80

ClusterIP: None

Selector:

App: nginx

Fourth, the method of accessing Pod or Service outside the cluster

1. Map the port number of the container application to the physical machine

a. Map the port applied by the container to the physical machine by setting the hostPort at the container level

Click (here) to collapse or open

ApiVersion: v1

Kind: Pod

Metadata:

Name: webapp

Labels:

App: webapp

Spec:

Containers:

-name: webapp

Image: tomcat

Ports:

-containerPort: 8080

HostPort: 8081b. By setting the hostNetwork=true at the Pod level, the port numbers of all containers in the Pod are mapped directly to the physical machine.

Click (here) to collapse or open

ApiVersion: v1

Kind: Pod

Metadata:

Name: webapp

Labels:

App: webapp

Spec:

HostNetwork: true

Containers:

-name: webapp

Image: tomcat

Ports:

-containerPort: 80802. Map the port number of Service to the physical machine

a. Map to the physical machine by configuring nodePort, and set the type of Service to NodePort

Click (here) to collapse or open

ApiVersion: v1

Kind: Service

Metadata:

Name: nginx

Labels:

App: nginx

Spec:

Type: NodePort

Ports:

-port: 80

TargetPort: 8080

NodePort: 8081

Selector:

App: nginxb. Map to the LoadBalancer address provided by the cloud service provider by setting LoadBalancer.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report