KubernetesにてLoad balancerを構築

## installation
https://metallb.universe.tf/installation/
$ kubectl edit configmap -n kube-system kube-proxy

Enter the text below
```
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
  strictARP: true
```

以下のコマンドを実行する事によって、上記の設定が出来る。
```
$ kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl diff -f - -n kube-system

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

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

#### shell scriptでもOK
```=bash
#!/bin/bash
export KUBECONFIG=/etc/kubernetes/admin.conf
source ~/.bashrc
kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl diff -f - -n kube-system

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

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

## creating congmap, IPAddressPool, L2ADvertisement
``` configmap
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    ipaddress-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.0.150-192.168.0.200
      autoAssign: true
```
``` ip address pool
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: default
  namespace: metallb-system
spec:
  addresses:
  - 192.168.0.150-192.168.0.200
  autoAssign: true
---
```

## creating deployment manifest
```deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-metallb
  name: nginx-metallb
spec:
  selector:
    matchLabels:
      app: nginx-metallb
  template:
    metadata:
      labels:
        app: nginx-metallb
    spec:
      containers:
      - image: nginx
        name: nginx-metallb
        ports:
        - containerPort: 80
```

## creating service manifest
``` service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-metallb
  name: nginx-metallb
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-metallb
  type: LoadBalancer
  #externalIPs:
  #- 172.24.20.61
status:
  loadBalancer: {}
```


By creating **configgmap, IPAddressPool, L2ADvertisement**, the IP address of service Loadbalancercan get it without configuration.

## Reference Site
- https://metallb.universe.tf/installation/
- https://stackoverflow.com/questions/44110876/kubernetes-service-external-ip-pending
- https://www.mtioutput.com/entry/k8s-metallb-tutorial
-  https://blog.framinal.life/entry/2020/04/16/022042
- https://metallb.universe.tf/usage/

coredns設定例
```
apiVersion: v1
kind: Service
metadata:
  name: coredns-service-tcp
  annotations:
    metallb.universe.tf/allow-shared-ip: "coredns-service-ip"
spec:
  type: LoadBalancer
  loadBalancerIP: 172.16.0.2
  ports:
    - name: tcp
      port: 53
      targetPort: 53
      protocol: TCP
  selector:
    app: coredns
---
apiVersion: v1
kind: Service
metadata:
  name: coredns-service-udp
  annotations:
    metallb.universe.tf/allow-shared-ip: "coredns-service-ip"
spec:
  type: LoadBalancer
  loadBalancerIP: 172.16.0.2
  ports:
    - name: udp
      port: 53
      targetPort: 53
      protocol: UDP
  selector:
    app: coredns
```