MetalLB를 설치하고 설정하여 LoadBalancer 서비스를 테스트하는 방법
MetalLB를 설치하고 설정하여 LoadBalancer 서비스를 테스트하는 방법
MetalLB는 쿠버네티스(Kubernetes) 클러스터에 LoadBalancer 서비스를 제공하기 위한 네트워크 부하 분산기입니다. MetalLB는 일반적으로 클라우드에서 제공되는 네이티브 LoadBalancer 서비스 대신 온프레미스(On-Premises) 또는 클라우드에 독립적인 환경에서 동작하는 LoadBalancer 기능을 제공합니다.
테스트 환경
kubectl get nodes -o custom-columns="NAME:.metadata.name,STATUS:.status.conditions[-1].type,ROLES:.metadata.labels['kubernetes\.io/role'],AGE:.metadata.creationTimestamp,VERSION:.status.nodeInfo.kubeletVersion,INTERNAL-IP:.status.addresses[0].address,EXTERNAL-IP:.status.addresses[1].address"
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP
control-plane Ready <none> 2024-08-21T12:04:32Z v1.30.4 192.168.10.111 control-plane
worker1 Ready <none> 2024-08-21T12:19:20Z v1.30.4 192.168.10.114 worker1
ARP 모드 활성화
kube-proxy 구성 편집
- mode: "ipvs"
- strictARP: true(false -> true)
kubectl edit configmap -n kube-system kube-proxy
mode: ""
...
ipvs:
...
strictARP: false
mode: "ipvs"
...
ipvs:
...
strictARP: true
ARP(strictARP) 모드 확인
kubectl get configmap kube-proxy -n kube-system -o yaml | grep strictARP
1. MetalLB 설치
MetalLB Namespace 및 구성 설치
- MetalLB를 설치하기 위해 필요한 Namespace 및 기본 리소스를 생성합니다.
- MetalLB의 Namespace와 데몬셋을 클러스터에 배포합니다.
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/main/config/manifests/metallb-native.yaml
---
namespace/metallb-system created
customresourcedefinition.apiextensions.k8s.io/bfdprofiles.metallb.io created
customresourcedefinition.apiextensions.k8s.io/bgpadvertisements.metallb.io created
customresourcedefinition.apiextensions.k8s.io/bgppeers.metallb.io created
customresourcedefinition.apiextensions.k8s.io/communities.metallb.io created
customresourcedefinition.apiextensions.k8s.io/ipaddresspools.metallb.io created
customresourcedefinition.apiextensions.k8s.io/l2advertisements.metallb.io created
customresourcedefinition.apiextensions.k8s.io/servicel2statuses.metallb.io created
serviceaccount/controller created
serviceaccount/speaker created
role.rbac.authorization.k8s.io/controller created
role.rbac.authorization.k8s.io/pod-lister created
clusterrole.rbac.authorization.k8s.io/metallb-system:controller created
clusterrole.rbac.authorization.k8s.io/metallb-system:speaker created
rolebinding.rbac.authorization.k8s.io/controller created
rolebinding.rbac.authorization.k8s.io/pod-lister created
clusterrolebinding.rbac.authorization.k8s.io/metallb-system:controller created
clusterrolebinding.rbac.authorization.k8s.io/metallb-system:speaker created
configmap/metallb-excludel2 created
secret/metallb-webhook-cert created
service/metallb-webhook-service created
deployment.apps/controller created
daemonset.apps/speaker created
validatingwebhookconfiguration.admissionregistration.k8s.io/metallb-webhook-configuration created
---
kubectl get all -n metallb-system
NAME READY STATUS RESTARTS AGE
pod/controller-7bcd9b5f47-vg99x 1/1 Running 0 42s
pod/speaker-jkqzk 1/1 Running 0 42s
pod/speaker-jmbgw 1/1 Running 0 42s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/metallb-webhook-service ClusterIP 10.101.60.213 <none> 443/TCP 42s
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/speaker 2 2 2 2 2 kubernetes.io/os=linux 42s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/controller 1/1 1 1 42s
NAME DESIRED CURRENT READY AGE
replicaset.apps/controller-7bcd9b5f47 1 1 1 42s
2. MetalLB 설정
MetalLB는 IP 주소 풀을 정의하는 ConfigMap을 통해 작동합니다. 이 ConfigMap을 사용하여 IP 주소 범위를 지정하고 MetalLB가 사용할 수 있는 IP 주소를 정의합니다.
IP 주소 풀 설정
MetalLB를 사용하여 IP 주소 풀을 설정합니다. 이 파일을 metallb-config.yaml로 저장합니다.
여기서 addresses는 MetalLB가 사용할 수 있는 IP 주소 범위를 정의합니다. 이 범위는 클러스터가 배치된 네트워크의 실제 IP 주소 범위와 일치해야 합니다. IP 주소 범위는 실제 네트워크 환경에 맞게 조정해야 합니다.
cat <<EOF | kubectl apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: metallb-ip-pool
namespace: metallb-system
labels:
ipaddresspool: metallb-pool
spec:
addresses:
- 192.168.10.201-192.168.10.220
autoAssign: true
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: metallb-l2-advertisement
namespace: metallb-system
EOF
3. LoadBalancer 서비스 테스트
이제 MetalLB가 설정되었으므로 LoadBalancer 서비스를 생성하여 제대로 작동하는지 테스트합니다.
NGINX 웹 서버를 배포하고 LoadBalancer 서비스를 설정하여 MetalLB를 통해 외부 IP를 할당받습니다:
Deployment 생성(Nginx 생성)
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
EOF
LoadBalancer 서비스 생성
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: nginx-loadbalancer
namespace: default
labels:
app: nginx
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
EOF
서비스 상태 확인
- 서비스가 생성된 후 MetalLB가 외부 IP를 할당한 것을 확인할 수 있습니다.
kubectl get svc nginx-loadbalancer
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-loadbalancer LoadBalancer 10.102.107.238 192.168.10.201 80:32454/TCP 2m4s
4. 테스트
브라우저에서 또는 curl 명령어를 사용하여 할당된 IP 주소로 NGINX 웹 서버에 접근합니다.
curl http://<EXTERNAL-IP>
curl 192.168.10.201
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
참고URL
- MetalLB : Installation
- GitHub repo : metallb
- MetalLB : Release Notes