锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

k8s 使用 Service 控制器对外暴露服务

时间:2023-08-07 15:07:00 ep11连接器

Service 引入主要是解决方案 Pod 的动态变化,提供统一访问入口:

  1. 防止 Pod 失去联系,准备找到提供的服务 Pod (服务发现)
  2. 定义一组 Pod 的访问策略 (负载均衡)

部署 deploy

kubectl apply -f deploy.yaml
apiVersion: apps/v1 kind: Deployment metadata:   name: chiyi-nginx spec:   replicas: 3   selector:     matchLabels:       app: chiyi-nginx   template:     metadata:       labels:         app: chiyi-nginx     spec:       containers:       - name: nginx         image: nginx:1.14.2         ports:         - containerPort: 80

部署 service

kubectl apply -f service.yaml
apiVersion: v1 kind: Service metadata:   name: chiyi-nginx spec:   selector:     app: chiyi-nginx   ports:     - protocol: TCP       port: 80       targetPort: 80       nodePort: 30002   type: NodePort

查看 service 和 pod 的关系

kubectl get ep curl 10.244.1.58:80

说明:

Service 一组通过标签关联 Pod

Service 为一组 Pod 提供负载平衡能力

[root@k8s-master service]# kubectl get ep NAME          ENDPOINTS                                      AGE chiyi-nginx   10.244.1.58:80,10.244.1.59:80,10.244.2.46:80   5m19s kubernetes    172.17.28.225:6443                             23h [root@k8s-master service]# curl 10.244.1.58:80    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.

查看 service

kubectl get service curl 10.101.104.218
[root@k8s-master service]# kubectl get service NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE chiyi-nginx   NodePort    10.101.104.218           80:30002/TCP   6m3s kubernetes    ClusterIP   10.96.0.1                443/TCP        23h [root@k8s-master service]# curl 10.101.104.218    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.

查看端口

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

导出 yaml

kubectl get service chiyi-nginx -o yaml

筛选 service 关联 pod

kubectl get pods -l app=chiyi-nginx
[root@k8s-master service]# kubectl get pods -l app=chiyi-nginx NAME                           READY   STATUS    RESTARTS   AGE chiyi-nginx-5bbf8bff4b-6bwfz   1/1     Running   0          3m58s chiyi-nginx-5bbf8bff4b-bpvvc   1/1     Running   0          3m58s chiyi-nginx-5bbf8bff4b-pwwt4   1/1     Running   0          3m58s

扩容测试

kubectl scale deployment chiyi-nginx --replicas=1  kubectl get service,pods,ep

Service 三种常见类型

  • ClusterIP 任何节点服务器和集群内部使用 pod 内部可访问
  • NodePort 任何节点服务器公网(默认端口范围:3000-32767)IP 浏览器可以访问端口号。
  • LoadBalancer 适用于公共云
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章