MetalLB 설치 요구 사항

스터디용으로 MetalLB를 Layer 2 mode로 구성하기 위해서는 Kubernetes가 ipvs mode로 설치되어 있어야한다.
만약 kubespray로 kubernetes를 구성했다면 ipvs mode로 설치되어 있다. 아래 명령어로 어떻게 설치되어 있는지 확인해본다.

# kubectl get configmaps -n kube-system kube-proxy -o yaml |grep mode
    mode: ipvs

 

MetalLB 설치 준비

MetalLB를 설치하려면, 우선 strictARPtrue로 설정되어 있어야한다.

# kubectl get configmaps -n kube-system kube-proxy -o yaml |grep strictARP
      strictARP: false

위와 같은 상태라면 아래 명령어를 사용해서 true로 변경한다.

# kubectl get configmap kube-proxy -n kube-system -o yaml | sed -e "s/strictARP: false/strictARP: true/" | kubectl apply -f - -n kube-system

 

MetalLB 설치

아래 명령어를 사용해서 MetalLB를 설치한다.

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

https://metallb.universe.tf/installation/

설치 버전이 바뀔 수 있으니 위 사이트에 들어가서 설치 방법을 확인한다.

# kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.8/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

아래와 같이 POD가 구성된 것을 확인한다.

# kubectl get pod -n metallb-system
NAME                          READY   STATUS    RESTARTS   AGE
controller-77676c78d9-2rnml   1/1     Running   0          43s
speaker-8tbd9                 1/1     Running   0          43s
speaker-gms7t                 1/1     Running   0          42s
speaker-hvdtq                 1/1     Running   0          43s
speaker-lbjvv                 1/1     Running   0          43s
speaker-n62tt                 1/1     Running   0          42s

 

Layer 2 mode 구성을 위한 IPAddressPool , L2Advertisement 구성

IPAddressPool 설정

Loadbalancer가 할당 받을 IP Pool을 설정해서 적용한다.

ipAddressPool.yaml

virt-manager에서 할당된 네트워크는 192.168.122.0/24으로

192.168.122.11 ~ 192.168.122.20의 IP를 cluster-ip-pool 이름을 정하고 Loadbalancer에 할당하도록 설정했다.

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: cluster-ip-pool
  namespace: metallb-system
spec:
  addresses:
  - 192.168.122.11-192.168.122.20
# kubectl apply -f ipAddressPool.yaml

 

L2Advertisement 설정

cluster-ip-pool의 IP를 부여하고 이를 알리도록 구성한다.

l2Advertisement.yaml

apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: cluster-l2-advertisement
  namespace: metallb-system
spec:
  ipAddressPools:
  - cluster-ip-pool
 # kubectl apply -f l2Advertisement.yaml

 

MetalLB 구성 테스트

아래와 같이 테스트를 진행할 예정이다.

  1. nginx-pod 구성
  2. nginx-pod 연결용 Loadbalancer 구성
  3. 구성된 Loadbalancer에서 호출 테스트

nginx-pod 구성

아래 파일을 구성해서 kubernetes에서 배포한다.

nginx-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx-pod
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
# kubectl apply -f nginx-pod.yaml
# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2s

 

nginx-pod 연결용 Loadbalancer 구성

nginx-pod-loadbalancer.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-pod-loadbalancer

spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: nginx-pod
# kubectl apply -f nginx-pod-loadbalancer.yaml
# kubectl get svc
NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
kubernetes               ClusterIP      10.233.0.1     <none>           443/TCP        50m
nginx-pod-loadbalancer   LoadBalancer   10.233.2.193   192.168.122.11   80:32319/TCP   3s

위와 같이 EXTERNAL-IP로 "192.168.122.11" IP가 할당된 것을 확인할 수 있다.

 

구성된 Loadbalancer에서 호출 테스트

curl 명령어로 생성된 Loadbalancer IP로 호출해본다.

# curl -I 192.168.122.11
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Mon, 11 Nov 2024 05:33:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes