ClickHouse
September 15, 2023

Create cluster ClickHouse in YC

Создадим кластер ClickHouse и подключимся к нему.

Поехали!

export FOLDER=$(yc config get folder-id)
export SG=$(yc vpc sg get cam-db --format json | jq -r '.id')
export NETWORK=$(yc vpc network get cameda-test --format json | jq -r '.id')
export SA=$(yc iam service-account get cameda-service --format json | jq -r '.id')
yc clickhouse cluster create \
  --folder-id $FOLDER \
  --name cam-clickhouse \
  --shard-name first-shard \
  --description "Test cluster" \
  --labels cam=test \
  --environment prestable \
  --version 23.8 \
  --network-id $NETWORK \
  --security-group-ids $SG \
  --host type=clickhouse,zone-id=ru-central1-a,subnet-name=subnet-a,assign-public-ip \
  --host type=clickhouse,zone-id=ru-central1-b,subnet-name=subnet-b,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-a,subnet-name=subnet-a,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-b,subnet-name=subnet-b,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-c,subnet-name=subnet-c,assign-public-ip \
  --user name=cameda,password=P@$w0rd \
  --database name=db1 \
  --clickhouse-resource-preset s3-c4-m16 \
  --clickhouse-disk-type network-ssd \
  --clickhouse-disk-size 33 \
  --zookeeper-resource-preset s3-c2-m8 \
  --zookeeper-disk-type network-ssd \
  --zookeeper-disk-size 10 \
  --cloud-storage=true \
  --websql-access=true \
  --datalens-access=true \
  --enable-sql-database-management=false \
  --enable-sql-user-management=false \
  --metrika-access=false \
  --serverless-access=false \
  --datatransfer-access=false \
  --yandexquery-access=false \
  --service-account $SA \
  --deletion-protection=false \
  --read-admin-password=true \
  --async

Второй вариант. Управление через SQL.

export FOLDER=$(yc config get folder-id)
export SG=$(yc vpc sg get cam-db --format json | jq -r '.id')
export NETWORK=$(yc vpc network get cameda-test --format json | jq -r '.id')
export SA=$(yc iam service-account get cameda-service --format json | jq -r '.id')
yc clickhouse cluster create \
  --folder-id $FOLDER \
  --name cam-clickhouse2 \
  --shard-name first-shard \
  --description "Test cluster" \
  --labels cam=test \
  --environment prestable \
  --version 23.8 \
  --network-id $NETWORK \
  --security-group-ids $SG \
  --host type=clickhouse,zone-id=ru-central1-a,subnet-name=subnet-a,assign-public-ip \
  --host type=clickhouse,zone-id=ru-central1-b,subnet-name=subnet-b,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-a,subnet-name=subnet-a,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-b,subnet-name=subnet-b,assign-public-ip \
  --host type=zookeeper,zone-id=ru-central1-c,subnet-name=subnet-c,assign-public-ip \
  --clickhouse-resource-preset s3-c4-m16 \
  --clickhouse-disk-type network-ssd \
  --clickhouse-disk-size 100 \
  --zookeeper-resource-preset s3-c2-m8 \
  --zookeeper-disk-type network-ssd \
  --zookeeper-disk-size 10 \
  --cloud-storage=true \
  --websql-access=true \
  --datalens-access=true \
  --enable-sql-database-management=true \
  --enable-sql-user-management=true \
  --metrika-access=false \
  --serverless-access=false \
  --datatransfer-access=false \
  --yandexquery-access=false \
  --service-account $SA \
  --backup-window-start 01:00:00 \
  --deletion-protection=false \
  --read-admin-password=true \
  --async

Подключение к кластеру с использованием clickhouse-client.

Для аутентификации в кластере используются логин, пароль и корневой сертификат.

# Скачиваем и устанавливаем сертификат.
sudo mkdir -p /usr/local/share/ca-certificates/Yandex && \
sudo wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
     --output-document /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt && \
sudo chmod 0655 /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt

# Устанавливаем clickhouse репозиторий.
sudo apt update && sudo apt install --yes apt-transport-https ca-certificates dirmngr && \
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754 && \
echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \
/etc/apt/sources.list.d/clickhouse.list

# Устанавливаем клиент. Вариант для Ubuntu.
sudo apt update && sudo apt install --yes clickhouse-client

# Скачиваем файл конфигурации для clickhouse-client.
mkdir --parents ~/.clickhouse-client && \
wget "https://storage.yandexcloud.net/doc-files/clickhouse-client.conf.example" \
  --output-document ~/.clickhouse-client/config.xml

# Подключаемся к Clickhouse с помощью клиента.
clickhouse-client --host rc1a-rgkneqqdjurmni46.mdb.yandexcloud.net \
                  --secure \
                  --user admin \
                  --database default \
                  --port 9440 \
                  --ask-password

Подключение к кластеру с использованием config.xml

# Скачиваем и устанавливаем сертификат.
sudo mkdir -p /usr/local/share/ca-certificates/Yandex && \
sudo wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
     --output-document /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt && \
sudo chmod 0655 /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt

# Устанавливаем clickhouse репозиторий.
sudo apt update && sudo apt install --yes apt-transport-https ca-certificates dirmngr && \
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754 && \
echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \
/etc/apt/sources.list.d/clickhouse.list

# Устанавливаем клиент. Вариант для Ubuntu.
sudo apt update && sudo apt install --yes clickhouse-client

# Скачиваем файл конфигурации для clickhouse-client.
mkdir --parents ~/.clickhouse-client && \
wget "https://storage.yandexcloud.net/doc-files/clickhouse-client.conf.example" \
  --output-document ~/.clickhouse-client/config.xml

vi ~/.clickhouse-client/config.xml
---------------------------------------------------------------------------
# Приводим конфиг к следующему виду:
<config>
    <host>rc1a-rgkneqqdjurmni46.mdb.yandexcloud.net</host>
    <user>admin</user>
    <password>P@$w0rd</password>
    <secure>true</secure>
    <openSSL>
    <client>
        <loadDefaultCAFile>true</loadDefaultCAFile>
        <caConfig>/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt</caConfig>
        <cacheSessions>true</cacheSessions>
        <disableProtocols>sslv2,sslv3</disableProtocols>
        <preferServerCiphers>true</preferServerCiphers>
        <invalidCertificateHandler>
        <name>RejectCertificateHandler</name>
        </invalidCertificateHandler>
    </client>
    </openSSL>
</config>
---------------------------------------------------------------------------
cd ~/.clickhouse-client
clickhouse-client --config=config.xml

Минус этого варианта в том, что пароль лежит в открытом виде. Из плюсов, что подключение можно автоматизировать.