关于k8s 使用 Service 控制器对外暴露服务的问题
目录
- 部署 deploy
- 部署 service
- 查看 service 和 pod 的关系
- 查看 service
- 查看端口
- 导出 yaml
- 筛选 service 关联 pod
- 扩容测试
- Service 三种常用类型
Service 引入主要是解决 Pod 的动态变化,提供统一访问入口:
- 防止 Pod 失联,准备找到提供同一个服务的 Pod (服务发现)
- 定义一组 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:80Welcome 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.21880: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 对外暴露应用(端口默认范围:30000-32767),任一节点服务器公网IP+端口号,可在浏览器访问。
- LoadBalancer 对外暴露应用,适合公有云
您可能感兴趣的文章
- 12-20Kubernetes中使用临时容器进行故障排查的方法
- 12-20Nginx设置HTTPS的方法步骤
- 12-20二进制方式安装 Kubernetes1.18.3版本实现脚本
- 12-20Nginx工作模式及代理配置的使用细节
- 12-20ZooKeeper分布式协调服务设计核心概念及安装配置
- 12-20Kubernetes部署可视化地图的十个步骤
- 12-20关于docker清理Overlay2占用磁盘空间的问题(亲测有效)
- 12-20Docker compose配置文件写法及命令使用示例
- 12-20openwrt安装docker并启动的操作方法
- 12-20云原生Kubernetes初始化容器Init使用教程


阅读排行
推荐教程
- 12-07一文教你怎么选择Tomcat对应的JDK版本
- 12-07新版Eclipse集成Tomcat时找不到server选项的解决方法
- 12-06IIS7 应用程序池自动回收关闭的解决方案
- 12-05Windows Server 2019安装VMware
- 12-05Windows服务器默认IE浏览器无法下载文件的解决方法
- 12-05Docker安装Jenkins全过程
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法





