2015年12月20日日曜日

ホテル用Wi-Fi ルータを自宅で使ってみた


Buffaloの「無線LAN親機(Wi-Fiルータ)WMR-433シリーズ」というものを見かけた。

広告を見ると以下のように紹介されていたので、気になった。

「ホテルの有線LAN端子に本製品を接続すれば、部屋中どこからでも、高速で電波干渉に強い11acWi-Fiインターネットが利用できるようになります。報告書の送信、営業先の場所の検索、YouTubeやhuluなどの動画鑑賞もらくらく快適。さらにスマホとパソコンも無線経由でつながるのでデータの整理・移動もできます。 本製品の電波を使えるのは、接続設定をしたあなただけ。マイWi-Fi環境を独り占めできるから、他人の機器の接続に邪魔されることなく仕事でもプライベートでもできることが広がります。ホテルでWi-Fiしたい方におすすめの製品です。」



ということで、安かったので、買って自宅のLANにつなげて使ってみた。
このルータ、小さくて、軽い。

このルータをLANにつなげるだけで、
スマホとノートPCから 「Wi-Fi⇒自宅LAN⇒インターネット」 を使えるようになった。
スマホとノートPCを同時に接続して使っても問題なかった。

スマホから自宅LAN経由のインターネットを使えるのは、うれしい。
自宅にいるときは、スマホのデータ通信量を気にせず使える。

メーカのページはこちら





2015年12月19日土曜日

Puppy Linux 6.3 でUSBテザリングできた



外に持ち出せるノートPC(ネットが使いたい)が必要になり、古いPCにLinuxを入れてみようと思いました。


昔、WindowsXPで使っていたDell Inspiron 700m (Pentium-M 非PAE/1GBメモリ)に軽量Linuxを入れようとしたのですが、最近のLinuxはPAE対応のCPUじゃないと動かないらしい。

いろいろ調べた結果、軽くて非PAEのCPUでも動く、PuppyLinux6.3 をインストールしてみることにしました。

PuppyLinux6.3 (32Bit)のISOイメージを使い、LiveCDで動作確認できたので、内臓HDDにインストールしました。

とりあえず、日本語表示、入力もでき、思ったより軽く、サクサク動いてます。




FirefoxでYouTubeの動画を再生したら、さすがに、少し重かったけど、見れないことはない。
音も問題ない。ちゃんと聞こえる。

ネットワークは、LANケーブルを認識して、ちゃんと通信できます。
無線LANもOKでした。

また、もってるスマホがUSBテザリングが使える機種だったので試してみました。
スマホ(ZenFone2)とノートPCをUSBでつなぎ、USBテザリングをしてみたら、問題なく、通信できました。
これなら、以下のようにしてノートPCでインターネット を使えます。

 ノートPC→(USBテザリング)→スマホ→インターネット

捨てようと思ってたノートPCだけど、外出先の作業用に現役復帰できそう。。。

この記事は、PuppyLinuxを入れたDELLで書いてみました。


[ 参考URL ]


2015年12月10日木曜日

[CentOS6] シェルでOSユーザのパスワードを変更する


OSユーザのパスワードを変更したい場合、以下のように passwd コマンドを使うことが多いです。

[root@node01 ~]# passwd user01
ユーザー user01 のパスワードを変更。
新しいパスワード:
よくないパスワード: 辞書の単語に基づいています
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
[root@node01 ~]#

しかし、これだと大量のサーバのパスワードを変更したい場合に面倒です。

usermod コマンドを使うuto、引数にパスワードを指定して変更できます。
[root@node01 ~]# usermod -p `openssl passwd p@ssw0rd` user01

この例では user01 のパスワードを "p@ssw0rd" に設定しています。

usermod では -p の引数でパスワード指定しますが、crypt 関数で暗号化する必要があります。
そこで、openssl コマンドでパスワードにしたい文字列を暗号化しています。
使用できる暗号化形式は以下のとおり。
[root@node01 ~]# openssl passwd --help
Usage: passwd [options] [passwords]
where options are
-crypt             standard Unix password algorithm (default)
-1                 MD5-based password algorithm
-apr1              MD5-based password algorithm, Apache variant
-salt string       use provided salt
-in file           read passwords from file
-stdin             read passwords from stdin
-noverify          never verify when reading password from terminal
-quiet             no warnings
-table             format output as table
-reverse           switch table columns

OSの暗号化形式は以下のようにして調べます。
[root@node01 ~]# authconfig --test | grep password
 shadow passwords are enabled
 password hashing algorithm is sha512

この例だと sha512 になります。
openssl では対応していないので grub-crypt を使用します。
[root@node01 ~]# grub-crypt --help
Usage: grub-crypt [OPTION]...
Encrypt a password.

  -h, --help              Print this message and exit
  -v, --version           Print the version information and exit
  --md5                   Use MD5 to encrypt the password
  --sha-256               Use SHA-256 to encrypt the password
  --sha-512               Use SHA-512 to encrypt the password (default)

Report bugs to <bug-grub@gnu.org>.
EOF

usermod コマンドでパスワードを変更したい場合は、以下のとおり。getent コマンドで /etc/shadow に登録されたユーザの情報を表示して、暗号化化されたパスワードが設定したものと同じか確認しています。
[root@node01 ~]# grub-crypt
Password: <パスワード>
Retype password: <パスワード>
$6$sR7OHkosbrt01t.R$mJZyMcBDWr0WUpVcMfNX512PPBZ.jVsq7LFUiUu38MKsOXGl4MtyzKs2fOOwklMNKaLwcFc5GWjZ8OS2jSJ4H1
[root@node01 ~]# usermod -p '$6$sR7OHkosbrt01t.R$mJZyMcBDWr0WUpVcMfNX512PPBZ.jVsq7LFUiUu38MKsOXGl4MtyzKs2fOOwklMNKaLwcFc5GWjZ8OS2jSJ4H1' user01
[root@node01 ~]# getent shadow user01
user01:$6$sR7OHkosbrt01t.R$mJZyMcBDWr0WUpVcMfNX512PPBZ.jVsq7LFUiUu38MKsOXGl4MtyzKs2fOOwklMNKaLwcFc5GWjZ8OS2jSJ4H1:16832:0:99999:7:::

passwdやusermod コマンド以外に、chpasswd コマンドでもパスワードを変更できます。
[root@node01 ~]# echo "user01:password" | chpasswd
[root@node01 ~]# getent shadow user01
user01:$6$aN3jC//BhiZ/tPe$.aE8K6SrGmj9BIWbaFX/4/BFjB9Npaa78GCR2NVPFGj.DFLipL2r.SDb3.rGDYeSNOAii3JZcsITMOi172Z.u1:16832:0:99999:7:::

chpasswd コマンドを使うと、ファイルに記載した複数のユーザ名とパスワードを一括して変更できます。
[root@node01 ~]# cat pass.txt
user01:passwd1
user02:passwd2
[root@node01 ~]# chpasswd < ./pass.txt
[root@node01 ~]# getent shadow user01
user01:$6$HaAHA/azO$OrDCCKRav0BByAXzxRF3wygAG52dwzwbNiLVoX2DxDr16JBPfwQTJj1mT3nsMB9Y3oJKOgKDcFF5xmzOLSD3P1:16832:0:99999:7:::
[root@node01 ~]# getent shadow user02
user02:$6$CoOmN/8a$IojLTXxe8AJ/vcbIQEUoztapXPlVKHxgBXDsDiCom4r4F/uSFrlighfZdO36YcE8bauHsECOCckFAbMVtKolV.:16832:0:99999:7:::


OSユーザの管理については下記が参考になります。





2015年11月29日日曜日

[CentOS6][Ansible] EC2インスタンスで Webサーバを作成する


Ansible で EC2インスタンスのWebサーバ(CentOS6+Apache)を1台構築してみました。
自宅からAWSに接続することを前提にしています。

Ansible では以下のタスクを行います。
  • CentOS公式AMIからEC2インスタンスの作成
  • ルートパーティションの拡張(8GB ⇒ 10GB)
  • CentOS6の基本設定
    • SElinux無効化
    • ipv6無効化
    • ファイヤーウォール無効化
    • 日本語(ja_JP.UTF-8)設定
    • タイムゾーン(Asia/Tokyo)設定
  • Apache導入

AWS側は、事前に以下のリソースを用意します。
  • サブネット(パブリック)
  • セキュリティグループ(SSH、HTTPを許可)
  • キーペア


Ansibleのインストール


Ansible のインストールは以下のとおり。 
libselinux-python は、SELinuxの設定で使用します。
python-boto は EC2 の作成で使用します。
[root@node01 ~]# yum install ansible libselinux-python python-boto

Ansible のバージョンは以下のとおり
[root@node01 ~]# ansible --version
ansible 1.9.4
  configured module search path = None

Ansible プレイブックのディレクトリ構成


今回、ansible は 一般ユーザの infraユーザで実行することにしました。
Ansible のプレイブックは、ベストプラクティスを参考に以下のように配置しました。
[infra@node01 ansible]$ tree .
.
├── aws.yml
├── group_vars
├── host_vars
├── key.pem
├── production
├── roles
│   ├── apache
│   │   └── tasks
│   │       └── main.yml
│   ├── common
│   │   ├── defaults
│   │   ├── files
│   │   │   └── disable-ipv6.conf
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   │       └── main.yml
│   ├── ec2
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── vars
│   │       └── main.yml
│   └── growpart
│       ├── tasks
│       │   └── main.yml
│       └── vars
│           └── main.yml
├── site.yml
└── webservers.yml

19 directories, 13 files


Ansible プレイブックの内容


各ファイルの内容は以下のとおり。
key.pem はAWSで作成したキーペアの秘密鍵です。
CentOS6の公式AMで作成したEC2インスタンスには、この秘密鍵を使用して centos ユーザでログインします。
centosユーザはパスワードなしの sudo が使用できるようになっています。
[infra@node01 ansible]$ more * | cat -
::::::::::::::
aws.yml
::::::::::::::
- hosts: localhost
  connection: local
  gather_facts: False
  roles:
    - ec2
- hosts: webservers
  gather_facts: True
  sudo: yes
  roles:
    - growpart
::::::::::::::
key.pem
::::::::::::::
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
::::::::::::::
production
::::::::::::::
[local]
localhost

[webservers:vars]
ansible_ssh_user=centos
ansible_ssh_private_key_file=/home/infra/ansible/key.pem

::::::::::::::
site.yml
::::::::::::::
- include: aws.yml
- include: webservers.yml
::::::::::::::
webservers.yml
::::::::::::::
- hosts: webservers
  gather_facts: True
  sudo: yes
  roles:
    - common
    - apache
