1. 什么是Endpoint?
-
我們創建Service的時候會自動給我們創建一個同名的Endpoint資源,每一個同名的 Servie都有一個Endpoints資源,因為Service自己并不直接匹配后端Pod的標簽,而是由Endpoint匹配的,這個匹配程序是由Endpoint控制器來完成的,Endpoint是由Endpoint控制器來控制的;
-
事實上我們Service不但能夠把標簽選擇器選中的Pod識別為自己的后端端點,還能夠對后端端點做"就緒狀態檢測",如果后端的Pod是就緒的,就把它加到后端可用端點串列中來,否則就會移除掉,這個功能其實不是Service來做的,而是Service借助一個中間的組件,這個中間組件也是一個"標準的資源型別",就叫做"Endpoint";
-
Service通過Selector和Pod建立關聯,K8s會根據Service關聯到的PodIP資訊組合成一個Endpoint,若Service定義中沒有Selector欄位,Service被創建時,Endpoint Controller不會自動創建Endpoint;
-
我們可以通過配置清單創建Service,而無需使用標簽選擇器,而后自行創建一個同名的Endpoint物件,指定對應的IP,這種一般用于將外部Mysql\Redis等應用引入kubernetes集群內部,讓內部通過Service的方式訪問外部資源;
-
官方檔案: https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/service-resources/endpoints-v1/#Endpoints
1.2 Service與Endpoints的關系?
Service物件借助Endpoint資源來觀察和跟蹤其后段端點,Eendpoint物件會根據Service標簽選擇器篩選出來的后端端點的IP地址分別保存在subsets.address欄位和subsets.notReadyAddress欄位中,它通過API-Server持續動態跟蹤每個端點的狀態變化,并及時反應到端點IP所屬的欄位;
-
subsets.address: 保存就緒Pod的IP,也就意味Service可以直接將請求調度給就緒下的Pod;
-
subsets.notReadyAddress: 保存未就緒Pod的IP,也就意味著Service不會將請求調度給歸類為不就緒的Pod;
2. 自定義Endpoints,引入Mysql服務;
2.1 安裝Mysql服務
1.下載安裝Mysql或者Mariadb,這里直接安裝Mariadb
[root@knative-k8s-master-139 ~]# apt install mariadb-server -y
2.設定開機自啟動
[root@knative-k8s-master-139 ~]# systemctl enable mariadb --now
[root@knative-k8s-master-139 ~]# systemctl status mariadb
2.1.1 登陸Mysql并授權;
MariaDB [(none)]> grant all privileges on *.* to 'haitang'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.2 自定義Endpoints和Service
2.2.1 創建Service資源;
[root@knative-k8s-master-139 ~]# cat service-endpoint-mysql.yaml
# 自定義Servie和endpoint
apiVersion: v1
kind: Service
metadata:
name: mysql-external
spec:
type: ClusterIP
ports:
- port: 3366 # 負載均衡的對外埠
targetPort: 3306 # 后端mysql的埠
[root@knative-k8s-master-139 ~]# kubectl apply -f service-endpoint-mysql.yaml
查看Service資源
[root@knative-k8s-master-139 ~]# kubectl get svc mysql-external
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-external ClusterIP 10.102.169.77 <none> 3366/TCP 22s
2.2.2 創建需要的Endpoints資源;
[root@knative-k8s-master-139 ~]# cat service-endpoint-mysql.yaml
---
apiVersion: v1
kind: Endpoints
metadata:
name: mysql-external
subsets:
- addresses:
- ip: 10.x.x.xxx
ports:
- protocol: TCP
port: 3306 # 定義后端的埠是多少
root@kubernetes-node01:~# kubectl apply -f service-endpoint-mysql.yaml
可以看到后段端點的IP為節點IP;
[root@knative-k8s-master-139 ~]# kubectl describe svc mysql-external
Name: mysql-external
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.102.169.77
Port: <unset> 3366/TCP
TargetPort: 3306/TCP
Endpoints: 10.x.x.xx:3306
Session Affinity: None
Events: <none>
2.3 測驗訪問;
# 可通過Service的IP來訪問
[root@tools-test-8444596cb5-xb86z /]# mysql -h 10.102.169.77 -P3366 -uhaitang -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.1.48-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
# 亦可以通過Service的Name訪問,
[root@tools-test-8444596cb5-xb86z /]# mysql -h mysql-external -P3366 -uhaitang -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.48-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
我們一直奔跑在進步的旅途
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/502232.html
標籤:其他
下一篇:ACM模式細節
