Raspberry Pi(arm64)のKubernetesにElasticsearchとKibana環境を構築する

![Raspberry_Pi_OS-2020--05--27--raspios--buster--arm64] (https://img.shields.io/badge/Raspberry_Pi_OS-2020--05--27--raspios--buster--arm64-brightgreen) ![Kubernetes-v1.18.3](https://img.shields.io/badge/Kubernetes-v1.18.3-brightgreen) ![Minikube-v1.12.1](https://img.shields.io/badge/Minikube-v1.12.1-brightgreen) ![Helm-v3.3.1](https://img.shields.io/badge/Helm-v3.3.1-brightgreen) ![Elasticsearch-7.9.1](https://img.shields.io/badge/Elasticsearch-7.9.1-brightgreen) ![Kibana-7.9.1](https://img.shields.io/badge/Kibana-7.9.1-brightgreen)

arm64向けのKibanaのコンテナイメージの作成に成功したので、Kubernetes上で構築してみました。
（コンテナイメージの作成方法とDocker Composeでの構築は[こちら](https://qiita.com/moritalous/items/2d057354b6578d240858)を参照）

# Helmのインストール

```console
wget https://get.helm.sh/helm-v3.3.1-linux-arm64.tar.gz
tar zxvf helm-v3.3.1-linux-arm64.tar.gz
cd linux-arm64/
sudo install helm /usr/local/bin
```

動作確認

```console
 $ helm version
version.BuildInfo{Version:"v3.3.1", GitCommit:"249e5215cde0c3fa72e27eb7a30e8d55c9696144", GitTreeState:"clean", GoVersion:"go1.14.7"}
 $ 
```

# HelmのElasticリポジトリ設定

Elastic Stack Kubernetes Helm Charts
https://github.com/elastic/helm-charts

```console
helm repo add elastic https://helm.elastic.co
```

```console
$ helm search repo elastic
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                                       
elastic/elasticsearch   7.9.1           7.9.1           Official Elastic helm chart for Elasticsearch     
elastic/apm-server      7.9.1           7.9.1           Official Elastic helm chart for Elastic APM Server
elastic/filebeat        7.9.1           7.9.1           Official Elastic helm chart for Filebeat          
elastic/kibana          7.9.1           7.9.1           Official Elastic helm chart for Kibana            
elastic/logstash        7.9.1           7.9.1           Official Elastic helm chart for Logstash          
elastic/metricbeat      7.9.1           7.9.1           Official Elastic helm chart for Metricbeat        
 $ 
```

# Elasticsearch環境の構築

## PersistentVolumeの作成

Helmのインストールの前にPersistentVolumeを作成します。

```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: es-pv-volume
  labels:
    type: local
    app: elasticsearch-master
spec:
  storageClassName: standard
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Delete
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/elasticsearch"
```

/data/elasticsearchディレクトリを自動生成させると、rootユーザーが所有者となってPodの起動が失敗しましたので、先にホストOS側で準備しておきます

```console
mkdir /data/elasticsearch
chown 1000:1000 /data/elasticsearch
```

# Elasticsearch Helm Chartのインストール

minikube環境向けのサンプルがありますので、そちらを元にしました。`imageTag`と`replicas`だけ独自に足しました。

https://github.com/elastic/helm-charts/tree/master/elasticsearch/examples/minikube

```yaml
---
imageTag: 7.9.1

replicas: 1

# Permit co-located instances for solitary minikube virtual machines.
antiAffinity: "soft"

# Shrink default JVM heap.
esJavaOpts: "-Xmx128m -Xms128m"

# Allocate smaller chunks of memory per pod.
resources:
  requests:
    cpu: "100m"
    memory: "512M"
  limits:
    cpu: "1000m"
    memory: "512M"

# Request smaller persistent volumes.
volumeClaimTemplate:
  accessModes: [ "ReadWriteOnce" ]
  storageClassName: "standard"
  resources:
    requests:
      storage: 100M
```

```console
helm install elasticsearch elastic/elasticsearch --values ./values.yml
```

うまく行けば一式起動します

```
 $ kubectl get service,statefulset,pod,persistentvolumeclaim,persistentvolume --namespace=default -l app=elasticsearch-master
NAME                                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
service/elasticsearch-master            ClusterIP   10.103.52.98   <none>        9200/TCP,9300/TCP   6h38m
service/elasticsearch-master-headless   ClusterIP   None           <none>        9200/TCP,9300/TCP   6h38m

NAME                                    READY   AGE
statefulset.apps/elasticsearch-master   1/1     6h38m

NAME                         READY   STATUS    RESTARTS   AGE
pod/elasticsearch-master-0   1/1     Running   2          6h38m

NAME                                                                STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/elasticsearch-master-elasticsearch-master-0   Bound    es-pv-volume   1Gi        RWO            standard       6h38m

NAME                            CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                                 STORAGECLASS   REASON   AGE
persistentvolume/es-pv-volume   1Gi        RWO            Delete           Bound    default/elasticsearch-master-elasticsearch-master-0   standard                6h43m
 $ 
```

# Kibana環境の構築

# Kibana Helm Chartのインストール

Kibanaもvalues.ymlを用意します。

Kibanaはarm64向けコンテナイメージが提供されていないので、自前で作成しました。
（詳しい説明は[こちら](https://qiita.com/moritalous/items/2d057354b6578d240858)）

```yaml
---
image: "ghcr.io/moritalous/ghcr/kibana-arm64"
imageTag: 7.9.1

service:
  type: NodePort
  port: 5601
  nodePort: 30601
```

```console
helm install kibana elastic/kibana --values ./values.yml
```

起動確認

```console
$ kubectl get service,deployment,pod --namespace=default -l app=kibana
NAME                    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/kibana-kibana   NodePort   10.109.131.74   <none>        5601:30601/TCP   8h

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/kibana-kibana   1/1     1            1           8h

NAME                                 READY   STATUS    RESTARTS   AGE
pod/kibana-kibana-56b7b6c95c-vj6sv   1/1     Running   0          7h37m
 $ 
```

# 動作確認

`http://raspberrypi.local:30601/`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/41574/167f2011-586b-0b58-186f-30c1a27e76f9.png)