Kubernetes Service 之 LoadBalancer

Kubernetes 之 LoadBalancer

定义

负载均衡器 (LoadBalancer) 是 Kubernetes 中用来对外暴露 Service 服务的,它可以将服务集中到一个公共 IP 上。我们常用 MetalLB 作为自建均衡器。

使用

安装 MetalLB

shell 复制代码
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.7/config/manifests/metallb-native.yaml

配置 ARP IP 池

yaml 复制代码
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
  namespace: metallb-system
spec:
  addresses:
    - 192.168.31.70-192.168.31.90
shell 复制代码
kubectl apply -f metalb-ips.yml

导入新镜像statis-web-1.0.90.tar

复制代码
ctr -n=k8s.io images import static-web-1.0.90.tar
go 复制代码
// go 语言编写的 web static 镜像代码
package main

import (
	"fmt"
	"net/http"
	"os"
)

func main() {
	htmlContent := os.Getenv("HTML_BODY_CONTENT")
	if htmlContent == "" {
		htmlContent = "<html><body><h1>No Content Set</h1></body></html>"
	} else {
		htmlContent = "<html><body><h1>" + htmlContent + "</h1></body></html>"
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprint(w, htmlContent)
	})

	port := ":80"
	fmt.Printf("Server is listening on port %s\n", port)
	err := http.ListenAndServe(port, nil)
	if err != nil {
		fmt.Printf("Error starting server: %v\n", err)
	}
}

创建测试 Deployment 和 Service

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-web-static
  namespace: default
  labels:
    app: deployment-web-static
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pod-web-static
  template:
    metadata:
      labels:
        app: pod-web-static
    spec:
      containers:
        - name: web-static
          image: static-web:1.0.90
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
          env:
            - name: HTML_BODY_CONTENT
              value: "This is a test"
---
apiVersion: v1
kind: Service
metadata:
  name: service-static-web
  namespace: default
  labels:
    app: service-static-web
spec:
  type: LoadBalancer
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: pod-web-static

测试

复制代码
root@k8s-master1:~# kubectl get service
NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
kubernetes           ClusterIP      10.96.0.1       <none>          443/TCP        67d
service-static-web   LoadBalancer   10.102.54.255   192.168.31.70   80:30487/TCP   3m13s
root@k8s-master1:~# curl 192.168.31.70
<html><body><h1>This is a test</h1></body></html>
相关推荐
IT摆渡者8 分钟前
基于 Docker 部署 Nextcloud 私有云盘(外挂 Windows 共享)项目实践
运维·docker·容器
Jeromefromhk18 分钟前
我把所有服务搬上 Kubernetes,又全部搬了回来
kubernetes·devops
墨香幽梦客2 小时前
Kubernetes+Istio实现CRM系统弹性伸缩:服务网格下的流量治理与熔断策略
容器·kubernetes·istio
新时代牛马2 小时前
单机 docker run 管不住了?从编排需求、期望状态到调度/自愈闭环讲透
运维·docker·容器
binqian2 小时前
Docker Desktop(WSL2 后端)三层网络互通技术文档
网络·docker·容器
闲云野鹤在人间3 小时前
Docker入门|第1章 容器生态系统完整解析
运维·服务器·docker·容器·centos
2601_962218474 小时前
万象生鲜系统协议价底层架构实现生鲜企业报价业务数字化管理
大数据·运维·微服务·云原生·架构
KIDULT°5 小时前
Docker 从 0 入门|吃透容器生态、架构与 Dockerfile 实战
docker·容器·架构
putItInYourHand6 小时前
Docker Compose 部署 Mosquitto MQTT Broker 完整实战指南
运维·docker·容器
生活爱好者!15 小时前
哇!影视新玩法!docker部署NEOWATCH NAS
运维·docker·容器