关于k8s|关于k8s 使用 Service 控制器对外暴露服务的问题

目录

  • 部署 deploy
  • 部署 service
  • 查看 service 和 pod 的关系
  • 查看 service
  • 查看端口
  • 导出 yaml
  • 筛选 service 关联 pod
  • 扩容测试
  • Service 三种常用类型
Service 引入主要是解决 Pod 的动态变化,提供统一访问入口:
  1. 防止 Pod 失联,准备找到提供同一个服务的 Pod (服务发现)
  2. 定义一组 Pod 的访问策略 (负载均衡)

部署 deploy
kubectl apply -f deploy.yaml

apiVersion: apps/v1kind: Deploymentmetadata:name: chiyi-nginxspec:replicas: 3selector:matchLabels:app: chiyi-nginxtemplate:metadata:labels:app: chiyi-nginxspec:containers:- name: nginximage: nginx:1.14.2ports:- containerPort: 80


部署 service
kubectl apply -f service.yaml

apiVersion: v1kind: Servicemetadata:name: chiyi-nginxspec:selector:app: chiyi-nginxports:- protocol: TCPport: 80targetPort: 80nodePort: 30002type: NodePort


查看 service 和 pod 的关系
kubectlget epcurl 10.244.1.58:80

说明:
Service 通过标签关联一组 Pod
Service 为一组 Pod 提供负载均衡能力
[root@k8s-master service]# kubectl get epNAMEENDPOINTSAGEchiyi-nginx10.244.1.58:80,10.244.1.59:80,10.244.2.46:805m19skubernetes172.17.28.225:644323h[root@k8s-master service]# curl 10.244.1.58:80Welcome to nginx!body {width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }Welcome to nginx!If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.


查看 service
kubectlget servicecurl 10.101.104.218

[root@k8s-master service]# kubectl get serviceNAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGEchiyi-nginxNodePort10.101.104.21880:30002/TCP6m3skubernetesClusterIP10.96.0.1443/TCP23h[root@k8s-master service]# curl 10.101.104.218Welcome to nginx!body {width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }Welcome to nginx!If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.


查看端口
ss -antp |grep 30002

[root@k8s-master service]# ss -antp |grep 30002LISTEN0128*:30002*:*users:(("kube-proxy",pid=3544,fd=13))

【关于k8s|关于k8s 使用 Service 控制器对外暴露服务的问题】
导出 yaml
kubectlget service chiyi-nginx -o yaml


筛选 service 关联 pod
kubectl get pods -l app=chiyi-nginx

[root@k8s-master service]# kubectl get pods -l app=chiyi-nginxNAMEREADYSTATUSRESTARTSAGEchiyi-nginx-5bbf8bff4b-6bwfz1/1Running03m58schiyi-nginx-5bbf8bff4b-bpvvc1/1Running03m58schiyi-nginx-5bbf8bff4b-pwwt41/1Running03m58s


扩容测试
kubectl scale deployment chiyi-nginx --replicas=1kubectlget service,pods,ep


Service 三种常用类型
  • ClusterIP 集群内部使用,任一节点服务器和 pod 内部都可以访问
  • NodePort 对外暴露应用(端口默认范围:30000-32767),任一节点服务器公网IP+端口号,可在浏览器访问。
  • LoadBalancer 对外暴露应用,适合公有云
到此这篇关于k8s 使用 Service 控制器对外暴露服务的文章就介绍到这了,更多相关k8s对外暴露服务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读