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

相关文章

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术

Ollama整合open-webui的步骤及访问

《Ollama整合open-webui的步骤及访问》:本文主要介绍如何通过源码方式安装OpenWebUI,并详细说明了安装步骤、环境要求以及第一次使用时的账号注册和模型选择过程,需要的朋友可以参考... 目录安装环境要求步骤访问选择PjrIUE模型开始对话总结 安装官方安装地址:https://docs.

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程