[infra@node01 ansible]$ more */*/*/* | cat -
::::::::::::::
roles/apache/tasks/main.yml
::::::::::::::
- name: Apache のインストール
  yum: name=httpd state=latest
  tags:
    - apache

- name: Apache の自動起動ON
  service: name=httpd enabled=yes
  tags:
    - apache

- name: Apache の起動
  service: name=httpd state=started
  tags:
    - apache
::::::::::::::
roles/common/files/disable-ipv6.conf
::::::::::::::
options ipv6 disable=1
::::::::::::::
roles/common/tasks/main.yml
::::::::::::::
#-----------------------
# OS 基本設定
#-----------------------

# SElinux off
# yum install libselinux-python
- name: SELinux を無効にする
  selinux: state=disabled
  tags:
    - os_basic
    - selinux

# ipv6 off
- name: ipv6 を無効にする(/etc/sysconfig/network)
  lineinfile: >
    dest=/etc/sysconfig/network
    state=present
    backup=yes
    backrefs=no
    insertafter=EOF
    regexp='^NETWORKING_IPV6='
    line='NETWORKING_IPV6=no'
  tags:
    - os_basic
    - ipv6

- name: ipv6 を無効にする(/etc/modprobe.d/)
  copy: >
    src=disable-ipv6.conf
    dest=/etc/modprobe.d/disable-ipv6.conf
    group=root
    owner=root
    mode=0644
    backup=yes
  tags:
    - os_basic
    - ipv6

# Firewall off
- name: Firewall を無効にする
  service: name={{ item }} enabled=no
  with_items: firewall_srv
  tags:
    - os_basic
    - firewall

# LANG=ja_JP.UTF-8
- name: 日本語(ja_JP.UTF-8) にする
  lineinfile: >
    dest=/etc/sysconfig/i18n
    state=present
    backup=yes
    backrefs=no
    insertafter=EOF
    regexp='^LANG='
    line='LANG=ja_JP.UTF-8'
  tags:
    - os_basic
    - lang

# TIMEZONE=Asia/Tokyo
- name: タイムゾーンを Asia/Tokyo にする
  lineinfile: >
    dest=/etc/sysconfig/clock
    state=present
    backup=yes
    backrefs=no
    insertafter=EOF
    regexp='^ZONE='
    line='ZONE=Asia/Tokyo'
  tags:
    - os_basic
    - zone

- name: ローカルタイムを Asia/Tokyo にする
  file: >
    src=/usr/share/zoneinfo/Asia/Tokyo
    dest=/etc/localtime
    state=link
    force=yes
    owner=root
    group=root
  tags:
    - os_basic
    - zone

#-----------------------
# OS パッケージの最新化
#-----------------------

- name: yum アップデート
  yum: name=* enablerepo=epel state=latest
  tags:
    - yum_update

- name: reboot!
  command: shutdown -r now
  tags:
    - yum_update

- name: wait for SSH port down
  local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped
  sudo: no
  tags:
    - yum_update

- name: wait for SSH port up
  local_action: wait_for host={{ inventory_hostname }} port=22 state=started timeout=300
  sudo: no
  tags:
    - yum_update

::::::::::::::
roles/common/vars/main.yml
::::::::::::::

firewall_srv:
 - iptables
 - ip6tables
::::::::::::::
roles/ec2/tasks/main.yml
::::::::::::::
# yum install python-boto

- name: EC2インスタンスを作成
  ec2:
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
    assign_public_ip: "{{ assign_public_ip }}"
    region: "{{region}}"
    zone: "{{zone}}"
    image: "{{ ami_image }}"
    instance_type: "{{ instance_type }}"
    key_name: "{{ key_name }}"
    group_id: "{{ sg_id }}"
    private_ip: "{{ ip }}"
    vpc_subnet_id: "{{ vpc_subnet_id }}"
    instance_tags:
      Name: "{{ hostname }}"
      System: "Test"
    wait: yes
    wait_timeout: 300
    volumes:
      - device_name: "{{ device_name }}"
        volume_size: "{{ volume_size }}"
        delete_on_termination: yes
  register: ec2
  tags:
    - ec2
#- debug: var=ec2

- name: SSHで接続できるようになるまで待機
  local_action: wait_for port=22 host={{ item.public_dns_name }} timeout=300 state=started
  with_items: ec2.instances
  when: ec2.changed == true
  tags:
    - ec2

- name: 作成したEC2インスタンスをインベントリに追加
  local_action: add_host hostname="{{ item.public_ip }}" groupname="webservers"
  with_items: ec2.instances
  when: ec2.changed == true
  tags:
    - ec2

::::::::::::::
roles/ec2/vars/main.yml
::::::::::::::

# EC2
region: us-east-1
zone: us-east-1a
aws_access_key: xxxxxxxxxxxxx
aws_secret_key: xxxxxxxxxxxxx
assign_public_ip: yes
ami_image: ami-57cd8732
key_name: keypaire
hostname: apache01
ip: 10.0.0.11
vpc_subnet_id: "subnet-xxxx"
sg_id: [ "sg-xxxx", "sg-xxxx" ]
instance_type: "t2.micro"
device_name: "/dev/sda1"
volume_size: 10

::::::::::::::
roles/growpart/tasks/main.yml
::::::::::::::
# ルートパーティション拡張
- name: epel レポジトリのインストール
  yum: name=epel-release state=latest
  tags:
    - growpart

- name: yum パッケージのインストール
  yum: name={{ item }} enablerepo=epel state=latest
  with_items: grow_part_pkg
  tags:
    - growpart

- name: パーティション拡張の指示
  shell: "dracut --force --add growroot /boot/initramfs-$(uname -r).img"
  register: dracut
  ignore_errors: true
  changed_when: false
  tags:
    - growpart

- name: reboot!
  command: shutdown -r now
  tags:
    - growpart

- name: wait for SSH port down
  local_action: wait_for host={{ inventory_hostname }} port=22 state=stopped
  sudo: no
  tags:
    - growpart

- name: wait for SSH port up
  local_action: wait_for host={{ inventory_hostname }} port=22 state=started timeout=300
  sudo: no
  tags:
    - growpart

::::::::::::::
roles/growpart/vars/main.yml
::::::::::::::
grow_part_pkg:
 - cloud-utils-growpart
 - dracut-modules-growroot
[infra@node01 ansible]$

イベントリ(puroduction)の webservers グループには、ホスト(IPアドレス)を指定していませんが、EC2作成時に自動的にIPアドレスが追加されるようにしています。
プレイブック実行時に、作成したEC2インスタンスのパブリックIPがwebsrversグループに追加され、後続の処理では、追加されたIPアドレスに対してタスクを実行します。

Ansibleプレイブックのタスク一覧


このプレイブックが実施するタスクは以下のとおり。
上から順番に実行されます。
[infra@node01 ansible]$ ansible-playbook site.yml -i production --list-tasks

playbook: site.yml

  play #1 (localhost):  TAGS: []
    EC2インスタンスを作成       TAGS: [ec2]
    SSHで接続できるようになるまで待機   TAGS: [ec2]
    作成したEC2インスタンスをインベントリに追加 TAGS: [ec2]

  play #2 (webservers): TAGS: []
    epel レポジトリのインストール       TAGS: [growpart]
    yum パッケージのインストール        TAGS: [growpart]
    パーティション拡張の指示    TAGS: [growpart]
    reboot!     TAGS: [growpart]
    wait for SSH port down      TAGS: [growpart]
    wait for SSH port up        TAGS: [growpart]

  play #3 (webservers): TAGS: []
    SELinux を無効にする        TAGS: [os_basic, selinux]
    ipv6 を無効にする(/etc/sysconfig/network)   TAGS: [ipv6, os_basic]
    ipv6 を無効にする(/etc/modprobe.d/) TAGS: [ipv6, os_basic]
    Firewall を無効にする       TAGS: [firewall, os_basic]
    日本語(ja_JP.UTF-8) にする  TAGS: [lang, os_basic]
    タイムゾーンを Asia/Tokyo にする    TAGS: [os_basic, zone]
    ローカルタイムを Asia/Tokyo にする  TAGS: [os_basic, zone]
    yum アップデート    TAGS: [yum_update]
    reboot!     TAGS: [yum_update]
    wait for SSH port down      TAGS: [yum_update]
    wait for SSH port up        TAGS: [yum_update]
    Apache のインストール       TAGS: [apache]
    Apache の自動起動ON TAGS: [apache]
    Apache の起動       TAGS: [apache]



Ansible実行(checkモード)


実際に変更は行わないようにして実行して、動きを確認してみます。
[infra@node01 ansible]$ ansible-playbook site.yml -i production --check

PLAY [localhost] **************************************************************

TASK: [ec2 | EC2インスタンスを作成] **********************************
skipping: [localhost]
ok: [localhost]

TASK: [ec2 | SSHで接続できるようになるまで待機] ****************
skipping: [localhost] => (item=ec2.instances)

TASK: [ec2 | 作成したEC2インスタンスをインベントリに追加] ***
skipping: [localhost] => (item=ec2.instances)

PLAY [webservers] *************************************************************
skipping: no hosts matched

PLAY [webservers] *************************************************************
skipping: no hosts matched

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

[infra@node01 ansible]$

実際にEC2インスタンスを作成しないとイベントリにIPアドレスが追加されないので、webservers グループのタスクは実行されません。

Ansible実行


今度は、実際に変更を行います。
途中、EC2インスタンスに初めてSSHログインするタイミングで、接続確認入力を求められるので "yes" を入力します。
[infra@node01 ansible]$ ansible-playbook site.yml -i production

PLAY [localhost] **************************************************************

TASK: [ec2 | EC2インスタンスを作成] **********************************
changed: [localhost]

TASK: [ec2 | SSHで接続できるようになるまで待機] ****************
ok: [localhost -> 127.0.0.1] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-10-0-0-11.ec2.internal', u'tags': {}, u'key_name': u'virginia_key', u'public_ip': u'52.23.158.7', u'image_id': u'ami-57cd8732', u'tenancy': u'default', u'private_ip': u'10.0.0.11', u'groups': {u'sg-bbf176df': u'default', u'sg-01237c67': u'webgroup'}, u'public_dns_name': u'ec2-52-23-158-7.compute-1.amazonaws.com', u'state_code': 16, u'id': u'i-a6f60810', u'placement': u'us-east-1a', u'ami_launch_index': u'0', u'dns_name': u'ec2-52-23-158-7.compute-1.amazonaws.com', u'region': u'us-east-1', u'ebs_optimized': False, u'launch_time': u'2015-11-29T04:24:40.000Z', u'instance_type': u't2.micro', u'state': u'running', u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'architecture': u'x86_64'})

