2022年3月29日火曜日

Minikubeでlocal永続ボリュームを使用するClickHouseをデプロイする

Minikubeでlocal永続ボリュームを使用するClickHouseをデプロイするには、以下の手順を実行します。

実行手順

1. ClickHouseデータ格納用ディレクトリを作成
以下のコマンドでデータ格納用ディレクトリを作成します。
minikube ssh

sudo mkdir -p /var/clickhouse-data
sudo chown 101:101 /var/clickhouse-data

sudo mkdir -p /var/clickhouse-logs
sudo chown 101:101 /var/clickhouse-logs

sudo mkdir -p /var/clickhouse-config
sudo chown 101:101 /var/clickhouse-config
cat << EOF | sudo tee -a /var/clickhouse-config/myconfig.xml
<yandex>
    <listen_host>::</listen_host>
    <listen_host>0.0.0.0</listen_host>
    <listen_try>1</listen_try>
</yandex>
EOF
sudo chown 101:101 /var/clickhouse-config/*


sudo mkdir -p /var/clickhouse-users
sudo chown 101:101 /var/clickhouse-users

exit

2. local永続ボリュームの作成
以下のコマンドでlocal永続ボリュームを作成します。
〇データ用
cat << EOF > local-clickhouse-data-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-clickhouse-data-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /var/clickhouse-data
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube
EOF

kubectl apply -f ./local-clickhouse-data-pv.yml

〇ログ用
cat << EOF > local-clickhouse-logs-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-clickhouse-logs-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /var/clickhouse-logs
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube
EOF

kubectl apply -f ./local-clickhouse-logs-pv.yml

〇設定用
cat << EOF > local-clickhouse-config-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-clickhouse-config-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /var/clickhouse-config
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube
EOF

kubectl apply -f ./local-clickhouse-config-pv.yml

〇ユーザ用
cat << EOF > local-clickhouse-users-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-clickhouse-users-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage
  local:
    path: /var/clickhouse-users
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube
EOF

kubectl apply -f ./local-clickhouse-users-pv.yml

3. 永続ボリューム要求の作成
以下のコマンドで永続ボリューム要求を作成します。
〇データ用
cat << EOF > local-clickhouse-data-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-clickhouse-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 5Gi
  volumeName: local-clickhouse-data-pv
EOF

kubectl apply -f ./local-clickhouse-data-pvc.yml

〇ログ用
cat << EOF > local-clickhouse-logs-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-clickhouse-logs-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 5Gi
  volumeName: local-clickhouse-logs-pv
EOF

kubectl apply -f ./local-clickhouse-logs-pvc.yml

〇設定用
cat << EOF > local-clickhouse-config-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-clickhouse-config-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 5Gi
  volumeName: local-clickhouse-config-pv
EOF

kubectl apply -f ./local-clickhouse-config-pvc.yml

〇ユーザ用
cat << EOF > local-clickhouse-users-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: local-clickhouse-users-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  resources:
    requests:
      storage: 5Gi
  volumeName: local-clickhouse-users-pv
EOF

kubectl apply -f ./local-clickhouse-users-pvc.yml

4. Deploymentの作成
以下のコマンドでClickHouseのイメージと作成したlocal永続ボリュームを使用するDeploymentを作成します。
cat << EOF > clickhouse-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: clickhouse-deployment
  labels:
    app: myclickhouse
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myclickhouse
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: myclickhouse
    spec:
      containers:
      - name: myclickhouse
        image: clickhouse/clickhouse-server
        ports:
        - containerPort: 9000
        - containerPort: 8123
        volumeMounts:
        - name: clickhouse-data
          mountPath: /var/lib/clickhouse
        - name: clickhouse-logs
          mountPath: /val/log/clickhouse-server
        - name: clickhouse-config
          mountPath: /etc/clickhouse-server/config.d
        - name: clickhouse-users
          mountPath: /etc/clickhouse-server/users.d
      volumes:
      - name: clickhouse-data
        persistentVolumeClaim:
          claimName: local-clickhouse-data-pvc
      - name: clickhouse-logs
        persistentVolumeClaim:
          claimName: local-clickhouse-logs-pvc
      - name: clickhouse-config
        persistentVolumeClaim:
          claimName: local-clickhouse-config-pvc
      - name: clickhouse-users
        persistentVolumeClaim:
          claimName: local-clickhouse-users-pvc
EOF

kubectl apply -f ./clickhouse-deployment.yml

5. サービスの作成
以下のコマンドでサービスを作成します。
cat << EOF > clickhouse-service.yml
apiVersion: v1
kind: Service
metadata:
  name: clickhouse-service
spec:
  selector:
    app: myclickhouse
  ports:
  - name: native
    protocol: TCP
    port: 9000
    targetPort: 9000
  - name: httpinterface
    protocol: TCP
    port: 8123
    targetPort: 8123
EOF

kubectl apply -f ./clickhouse-service.yml

6. Port-Fowardingで外部からアクセスできるようにする
kubectl port-forward --address 0.0.0.0 service/clickhouse-service 9000:9000 8123:8123

7. (クライアント側)clickhouse-clientのインストール(Ubuntu 20.04/Debianなど)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv E0C56BD4

echo "deb http://repo.yandex.ru/clickhouse/deb/stable/ main/" | sudo tee -a /etc/apt/sources.list.d/clickhouse.list

sudo apt-get update

sudo apt-get -y install clickhouse-client

8. (クライアント側)サンプルテーブルの作成
192.168.1.1の部分はMinikubeのホスト名またはIPに読み替えてください
clickhouse-client -h 192.168.1.1 --query="CREATE TABLE test (test_date Date, test_id UInt16, test_message String) ENGINE = MergeTree(test_date, (test_id), 8192);"

cat << EOF > /tmp/test.csv
2022-01-01,100,hello world!
2022-01-02,200,test data
EOF

cat /tmp/test.csv | clickhouse-client -h 192.168.1.1 --query="INSERT INTO test FORMAT CSV"

clickhouse-client -h 192.168.1.1 --query="SELECT * FROM test"

9. ブラウザからのアクセス
ブラウザからhttp://<サーバ名またはIPアドレス>:8123にアクセスします。

0 件のコメント:

コメントを投稿