k8s中service代理外部的服务

前言

环境:centos7.9 docker-ce-20.10.9 kubernetes-version v1.22.6

有我们,当我们的某个服务在外部集群的时候,但是又想k8s集群内的应用连接它,这时可以创建一个service,用service代理外部服务,然后集群内的应用就能连接该service,从而间接的访问外部服务了。

创建一个service代理外部的服务

创建一个没有标签选择器的service,这样的service就不会关联到任何pod,从而也不会自动创建endpoint。这时我们手动创建一个与service同名的endpoint,使用endpoint来定义外部服务的地址端口,service会自动关联该endpoint。

service是如何与endpoint关联的?需要特别注意一下几点:

1、endpoint的名称必须要与service的名称相同,这样两者才能关联;
2、service定义spec.ports的ports的时候,包含port端口、name端口名称、protocol协议;
3、endpoint中定义外部服务的IP和端口,endpoint的名称一定要与service的名称一样,协议也要一样,端口的name也要与service的端口的name一样,端口协议也要与service的端口协议一样,不然endpoint不能与service进行关联。

下面是演示示例:

cat > outside_agent_nginx.yaml <<'EOF'
---
apiVersion: v1
kind: Service
metadata:
  name: outside-agent-svc				#service的名称叫做outside-agent-svc
  namespace: default
spec:
  ports:
  - name: out-agent-port				#service端口的名称
    port: 8056							#service的端口
    protocol: TCP						#端口协议
#   targetPort: 80						#目标端口可以不定义,因为我们代理的不是pod,不定义targetPort,其默认等于port
  sessionAffinity: None
  type: ClusterIP

---
apiVersion: v1
kind: Endpoints
metadata:
  name: outside-agent-svc				#endpoint的名称一定要与service的名称一致
  namespace: default
subsets:
- addresses:
  - ip: 192.168.118.129					#定义外部服务地址
  ports:
  - port: 80							#外部服务的端口
    name: out-agent-port				#端口的name,这个名称一定要与service端口的名称
    protocol: TCP						#端口协议,这个协议一定要与service的端口协议一致
EOF

查看两者是否关联

[root@master ~]# kubectl describe   -f outside_agent_nginx.yaml 
Name:              outside-agent-svc
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.111.9.114
IPs:               10.111.9.114
Port:              out-agent-port  8056/TCP
TargetPort:        8056/TCP	
Endpoints:         192.168.118.129:80		#可以发现,service已经与同名的endpoint关联起来了
Session Affinity:  None
Events:            <none>


Name:         outside-agent-svc
Namespace:    default
Labels:       <none>
Annotations:  <none>
Subsets:
  Addresses:          192.168.118.129
  NotReadyAddresses:  <none>
  Ports:
    Name            Port  Protocol
    ----            ----  --------
    out-agent-port  80    TCP

Events:  <none>
[root@master endpoint]# 

测试

#访问service,查看是否能正常访问外部服务
[root@master endpoint]# curl   10.111.9.114:8056			#访问service的ip和端口,能正常访问外部服务
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Welcome to CentOS</title>
  <style rel="stylesheet" type="text/css"> 

        html {
        background-image:url(img/html-background.png);
        background-color: white;
        font-family: "DejaVu Sans", "Liberation Sans", sans-serif;
        font-size: 0.85em;
        line-height: 1.25em;
        margin: 0 4% 0 4%;
        }

        body {
        border: 10px solid #fff;
        margin:0;
        padding:0;
        background: #fff;
        }

        /* Links */

        a:link { border-bottom: 1px dotted #ccc; text-decoration: none; color: #204d92; }
        a:hover { border-bottom:1px dotted #ccc; text-decoration: underline; color: green; }
        a:active {  border-bottom:1px dotted #ccc; text-decoration: underline; color: #204d92; }
        a:visited { border-bottom:1px dotted #ccc; text-decoration: none; color: #204d92; }
        a:visited:hover { border-bottom:1px dotted #ccc; text-decoration: underline; color: green; }
 
        .logo a:link,
        .logo a:hover,
        .logo a:visited { border-bottom: none; }

        .mainlinks a:link { bord

总结教训

1、创建service时不指定selector标签选择器,但需要指定service的port端口、端口的name、端口协议等,这样创建出来的service因为没有指定标签选择器就不会自动创建endpoint;
2、手动创建一个与service同名的endpoint,endpoint中定义外部服务的IP和端口,endpoint的名称一定要与service的名称一样,端口协议也要一样,端口的name也要与service的端口的name一样,不然endpoint不能与service进行关联。
完成以上两步,k8s会自动将service和同名的endpoint进行关联,这样,k8s集群内的应用服务直接访问这个service就可以相当于访问外部的服务了。

如果发现,创建的endpoint无法与service关联,必须要检查这4样:两者名称是否一样、两者端口协议是否一样、两者端口的name名称是否一样。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值