2018年11月5日月曜日

Vagrantでレプリケーション構成のRedisサーバ(マスタ・スレーブ)の仮想マシン(Ubuntu18.04)を構築する

Redisはオープンソースのキー・バリュー型のデータベースです。

〇構築方法
以下のVagrantfileを使用してレプリケーション構成のRedisサーバ(マスタ・スレーブ)の仮想マシン(Ubuntu18.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "redismaster" do |server|
    server.vm.box = "bento/ubuntu-18.04"
    server.vm.hostname = "redismater"
server.vm.network "private_network", ip: "192.168.55.115", :netmask => "255.255.255.0"
server.vm.network :public_network, ip:"192.168.1.115"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "redismaster"
      vbox.cpus = 2
      vbox.memory = 2048
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install redis serrver
apt-get -y install redis-server

# change bind address.
sed -i -e 's/bind 127.0.0.1 ::1/bind 0.0.0.0/' /etc/redis/redis.conf

systemctl restart redis

# wait until port 6379 is opened.
while netstat -lnt | awk '$4 ~ /:6379$/ {exit 1}'; do sleep 10; done
sleep 10

# test execution
redis-cli -h 192.168.55.115 << EOF
ping
flushall
set mykey "hello world."
get mykey
EOF


SHELL
  end
  #----------------------------------------------------------------------------------------
  config.vm.define "redisslave" do |server|
    server.vm.box = "bento/ubuntu-18.04"
    server.vm.hostname = "redisslave"
server.vm.network "private_network", ip: "192.168.55.116", :netmask => "255.255.255.0"
server.vm.network :public_network, ip:"192.168.1.116"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "redisslave"
      vbox.cpus = 2
      vbox.memory = 2048
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8


# install redis serrver
apt-get -y install redis-server

# configure redis.conf
echo 'slaveof 192.168.55.115 6379' >> /etc/redis/redis.conf

# change bind address.
sed -i -e 's/bind 127.0.0.1 ::1/bind 0.0.0.0/' /etc/redis/redis.conf

systemctl restart redis

# wait until port 6379 is opened.
while netstat -lnt | awk '$4 ~ /:6379$/ {exit 1}'; do sleep 10; done
sleep 10

# test execution
redis-cli -h 192.168.55.116 << EOF
ping
get mykey
EOF


SHELL
  end
end

0 件のコメント:

コメントを投稿