MetalLB インストール

# 概要
 - k8sテスト環境構築<br>
<b>MetalLB インストール</b>

# 構築目次
 - [全体目次] (https://qiita.com/prodigy413/items/89fcb8cfb71e80f541d8)

# 環境
 - Rancher: v2.6.3
 - kubernetes(Client): v1.22.4
 - kubernetes(Server): v1.22.4
 - MetalLB: v0.11.0

# インストール
 - 作業場所: ClientPC
 - MetalLB Install Page<br>
https://metallb.universe.tf/installation/
 - Configmap設定<br>
https://metallb.universe.tf/configuration/#layer-2-configuration

### インストール
~~~
## Namespace 作成
$ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/namespace.yaml

## metallb インストール
$ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/metallb.yaml


## 確認 ##
$ kubectl -n metallb-system get all
NAME                              READY   STATUS    RESTARTS   AGE
pod/controller-7dcc8764f4-hq8rd   1/1     Running   0          3m34s
pod/speaker-g5289                 1/1     Running   0          3m34s
pod/speaker-p8xkk                 1/1     Running   0          3m34s
pod/speaker-psq52                 1/1     Running   0          3m34s

NAME                     DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
daemonset.apps/speaker   3         3         3       3            3           kubernetes.io/os=linux   3m34s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/controller   1/1     1            1           3m34s

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/controller-7dcc8764f4   1         1         1       3m34s
~~~

### Configmap 作成
 - LoadBalancerに割り当てるIPを設定<br>
(<b>192.168.245.111 - 192.168.245.120</b>)  
※「<b>metadata.name</b>」は「<b>config</b>」固定

~~~yaml:metallb-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.245.111-192.168.245.120
~~~

 - Configmap 適用

~~~
$ kubectl apply -f metallb-configmap.yaml
~~~

 - Load Balancer 確認<br>
[`Nginx Ingress インストール`](https://qiita.com/prodigy413/items/b26bac84bbb4555ac8f0)で「pending」状態だったEXTERNAL-IPのIPを確認

~~~
$ kubectl get svc -n ingress-system
NAME                                          TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)                      AGE
obi-test-ingress-nginx-controller             LoadBalancer   10.43.58.216    192.168.245.111   80:30769/TCP,443:30528/TCP   3h18m
obi-test-ingress-nginx-controller-admission   ClusterIP      10.43.146.222   <none>            443/TCP                      3h18m
~~~

### 動作確認
※NGINX Ingressを利用するため、Ingressのインストールが必要
リンク：[`Nginx Ingress インストール`](https://qiita.com/prodigy413/items/b26bac84bbb4555ac8f0)

 - マニフェスト作成

~~~yaml:test-metallb.yaml
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: nginx.test.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.21.4
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
~~~

 - マニフェスト適用

~~~
$ kubectl apply -f test-metallb.yaml
~~~

 - hosts 設定追加  
Load BalancerのIPとIngressで設定したhostを紐づけ

~~~
$ cat /etc/hosts
........
192.168.245.111 nginx.test.local
........
~~~

 - 接続確認

~~~
$ curl -I http://nginx.test.local
HTTP/1.1 200 OK
.........
.........
~~~