十念 发表于 2022-9-16 17:20:16

kubernetes之Endpoint引入外部资源实践;

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
#apt install mariadb-server -y

2.设置开机自启动
#systemctl enable mariadb --now
#systemctl status mariadb2.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资源;

#cat service-endpoint-mysql.yaml
# 自定义Servie和endpoint
apiVersion: v1
kind: Service
metadata:
name: mysql-external
spec:
type: ClusterIP
ports:
- port: 3366      # 负载均衡的对外端口
    targetPort: 3306 # 后端mysql的端口
       
#kubectl apply -f service-endpoint-mysql.yaml查看Service资源
# kubectl get svcmysql-external
NAME             TYPE      CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
mysql-external   ClusterIP   10.102.169.77   <none>      3366/TCP   22s2.2.2 创建需要的Endpoints资源;

#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;
#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来访问
# 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访问。
# 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)]>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: kubernetes之Endpoint引入外部资源实践;