k8s中Service(svc)的类型使用

前言

k8s中svc有三种类型,分别为ClusterIP、NodePort、LoadBalancer

作用
  • 能够解耦前端和后端的关联
类型用途及其使用

部署的应用为无状态应用服务,参考k8s中部署无状态(deployment)服务,此技术文档以此参考,通过deployment控制器部署了Pod资源,在此基础上,去实现service的类型应用实现

ClusterIP
  • 概述
    分配一个内部集群IP地址,只能在集群内部访问(同Namespace内的Pod),默认ServiceType,ClusterIP模式的Service默认提供的,就是一个Pod的稳定的IP地址,即VIP,访问地址: :

  • yaml文本示例

cat > ng-svc-clusterip.yaml <<-EOF
apiVersion: v1
kind: Service
metadata:
    name: nginx-clusterip
    namespace: default
spec:
    selector:
        app: nginx-dev	# 为Pod资源的app键值
    type: ClusterIP
    ports:
       - protocol: TCP
         targetPort: 80
         port: 80
EOF         
  • 查看其svc相关信息
]# kubectl apply -f ng-svc-clusterip.yaml
]# kubectl get svc -n default | grep nginx
nginx--clusterip              ClusterIP      172.21.6.109    <none>                                        80/TCP               18m
  • 访问其应用服务
]# curl 172.21.6.109
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
NodePort
  • 概述
    分配一个内部集群IP地址,并在每个节点上启用一个端口来暴露服务,可以在集群内部访问,访问地址: :

  • 要点
    类型缺陷

a. 端口容器冲突
b. NodePort属于四层,不能做七层的事,比如根据域名/url进行转发
c. 不能统一入口
  • yaml文本示例
cat > ng-svc-nodeport.yaml <<-EOF
apiVersion: v1
kind: Service
metadata:
    name: nginx-nodeport
    namespace: default
spec:
    selector:
        app: nginx-dev
    type: NodePort
    ports:
       - protocol: TCP
         targetPort: 80
         port: 8020
EOF         
  • 查看其svc相关信息
]# kubectl apply -f ng-svc-nodeport.yaml
]# kubectl get svc -n default | grep nginx
nginx-nodeport          NodePort       172.21.12.177   <none>                                        8020:30076/TCP       48m

其中8020:30076的8020为内部集群端口,而30076为公网ip/NodeIP端口

  • 访问其应用服务

a.内部集群访问

]# curl 172.21.12.177:8020
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

b.通过公网ip/NodeIP访问

]# curl 公网ip/NodeIP:30076
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
LoadBalancer
  • 概述
    分配一个内部集群IP地址,并在每个节点上启用一个端口来暴露服务,除此之外,k8s会请求底层云平台上的负载均衡器,将每个Node ([NodeIP]:[NodePort])作为后端添加进去

  • yaml文本示例

cat > ng-svc-lb.yaml <<-EOF
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: "lb-wxxx"		# 负载均衡器的实例id值
    service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listeners: "true"
  name: nginx-lb
  namespace: default
spec:
  selector:
    app: nginx-dev
  type: LoadBalancer
  ports:
    - protocol: TCP
      targetPort: 80
      port: 8030
  • 查看其svc相关信息
]# kubectl apply -f ng-svc-lb.yaml
]# kubectl get svc -n default | grep nginx
nginx-lb                LoadBalancer   172.21.11.211   120.76.70.149                                  8030:30636/TCP       54s

其中8030:30076的8030为内部集群端口,而30636为公网ip/NodeIP端口

  • 访问其应用服务

a.内部集群访问

]# curl 172.21.11.211:8030
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

b.通过负载均衡器IP访问

]# curl 120.76.70.149:8030
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

c.通过公网ip/NodeIP访问

]# curl 公网ip/NodeIP:30636
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
结语

… …

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值