Windows上にDigdagサーバ構成を作る その1 PostgresSQL・NFS


やりたいこと

開発用WindowsマシンにVagrant/VirtualBoxで以下のCentOSを立ててDigdagサーバ構成を作りたい

  1. Postgresマシン
  2. NFSサーバマシン
  3. Digdagサーバマシン1
  4. Digdagサーバマシン2
  5. Digdagクライアントマシン

NFSが必要な理由 → http://qiita.com/pilot/items/69cbae56d85a443fb623#nfs%E3%81%AE%E5%BF%85%E8%A6%81%E6%80%A7

各マシンにホストマシン以外から接続したい場合

ホストマシンで管理者権限で起動したコマンドプロンプト内で以下を実行する

netsh advfirewall firewall add rule name="ssh to pgsv"        dir=in action=allow protocol=TCP localport=2200
netsh advfirewall firewall add rule name="ssh to digsv1"      dir=in action=allow protocol=TCP localport=2201
netsh advfirewall firewall add rule name="ssh to digsv2"      dir=in action=allow protocol=TCP localport=2202
netsh advfirewall firewall add rule name="ssh to nfssv"       dir=in action=allow protocol=TCP localport=2211
netsh advfirewall firewall add rule name="ssh to digcl"       dir=in action=allow protocol=TCP localport=2222
netsh advfirewall firewall add rule name="digdagUI to digsv1" dir=in action=allow protocol=TCP localport=12201
netsh advfirewall firewall add rule name="digdagUI to digsv2" dir=in action=allow protocol=TCP localport=12202

2016/12/15追記:0.9.0から追加になったUIのポート追加 http://docs.digdag.io/releases/release-0.9.0.html#server-mode-changes

Vagrantについて

http://qiita.com/pilot/items/f555dcf503f9880745c4 を参照のこと

Postgresマシン用Vagrantfile

Vagrant.configure("2") do |config|
  # 公式Box https://atlas.hashicorp.com/bento
  config.vm.box = "bento/centos-6.7"
  # ネットワーク設定
  config.vm.define :pgsv do |pgsv|
    pgsv.vm.hostname = "pgsv"
    pgsv.vm.network :private_network, ip: "192.168.1.11", virtualbox__intnet: "dignet"
    pgsv.vm.network :forwarded_port, guest: 22, guest_ip: "0.0.0.0", host: 2200, host_ip: "127.0.0.1", id: "ssh"
  end
  # VirtualBox独自設定
  config.vm.provider "virtualbox" do |vb|
    # メモリ
    vb.memory = "512"
    # CPU
    vb.cpus = 1
    # サスペンドからの復帰時に時刻をホストと同期
    vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]
  end
  config.vm.provision "shell", inline: <<-EOT
    # タイムゾーン設定
    rm /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
    # カーネルアップデート (Vagrantで共有フォルダマウントのため)
    yum update -y kernel
    # NTPサーバ設定
    yum -y install ntp
    cd /etc
    mv ntp.conf ntp.conf.org
    sed 's|\\(^server .*$\\)|#\\1|' ntp.conf.org >  ntp.conf
    echo                                         >> ntp.conf
    echo 'server ntp.nict.jp'                    >> ntp.conf
    echo 'server ntp1.jst.mfeed.ad.jp'           >> ntp.conf
    echo 'server ntp2.jst.mfeed.ad.jp'           >> ntp.conf
    echo 'server ntp3.jst.mfeed.ad.jp'           >> ntp.conf
    service ntpd start
    chkconfig ntpd on
    # PostgreSQL Serverインストール・設定
    wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
    rpm -ivh pgdg-centos96-9.6-3.noarch.rpm
    rm       pgdg-centos96-9.6-3.noarch.rpm
    yum install -y postgresql96-server postgresql96-contrib
    service postgresql-9.6 initdb
    cd /var/lib/pgsql/9.6/data
    mv postgresql.conf postgresql.conf.org
    sed -e "s/^#listen_addresses.*#\\(.*\\)$/listen_addresses = '*'                  #\\1/"               \
        -e "s/^log_line_prefix.*#\\(.*\\)$/log_line_prefix = ' %t %u %d '          #\\1/"                 \
        -e "s/^#ssl = .*#\\(.*\\)$/ssl = on                                #\\1/"                         \
        -e "s/^#ssl_cert_file.*#\\(.*\\)$/ssl_cert_file = 'server.crt'            #\\1/"                  \
        -e "s/^#ssl_key_file.*#\\(.*\\)$/ssl_key_file = 'server.key'             #\\1/"                   \
        postgresql.conf.org > postgresql.conf
    echo "hostssl  digdagdb        digdaguser      192.168.1.21/32         password" >> pg_hba.conf
    echo "hostssl  digdagdb        digdaguser      192.168.1.22/32         password" >> pg_hba.conf
    openssl genrsa -out server.key 2048
    openssl req -new -key server.key -x509 -days 3650 -out server.crt <<EOF > server.crt