TASK: [ec2 | 作成したEC2インスタンスをインベントリに追加] ***
ok: [localhost -> 127.0.0.1] => (item={u'ramdisk': None, u'kernel': None, u'root_device_type': u'ebs', u'private_dns_name': u'ip-10-0-0-11.ec2.internal', u'tags': {}, u'key_name': u'virginia_key', u'public_ip': u'52.23.158.7', u'image_id': u'ami-57cd8732', u'tenancy': u'default', u'private_ip': u'10.0.0.11', u'groups': {u'sg-bbf176df': u'default', u'sg-01237c67': u'webgroup'}, u'public_dns_name': u'ec2-52-23-158-7.compute-1.amazonaws.com', u'state_code': 16, u'id': u'i-a6f60810', u'placement': u'us-east-1a', u'ami_launch_index': u'0', u'dns_name': u'ec2-52-23-158-7.compute-1.amazonaws.com', u'region': u'us-east-1', u'ebs_optimized': False, u'launch_time': u'2015-11-29T04:24:40.000Z', u'instance_type': u't2.micro', u'state': u'running', u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'virtualization_type': u'hvm', u'architecture': u'x86_64'})

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
The authenticity of host '52.23.158.7 (52.23.158.7)' can't be established.
RSA key fingerprint is d8:8c:b2:3d:dd:d8:c3:7b:29:ec:41:72:29:ca:da:bf.
Are you sure you want to continue connecting (yes/no)? yes
ok: [52.23.158.7]

TASK: [growpart | epel レポジトリのインストール] ******************
changed: [52.23.158.7]

TASK: [growpart | yum パッケージのインストール] *******************
changed: [52.23.158.7] => (item=cloud-utils-growpart,dracut-modules-growroot)

TASK: [growpart | パーティション拡張の指示] ***********************
ok: [52.23.158.7]

TASK: [growpart | reboot!] ****************************************************
changed: [52.23.158.7]

TASK: [growpart | wait for SSH port down] *************************************
ok: [52.23.158.7 -> 127.0.0.1]

TASK: [growpart | wait for SSH port up] ***************************************
ok: [52.23.158.7 -> 127.0.0.1]

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [52.23.158.7]

TASK: [common | SELinux を無効にする] ***********************************
changed: [52.23.158.7]

TASK: [common | ipv6 を無効にする(/etc/sysconfig/network)] **************
changed: [52.23.158.7]

TASK: [common | ipv6 を無効にする(/etc/modprobe.d/)] ********************
changed: [52.23.158.7]

TASK: [common | Firewall を無効にする] **********************************
changed: [52.23.158.7] => (item=iptables)
changed: [52.23.158.7] => (item=ip6tables)

TASK: [common | 日本語(ja_JP.UTF-8) にする] *****************************
changed: [52.23.158.7]

TASK: [common | タイムゾーンを Asia/Tokyo にする] *******************
changed: [52.23.158.7]

TASK: [common | ローカルタイムを Asia/Tokyo にする] ****************
changed: [52.23.158.7]

TASK: [common | yum アップデート] ***************************************
changed: [52.23.158.7]

TASK: [common | reboot!] ******************************************************
changed: [52.23.158.7]

TASK: [common | wait for SSH port down] ***************************************
ok: [52.23.158.7 -> 127.0.0.1]

TASK: [common | wait for SSH port up] *****************************************
ok: [52.23.158.7 -> 127.0.0.1]

TASK: [apache | Apache のインストール] *********************************
changed: [52.23.158.7]

TASK: [apache | Apache の自動起動ON] *************************************
changed: [52.23.158.7]

TASK: [apache | Apache の起動] *********************************************
changed: [52.23.158.7]

PLAY RECAP ********************************************************************
52.23.158.7                : ok=22   changed=15   unreachable=0    failed=0
localhost                  : ok=3    changed=1    unreachable=0    failed=0

[infra@node01 ansible]$

動作確認


