LoginSignup
4
8

More than 5 years have passed since last update.

Apache Kafkaの環境構築 + Quickstart

Last updated at Posted at 2017-07-23

Kafkaを環境構築から、サクッと触ってみる。
環境はvirtualbox-vagrant-ansibleで作る

環境

  • macos 10.12.5
  • Vagrant 1.9.7
  • ansible 2.3.1.0
  • VirtualBox 5.1.24

quickstart

https://kafka.apache.org/quickstart
に記載していることをやってみる。

本来provider-blaker-consumerと3つの役割ごとにサーバを立てるべきだが、quickstartでは一つのサーバで実行している。

構築

vagrant Vagrantfile

  • Ubuntu 16.04 LTS “Xenial Xerus”を利用する。
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.provider "virtualbox" do |vb|
     vb.memory = "2048"
  end

  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

ansible playbook.yml

  • Kafkaの動作にはscalaとjavaが必要。
  • 20170723時点で最新バージョン(kafka_2.11-0.11.0.0.tgz)
playbook.yml
- hosts: all
  become: yes

  vars:
    scala_version: "2.11"
    kafka_version: "0.11.0.0"

    install_dir: "/opt"
    symlink_name: "kafka"
    kafka_name: "kafka_{{ scala_version }}-{{ kafka_version }}"
    kafka_download_url: "http://ftp.yz.yamagata-u.ac.jp/pub/network/apache/kafka/{{ kafka_version }}/{{ kafka_name }}.tgz"

  tasks:
  - name: ensure openjdk8 present
    apt:
      name: "openjdk-8-jdk"
      state: present
      update_cache: yes

  - name: ensure kafka present
    unarchive:
      remote_src: yes
      src: "{{ kafka_download_url }}"
      dest: "{{ install_dir }}"

  - name: ensure symlink present
    file:
      src: "{{ install_dir }}/{{ kafka_name }}"
      dest: "{{ install_dir }}/{{ symlink_name }}"
      mode: 0755
      state: link

Kafkaを動かしてみる

インストールしたフォルダに移動

$ cd /opt/kafka

フォルダ構成はこんな感じになってる。

kafka/
    config/ #サーバの設定ファイルなどが保管
    bin/ #起動やテストのシェル類
    libs/ #libs zookeeperをはじめとるlibが保管
    site-docs/ #tgz本体を保管
    LICENSE
    NOTICE

Kafka(Broker)の裏で、Zookeeperというサービスを動かす必要がある。大規模な分散処理はZooKeeperを利用しているんですね。

ZooKeeperを起動(localhost:2181)

$ bin/zookeeper-server-start.sh config/zookeeper.properties

Kafka(Broker localhost:9092)の起動

$ bin/kafka-server-start.sh config/server.properties

topic作成

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

作成したtopicを確認

$ bin/kafka-topics.sh --list --zookeeper localhost:2181
test

Producer,Consumerの動作についてはシェルが用意されており、それらを叩くことでテストができる。(実際の利用時はAPI形式になるんだっけな、ここではあくまで動作のテスト)

メッセージ送信(Producer)
※メッセージ入力し終えたらCtrl+Cで抜ける
```
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

This is a test message
This is a second test message
```

メッセージの受信(Consumer)

$bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This is a test message
This is a second test message

マルチクラスタ化する(未完了)

同一サーバ上でポートを変えてマルチクラスタを実装してみる。(本来なら別環境で構築するんだろうけど、設定を見ればどこを変えればいいかはなんとなく見えてくる)

config/server.propertiesを編集

$ sudo cp config/server.properties config/server-1.properties
$ sudo cp config/server.properties config/server-2.properties

…つかれたので今日はおしまい。また気分が向いたらquickstart編までは書き切ります。

参考

本家
https://kafka.apache.org

Kafkaの説明がよく分かるサイト
http://deeeet.com/writing/2015/09/01/apache-kafka/
http://fuji-151a.hatenablog.com/entry/2014/02/23/231639

vagrant,ansibleを利用した環境構築の際に参考にしたサイト
https://github.com/eucuepo/vagrant-kafka
http://bufferings.hatenablog.com/entry/2017/05/02/163901

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8