K8S的Service的LoadBanlance之Metallb解决方案
目录
一.部署metallb
1.metallb概述
如果我们需要在自己的Kubernetes中暴露LoadBalancer的应用,那么Metallb是一个不错的解决方案。
Metallb官网地址:
https://metallb.universe.tf/installation/
2.修改kube-proxy的configMap
# see what changes would be made, returns nonzero returncode if different
kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl diff -f - -n kube-system
# actually apply the changes, returns nonzero returncode on errors only
kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
sed -e 's#mode: ""#mode: "ipvs"#' | \
kubectl apply -f - -n kube-system
3.安装metallb
1.下载metallb
wget https://raw.githubusercontent.com/metallb/metallb/v0.13.12/config/manifests/metallb-native.yaml
2.部署metallb
kubectl apply -f metallb-native.yaml
3.查看metallb的状态
watch kubectl get all -o wide -n metallb-system
二.测试MetalLB
1.创建MetalLB地址池
可能会创建多个对外暴露的Service,所以需要分配多个没有用到的IP地址给MetalLB。
1.编写资源清单
[root@master231 ~]# cat metallb-ip-pool.yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
# 注意改为你自己为MetalLB分配的IP地址
- 172.30.1.150-172.30.1.180
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: example
namespace: metallb-system
spec:
ipAddressPools:
- first-pool
[root@master231 ~]#
2.创建地址池资源
[root@master231 ~]# kubectl apply -f metallb-ip-pool.yaml
ipaddresspool.metallb.io/first-pool created
l2advertisement.metallb.io/example created
[root@master231 ~]#
2.编写资源清单
[root@master231 ~]# cat deploy-web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20.1-alpine
ports:
- containerPort: 80
[root@master231 ~]#
3.部署服务
[root@master231 ~]# kubectl apply -f deploy-web.yaml
deployment.apps/nginx-deployment created
[root@master231 ~]#
[root@master231 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-f946c7d95-b52mx 1/1 Running 0 44s 10.100.1.12 worker232 <none> <none>
nginx-deployment-f946c7d95-qdrlj 1/1 Running 0 44s 10.100.2.11 worker233 <none> <none>
nginx-deployment-f946c7d95-w2d9l 1/1 Running 0 44s 10.100.1.13 worker232 <none> <none>
[root@master231 ~]#
4.创建svc
1.响应式创建
kubectl expose deployment nginx-deployment --name=nginx-svc --port=80 --target-port=80 --protocol=TCP --type=LoadBalancer
2.声明式创建
[root@master231 ~]# cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
type: LoadBalancer
ports:
- nodePort: 30080
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
[root@master231 ~]#
5.测试访问
温馨提示:
如果你的window系统和EXTERNAL-IP暴露的IP地址在同一个网段的话,也是可以直接访问的哟。
1.同集群测试访问
[root@master231 ~]# kubectl get svc nginx-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc LoadBalancer 10.200.107.242 172.30.1.150 80:31342/TCP 25s
[root@master231 ~]#
[root@master231 ~]# curl -I 172.30.1.150
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sun, 05 Nov 2023 22:44:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 25 May 2021 13:41:16 GMT
Connection: keep-alive
ETag: "60acfe7c-264"
Accept-Ranges: bytes
[root@master231 ~]#
2.windows访问
http://10.0.0.233:31342/
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/17811466.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。