EC2インスタンスにログインして、パーティションの拡張や、OS設定が成功しているか確認します。
[centos@ip-10-0-0-11 ~]$ getenforce
Disabled
[centos@ip-10-0-0-11 ~]$ lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  10G  0 disk
└─xvda1 202:1    0  10G  0 part /
[centos@ip-10-0-0-11 ~]$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/xvda1     ext4   9.8G  867M  8.4G  10% /
tmpfs          tmpfs  498M     0  498M   0% /dev/shm
[centos@ip-10-0-0-11 ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP qlen 1000
    link/ether 0a:7f:41:05:7d:b5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.11/24 brd 10.0.0.255 scope global eth0
[centos@ip-10-0-0-11 ~]$ date
2015年 11月 29日 日曜日 13:40:06 JST
[centos@ip-10-0-0-11 ~]$ chkconfig | grep iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off
[centos@ip-10-0-0-11 ~]$ chkconfig | grep ip6tables
ip6tables       0:off   1:off   2:off   3:off   4:off   5:off   6:off
[centos@ip-10-0-0-11 ~]$ chkconfig | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[centos@ip-10-0-0-11 ~]$ curl -I http://localhost
HTTP/1.1 403 Forbidden
Date: Sun, 29 Nov 2015 04:40:39 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 4961
Connection: close
Content-Type: text/html; charset=UTF-8

[centos@ip-10-0-0-11 ~]$

パーティションも10GBに拡張され、OS設定も成功しています。


◆ 補足

あとで、Webサーバの変更をしたい場合は、イベントリ(production)の webserversグループにEC2インスタンスのIPアドレスを記載します。
以下のように webservers グループだけ指定したり、タグを指定したりして一部のタスクのみ実行できます。

[infra@node01 ansible]$ ansible-playbook site.yml -i production -l webservers
[infra@node01 ansible]$ ansible-playbook site.yml -i production --tags os_basic












2015年11月12日木曜日

[CentOS6] yum と gem で serverspec をインストールする


CentOS6.7 に yum と gem で serverspec をインストールします。

まず、 yum で ruby と rubygems をいれます。
[root@localhost ~]# yum install ruby rubygems

つぎに、gem で serverspec をいれようとすると以下のようにエラーになりました。
[root@localhost ~]# gem install serverspec
ERROR:  Error installing serverspec:
       net-ssh requires Ruby version >= 2.0.


最新の net-ssh は、ruby2.0 以上が必要みたいですが、CentOS6.7 の ruby は 1.8.7 です。
[root@localhost ~]# ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]

インストール可能な net-ssh のバージョンを確認すると以下のとおり。
[root@localhost ~]# gem list net-ssh -ar

*** REMOTE GEMS ***

net-ssh (3.0.1, 2.9.2, 2.9.1, 2.9.0, 2.8.0, 2.7.0, 2.6.8, 2.6.7, 2.6.6, 2.6.5, 2.6.4, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.2, 2.5.1, 2.5.0, 2.4.0, 2.3.0, 2.2.2, 2.2.1, 2.2.0, 2.1.4, 2.1.3, 2.1.0, 2.0.24, 2.0.23, 2.0.22, 2.0.21, 2.0.20, 2.0.19, 2.0.18, 2.0.17, 2.0.16, 2.0.15, 2.0.14, 2.0.13, 2.0.11, 2.0.10, 2.0.9, 2.0.8, 2.0.7, 2.0.6, 2.0.5, 2.0.4, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 1.1.4, 1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.10, 1.0.9, 1.0.8, 1.0.7, 1.0.6, 1.0.5, 1.0.4, 1.0.3, 1.0.2, 1.0.1, 1.0.0, 0.9.0, 0.6.0, 0.5.0)
net-ssh-askpass (0.0.3)
net-ssh-gateway (1.2.0, 1.1.0, 1.0.1, 1.0.0)
net-ssh-kerberos (0.2.7, 0.2.6, 0.2.5, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0, 0.1.3, 0.1.2, 0.1.0)
net-ssh-krb (0.4.0, 0.3.0)
net-ssh-multi (1.2.1, 1.2.0, 1.1, 1.0.1, 1.0.0)
net-ssh-net-ssh (2.0.14, 2.0.13, 2.0.12)
net-ssh-ng (2.10.2.1, 2.10.2, 2.10.1, 2.10.0, 2.9.1)
net-ssh-open3 (0.1.6, 0.1.5, 0.1.4, 0.1.3, 0.1.2)
net-ssh-plus (0.0.2, 0.0.1)
net-ssh-session (0.1.6, 0.1.5, 0.1.4, 0.1.3, 0.1.2, 0.1.1, 0.1.0)
net-ssh-shell (0.2.0, 0.1.0)
net-ssh-simple (1.6.12, 1.6.11, 1.6.10, 1.6.9, 1.6.8, 1.6.7, 1.6.6, 1.6.5, 1.6.4, 1.6.3, 1.6.2, 1.6.1, 1.6.0, 1.5.9, 1.5.8, 1.5.4, 1.5.3, 1.5.2, 1.5.1, 1.5.0, 1.4.5, 1.4.1, 1.4.0, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.1, 1.1.0, 1.0.1, 1.0.0, 0.9.0)
net-ssh-socks (0.0.2, 0.0.1)
net-ssh-telnet (0.0.2, 0.0.1)

とりあえず、先に net-ssh だけ、古いバージョン(2.9.2)を入れます。
[root@localhost ~]# gem install net-ssh --version "=2.9.2"
Successfully installed net-ssh-2.9.2
1 gem installed
Installing ri documentation for net-ssh-2.9.2...
Installing RDoc documentation for net-ssh-2.9.2...

これで、serverspec が入ります
[root@localhost ~]# gem install serverspec
Successfully installed net-scp-1.2.1
Successfully installed net-telnet-0.1.1
Successfully installed sfl-2.2
Successfully installed specinfra-2.44.1
Successfully installed serverspec-2.24.2
5 gems installed
Installing ri documentation for net-scp-1.2.1...
Installing ri documentation for net-telnet-0.1.1...
Installing ri documentation for sfl-2.2...
Installing ri documentation for specinfra-2.44.1...
Installing ri documentation for serverspec-2.24.2...
Installing RDoc documentation for net-scp-1.2.1...
Installing RDoc documentation for net-telnet-0.1.1...
Installing RDoc documentation for sfl-2.2...
Installing RDoc documentation for specinfra-2.44.1...
Installing RDoc documentation for serverspec-2.24.2...

最後に rake をいれます。
[root@localhost ~]# gem install rake
Successfully installed rake-10.4.2
1 gem installed
Installing ri documentation for rake-10.4.2...
Installing RDoc documentation for rake-10.4.2...










2015年11月3日火曜日

[CentOS7] Pacemaker + DRBD (Apacheクラスタ)


前回までの記事で、Pacemaker に Apache関連のリソースを登録し、stonith も実装しました。

今回は、pm01とpm02のディスクをDRBDでミラーして、Apacheのドキュメントルートを2台で共有するようにします。
これで、フェイルオーバしても、Apache は同じ情報を提供できるし、リリース先も稼働系だけでOKになります。

環境は以下のとおり。
サーバはXenServer6.5の仮想マシンです。
  • pm01: 10.1.0.71
  • pm02: 10.1.0.72
  • drbd用ディスク: /dev/xvdb


1.DRBDを導入する


1.1.DRBDをインストールする


DRBDは、ppm01とpm02に yum でインストールします。
以下にようにDRBD用にレポジトリを登録してインストールします。
[root@pm01 ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@pm01 ~]# yum install http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
[root@pm01 ~]# yum install drbd84-utils kmod-drbd84
[root@pm02 ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@pm02 ~]# yum install http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
[root@pm02 ~]# yum install drbd84-utils kmod-drbd84

1.1.DRBD用のボリューム(ディスク)を用意する


DRBD用に pm01とpm02 に新規でディスクを追加しました。
XenServer の仮想マシンの場合は、パーティションを切らなくてもファイルシステムを作成できるので、パーティションはきりません。
以下は、pm01 の例ですが、/dev/xvdb が DRBD 用です。
pm02 も同じ構成にします。
[root@pm01 ~]# lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda    202:0    0    8G  0 disk
├─xvda1 202:1    0  500M  0 part /boot
└─xvda2 202:2    0  7.5G  0 part /
xvdb    202:16   0    2G  0 disk
[root@pm01 ~]# fdisk -l /dev/xvdb

Disk /dev/xvdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

1.2.DRBDのレポジトリ設定


DRBDのレポジトリを設定します。
pm01 の /etc/drbd.d/drbd0.res に以下の内容を記載します。
[root@pm01 ~]# cat /etc/drbd.d/drbd0.res
resource drbd0 {
        protocol C;

        disk /dev/xvdb;
        device /dev/drbd0;
        meta-disk internal;
        on pm01 {
                address 10.1.0.71:7789;
        }
        on pm02 {
                address 10.1.0.72:7789;
        }
}
同じファイルを pm02 にコピーします。
[root@pm01 ~]# scp -p /etc/drbd.d/drbd0.res pm02:/etc/drbd.d/.
pm01とpm02で /dev/xvdb にメタデータを書き込んで初期化します。
[root@pm01 ~]# drbdadm create-md drbd
~省略~
you are the 1574th user to install this version
initializing activity log
NOT initializing bitmap
Writing meta data...
New drbd meta data block successfully created.
success
[root@pm02 ~]# drbdadm create-md drbd
~省略~
you are the 9920th user to install this version
initializing activity log
NOT initializing bitmap
Writing meta data...
New drbd meta data block successfully created.
success 
pm01とpm02でリソースを有効にします。
[root@pm01 ~]# drbdadm up drbd0
[root@pm01 ~]# cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by phil@Build64R7, 2015-04-10 05:13:52
 0: cs:WFConnection ro:Secondary/Unknown ds:Inconsistent/DUnknown C r----s
    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:2097052
[root@pm02 ~]# drbdadm up drbd0
[root@pm02 ~]# cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by phil@Build64R7, 2015-04-10 05:13:52
 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:2097052
同期を開始します。
pm01をプライマリとして同期したいので pm01 で以下のように drbdadm を実行し、cat で同期の状態を見ます。
[root@pm01 ~]# drbdadm primary --force drbd0
[root@pm01 ~]#  cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by phil@Build64R7, 2015-04-10 05:13:52
 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
    ns:19356 nr:0 dw:0 dr:20084 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:2077696
        [>....................] sync'ed:  1.0% (2077696/2097052)K
        finish: 0:10:36 speed: 3,224 (3,224) K/sec
同期が完了すると以下のように表示されます。
[root@pm01 ~]#  cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by phil@Build64R7, 2015-04-10 05:13:52
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
    ns:2097052 nr:0 dw:0 dr:2097780 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
drbd-overview で見た drbd の状態は以下のとおり。
pm01 がプライマリで pm02 がセカンダリになり、pm01 ⇒ pm02の方向に同期されます。
[root@pm01 ~]# drbd-overview
 0:drbd0/0  Connected Primary/Secondary UpToDate/UpToDate
[root@pm02 ~]# drbd-overview
 0:drbd0/0  Connected Secondary/Primary UpToDate/UpToDate

1.3.ファイルシステムの作成


pm01 で DRBD用のボリュームにファイルシステムを作成します。
[root@pm01 ~]# mkfs.ext4 /dev/drbd0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524263 blocks
26213 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
マウントして、試しにディレクトリとファイルを作成してみます。
[root@pm01 ~]# mount /dev/drbd0 /mnt
[root@pm01 ~]# mkdir /mnt/test
[root@pm01 ~]# touch /mnt/xx0
[root@pm01 ~]# ls -l /mnt
合計 20
drwx------ 2 root root 16384 11月  3 08:49 lost+found
drwxr-xr-x 2 root root  4096 11月  3 08:49 test
-rw-r--r-- 1 root root     0 11月  3 08:50 xx0
[root@pm01 ~]# mount | grep drbd0
/dev/drbd0 on /mnt type ext4 (rw,relatime,data=ordered)
[root@pm01 ~]# df -Th
ファイルシス   タイプ   サイズ  使用  残り 使用% マウント位置
/dev/xvda2     ext4       7.3G  1.4G  5.6G   20% /
devtmpfs       devtmpfs   476M     0  476M    0% /dev
tmpfs          tmpfs      495M     0  495M    0% /dev/shm
tmpfs          tmpfs      495M   13M  483M    3% /run
tmpfs          tmpfs      495M     0  495M    0% /sys/fs/cgroup
/dev/xvda1     ext3       477M   66M  387M   15% /boot
/dev/drbd0     ext4       2.0G  6.1M  1.9G    1% /mnt

1.4.手動のフェイルオーバ


手動でフェイルオーバさせてみます。
まず、pm01 をセカンダリにします。
[root@pm01 ~]# umount /mnt
[root@pm01 ~]# drbdadm secondary drbd0
[root@pm01 ~]# drbd-overview
 0:drbd0/0  Connected Secondary/Secondary UpToDate/UpToDate
次に、pm02 をプライマリにして、ファイルが見えるか確認します。
[root@pm02 ~]# drbdadm primary drbd0
[root@pm02 ~]# mount /dev/drbd0 /mnt
[root@pm02 ~]# ls -l /mnt
合計 20
drwx------ 2 root root 16384 11月  3 08:49 lost+found
drwxr-xr-x 2 root root  4096 11月  3 08:49 test
-rw-r--r-- 1 root root     0 11月  3 08:50 xx0
[root@pm02 ~]# drbd-overview
 0:drbd0/0  Connected Primary/Secondary UpToDate/UpToDate /mnt ext4 2.0G 6.1M 1.9G 1%
[root@pm02 ~]#

2.Apacheのドキュメントルートを変更する


Apacheのドキュメントルート(/var/www/html)を変更します。
設定ファイルを変更してもよかったのですが、今回は、DRBD用のボリュームにシンボリックリンクしました。
pm01 と pm02 でドキュメントルートを変更します。
[root@pm01 ~]# cd /var/www/
[root@pm01 www]# ls
cgi-bin  html
[root@pm01 www]# mv html html_BK
[root@pm01 www]# ln -s /mnt ./html
[root@pm01 www]# ls -l
合計 8
drwxr-xr-x 2 root root 4096  8月 25 03:12 cgi-bin
lrwxrwxrwx 1 root root    4 11月  3 10:07 html -> /mnt
drwxr-xr-x 2 root root 4096  8月 25 03:12 html_BK
あとで、動作確認できるように、テスト用のページも作成します。
pm01 を DRBD のプライマリにして、/mnt/index.html を作成します。
内容は以下のとおり。
[root@pm01 www]# cat /mnt/index.html
<html>
<head>
<title>TEST PAGE</title>
</head>
<body>
<h1>TEST PAGE</h1>
</body>
</html>

3.PacemakerにDRBDのリソースを登録する


DRBDのサービスを有効にするリソースを登録します。
[root@pm01 ~]# pcs resource create drbd0_res ocf:linbit:drbd params drbd_resource="drbd0"
DRBDのサービスをマスタ/スレーブにします。
[root@pm01 ~]# pcs resource master ms_drbd0 drbd0_res master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
DRBD用のボリュームをマウントするリソースを登録します。
[root@pm01 ~]# pcs resource create fsdrbd0_res Filesystem device=/dev/drbd0 directory=/mnt fstype=ext4
DRBDのマウントがマスタで動くようにします。
[root@pm01 ~]# pcs constraint colocation add fsdrbd0_res ms_drbd0 INFINITY with-rsc-role=Master
[root@pm01 ~]# pcs constraint order promote ms_drbd0 then start fsdrbd0_res
Adding ms_drbd0 fsdrbd0_res (kind: Mandatory) (Options: first-action=promote then-action=start)
リソースグループを変更します。
drbdのマウントをWebGroupに入れて、VIPチェックの次に起動するようにします。
[root@pm01 ~]# pcs resource group remove WebGroup vipcheck_res vip_res httpd_res
[root@pm01 ~]# pcs resource group add WebGroup vipcheck_res fsdrbd0_res vip_res httpd_res
クラスタの状態を確認します。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Tue Nov  3 10:49:35 2015          Last change: Tue Nov  3 10:46:21 2015 by root via crm_attribute on pm02
Stack: corosync
Current DC: pm02 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 10 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]
 fence_pm01     (stonith:fence_xenapi): Started pm02
 fence_pm02     (stonith:fence_xenapi): Started pm01
 Master/Slave Set: ms_drbd0 [drbd0_res]
     Masters: [ pm01 ]
     Slaves: [ pm02 ]
 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     fsdrbd0_res        (ocf::heartbeat:Filesystem):    Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01

PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled

4.動作確認


まずは、pm01 が稼働系の場合に、VIPにアクセスしてテストページが表示できることを確認します。
[root@pm01 ~]# pcs status resources
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]
 Master/Slave Set: ms_drbd0 [drbd0_res]
     Masters: [ pm01 ]
     Slaves: [ pm02 ]
 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started
     fsdrbd0_res        (ocf::heartbeat:Filesystem):    Started
     vip_res    (ocf::heartbeat:IPaddr2):       Started
     httpd_res  (ocf::heartbeat:apache):        Started
