k8s从入门到放弃之Service负载均衡

k8s从入门到放弃之Service负载均衡

在 Kubernetes (K8s) 中,Service 是一种抽象,它定义了一组逻辑上的 Pod 和访问它们的策略。Service 的主要目的是提供一种可靠的方式来访问一组具有相同标签(Label)的 Pod,即使这些 Pod 可能会在集群中被动态地创建或销毁。

Service的核心功能

  • 服务发现:通过 DNS 或者环境变量的方式,让其他应用可以找到并访问到这个 Service。
  • 负载均衡:将到达 Service 的请求分发给后端的多个 Pod 实例。
  • 稳定的 IP 地址和 DNS 名称:为一组 Pod 提供一个固定的 IP 地址和 DNS 名称,即使后端的 Pod 发生了变化,如重启、扩展等操作。
  • 流量代理:通过 kube-proxy 组件实现对流量的转发,支持多种代理模式,包括 userspace、iptables 和 IPVS。

Service的类型

  • ClusterIP:默认类型,通过集群内部 IP 暴露服务,只能从集群内部访问。
  • NodePort:通过每个节点的 IP 和静态端口暴露服务,允许外部流量通过节点 IP 和 NodePort 访问服务。
  • LoadBalancer:通常用于云环境中,自动创建一个外部负载均衡器,并分配一个外部 IP 来暴露服务。

ClusterIP案例

资源文档kubectl explain Service.spec

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.20.0
          readinessProbe:
            tcpSocket:
              port: 80
          resources:
            limits:
              cpu: "100m"
          ports:
            - containerPort: 80
---

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: ClusterIP
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

NodePort案例

资源文档kubectl explain Service.spec

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.20.0
          readinessProbe:
            tcpSocket:
              port: 80
          resources:
            limits:
              cpu: "100m"
          ports:
            - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

查看集群外部访问地址

复制代码
[root@master /]# kubectl get svc 
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        20d
nginx-svc    NodePort    10.107.140.239   <none>        80:32017/TCP   2m16s

LoadBalancer案例

资源文档 kubectl explain Service.spec

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.20.0
          readinessProbe:
            tcpSocket:
              port: 80
          resources:
            limits:
              cpu: "100m"
          ports:
            - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

查看集群外部访问地址

复制代码
[root@master /]# kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>         443/TCP        20d
nginx-svc    LoadBalancer   10.101.116.99   10.244.2.240   80:30143/TCP   2m40s

Service 是 Kubernetes 中非常重要的概念之一,它使得应用程序可以在分布式系统中进行有效的通信和协作。通过合理配置 Service,可以确保应用即使在高动态性的容器化环境中也能保持良好的可用性和可访问性。

相关推荐
lichenyang4534 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4534 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang4534 天前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
运维开发故事6 天前
基于 Arthas 的多集群在线诊断系统设计与实现
kubernetes
Patrick_Wilson8 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
探索云原生9 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
云恒要逆袭9 天前
运行你的第一个Docker容器
后端·docker·容器
Java之美10 天前
一次k8s升级引发的DevicePlugin注册失败
云原生·kubernetes
程序员老赵10 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程