当前位置:网站首页>Service discovery and load balancing mechanism -service
Service discovery and load balancing mechanism -service
2022-07-03 21:02:00 【Zhong Ming】
What is? Service?
- Service Is a logical group Pod, One can access Pod The strategy of , And other Pod Can pass Service Visit this Service Acting Pod, It can be understood as the reverse proxy in the traditional architecture .
- be relative to Pod for ,Service Have a fixed name , No change , It also provides the function of load balancing .
- adopt Service The definition of , You can mask the backend for client applications Pod Number of instances and Pod IP Address change , The load balancing strategy is used to realize the request to the back end Pod Forwarding of instances , Provide a stable service access portal address for client applications .
- Service It implements several core functions in the microservice architecture : Automatic service registration 、 Service discovery 、 Service load balancing, etc .
Create a Service example
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-svc
name: nginx-svc
spec:
ports:
- name: http #service Port name
port: 80 #service Own port
protocol: TCP # Support TCP UDP SCTP etc.
targetPort: 80 # Back end application interface
- name: https
port: 443
protocol: TCP
targetPort: 443
selector:
app: nginx # This is the matching rule , The proxy tag contains nginx The back-end server
sessionAffinity: None
type: ClusterIP
- Execute the above yaml file , Create a service
[[email protected]-master01 ~]# kubectl create -f nginx-svc.yaml
service/nginx-svc created
[[email protected]-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
nginx-svc ClusterIP 10.110.150.87 <none> 80/TCP,443/TCP 15s
- We have access to svc Address to access the back-end nginx
[[email protected]-master01 ~]# curl 10.110.150.87
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
color-scheme: light dark; }
body {
width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- In the same namespace in , other Pod visit svc It only needs curl http://nginx-svc Can
- Span namespace Words , Need to be in svc Add after name
.namespace nameThat's all right. , Use with caution , There is no need not to recommend - It is not recommended to pass IP Address access , because IP Not fixed , After deletion or reconstruction IP Will randomly generate
Be careful , The above content can only be accessed within the cluster
————————————————————————
If you want to access... Outside the cluster svc Words , Below
Use Service agent k8s External application scenarios
- Want to use a fixed name in production instead of IP Address to access external middleware services
- hope Service Point to the other namespace Services in or other clusters
- A project is being migrated to k8s colony , But some services are still outside the cluster , You can use service Agent external services
Create a proxy for external applications Service example
- Let's define an external application service
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-svc-w
name: nginx-svc-w
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
# - name: https
# port: 443
# protocol: TCP
# targetPort: 443
# selector:
# app: nginx
sessionAffinity: None
type: ClusterIP
- The difference is that these two parameters are missing :
selector:
app: nginx
- View after creation , It can be found that although the creation was successful, there was no ENDPOINTS
[[email protected]-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
nginx-svc ClusterIP 10.110.150.87 <none> 80/TCP,443/TCP 31m
nginx-svc-w ClusterIP 10.110.144.61 <none> 80/TCP 58s
[[email protected]-master01 ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.10.2:6443,192.168.10.3:6443,192.168.10.4:6443 9d
nginx-svc 172.17.125.10:443,172.18.195.22:443,172.17.125.10:80 + 1 more... 31m
- Next we need to manually create a custom ENDPOINTS
- Borrow existing ep Make a new one ep file
[[email protected]-master01 ~]# kubectl get ep nginx-svc -o yaml > nginx-ep-w.yaml
apiVersion: v1
kind: Endpoints
metadata:
labels:
app: nginx-svc-w
name: nginx-svc-w
namespace: default
subsets:
- addresses:
- ip: 110.242.68.3 # The address of the external service of the proxy
ports:
- name: http
port: 80
protocol: TCP
- You can see that there are already external agents
[[email protected]-master01 ~]# kubectl create -f nginx-ep-w.yaml
endpoints/nginx-svc-w created
[[email protected]-master01 ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.10.2:6443,192.168.10.3:6443,192.168.10.4:6443 9d
nginx-svc 172.17.125.10:443,172.18.195.22:443,172.17.125.10:80 + 1 more... 47m
nginx-svc-w 110.242.68.3:80 11s
- Next, try whether you can access it successfully
# Direct access makes sense , Return value 200
[[email protected]-master01 ~]# curl baidu.com -I
HTTP/1.1 200 OK
Date: Mon, 14 Feb 2022 01:43:18 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Tue, 15 Feb 2022 01:43:18 GMT
Connection: Keep-Alive
Content-Type: text/html
# By visiting service Of IP It can be seen that it is also true , Return value 200
[[email protected]-master01 ~]# curl 10.110.144.61 -I
HTTP/1.1 200 OK
Date: Mon, 14 Feb 2022 01:44:20 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Tue, 15 Feb 2022 01:44:20 GMT
Connection: Keep-Alive
Content-Type: text/html
- If the business changes ep What if the address changes ? Only need ep Change the address of the agent .
- For example, we change one taobao Address test for :
# Take out taobao Of IP
[[email protected]-master01 ~]# ping taobao.com
PING taobao.com (140.205.94.189) 56(84) bytes of data.
64 bytes from 140.205.94.189 (140.205.94.189): icmp_seq=1 ttl=128 time=27.4 ms
64 bytes from 140.205.94.189 (140.205.94.189): icmp_seq=2 ttl=128 time=27.4 ms
# modify ep Of yaml file :nginx-ep-w.yaml
apiVersion: v1
kind: Endpoints
metadata:
labels:
app: nginx-svc-w
name: nginx-svc-w
namespace: default
subsets:
- addresses:
- ip: 140.205.94.189 # taobao
ports:
- name: http
port: 80
protocol: TCP
# Reload
[[email protected]-master01 ~]# kubectl replace -f nginx-ep-w.yaml
- Visit and test to see if it is connected : This returns 501 No problem .
[[email protected]-master01 ~]# curl 10.110.144.61 -I
HTTP/1.1 501 Not Implemented
Server: Tengine
Date: Mon, 14 Feb 2022 01:39:16 GMT
Content-Type: text/html
Content-Length: 583
Connection: close
Service Reverse the external domain name
Let's not talk about this , Just know how to match it .
Service Of yaml file :
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-svc-wname
name: nginx-svc-wname
spec:
type: ExternalName
externalName: www.baidu.com
Then create it :
kubectl apply -f nginx-svc-wname.yaml
Service The type of :
- ClusterIP: Use within the cluster , Default
- ExternalName: By returning the defined CNAME Alias
- NodePort: In all installed kube-proxy Open a port on the node of , This port can proxy to the back end Pod, Then the nodes can be used outside the cluster IP Address and NodePort The port number accesses the cluster Pod service . The default port value range is 30000-32767
- LoadBalancer: Use the load balancer provided by the cloud service provider to expose the service
边栏推荐
- Inventory 2021 | yunyuansheng embracing the road
- Phpexcel import export
- Sort out several network request methods of JS -- get rid of callback hell
- Analyse de REF nerf
- Cannot load driver class: com. mysql. cj. jdbc. Driver
- Etcd raft Based Consistency assurance
- 浅析 Ref-NeRF
- How to modify the network IP addresses of mobile phones and computers?
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- Brief analysis of ref nerf
猜你喜欢

Transformer structure analysis and the principle of blocks in it

Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output

Shortest path problem of graph theory (acwing template)

JS three families

Line segment tree blue book explanation + classic example acwing 1275 Maximum number

Qt6 QML Book/Qt Quick 3D/基础知识
![C 10 new feature [caller parameter expression] solves my confusion seven years ago](/img/32/2d81237d4f1165f710a27a7c4eb1e1.jpg)
C 10 new feature [caller parameter expression] solves my confusion seven years ago

What is the maximum number of concurrent TCP connections for a server? 65535?

MySQL master-slave synchronization principle

运维各常用命令总结
随机推荐
Phpexcel import export
Yyds dry goods inventory TCP & UDP
@Scenario of transactional annotation invalidation
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
11-grom-v2-04-advanced query
Qt6 QML Book/Qt Quick 3D/基础知识
2022 high voltage electrician examination and high voltage electrician reexamination examination
19、 MySQL -- SQL statements and queries
你真的知道自己多大了吗?
Analysis of gas fee setting under eip1559
MySQL dump - exclude some table data - MySQL dump - exclude some table data
Transformation between yaml, Jason and Dict
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Baohong industry | good habits that Internet finance needs to develop
Hcie security Day12: supplement the concept of packet filtering and security policy
淺析 Ref-NeRF
运维各常用命令总结
Read the root directory of the folder, write txt and generate random samples
Recommendation of books related to strong foundation program mathematics
Q&A:Transformer, Bert, ELMO, GPT, VIT