当前位置:网站首页>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 name
That'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
边栏推荐
- Battle drag method 1: moderately optimistic, build self-confidence (1)
- From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
- 2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
- Analysis of gas fee setting under eip1559
- Basic preprocessing and data enhancement of image data
- MySQL 8.0 data backup and recovery
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- Study diary: February 14th, 2022
- Operate BOM objects (key)
- Cap and base theory
猜你喜欢
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
Etcd raft Based Consistency assurance
Memory analyzer (MAT)
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
Discussion Net legacy application transformation
How to do Taobao full screen rotation code? Taobao rotation tmall full screen rotation code
JVM JNI and PVM pybind11 mass data transmission and optimization
如临现场的视觉感染力,NBA决赛直播还能这样看?
Introduction to golang garbage collection
Yyds dry goods inventory TCP & UDP
随机推荐
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
Design e-commerce seckill system
For in, foreach, for of
How to handle wechat circle of friends marketing activities and share production and release skills
Reinforcement learning - learning notes 1 | basic concepts
Phpexcel import export
Scientific research document management Zotero
How to set the system volume programmatically- How to programmatically set the system volume?
Visiontransformer (I) -- embedded patched and word embedded
设计电商秒杀系统
阻塞非阻塞和同步异步的区分 参考一些书籍
Kubernetes 通信异常网络故障 解决思路
Install and use Chrony, and then build your own time server
Offset related concepts + drag modal box case
Hcie security Day12: supplement the concept of packet filtering and security policy
Basic number theory -- Chinese remainder theorem
Ask and answer: dispel your doubts about the virtual function mechanism
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
Battle drag method 1: moderately optimistic, build self-confidence (1)
Discussion Net legacy application transformation