gokigenmaruのブログ

40から始めるクラウドエンジニア

AKSにkafkaを入れる

Azure上のKubernetesにkafkaを入れてみます。

Kafka

Kafkaについては、素晴らしいサイトがいくつもあるので、そちらを見ていただいたほうがいいと思います。
個人的にはちょっと前のブログですが、以下のサイトを元に勉強をさせていただきました。
qiita.com
qiita.com


kafka導入

AKS上にkafkaを入れる方法ですが、今回はhelmを使ってbitnamiにあるkafkaをインストールしようと思います。
まずは以下のブログに書いた方法でCLIAKSに接続します。
gokigenmaru.hatenablog.com

ここでhelmを使ってインストールするのですが、まずはhelmコマンドをインストールするところからですね。

helmコマンドのインストール

helmのサイトにインストール方法が載っています。
自分はUbuntuなので、Aptで入れる方法を実施しました。
※サイトはバイナリを持ってきていれる方法がページ上部にあり、パッケージマネージャを使う方法はバイナリで入れる方法の下にあるので注意です。
helm.sh

サイトにある手順通りにコマンドを実行すればhelmのインストール完了です。

$ type helm
helm is hashed (/usr/sbin/helm)
$ helm version
version.BuildInfo{Version:"v3.12.3", GitCommit:"3a31588ad33fe3b89af5a2a54ee1d25bfe6eaa5e", GitTreeState:"clean", GoVersion:"go1.20.7"}
bitnami

bitnami上のkafkaを使うのですが、サイトは以下にあります。
github.com
こちらにあるvalues.yamlがkafkaのインストール内容になります。

まずはデフォルトで入れてみる

helmコマンドを使ってまずはbitnami上にあるデフォルトの値でインストールしてみます。
まずはbitnamiのリポジトリをhelmで追加します。

$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
$ helm search repo bitnami/kafka
NAME            CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/kafka   25.1.10         3.5.1           Apache Kafka is a distributed streaming platfor...

見てみると、今のチャートのバージョンは25.1.10ですね。
では、インストールしてみます。

$ helm install kafka bitnami/kafka
NAME: kafka
LAST DEPLOYED: Sun Sep 10 10:35:44 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: kafka
CHART VERSION: 25.1.10
APP VERSION: 3.5.1

** Please be patient while the chart is being deployed **

Kafka can be accessed by consumers via port 9092 on the following DNS name from within your cluster:

    kafka.default.svc.cluster.local

Each Kafka broker can be accessed by producers via port 9092 on the following DNS name(s) from within your cluster:

    kafka-controller-0.kafka-controller-headless.default.svc.cluster.local:9092
    kafka-controller-1.kafka-controller-headless.default.svc.cluster.local:9092
    kafka-controller-2.kafka-controller-headless.default.svc.cluster.local:9092

The CLIENT listener for Kafka client connections from within your cluster have been configured with the following security settings:
    - SASL authentication

To connect a client to your Kafka, you need to create the 'client.properties' configuration files with the content below:

security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
    username="user1" \
    password="$(kubectl get secret kafka-user-passwords --namespace default -o jsonpath='{.data.client-passwords}' | base64 -d | cut -d , -f 1)";

To create a pod that you can use as a Kafka client run the following commands:

    kubectl run kafka-client --restart='Never' --image docker.io/bitnami/kafka:3.5.1-debian-11-r44 --namespace default --command -- sleep infinity
    kubectl cp --namespace default /path/to/client.properties kafka-client:/tmp/client.properties
    kubectl exec --tty -i kafka-client --namespace default -- bash

    PRODUCER:
        kafka-console-producer.sh \
            --producer.config /tmp/client.properties \
            --broker-list kafka-controller-0.kafka-controller-headless.default.svc.cluster.local:9092,kafka-controller-1.kafka-controller-headless.default.svc.cluster.local:9092,kafka-controller-2.kafka-controller-headless.default.svc.cluster.local:9092 \
            --topic test

    CONSUMER:
        kafka-console-consumer.sh \
            --consumer.config /tmp/client.properties \
            --bootstrap-server kafka.default.svc.cluster.local:9092 \
            --topic test \
            --from-beginning
$ kubectl get pod
NAME                 READY   STATUS    RESTARTS   AGE
kafka-controller-0   1/1     Running   0          84s
kafka-controller-1   1/1     Running   0          84s
kafka-controller-2   1/1     Running   0          84s

簡単にインストールできました。
helm使うとインストールらくちんです。