WSL2(kind)+metallb+traefik構成を試す


WSL2(kind)+metallb+traefik構成を試す

## kindのインストールと立ち上げ

下記コマンドを実行する。今のところ、kind-workerを2個以上立ち上げているとmetallbが起動しないので注意。

```
go install sigs.k8s.io/kind@v0.12.0
kind create cluster --config=kind-traefik.yaml
```

```yaml:kind-traefik.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  # traefukはcontainerPort80で公開できないので、containerPortを8000にする
  - containerPort: 8000
    hostPort: 8000
    protocol: TCP
  # dashboard用
  - containerPort: 9000
    hostPort: 9000
    protocol: TCP
```

## helm:traefikの場合

metallbをinstallする。これによりloadbalancerでサービスを公開できる。
https://artifacthub.io/packages/helm/metallb/metallb

```
helm repo add metallb https://metallb.github.io/metallb
helm install metallb metallb/metallb --version 0.13.3 -f metallb.yaml 
```

```yaml:metallb.yaml 
configInline:
  address-pools:
  - name: default
    protocol: layer2
    addresses:
    - 172.19.0.0/24
```

```
> kubectl get pods -A -o wide
NAMESPACE            NAME                                         READY   STATUS    RESTARTS   AGE     IP           NODE                 NOMINATED NODE   READINESS GATES
default              metallb-controller-69bbb4669c-qjp6f          1/1     Running   0          71s     10.244.1.2   kind-worker          <none>           <none>
default              metallb-speaker-dlzk2                        1/1     Running   0          71s     172.19.0.2   kind-control-plane   <none>           <none>
default              metallb-speaker-jb599                        1/1     Running   0          71s     172.19.0.3   kind-worker          <none>           <none>
kube-system          coredns-558bd4d5db-758gp                     1/1     Running   0          8m21s   10.244.0.3   kind-control-plane   <none>           <none>
kube-system          coredns-558bd4d5db-nc24l                     1/1     Running   0          8m21s   10.244.0.4   kind-control-plane   <none>           <none>
kube-system          etcd-kind-control-plane                      1/1     Running   0          8m31s   172.19.0.2   kind-control-plane   <none>           <none>
kube-system          kindnet-bbv2x                                1/1     Running   0          8m22s   172.19.0.2   kind-control-plane   <none>           <none>
kube-system          kindnet-dkhkh                                1/1     Running   0          8m10s   172.19.0.3   kind-worker          <none>           <none>
kube-system          kube-apiserver-kind-control-plane            1/1     Running   0          8m31s   172.19.0.2   kind-control-plane   <none>           <none>
kube-system          kube-controller-manager-kind-control-plane   1/1     Running   0          8m31s   172.19.0.2   kind-control-plane   <none>           <none>
kube-system          kube-proxy-mx5qw                             1/1     Running   0          8m10s   172.19.0.3   kind-worker          <none>           <none>
kube-system          kube-proxy-p66zl                             1/1     Running   0          8m22s   172.19.0.2   kind-control-plane   <none>           <none>
kube-system          kube-scheduler-kind-control-plane            1/1     Running   0          8m31s   172.19.0.2   kind-control-plane   <none>           <none>
local-path-storage   local-path-provisioner-547f784dff-45nh9      1/1     Running   0          8m21s   10.244.0.2   kind-control-plane   <none>           <none>
```



続いて、traefikをinstallする
https://artifacthub.io/packages/helm/traefik/traefik

```
helm repo add traefik https://helm.traefik.io/traefik
helm install traefik traefik/traefik --version 10.19.5 --set ingressClass.enabled=true --set hostNetwork=true --set ports.traefik.expose=true
```

さて、ここでingressclassとingressを確認してみましょう。
```
kubectl get ingressclass -A
kubectl get ingress -A
```



```
kubectl apply -f kroki-deployment.yaml -f kroki-service.yaml
kubectl apply -f traefik-ingress.yaml
```

さて、もう一度ingressclassとingressを確認してみましょう。
```
kubectl get ingressclass -A
kubectl get ingress -A
```

ステートレスなコンテナ故にわかりやすいので、今回はkrokiを使用する。

```kroki-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  labels:
    app: kroki
  name: kroki
spec:
  replicas: 3
  selector:
    matchLabels:
      app: kroki
  template:
    metadata:
      labels:
        app: kroki
    spec:
      containers:
      - image: yuzutech/kroki
        name: kroki
        ports:
        - containerPort: 8000
      restartPolicy: Always

```

```kroki-service.yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
  labels:
    app: kroki
  name: kroki
spec:
  ports:
  - name: "8000"
    port: 8000
    targetPort: 8000
  selector:
    app: kroki
```


```traefik-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kroki
spec:
  ingressClassName: traefik
  rules:
 # hostが"localhosts"のときを除いて、port8000(ホストポート80)でkrokiバックエンドを公開
 - http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: kroki
            port:
              number: 8000
  # hostが"localhosts"のときだけ、port9000でダッシュボードを公開
  - host: "localhosts"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: traefik
            port:
              number: 9000
```

### 疎通確認

```
curl localhost/health
curl localhost/graphviz/svg/eNp9kM0KgzAQhO8-xZIHaKlX6Ulp6S-FPoDEuGhqbML6cyl594JVGyv1OMPMt8ukMiNucojg5QFUTfKRQjVVjRSbzgZQPEEFW2An0oVkQWfO0mLTx53CHalF6hsAO6kwQV4PesbIRPylOJxIiwJp_YMDOPKWD8ouQP0F6AWp5DJ1qOyqU1w9Kte6NcZgjZPrLMxJl8imH9g_8_jzecLzYSzvteLPLBgR1rNvJ7Rwmw==
```

### 参考
https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json