Linux:k8s集群访问集群外部服务(Endpoints)

2023-10-14 20:08

本文主要是介绍Linux:k8s集群访问集群外部服务(Endpoints),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

k8s集群访问集群外部服务(Endpoints)

像数据库这个的服务我们一般是不会用k8s直接来跑的,最好是部署在集群外部的服务器,那么集群内部的pod怎么去访问外部的服务呢?可以使用Endpoints将外部的服务映射到集群内部,然后集群内部就能进行解析,直接访问。实际上,不映射到集群内部,也是可以访问的,下面以mysql服务为例进行说明。

环境准备
(搭建一个K8S集群,略)
master 192.168.146.10
node1 192.168.146.11
node2 192.168.146.12
node3 192.168.146.13
在任意一台机器安装数据库

[root@node3 ~]# yum -y install mariadb-server
#授权账户
[root@node3 ~]# systemctl start mariadb
[root@node3 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB ServerCopyright (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)]> delete from mysql.user where user="";
Query OK, 2 rows affected (0.00 sec)MariaDB [(none)]> grant all on *.* to "pod"@"192.168.146.%" identified by "123";
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> select user,host,password from mysql.user;
+------+---------------+-------------------------------------------+
| user | host          | password                                  |
+------+---------------+-------------------------------------------+
| root | localhost     |                                           |
| root | node3         |                                           |
| root | 127.0.0.1     |                                           |
| root | ::1           |                                           |
| pod  | 192.168.146.% | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+------+---------------+-------------------------------------------+
5 rows in set (0.00 sec)

创建一个pod
(哪个节点都可以,只要你配置了使用kubectl命令)

---
apiVersion: apps/v1
kind: Deployment
metadata:name: yum
spec:selector:matchLabels:app: yumreplicas: 1template:metadata:labels:app: yumspec:containers:- name: yumimage: registry.cn-shenzhen.aliyuncs.com/jay23/centos_yum:v1.0command: ["sh","-c","sleep 10000"]

进入pod,尝试连接数据库

[root@master mysql]# kubectl apply -f deploy2.yaml
deployment.apps/yum created
[root@master mysql]# kubectl get po
NAME                          READY   STATUS        RESTARTS   AGE
counter                       1/1     Running       0          5h4m
dummylogs-6d66db57f8-bp2t5    1/1     Running       1          4h44m
dummylogs-6d66db57f8-k4z76    1/1     Running       1          4h44m
dummylogs-6d66db57f8-m5b2k    1/1     Running       1          4h44m
dummylogs2-77f4d88788-52cmn   1/1     Running       1          4h44m
dummylogs2-77f4d88788-t996h   1/1     Running       1          4h44m
dummylogs2-77f4d88788-vk4h6   1/1     Running       1          4h44m
yum-d9fc97f8-w6mp8            1/1     Running       0          4s
[root@master mysql]# kubectl exec -it yum-d9fc97f8-w6mp8 -- bash
#安装一个数据库客户端
[root@yum-d9fc97f8-w6mp8 /]# yum -y install mariadb
#测试连接
[root@yum-d9fc97f8-w6mp8 /]# mysql -upod -h192.168.146.13 -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.65-MariaDB MariaDB ServerCopyright (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)]>

通过以上的实验可以看出,只要宿主机可以通讯,那么pod内也是可以直接通过宿主机的IP访问集群之外的服务。因为虽然是pod发出的请求,实际上经过转发,出去的时候是以宿主机的IP进行访问的,所以我们授权的时候,保证运行pod的宿主机能登录数据库就行了,并不是对pod的IP进行授权。

那么Endpoints的作用是什么呢?
下面我们来创建Endpoints:

---
kind: Service
apiVersion: v1
metadata:name: testep          #通过name绑定到下面的Endpoints,否则就用自己的Endpoints
spec:ports:- port: 3306
---
kind: Endpoints
apiVersion: v1
metadata:name: testep      #与上面的name要对应
subsets:
- addresses:- ip: 192.168.146.13ports:- port: 3306

查看Endpoints和Service的关系

[root@master mysql]# vim deploy.yaml
[root@master mysql]# kubectl apply -f deploy.yaml
service/testep unchanged
endpoints/testep configured
[root@master mysql]# kubectl describe svc testep
Name:              testep
Namespace:         default
Labels:            <none>
Annotations:       Selector:  <none>
Type:              ClusterIP
IP:                10.111.21.40
Port:              <unset>  3306/TCP
TargetPort:        3306/TCP
Endpoints:         192.168.146.13:3306   #如果不指定Endpoints,这里就是service的ClusterIP
Session Affinity:  None
Events:            <none>

此时,再进入刚刚的pod进行测试

[root@master mysql]# kubectl exec -it yum-d9fc97f8-w6mp8 -- bash
[root@yum-d9fc97f8-w6mp8 /]# mysql -upod -htestep -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.65-MariaDB MariaDB ServerCopyright (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)]> 

发现可以使用Service的名称访问到数据库,大体的流程是这样的:
pod里面是可以解析svc的名字的,又因为svc的Endpoints链接的是其他机器的数据库,所以可以直接通过svc访问到集群之外的数据库。这就是Endpoints的作用。

这篇关于Linux:k8s集群访问集群外部服务(Endpoints)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/212836

相关文章

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

HDFS—集群扩容及缩容

白名单:表示在白名单的主机IP地址可以,用来存储数据。 配置白名单步骤如下: 1)在NameNode节点的/opt/module/hadoop-3.1.4/etc/hadoop目录下分别创建whitelist 和blacklist文件 (1)创建白名单 [lytfly@hadoop102 hadoop]$ vim whitelist 在whitelist中添加如下主机名称,假如集群正常工作的节

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3