[root@pm01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 4a:73:98:ab:45:82 brd ff:ff:ff:ff:ff:ff
    inet 10.1.0.71/32 brd 10.1.0.71 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.1.0.70/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4873:98ff:feab:4582/64 scope link
       valid_lft forever preferred_lft forever
[root@pm01 ~]# curl http://10.1.0.70
<html>
<head>
<title>TEST PAGE</title>
</head>
<body>
<h1>TEST PAGE</h1>
</body>
</html>
[root@pm01 ~]#
手動でフェイルオーバさせます。
[root@pm01 ~]# pcs cluster standby pm01
VIPにアクセスしてテストページが見えることを確認します。
[root@pm01 ~]# pcs status resources
 Clone Set: ping-clone [ping]
     Started: [ pm02 ]
     Stopped: [ pm01 ]
 Master/Slave Set: ms_drbd0 [drbd0_res]
     Masters: [ pm02 ]
     Stopped: [ pm01 ]
 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started
     fsdrbd0_res        (ocf::heartbeat:Filesystem):    Started
     vip_res    (ocf::heartbeat:IPaddr2):       Started
     httpd_res  (ocf::heartbeat:apache):        Started
[root@pm01 ~]# curl http://10.1.0.70
<html>
<head>
<title>TEST PAGE</title>
</head>
<body>
<h1>TEST PAGE</h1>
</body>
</html>
[root@pm01 ~]# 


2015年11月1日日曜日

[CentOS7] Pacemaker に stonith を実装する(fence_xenapi)


前回までに リソースを登録して、Apacheクラスタ(Active/Standby)を構築しました。

今回は、stonith を実装します。 stonith については下記URLが参考になります。
私の検証環境は、XenServer6.5 の仮想マシンです。
XenServer用の fence デバイスを使って stonith を実装します。
環境は以下のとおり。
  • pm01(L81pm01): 10.1.0.71
  • pm02(L82pm02): 10.1.0.72
  • XenServerホスト(Dom0): 192.168.1.100


1.fence_xenapi の導入


pm01 と pm02 に fence_xenapi をインストールします。

fence_agent のパッケージをインストールします。
[root@pm01 ~]# yum install fence-agents-all
下記URLから fence_xenapi をダウンロードします。
ダウンロードしたファイルを、/usr/sbin/fence_xenapi に保存して、実行権限をつけます。
[root@pm01 ~]# vi /usr/sbin/fence_xenapi
[root@pm01 ~]# chmod 755 /usr/sbin/fence_xenapi
下記URLから XenAPI をダウンロードします。
ダウンロードしたファイルを、/usr/sbin/XenAPI.py に保存します。
[root@pm01 ~]# vi /usr/sbin/XenAPI.py
動作を確認します。
以下のように実行して、仮想マシンのステータスを表示できればOKです。
[root@pm01 ~]# fence_xenapi -l root -p <パスワード> -o status -s https://192.168.1.100 -n L80pm01
Status: ON

2.stonith の設定


pm01 をリブートする stonith を登録します。
相打ちにならないように delay を設定します。
[root@pm01 ~]# pcs stonith create fence_pm01 fence_xenapi session_url="https://192.168.1.100" action="reboot" login="root" passwd_script="/root/xs.pw" port="L80pm01" delay=15 pcmk_host_list=pm01 pcmk_host_check=static-list pcmk_host_map="" op monitor interval=60s
pm02 をリブートする stonith を登録します。
[root@pm01 ~]# pcs stonith create fence_pm02 fence_xenapi session_url="https://192.168.1.100" action="reboot" login="root" passwd_script="/root/xs.pw" port="L81pm02" pcmk_host_list=pm02 pcmk_host_check=static-list pcmk_host_map="" op monitor interval=60s
stonith では、自ノードをリブートしない。相手をリブートするようにします。
fence_pm01 を pm01 以外で起動するように設定します。
[root@pm01 ~]# pcs constraint location fence_pm01 prefers pm01="-INFINITY"
fence_pm02 を pm02 以外で起動するように設定します。
[root@pm01 ~]# pcs constraint location fence_pm02 prefers pm02="-INFINITY"
stonith を有効にします。
[root@pm01 ~]# pcs property set stonith-enabled=true
状態を確認します。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 16:52:16 2015          Last change: Sun Nov  1 14:49:56 2015 by root via cibadmin on pm02
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 7 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]
 fence_pm01     (stonith:fence_xenapi): Started pm02
 fence_pm02     (stonith:fence_xenapi): Started pm01

PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled

3.動作確認


手動でフェンシングを発動します。
以下のように実行して pm02 がリブートされたらOKです。
[root@pm01 ~]# stonith_admin --reboot pm02

ハートビートを遮断してみます。
pm01 で以下のように実行します。
[root@pm01 ~]# iptables -I INPUT -p all -j DROP -s 10.1.0.72
pm02 がリブートされます。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 16:57:43 2015          Last change: Sun Nov  1 14:49:56 2015 by root via cibadmin on pm02
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 7 resources configured

