K8S Service
2021/7/1 23:26:31
本文主要是介绍K8S Service,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 Service的作用
- 服务发现,防止Pod失联
- 定义Pod的访问策略,负载均衡
本质是创建Servicer后Kube-proxy生成一套访问规则存储到ETCD,本质生效的是这套访问规则,Service只是表象!
2 Pod与Service关联
Service与Pod,就像Deployment与Pod的关联一样,都是通过labels进行关联。
3 常用Service类型
kubectl expose --help
ClusterIP:集群的内部使用
NodePort:用于对外暴露端口进行访问
LoadBalancer:对外访问应用使用,公有云
3.1 创建ClusterIP的Service实例
先创建Controller控制的Pod
vim web-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web name: web spec: replicas: 1 selector: matchLabels: app: web strategy: {} template: metadata: creationTimestamp: null labels: app: web spec: containers: - image: nginx name: nginx resources: {} status: {}
kubectl apply -f web-deployment.yaml
创建
vim web-service.yaml
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: web name: web-service spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web status: loadBalancer: {}
Service默认即是ClusterIP
kubectl apply -f web-service.yaml kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d22h web-service ClusterIP 10.98.145.89 <none> 80/TCP 5s
到任意节点访问
curl 10.98.145.89
3.2 创建NodePort的Service
vim web1-service.yaml
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: web name: web1-service spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web type: NodePort status: loadBalancer: {}
kubectl apply -f web1-service.yaml kubectl get svc
这篇关于K8S Service的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-2610 个你不知道自己需要的 Kubernetes 工具
- 2024-09-25MicroK8s 概览 — 使用一条命令部署自己的 Kubernetes 集群
- 2024-08-19云原生周刊:Kubernetes v1.31 发布
- 2024-08-13探索 Kubernetes 持久化存储之 Rook Ceph 初窥门径
- 2024-07-25探索 Kubernetes 持久化存储之 Longhorn 初窥门径
- 2024-07-23云原生周刊:Kubernetes v1.31 中的移除和主要变更|2024.7.22
- 2024-07-22Kubernetes 持久化存储之 NFS 终极实战指南
- 2024-07-15云原生周刊:Argo Rollouts 支持 Kubernetes Gateway API 1.0 | 2024.7.1
- 2024-07-15云原生周刊:一条 Kubernetes 命令引发的悲剧
- 2024-07-15K8s GPU 资源管理探索:在 KubeSphere 上部署 AI 大模型 Ollama