JP
XXX
XXX
XXX
XXX
pgsv
.
EOF
    chown postgres:postgres postgresql.conf server.crt server.key
    chmod 600               postgresql.conf server.crt server.key
    service   postgresql-9.6 start
    chkconfig postgresql-9.6 on
    sudo -u postgres createdb   digdagdb
    sudo -u postgres createuser digdaguser
    sudo -u postgres psql -c "ALTER USER digdaguser WITH PASSWORD 'digdagpassword'"
    sudo -u postgres psql -c "ALTER USER digdaguser WITH SUPERUSER"
    psql --version
  EOT
end

ポイント

  1. マシン間通信ができるようにネットワーク設定
  2. クライアントとの通信はSSL+パスワード
    • 開発環境ならそこまでしなくても良かったか
  3. DB・ユーザは事前に作っておく
    • 作っておかないとDigdagサーバでエラー
  4. ユーザはスーパーユーザ
    • そうしないとDigdagサーバでエラー

NFSサーバマシン用Vagrantfile

Vagrant.configure("2") do |config|
  # 公式Box https://atlas.hashicorp.com/bento
  config.vm.box = "bento/centos-6.7"
  # ネットワーク設定
  config.vm.define :nfssv do |nfssv|
    nfssv.vm.hostname = "nfssv"
    nfssv.vm.network :private_network, ip: "192.168.1.31", virtualbox__intnet: "dignet"
    nfssv.vm.network :forwarded_port, guest: 22, guest_ip: "0.0.0.0", host: 2211, host_ip: "127.0.0.1", id: "ssh"
  end
  # VirtualBox独自設定
  config.vm.provider "virtualbox" do |vb|
    # メモリ
    vb.memory = "512"
    # CPU
    vb.cpus = 1
    # サスペンドからの復帰時に時刻をホストと同期
    vb.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]
  end
  config.vm.provision "shell", privileged: false, inline: <<-EOT
    # NFS共有ディレクトリ作成
    mkdir /home/vagrant/nfs
  EOT
  config.vm.provision "shell", inline: <<-EOT
    # タイムゾーン設定
    rm /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
    # カーネルアップデート (Vagrantで共有フォルダマウントのため)
    yum update  -y kernel
    # NTPサーバ設定
    yum -y install ntp
    cd /etc
    mv ntp.conf ntp.conf.org
    sed 's|\\(^server .*$\\)|#\\1|' ntp.conf.org >  ntp.conf
    echo                                         >> ntp.conf
    echo 'server ntp.nict.jp'                    >> ntp.conf
    echo 'server ntp1.jst.mfeed.ad.jp'           >> ntp.conf
    echo 'server ntp2.jst.mfeed.ad.jp'           >> ntp.conf
    echo 'server ntp3.jst.mfeed.ad.jp'           >> ntp.conf
    service ntpd start
    chkconfig ntpd on
    # NFS設定
    yum install -y nfs-utils
    echo '/home/vagrant/nfs 192.168.1.0/24(rw,no_root_squash)' >> /etc/exports
    echo 'all: 192.168.1.0/24'                                 >> /etc/hosts.allow
    service   rpcbind  start
    chkconfig rpcbind  on
    service   nfs      start
    chkconfig nfs      on
    service   iptables stop
    chkconfig iptables off
  EOT
end

続き

http://qiita.com/pilot/items/a49e6985f1c18aa37b5c
http://qiita.com/pilot/items/ecf5b7277947710189c3