admin 管理员组文章数量: 1087652
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-clusteripnamespace: default
spec:selector:app: nginx-dev # 为Pod资源的app键值type: ClusterIPports:- protocol: TCPtargetPort: 80port: 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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</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-nodeportnamespace: default
spec:selector:app: nginx-devtype: NodePortports:- protocol: TCPtargetPort: 80port: 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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</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-lbnamespace: default
spec:selector:app: nginx-devtype: LoadBalancerports:- protocol: TCPtargetPort: 80port: 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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</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="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
结语
… …
本文标签: k8s中Service(svc)的类型使用
版权声明:本文标题:k8s中Service(svc)的类型使用 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1700300972a386858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论