自定义endpoint实现内部pod访问外部应用
endpoint除了可以暴露pod的IP和端口还可以代理到外部的ip和端口
使用场景
1. 公司业务还还没有完成上云, 一部分云原生的,一部分是实体的
2. 业务上云期间逐步实现上云,保证各个模块之间的解耦性
比如使用云数据库或者实体数据库服务器啥的,因为像数据库实现容器化的话在实际生产环境中是不推荐的
所以一些静态服务上云以后pod还是需要访问外部应用服务的
第一章:kubernetes连接外部mysql数据库
1、#创建service配置
[root@k8s-master1-60 endpoints]# cat mysql_service.yaml
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: beijia
spec:
ports:
- port: 3306
protocol: TCP
name: mysqlport
targetPort: 3306
type: ClusterIP
2、#创建Endpoints配置
[root@k8s-master1-60 endpoints]# cat mysql_endpoints.yaml
apiVersion: v1
kind: Endpoints
metadata:
#定义endpoints名称、必须与service名称一致
name: mysql
namespace: beijia
subsets:
- addresses:
#外部的mysql真实地址
- ip: 172.30.42.67
ports:
#外部mysql真实启用端口
- port: 3306
# 定义端口的名称,必须与 service 中的 ports.name 一致
name: mysqlport
protocol: TCP
3、#加载配置文件
[root@k8s-master1-60 endpoints]# kubectl apply -f mysql_service.yaml
[root@k8s-master1-60 endpoints]# kubectl apply -f mysql_endpoints.yaml
4、查看mysql的Endpoints信息
[root@k8s-master1-60 endpoints]# kubectl describe endpoints -n beijia mysql
Name: mysql
Namespace: beijia
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 172.30.42.67
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
mysqlport 3306 TCP
Events: <none>
[root@k8s-master1-60 endpoints]# kubectl describe svc -n beijia mysql
Name: mysql
Namespace: beijia
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.107.201.145
IPs: 10.107.201.145
Port: paw5e6 3306/TCP
TargetPort: 3306/TCP
Endpoints: 172.30.42.67:3306
Session Affinity: None
Events: <none>
##################
出现这个关联信息说明service已经关联到了endpoints
Endpoints: 172.30.42.67:3306
#至此自定义service已经完成、集群内可以使用mysql这个service_name的地址来进行连接
第二章:Kubernetes连接外部redis cluster集群
1、#创建service配置
[root@k8s-master1-60 endpoints]# cat redis_service.yaml
kind: Service
apiVersion: v1
metadata:
name: redis-cluster
namespace: beijia
spec:
ports:
- port: 6379
protocol: TCP
name: redis
targetPort: 6379
type: ClusterIP
2、#创建Endpoints配置
[root@k8s-master1-60 endpoints]# cat redis_endpoints.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: redis-cluster
namespace: beijia
subsets:
- addresses:
- ip: 172.30.42.60
- ip: 172.30.42.61
- ip: 172.30.42.62
- ip: 172.30.42.63
- ip: 172.30.42.64
- ip: 172.30.42.69
ports:
- port: 6379
name: redis
3、#加载配置文件
[root@k8s-master1-60 endpoints]# kubectl apply -f redis_service.yaml
[root@k8s-master1-60 endpoints]# kubectl apply -f redis_endpoints.yaml
4、查看redis-cluster的service信息
[root@k8s-master1-60 endpoints]# kubectl describe svc -n beijia redis-cluster
Name: redis-cluster
Namespace: beijia
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.98.253.8
IPs: 10.98.253.8
Port: redis 6379/TCP
TargetPort: 6379/TCP
Endpoints: 172.30.42.60:6379,172.30.42.61:6379,172.30.42.62:6379 + 3 more...
Session Affinity: None
Events: <none>
5、查看redis-cluster的endpoints信息
[root@k8s-master1-60 endpoints]# kubectl describe ep -n beijia redis-cluster
Name: redis-cluster
Namespace: beijia
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 172.30.42.60,172.30.42.61,172.30.42.62,172.30.42.63,172.30.42.64,172.30.42.69
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
redis 6379 TCP
Events: <none>
##################
出现这个关联信息说明service已经关联到了endpoints
Endpoints: 172.30.42.60:6379,172.30.42.61:6379,172.30.42.62:6379 + 3 more...
#至此自定义连接redis cluster集群的service已经完成、集群内的pod可以使用redis-cluster这个service_name的地址来进行连接
例子:
spring:
redis:
cluster:
nodes:
- redis-cluster:6379
password: 1qaz@WSX
timeout: 5000ms