Node pm02: UNCLEAN (offline)
Online: [ pm01 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01
 Clone Set: ping-clone [ping]
     ping       (ocf::pacemaker:ping):  Started pm02 (UNCLEAN)
     Started: [ pm01 ]
 fence_pm01     (stonith:fence_xenapi): Started pm02 (UNCLEAN)
 fence_pm02     (stonith:fence_xenapi): Started pm01

PCSD Status:
  pm01: Online


[CentOS7] Pacemaker のリソースの動作確認(Apacheのクラスタ)


前回は、Pacemaker にリソースを登録して、Apacheクラスタを構築しました。

今回は、Pacemaker の動作確認をします。


1.クラスタ状態の確認


"pcs status" で状態を見ようとしたところ、反応が鈍くて、下記表示で少し待ちます。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 08:43:48 2015          Last change: Sun Nov  1 08:42:10 2015 by root via crm_resource on pm02
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

PCSD Status:
  pm01: Online

VIPのDNSの逆ひきで遅くなってたようなので、pm01とpm02の hosts にVIPを登録したら、反応が良くなりました。
10.1.0.70   pm00
10.1.0.71   pm01
10.1.0.72   pm02

なお、もう一つ、検証時に使える確認方法があります。
"crm_mon" コマンドを使用すると、クラスタの状態をリアルタイムで監視できます。



pm01 の仮想IPアドレスは以下のとおり。
[root@pm01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 4a:73:98:ab:45:82 brd ff:ff:ff:ff:ff:ff
    inet 10.1.0.71/32 brd 10.1.0.71 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.1.0.70/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4873:98ff:feab:4582/64 scope link
       valid_lft forever preferred_lft forever
Apacheのプロセスは以下のとおり。
[root@pm01 ~]# ps -ef | grep httpd
root     17503     1  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
apache   17506 17503  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
apache   17507 17503  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
apache   17509 17503  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
apache   17510 17503  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
apache   17512 17503  0 09:56 ?        00:00:00 /sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf -c PidFile /var/run//httpd.pid
root     17935  2102  0 09:57 pts/0    00:00:00 grep --color=auto httpd

2.ApacheプロセスKILLによるF/Oの確認


Apache のプロセスを KILL してF/Oするかどうか確認します。
以下のようにして pm01 で httpd をKILLします。
[root@pm01 ~]# pkill httpd
しばらく待つと、以下のように pm02 にフェイルオーバしました。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 09:53:00 2015          Last change: Sun Nov  1 09:45:44 2015 by root via crm_attribute on pm01
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm02
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm02
     httpd_res  (ocf::heartbeat:apache):        Started pm02
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

Failed Actions:
* httpd_res_monitor_10000 on pm01 'not running' (7): call=139, status=complete, exitreason='none',
    last-rc-change='Sun Nov  1 09:51:23 2015', queued=0ms, exec=0ms


PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled
リソース再起動の回数を変えて、もう一度試してみます。
まず、エラーをクリアします。
[root@pm01 ~]# pcs resource cleanup httpd_res
Resource: httpd_res successfully cleaned up
次にリソースを pm01 に戻します。
pm02 を OFFLINE にして、リソースの移動を確認した後、ONLINEにもどします。
[root@pm01 ~]# rpcs cluster standby pm02
[root@pm01 ~]# pcs status
[root@pm01 ~]# pcs cluster unstandby pm02
リソースの再起動回数を "2" にします。
[root@pm01 ~]# pcs resource defaults resource-stickiness=INFINITY migration-threshold=2
これで、再テストできるようになったので、また、httpd を KILL します。
今度は、pm01 で httpd が再起動されました。
[root@pm01 ~]# pkill httpd
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 10:25:16 2015          Last change: Sun Nov  1 10:24:51 2015 by root via crm_attribute on pm01
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

Failed Actions:
* httpd_res_monitor_10000 on pm01 'not running' (7): call=271, status=complete, exitreason='none',
    last-rc-change='Sun Nov  1 10:25:12 2015', queued=0ms, exec=0ms


PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled
この状態で、また、httpd をKILL すると、こんどは、pm02 にF/Oしました。
[root@pm01 ~]# pkill httpd
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 10:26:11 2015          Last change: Sun Nov  1 10:24:51 2015 by root via crm_attribute on pm01
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm02
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm02
     httpd_res  (ocf::heartbeat:apache):        Started pm02
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

Failed Actions:
* httpd_res_monitor_10000 on pm01 'not running' (7): call=275, status=complete, exitreason='none',
    last-rc-change='Sun Nov  1 10:25:53 2015', queued=0ms, exec=0ms


PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled


3.ネットワーク障害によるF/Oの確認


デフォルトGWへの疎通障害でF/Oするかどうか確認します。
pm01で以下のようにして、デフォルトGWへの ping が失敗するようにします。
[root@pm01 ~]# iptables -I INPUT -p icmp -j DROP -s 10.1.0.254
しばらくすると、以下のように pm02 にF/Oします。
[root@pm01 ~]# pcs status
Cluster name: cluster_test
Last updated: Sun Nov  1 10:34:39 2015          Last change: Sun Nov  1 10:31:09 2015 by root via crm_attribute on pm01
Stack: corosync
Current DC: pm01 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm02
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm02
     httpd_res  (ocf::heartbeat:apache):        Started pm02
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled

pm01、pm02 の両方が、デフォルトGWに疎通NGの場合は、リソースが停止します。






2015年10月31日土曜日

[CentOS7] Pacemaker のリソースを登録する(Apacheのクラスタ)


以前書いた記事で、CentOS7にPacemaker+Corosyncをインストールしました。

今回は、下記のリソースを使用して、Apacheのクラスタ(Active/Standby) を試してみます。

  • VIPCheck
    一番最初に起動させてVIPに疎通できるかチェックし、疎通できた場合は後続のリソースを起動しないようにします。(スプリットブレインの防止)
  • IPaddr2
    仮想IPアドレス(VIP)を作成します。
  • apache
    apache を起動します。
  • ping
    ネットワークを監視して、デフォルトGWに疎通できるサーバでリソースが起動するようにします。

ping 以外のリソースは同一グループ(WebGroup)に所属させることで、一緒に行動するようにします。VIPCheck/IPaddr2/apacheは常に同じサーバ(Active)で動くようにします。
ping は Active/Standby の両方で動かします。

構成情報は以下のとおり
  • pm01: 10.1.0.71
  • pm02: 10.1.0.72
  • vip: 10.1.0.70
  • デフォルトGW: 10.1.0.254


1.VIPCheck の設定


VIPCheckは、下記URLで配布しているので、ソース(シェルスクリプト)を入手します。
pm01 と pm02 に VIPCheck を配置します。私はコピペしました。
[root@pm01 ~]# cd /usr/lib/ocf/resource.d/
[root@pm01 resource.d]# mkdir myres
[root@pm01 resource.d]# vi myres/VIPCheck
[root@pm01 resource.d]# chmod 755 myres/VIPCheck
[root@pm01 resource.d]# ls -l myres/VIPCheck
-rwxr-xr-x 1 root root 5463 10月 31 20:30 myres/VIPCheck
[root@pm02 ~]# cd /usr/lib/ocf/resource.d/
[root@pm02 resource.d]# mkdir myres
[root@pm02 resource.d]# vi myres/VIPCheck
[root@pm02 resource.d]# chmod 755 myres/VIPCheck
[root@pm02 resource.d]# ls -l myres/VIPCheck
-rwxr-xr-x 1 root root 5463 10月 31 20:30 myres/VIPCheck
以下のようにリソースを登録します。"--group" でリソースを所属させるグループを指定しています。グループに追加した順番で起動するようになります。
[root@pm01 ~]# pcs resource create vipcheck_res ocf:myres:VIPCheck target_ip=10.1.0.70 count=1 wait=10 --group WebGroup
stop と monitor は不要なので削除します。
[root@pm01 ~]# pcs resource op remove vipcheck_res stop
[root@pm01 ~]# pcs resource op remove vipcheck_res monitor
start のパラメータを変更したいので、一旦削除して追加します。
[root@pm01 ~]# pcs resource op remove vipcheck_res start
[root@pm01 ~]# pcs resource op add vipcheck_res start interval=0s timeout=90s on-fail=restart start-delay=4s

2.VIPの設定


pm01とpm02のNICは以下のように eth0 という名前にしています。
[root@pm01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 4a:73:98:ab:45:82 brd ff:ff:ff:ff:ff:ff
    inet 10.1.0.71/32 brd 10.1.0.71 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4873:98ff:feab:4582/64 scope link
       valid_lft forever preferred_lft forever
[root@pm02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether ca:f9:d7:75:bd:8b brd ff:ff:ff:ff:ff:ff
    inet 10.1.0.72/32 brd 10.1.0.72 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::c8f9:d7ff:fe75:bd8b/64 scope link
       valid_lft forever preferred_lft forever
eth0に仮想IPアドレスを設定します。
以下のようにリソースを登録します。
[root@pm01 ~]# pcs resource create vip_res ocf:heartbeat:IPaddr2 nic=eth0 ip=10.1.0.70 cidr_netmask=24 --group WebGroup

3.Apacheの設定


Apacheは、server-status が使用できるように設定します。
pm01、pm02の/etc/httpd/conf.d/status.conf に以下の内容を記述します。
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from localhost
</Location>
Apacheは、Pacemakerで起動/停止するので、OSの自動起動/停止はOFFにします。
[root@pm01 ~]# systemctl list-unit-files -t service | grep httpd
httpd.service                               disabled
[root@pm02 ~]# systemctl list-unit-files -t service | grep httpd
httpd.service                               disabled
以下のようにリソースを登録します。
[root@pm01 ~]# pcs resource create httpd_res apache configfile="/etc/httpd/conf/httpd.conf" statusurl="http://127.0.0.1/server-status" --group WebGroup

4.ping の設定


以下のようにリソースを登録します。 "--clone" を指定することで、クラスタの全ノードで起動するようになります。
[root@pm01 ~]# pcs resource create ping ocf:pacemaker:ping dampen=5s multiplier=1000 host_list=10.1.0.254 --clone
デフォルトGWに疎通可能なサーバで WebGrop のリソースが起動するように設定します。
[root@pm01 ~]# pcs constraint location WebGroup rule score=-INFINITY pingd lt 1 or not_defined pingd

5.自動フェイルバックを無効にする


自動フェイルバックを無効にし、リソース異常時に同一サーバでリソースを再起動する回数を1回にします。
[root@pm01 ~]# pcs resource defaults resource-stickiness=INFINITY migration-threshold=1

6.pm01 が優先して Active になるようにする


pm01/pm02を同時に起動した場合などは、pm01が優先して Activeになるように設定します。
[root@pm01 ~]# pcs constraint location WebGroup prefers pm01=50

7.確認


設定が成功していれば、以下のようにリソースが起動します
[root@pm01 resource.d]# pcs status
Cluster name: cluster_test
Last updated: Sat Oct 31 22:41:27 2015          Last change: Sat Oct 31 22:41:23 2015 by root via cibadmin on pm01
Stack: corosync
Current DC: pm02 (version 1.1.13-a14efad) - partition with quorum
2 nodes and 5 resources configured

Online: [ pm01 pm02 ]

Full list of resources:

 Resource Group: WebGroup
     vipcheck_res       (ocf::myres:VIPCheck):  Started pm01
     vip_res    (ocf::heartbeat:IPaddr2):       Started pm01
     httpd_res  (ocf::heartbeat:apache):        Started pm01
 Clone Set: ping-clone [ping]
     Started: [ pm01 pm02 ]

PCSD Status:
  pm01: Online
  pm02: Online

Daemon Status:
  corosync: active/disabled
  pacemaker: active/disabled
  pcsd: active/enabled
設定内容は以下のようにして確認できます。
[root@pm01 resource.d]# pcs config
Cluster Name: cluster_test
Corosync Nodes:
 pm01 pm02
Pacemaker Nodes:
 pm01 pm02

Resources:
 Group: WebGroup
  Resource: vipcheck_res (class=ocf provider=myres type=VIPCheck)
   Attributes: target_ip=10.1.0.70 count=1 wait=10
   Operations: start timeout=90s on-fail=restart interval=0s start-delay=4s (vipcheck_res-name-start-interval-0s-on-fail-restart-start-delay-4s-timeout-90s)
  Resource: vip_res (class=ocf provider=heartbeat type=IPaddr2)
   Attributes: nic=eth0 ip=10.1.0.70 cidr_netmask=24
   Operations: start interval=0s timeout=20s (vip_res-start-timeout-20s)
               stop interval=0s timeout=20s (vip_res-stop-timeout-20s)
               monitor interval=10s timeout=20s (vip_res-monitor-interval-10s)
  Resource: httpd_res (class=ocf provider=heartbeat type=apache)
   Attributes: configfile=/etc/httpd/conf/httpd.conf statusurl=http://127.0.0.1/server-status
   Operations: start interval=0s timeout=40s (httpd_res-start-timeout-40s)
               stop interval=0s timeout=60s (httpd_res-stop-timeout-60s)
               monitor interval=10 timeout=20s (httpd_res-monitor-interval-10)
 Clone: ping-clone
  Resource: ping (class=ocf provider=pacemaker type=ping)
   Attributes: dampen=5s multiplier=1000 host_list=10.1.0.254
   Operations: start interval=0s timeout=60 (ping-start-timeout-60)
               stop interval=0s timeout=20 (ping-stop-timeout-20)
               monitor interval=10 timeout=60 (ping-monitor-interval-10)

Stonith Devices:
Fencing Levels:

Location Constraints:
  Resource: WebGroup
    Enabled on: pm01 (score:50) (id:location-WebGroup-pm01-50)
    Constraint: location-WebGroup
      Rule: score=-INFINITY boolean-op=or  (id:location-WebGroup-rule)
        Expression: pingd lt 1  (id:location-WebGroup-rule-expr)
        Expression: not_defined pingd  (id:location-WebGroup-rule-expr-1)
Ordering Constraints:
Colocation Constraints:

Cluster Properties:
 cluster-infrastructure: corosync
 cluster-name: cluster_test
 dc-version: 1.1.13-a14efad
 have-watchdog: false
 no-quorum-policy: ignore
 stonith-enabled: false



次回は、F/Oさせてみたり、リソース関連のpcsコマンドを試してみたいと思います。



2015年10月24日土曜日

[VirtualBox5] CentOS6インストール後の設定

VirtualBox5 で CentOS-6.7-x86_64-minimal.iso を使って仮想マシンを作成してみた。

仮想マシン作成後に行った VirtualBox 関連の作業は、以下のとおり

1.ネットワーク設定


仮想マシンのネットワークはブリッジにしました。
仮想マシンのネットワークについては、下記URLのページが参考になります。
CentOSのインストール直後は、ネットワークが使えませんでした。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 08:00:27:13:10:2d brd ff:ff:ff:ff:ff:ff
/etc/sysconfig/network-scripts/ifcfg-eth0 をみると ONBOOT が "no" になっていたので、yes に修正しました。
DEVICE=eth0
HWADDR=08:00:27:13:10:2D
TYPE=Ethernet
UUID=a3f14be7-d818-4b1b-807b-755f2edd085c
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
ネットワークを再起動したら、DHCPでIPアドレスが割り当てられて、使えるようになりました。
[root@localhost ~]# service network restart
インターフェース eth0 を終了中:                            [  OK  ]
ループバックインターフェースを終了中                       [  OK  ]
ループバックインターフェイスを呼び込み中                   [  OK  ]
インターフェース eth0 を活性化中:
eth0 のIP情報を検出中... 完了。
                                                           [  OK  ]
[root@localhost ~]# ip a
1: lo: <loopback> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <broadcast> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:13:10:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.40/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::a00:27ff:fe13:102d/64 scope link tentative
       valid_lft forever preferred_lft forever
[root@localhost ~]#

2.Guest Addition のインストール


Guest Addition をインストールすると、いろいろと便利な機能を使えるようになります。
詳しくは、下記URLを参照。
まず、VirrtualBoxで Guest Additions の isoイメージを仮想マシンに挿入します。
[デバイス] > [Guest Additions CDイメージの挿入]を選択します。



仮想マシンにログインして、マウントします。 /dev/cdrom がない場合は、 /dev/sr0 でマウントします。
[root@localhost ~]# mount /dev/cdrom /mnt
mount: ブロックデバイス /dev/sr0 は書き込み禁止です、読込み専用でマウントします
インストーラを実行すると、以下のようにエラーになりました。
[root@localhost mnt]# ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.6 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The gcc utility was not found. If the following module compilation fails then
this could be the reason and you should try installing it.

The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-573.el6.x86_64

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.

gcc, kernel-devel, perl が必要みたいなので、インストールします。
[root@localhost mnt]# yum install perl gcc kernel-devel-2.6.32-573.el6.x86_64
再度、インストーラーを実行したら以下のようになりました。
[root@localhost mnt]# ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.6 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.6 of VirtualBox Guest Additions...
Stopping VirtualBox Additions                              [  OK  ]
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong. The module is not built but the others are.)
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.


GUI は使用しないので OpenGL の失敗は無視しました。

3.ACPIシャットダウンをつかえるようにする


初期状態では、ACPIシャットダウンがつかえませんでした。
acipd をインストールして、起動し、自動起動がONになっていることを確認します。
[root@localhost ~]# yum install acpid
[root@localhost ~]# service acpid start
[root@localhost ~]# chkconfig --list | grep acpid
acpid           0:off   1:off   2:on    3:on  4:on  5:on        6:off
これで、[仮想マシン] > [ACPIシャットダウン]でシャットダウンできるようになります。



4.共有フォルダを設定する


ホストマシンと仮想マシンの間でファイルを共有できるようにします。
この機能を使用するには GustAddition のインストールが必要です。

まず、ホストマシンの Windows にフォルダを作成します。
今回は、フォルダ名を vmshare としました。

VirtualBox で[仮想マシン] > [設定] で設定画面を開き、下図のように 共有フォルダの設定をします。



これで、仮想マシンを再起動すると、ホストマシンのWindows と仮想マシンの CentOSでファイルを共有できるようになります。
上記設定では、仮想マシンが起動すると、自動的にマウントされます。
マウント場所 /media/sf_<フォルダー名> になります。
[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmshare on /media/sf_vmshare type vboxsf (gid=498,rw)
[root@localhost ~]# ls -l /media
合計 0
drwxrwx---. 1 root vboxsf 0 10月 23 22:17 2015 sf_vmshare

ちなみに、ネットワーク設定がNGで通信できない場合でも、共有フォルダでファイルの共有はできます。

5.仮想マシンをクローンした後のネットワーク設定


VirtualBox では、仮想マシンの複製をつくることができます。



同じ MACアドレスのマシンを複数起動すると通信できなくなるので、1台をクローンして複数のマシンを起動したい場合は、上記のように MACアドレスを初期化します。

クローンしたマシンの初期状態では、ネットワークが使用できません。
以下のように、NIC が eth1 として認識されます。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 08:00:27:ac:86:bd brd ff:ff:ff:ff:ff:ff
/etc/udev/rules.d/70-persistent-net.rules を見ると、以下のとおり。
クローンしたマシンの新しいMACアドレスが eth1 に割り当てられています。
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:13:10:2d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:ac:86:bd", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

自分のMACアドレスを eth0 に割り当てるように修正します。
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:13:10:2d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:ac:86:bd", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

/etc/sysconfig/network-scripts/ifcfg-eth0 でも新しいMACアドレスを使うように修正します。 以下のように、UUID と HWADDR をコメントアウトします。
DEVICE=eth0
#HWADDR=08:00:27:13:10:2D
TYPE=Ethernet
#UUID=a3f14be7-d818-4b1b-807b-755f2edd085c
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
これで、仮想マシンを再起動するとネットワークがつかえるようになります。
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:ac:86:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.41/24 brd 192.168.1.255 scope global eth0
    inet6 2408:210:d340:c000:a00:27ff:feac:86bd/64 scope global dynamic
       valid_lft 2591814sec preferred_lft 604614sec
    inet6 fe80::a00:27ff:feac:86bd/64 scope link
       valid_lft forever preferred_lft forever







2015年10月18日日曜日

[CentOS7] Pacemaker + corosync のインストール


CentOS7 に Pacemaker(corosync) をインストールして基本動作を確認します。

Pacemaker(corosync)のインストール、設定については、以下のページを参考にしました。
特に、RedHatのマニュアルが参考になりました。

検証環境は以下のとおり。

[環境]
  • サーバ1
    • OS: CentOS7.0
    • IPアドレス: 10.1.0.71
    • ホスト名: mysql01
  • サーバ2
    • OS: CentOS7.0
    • IPアドレス: 10.1.0.72
    • ホスト名: mysql02

1.Pacemakerのインストール


CentOS7.0の標準レポジトリから yum でpacemaker、corosync、pcs をインストールします。
「サーバ1」と「サーバ2」にインストールします。
# yum install pacemaker corosync pcs
pcs はクラスタ管理用のCLIツールです。
pcsを使用したクラスタ操作は、クラスタメンバのサーバであれば、どれか1サーバで実行すれば良いので、ここでは、基本的に「サーバ1」で pcs コマンドを実行するようにしています。

2.OS設定


「サーバ1」と「サーバ2」でSELinuxとファイヤーウォールが無効になっていることを確認します。
# getenforce
Disabled
# systemctl list-unit-files -t service | grep firewalld
firewalld.service                           disabled
# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
クラスタ設定ではホスト名を使用したいので、「サーバ1」と「サーバ2」の /etc/hosts にホスト名を登録します。
10.1.0.71 mysql01
10.1.0.72 mysql02
「サーバ1」と「サーバ2」の双方向で疎通を確認します。
# ping -c1 mysql01(または、mysql02)
pcsでクラスタの設定を行うときに、ssh を使用します。「サーバ1」と「サーバ2」の双方向でsshでログインできることを確認します。
# ssh -l root mysql01(または、mysql02)
pcsでコマンドを実行するときに hacluster ユーザを使用しますが、初期状態ではパスワードが設定されていないので、パスワードを設定します。「サーバ1」と「サーバ2」で実施します。
# passwd hacluster

3.クラスタ設定


pcs でクラスタの設定を行います。ます、pcsd を起動し、自動起動を有効にします。
# systemctl start pcsd
# systemctl enable pcsd
「サーバ1」と「サーバ2」の信頼関係を設定します。hacluster ユーザで認証できるようにします。[Password] は、上記2で設定したパスワードを指定します。「サーバ1」だけで実行します。
# pcs cluster auth mysql01 mysql02
Username: hacluster
Password:
mysql01: Authorized
mysql02: Authorized
クラスタ名を "cluster_mysql" として、mysql01 と mysql02 でクラスタをセットアップします。「サーバ1」だけで実行します。
# pcs cluster setup --name cluster_mysql mysql01 mysql02
Shutting down pacemaker/corosync services...
Redirecting to /bin/systemctl stop  pacemaker.service
Redirecting to /bin/systemctl stop  corosync.service
Killing any remaining services...
Removing all cluster configuration files...
mysql01: Succeeded
mysql02: Succeeded
このコマンドの実行により、「サーバ1」と「サーバ2」に /etc/corosync/corosync.conf が作成されます。内容は以下のとおり。
# cat /etc/corosync/corosync.conf
totem {
version: 2
secauth: off
cluster_name: cluster_mysql
transport: udpu
}

nodelist {
  node {
        ring0_addr: 10.1.0.71
        nodeid: 1
       }
  node {
        ring0_addr: 10.1.0.72
        nodeid: 2
       }
}

quorum {
provider: corosync_votequorum
two_node: 1
}

logging {
to_syslog: yes
}
この設定だとPacemakerのログは /var/log/messages に出力されます。/etc/corosync に設定のサンプルファイルがあるので、logging の設定を変更して、Pacemaker用のログファイルに出力するようにしたほうが見やすいと思います。
直接、このファイルを編集して設定変更できます。設定変更後はクラスタを再起動します。

4.クラスタ起動と状態確認


「サーバ1」と「サーバ2」のクラスタ(pacemaker + corosync)を起動します。
下記コマンドを「サーバ1」で実行します。
# pcs cluster start --all
mysql01: Starting Cluster...
mysql02: Starting Cluster...
クラスタの自動起動/停止は、pacemaker + corocyncを自動起動/停止するように「サーバ1」と「サーバ2」に設定します。
なお、corosync の自動起動に失敗する場合があるようです。こちら のページの最後のほうを参考にしてください。
私の環境では問題なく起動しました。
# systemctl enable pacemaker
# systemctl enable corosync
クラスタのプロセスは以下のとおり。
「サーバ1」と「サーバ2」でpacemakerとcorosyncが起動します。
# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
~ 省略 ~
root     23266     1  0 20:56 ?        00:00:00 /bin/sh /usr/lib/pcsd/pcsd start
root     23270 23266  0 20:56 ?        00:00:00 /bin/bash -c ulimit -S -c 0 >/dev/null 2>&1 ; /usr/bin/ruby -I/usr/lib/pcsd /usr/lib/pcsd/ssl.
root     23271 23270  0 20:56 ?        00:00:01 /usr/bin/ruby -I/usr/lib/pcsd /usr/lib/pcsd/ssl.rb
root     23762     2  0 21:05 ?        00:00:00 [kworker/0:0]
root     23995     2  0 21:07 ?        00:00:00 [kworker/1:0]
root     24950     1  2 21:24 ?        00:00:00 corosync
root     24965     1  0 21:24 ?        00:00:00 /usr/sbin/pacemakerd -f
haclust+ 24966 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/cib
root     24967 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/stonithd
root     24968 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/lrmd
haclust+ 24969 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/attrd
haclust+ 24970 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/pengine
haclust+ 24971 24965  0 21:24 ?        00:00:00 /usr/libexec/pacemaker/crmd
root     25015   626  0 21:25 ?        00:00:00 sleep 60
root     25017  2472  0 21:25 pts/0    00:00:00 ps -ef
クラスタの状態を確認します。「サーバ1」で実行します。
# pcs status cluster
Cluster Status:
 Last updated: Mon May  4 11:19:05 2015
 Last change: Mon May  4 10:50:30 2015
 Stack: corosync
 Current DC: mysql01 (1) - partition with quorum
 Version: 1.1.12-a14efad
 2 Nodes configured
 0 Resources configured
クラスタメンバ(ノード)の状態を確認します。「サーバ1」で実行します。
# pcs status nodes
Pacemaker Nodes:
 Online: mysql01 mysql02
 Standby:
 Offline:
# corosync-cmapctl | grep members
runtime.totem.pg.mrp.srp.members.1.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.1.ip (str) = r(0) ip(10.1.0.71)
runtime.totem.pg.mrp.srp.members.1.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.1.status (str) = joined
runtime.totem.pg.mrp.srp.members.2.config_version (u64) = 0
runtime.totem.pg.mrp.srp.members.2.ip (str) = r(0) ip(10.1.0.72)
runtime.totem.pg.mrp.srp.members.2.join_count (u32) = 1
runtime.totem.pg.mrp.srp.members.2.status (str) = joined
# pcs status corosync

Membership information
----------------------
    Nodeid      Votes Name
         1          1 mysql01 (local)
         2          1 mysql02
クラスタ、ノード、リソース、デーモンの状態を確認します。「サーバ1」で実行します。
# pcs status
Cluster name: cluster_mysql
Last updated: Mon May  4 12:29:19 2015
Last change: Mon May  4 11:01:42 2015
Stack: corosync
Current DC: mysql01 (1) - partition with quorum
Version: 1.1.12-a14efad
2 Nodes configured
0 Resources configured


Online: [ mysql01 ]
OFFLINE: [ mysql02 ]

Full list of resources:


PCSD Status:
  mysql01: Online
  mysql02: Online

Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

5.クラスタ設定のチェック


クラスタ設定をチェックします。「サーバ1」で実行します。
# crm_verify -L -V
   error: unpack_resources:     Resource start-up disabled since no STONITH resources have been defined
   error: unpack_resources:     Either configure some or disable STONITH with the stonith-enabled option
   error: unpack_resources:     NOTE: Clusters with shared data need STONITH to ensure data integrity
Errors found during check: config not valid
STONITHの設定をしていないのでエラーになっています。STONITHは、Pacemakerがスプリットブレインを検知したときに強制的にH/Wを電源OFF/ONする機能だそうです。STONISHについては、以下のページが詳しいです。
STONISH はデフォルトで有効になっていますが、検証用の仮想化環境では使用しないので無効にします。
# pcs property set stonith-enabled=false
エラーにはなっていませんが、クォーラムの設定も変更します。クォーラムについては、以下が詳しいです。
今回は2台構成なので、スプリットブレインが発生しても quorum が特別な動作を行わないように無効にします。
# pcs property set no-quorum-policy=ignore
パラメータを確認します。
# pcs property
Cluster Properties:
 cluster-infrastructure: corosync
 cluster-name: cluster_mysql
 dc-version: 1.1.12-a14efad
 have-watchdog: false
 no-quorum-policy: ignore
 stonith-enabled: false
クラスタの再起動は不要です。変更は即時反映されます。

6.クラスタ操作


pcsコマンドでクラスタを操作してみます。ここでは、「サーバ1」でpcsコマンドを実行します。

クラスタを停止する場合は、以下のようにします。
# pcs cluster stop --all
mysql01: Stopping Cluster (pacemaker)...
mysql02: Stopping Cluster (pacemaker)...
mysql02: Stopping Cluster (corosync)...
mysql01: Stopping Cluster (corosync)...
クラスタを再起動したい場合は、stop してから start します。
# pcs cluster stop --all && pcs cluster start --all
「サーバ2」 をスタンバイにして、状態を確認してみます。
# pcs cluster standby mysql02
# pcs status nodes
Pacemaker Nodes:
 Online: mysql01
 Standby: mysql02
 Offline:
「サーバ2」 をオンラインに戻します。
# pcs cluster unstandby mysql02
# pcs status nodes
Pacemaker Nodes:
 Online: mysql01 mysql02
 Standby:
 Offline:
「サーバ2」 だけ停止します。
# pcs cluster stop mysql02
mysql02: Stopping Cluster (pacemaker)...
mysql02: Stopping Cluster (corosync)...
# pcs status nodes
Pacemaker Nodes:
 Online: mysql01
 Standby:
 Offline: mysql02


2015年9月20日日曜日

[CentOS7][Rundeck] ジョブを計画実行する

[CentOS7][Rundeck] ジョブを即時実行する」の続きです。

前回作成したジョブを修正して、実行時間を設定し、計画実行してみます。
環境は以下のとおりです。
  • Rundeckサーバ(ローカル): centos7 [10.1.0.80]
  • ノード1(リモート): pm01 [10.1.0.71]
  • ノード2(リモート): pm01 [10.1.0.73]
  • プロジェクト: sandbox02
  • ジョブ: job01 

実行スケジュールの設定


ジョブを修正します。
メニューバーの[Jobs]をクリックしてジョブの一覧を表示したら、"job01" のメニューを表示して、[Edit this Job] を選択します。



画面をスクロールして、[Schedule to run repeatedly?] の "Yes" を選択すると下図のように、実行時刻を設定できるようになります。



[Simple]タブでは、下図のように、時間、曜日、月を指定できます。



[Crontab] タブでは、crontab と同じ設定方法で実行時間を指定できます。



実行時間を指定したら、[Save]をクリックして設定変更を保存します。



設定を保存すると、下図の赤線部分に、実行予定時間(実行までの残り時間)が表示されます。



実行結果の確認


計画実行されたジョブの結果は、[Activity]で確認できます。







使い方の詳細は、「ユーザマニュアル(日本語)」を参照してください。





2015年9月13日日曜日

[CentOS7][Rundeck] ジョブを即時実行する


[CentOS7][Rundeck] コマンドを実行する」の続きです。

前回は、コマンドを実行したので、今回は、ジョブを作って即時実行してみます。
環境は以下のとおりです。
  • Rundeckサーバ(ローカル): centos7 [10.1.0.80]
  • ノード1(リモート): pm01 [10.1.0.71]
  • ノード2(リモート): pm01 [10.1.0.73]
  • プロジェクト: sandbox02
Rundeck のジョブは、複数の処理ステップで構成することができます。
今回は下記の3つのステップを持つジョブを作成し、各サーバで実行します。
  • ジョブ(job01)
    • step1: /tmp/test.sh のファイル名で自サーバ上にスクリプトを作成する
    • step2: step1で作成したRundeck サーバ上の /tmp/test.sh を各サーバに送信して実行する
    • step3: step1で作成した自サーバ上の /tmp/test.sh を実行する
また、ジョブをグループ化すると、フォルダ構成のように表示されて管理しやすいので、グループ化することにします。(グループ化は任意です)


ジョブの作成


メニューバーの[Job]をクリックすると下図の画面が表示されます。
[Create Job] をクリックして新しいジョブを作成します。


[Job Name]、[Description]を入力します。
今回は、ジョブのグループ管理を試したい、demoグループ配下のgroup01グループにジョブが所属するように "demo/group01" と入力しています。


下にスクロールして、[Workflow]を入力します。
Workfrow に入力するステップの種類は、複数の選択肢がありますが、今回試すのは、"Command"、"Script"、"Script file or URL" の3つです。

まず、"step1" の処理を入力します。
[Script]をクリックします。


下図の赤枠で示した部分にスクリプトを入力します。
内容は以下のとおり。
echo "++ 開始 + `date '+%Y-%m-%d %H:%M:%S'`"
echo "++ 実行ホスト: `hostname`"
echo "++ 実行ユーザ: `id`"
echo "++ 実行シェル: ${SHELL}"
echo "++ 実行プログラム: $0"

# テストスクリプト作成
echo "++ テストスクリプト作成: /tmp/test.sh"
cat<<'EOF' > /tmp/test.sh
#!/bin/bash
echo "-- START - `date '+%Y-%m-%d %H:%M:%S'`"
EOF
cat<<EOF >> /tmp/test.sh
echo "-- 格納場所: `hostname`"
EOF
cat<<'EOF' >> /tmp/test.sh
echo "-- 実行ホスト: `hostname`"
echo "-- 実行ユーザ: `id`"
echo "-- 実行シェル: ${SHELL}"
echo "-- 実行プログラム: $0"
echo "-- END - `date '+%Y-%m-%d %H:%M:%S'`"
exit 0
EOF

# 実行権限付与
chmod 777 /tmp/test.sh

echo "++ 終了 + `date '+%Y-%m-%d %H:%M:%S'`"
exit 0
入力後、[save]をクリックして保存します。


[Add a step]をクリックして、"step2" の処理を入力します。


[Script file or URL] をクリックします。


[File Path/URL] に実行したいプログラムを入力します。
 "/tmp/test.sh" を入力したら、[Save] をクリックして保存します。



[Add a step]をクリックして "step3" の処理を入力します。


[Command]をクリックします。



[Command]に実行したコマンドを入力します。
"/tmp/test.sh" を入力したら、[Save]コマンドをクリックして保存します。


これで、下図のように3つのステップを持つ Warkfow ができました。
ステップを修正したい場合は、修正したいステップをクリックします。


ステップが作成できたら、
[Nodes]で、このジョブを実行するノードを指定します。
今回は、".*"を入力して、centos7(localhost)/pm01/pm02 で実行するように設定します。


最後に、スクロールして、[Create]をクリックします。
これで、ジョブが保存されます。


ジョブの即時実行


ジョブの作成が成功すると、下図のように表示されます。
[Run Job Now]をクリックすると、ジョブが実行されます。



ジョブの実行が完了すると下図のように表示されます。
[Log Output]タブをクリックするとログを見ることができます。



標準出力のメッセージを下図のように見ることができます。
日本語も表示できます。


ログは、ステップを実行した順番で表示されています。
pm01のステップ2のログは、以下のように表示されています。
このログを見ると、Rundeckサーバ(centos7)上の /tmp/test.sh を /tmp/67-52-pm01-test.sh に転送して、rootユーザ(ノード追加時に指定)で実行したことがわかります。 ドキュメントによるとスクリプトの転送には scp を使うようです。
-- START - 2015-09-13 11:17:15
-- 格納場所: centos7
-- 実行ホスト: pm01
-- 実行ユーザ: uid=0(root) gid=0(root) 所属グループ=0(root)
-- 実行シェル: /bin/bash
-- 実行プログラム: /tmp/67-52-pm01-test.sh
-- END - 2015-09-13 11:17:15

pm01のステップ3のログは、以下のように表示されています。
このログを見ると、ステップ1で作成したスクリプトを実行していることがわかります。
-- START - 2015-09-13 11:17:17
-- 格納場所: pm01
-- 実行ホスト: pm01
-- 実行ユーザ: uid=0(root) gid=0(root) 所属グループ=0(root)
-- 実行シェル: /bin/bash
-- 実行プログラム: /tmp/test.sh
-- END - 2015-09-13 11:17:17


今回は、[WorkflowのStrategy] や [Tred Count] をデフォルト設定でジョブを実行したので、各ステップがシーケンシャルに実行されましたが、各サーバでジョブを並列処理させることも可能です。

使い方の詳細は、「ユーザマニュアル(日本語)」を参照してください。