Kubernetes Service 之 LoadBalancer

Kubernetes 之 LoadBalancer

定义

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

使用

安装 MetalLB

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.7/config/manifests/metallb-native.yaml

配置 ARP IP 池

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
  namespace: metallb-system
spec:
  addresses:
    - 192.168.31.70-192.168.31.90
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 语言编写的 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

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>

相关推荐

  1. Kubernetes Service LoadBalancer

    2024-07-20 03:48:03       19 阅读
  2. LoadBalancer 替换 Ribbon

    2024-07-20 03:48:03       46 阅读
  3. 负载均衡 LoadBalancer

    2024-07-20 03:48:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 03:48:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 03:48:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 03:48:03       45 阅读
  4. Python语言-面向对象

    2024-07-20 03:48:03       55 阅读

热门阅读

  1. 第五节 LLaVA模型lora推理模型解读(下篇)

    2024-07-20 03:48:03       17 阅读
  2. element ui 怎么调整table的行高

    2024-07-20 03:48:03       21 阅读
  3. vscode使用技巧及问题 VSCode/bugs/如何给VSCode降级

    2024-07-20 03:48:03       17 阅读
  4. python selenium4 EdgeDriver动态页面爬取

    2024-07-20 03:48:03       23 阅读
  5. 白骑士的C++教学高级篇 3.2 多线程与并发

    2024-07-20 03:48:03       23 阅读
  6. gpg-agent

    2024-07-20 03:48:03       19 阅读
  7. 举一个产生Redis分布式锁死锁的场景。

    2024-07-20 03:48:03       14 阅读
  8. 蒙特卡洛模拟

    2024-07-20 03:48:03       16 阅读
  9. 关于Flutter的build

    2024-07-20 03:48:03       14 阅读