kubernetes之Endpoint引入外部資源實踐;
- 2022 年 8 月 18 日
- 筆記
- Kubernetes
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的方式訪問外部資源;
-
官方文檔: //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)]>

