当前位置:网站首页>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
边栏推荐
- Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
- MySQL master-slave synchronization principle
- Transformer structure analysis and the principle of blocks in it
- Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
- MySQL 8.0 data backup and recovery
- TLS environment construction and plaintext analysis
- Thread, thread stack, method stack, the difference of creating thread
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- 强基计划 数学相关书籍 推荐
- In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
猜你喜欢
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
Interval product of zhinai sauce (prefix product + inverse element)
In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028
Hcie security Day10: six experiments to understand VRRP and reliability
C 10 new feature [caller parameter expression] solves my confusion seven years ago
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
Transformer structure analysis and the principle of blocks in it
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
随机推荐
Link aggregation based on team mechanism
Design e-commerce seckill system
Etcd 基于Raft的一致性保证
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Analysis of gas fee setting under eip1559
设计电商秒杀系统
JVM JNI and PVM pybind11 mass data transmission and optimization
Yyds dry goods inventory TCP & UDP
App compliance
First knowledge of database
Test changes in Devops mode -- learning and thinking
Discussion Net legacy application transformation
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
同花顺开户注册安全靠谱吗?有没有风险的?
浅议.NET遗留应用改造
Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?
Wargames study notes -- Leviathan
Instructions for common methods of regular expressions
AST (Abstract Syntax Tree)