vagrant介紹
vagrant是一個(gè)很適合開發(fā)者的虛擬環(huán)境部署工具映屋,本身集成了主流的虛擬器管理工具誓军,支持vmvare和virtualbox袱讹。vagrant的精髓在一個(gè)Vagrantfile里面,和docker的Dockerfile功能上一樣昵时。我們只需要把需要安裝部署的步驟寫在Vagrantfile里面捷雕,便可以實(shí)現(xiàn)輕松部署。vagrant還支持把當(dāng)前系統(tǒng)做成一個(gè).box后綴命名的鏡像债查,類似docker的image非区,可輕松實(shí)現(xiàn)環(huán)境的移植瓜挽。
集群環(huán)境預(yù)覽
VM Name | Host Name | IP Address |
---|---|---|
zookeeper1 | vkc-zk1 | 10.30.3.2 |
zookeeper2 | vkc-zk2 | 10.30.3.3 |
zookeeper3 | vkc-zk3 | 10.30.3.4 |
broker1 | vkc-br1 | 10.30.3.30 |
broker2 | vkc-br2 | 10.30.3.20 |
broker3 | vkc-br3 | 10.30.3.10 |
準(zhǔn)備
軟件
下載并安裝如下的軟件:
- virtualbox 官網(wǎng)地址
- vagrant2.2.7 官網(wǎng)地址
- kafka_2.11-2.4.0.tgz下載地址
- 鏡像:vagrant-centos-7.2.box
下載地址:
鏈接:https://pan.baidu.com/s/15S2OZq37FcL9RWWSTWntIw
提取碼:3xb0
空間:
15G的硬盤空間
vagrant添加鏡像
vagrant box add centos72 D:\\zjdata\\virtualbox\\centos72\\vagrant-centos-7.2.box
vagrant box add命令會(huì)在c盤用戶下面的.vagrant.d/boxes/目錄下面創(chuàng)建一個(gè)和鏡像大小差不多的文件(700M左右)盹廷。
下載vagrant-kafka項(xiàng)目
注意:以下的命令行都是在git-bash下面執(zhí)行的。
cd /c/zjdata/vagrant-vm/
git clone https://github.com/xueyubingsen/vagrant-kafka.git
初始化vagrant
cd vagrant-kafka
mv Vagrantfile Vagrantfile.bak
vagrant init vagrant-kafka
- vagrant init 會(huì)生成一個(gè)Vagrantfile文件和一些隱藏目錄久橙,這個(gè)vagrant-kafka是隨便起的俄占。
- 先備份一下從github上下載的Vagrantfile文件,等init完之后淆衷,再替換回來(lái)缸榄。
mv Vagrantfile.bak Vagrantfile
修改Vagrantfile
將config.vm.box的值改為我們剛剛添加的box的名字
將KAFKA_VERSION的值改為2.4.0
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos72"
config.ssh.forward_agent = true # So that boxes don't have to setup key-less ssh
config.ssh.insert_key = false # To generate a new ssh key and don't use the default Vagrant one
vars = {
"KAFKA_VERSION" => "2.4.0",
"KAFKA_NAME" => "kafka_2.11-$KAFKA_VERSION",
"KAFKA_TARGET" => "/vagrant/tars/",
"KAFKA_HOME" => "$HOME/$KAFKA_NAME"
}
# escape environment variables to be loaded to /etc/profile.d/
as_str = vars.map{|k,str| ["export #{k}=#{str.gsub '$', '\$'}"] }.join("\n")
# common provisioning for all
config.vm.provision "shell", path: "scripts/hosts-file-setup.sh", env: vars
config.vm.provision "shell", inline: "echo \"#{as_str}\" > /etc/profile.d/kafka_vagrant_env.sh", run: "always"
config.vm.provision "shell", path: "scripts/init.sh", env: vars
# configure zookeeper cluster
(1..3).each do |i|
config.vm.define "zookeeper#{i}" do |s|
s.vm.hostname = "zookeeper#{i}"
s.vm.network "private_network", ip: "10.30.3.#{i+1}"
#s.vm.network "private_network", ip: "10.30.3.#{i+1}", netmask: "255.255.255.0", virtualbox__intnet: "my-network", drop_nat_interface_default_route: true
s.vm.provision "shell", run: "always", path: "scripts/zookeeper.sh", args:"#{i}", privileged: false, env: vars
end
end
# configure brokers
(1..3).each do |i|
config.vm.define "broker#{i}" do |s|
s.vm.hostname = "broker#{i}"
s.vm.network "private_network", ip: "10.30.3.#{4-i}0"
#s.vm.network "private_network", ip: "10.30.3.#{4-i}0", netmask: "255.255.255.0", virtualbox__intnet: "my-network", drop_nat_interface_default_route: true
s.vm.provision "shell", run: "always", path: "scripts/broker.sh", args:"#{i}", privileged: false, env: vars
end
end
config.vm.provider "virtualbox" do |v|
# This setting controls how much cpu time a virtual CPU can use. A value of 50 implies a single virtual CPU can use up to 50% of a single host CPU.
v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
end
end
拷貝kafka安裝包
把開始的時(shí)候下載的kafka_2.11-2.4.0.tgz安裝包拷貝到項(xiàng)目目錄的tars目錄下,tars目錄需要手工創(chuàng)建祝拯。
啟動(dòng)集群
只需要啟動(dòng)集群甚带,vagrant就會(huì)按照Vagrantfile里面配置的那樣,開始逐個(gè)安裝集群佳头。
vagrant up
安裝之后的樣子
$ vagrant global-status
id name provider state directory
---------------------------------------------------------------------------
436fb44 zookeeper1 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
6292373 zookeeper2 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
5325c42 zookeeper3 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
6b594e2 broker1 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
690398d broker2 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
a26ed4b broker3 virtualbox running C:/zjdata/vagrant-vm/vagrant-kafka
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
注意:可以單獨(dú)針對(duì)某一個(gè)節(jié)點(diǎn)運(yùn)行provision
vagrant provision zookeeper1
連接到zookeeper1節(jié)點(diǎn)
vagrant ssh zookeeper1