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                                            80/TCP               18m
  • 访问其应用服务
]# curl 172.21.6.109



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

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                                           8020:30076/TCP       48m

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

  • 访问其应用服务

a.内部集群访问

]# curl 172.21.12.177:8020



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

b.通过公网ip/NodeIP访问

]# curl 公网ip/NodeIP:30076



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

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



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

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

]# curl 120.76.70.149:8030



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

c.通过公网ip/NodeIP访问

]# curl 公网ip/NodeIP:30636



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

结语

… …


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部