【Docker】redashの構築【CentOS7】

担当した案件で CentOS7 に redash を導入したんでその時の備忘録になります。
なんか色々と嵌った気がします。

# 必要なパッケージの導入
yum install epel-release -y
yum install docker docker-compose -y

# docker操作用の権限を一般ユーザに追加
sudo groupadd docker
sudo gpasswd -a $USER docker
sudo systemctl restart docker


# docker-compose.ymlファイルを用意
cat <<EOF > docker-compose-redash.yml
version: '2'
services:
  redis:
    image: redis:3.0-alpine
    restart: always

  server:
    image: redash/redash:latest
    command: server
    depends_on:
      - postgres
      - redis
    ports:
      - "5000:5000"
    environment:
      PYTHONUNBUFFERED: 0
      REDASH_LOG_LEVEL: "INFO"
      REDASH_REDIS_URL: "redis://redis:6379/0"
      REDASH_DATABASE_URL: "postgresql://postgres@postgres/postgres"
      REDASH_COOKIE_SECRET: veryverysecret
      REDASH_WEB_WORKERS: 4
    restart: always

  worker:
    image: redash/redash:latest
    command: scheduler
    environment:
      PYTHONUNBUFFERED: 0
      REDASH_LOG_LEVEL: "INFO"
      REDASH_REDIS_URL: "redis://redis:6379/0"
      REDASH_DATABASE_URL: "postgresql://postgres@postgres/postgres"
      QUEUES: "queries,scheduled_queries,celery"
      WORKERS_COUNT: 2
    restart: always

  postgres:
    image: postgres:9.5.6-alpine
    volumes:
      - ~/postgres-data:/var/lib/postgresql/data
    restart: always
EOF

# docker上に環境構築
docker-compose -f docker-compose-redash.yml run --rm server create_db

# docker上のredash起動
docker-compose -f docker-compose-redash.yml up -d

5000ポートで listen するんで、フロントに Nginx を置いてリバプロするのがいいですね。

【rbenv】rubyの導入、操作方法【チラ裏】

よく忘れちゃうんで纏めておきます。

# 必要なパッケージ導入
yum install -y git gcc gcc-c++ openssl-devel readline-devel

# rbenvのインストール
cd /usr/local
git clone git://github.com/sstephenson/rbenv.git rbenv
git clone git://github.com/sstephenson/ruby-build.git rbenv/plugins/ruby-build

# 環境変数の設定
vi /etc/profile.d/rbenv.sh
=====================================
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init --no-rehash -)"
=====================================
# 設定の反映
source /etc/profile.d/rbenv.sh

# 導入できるバージョンを確認
rbenv install --list

# rubyのインストール
rbenv install 2.2.2
rbenv global 2.2.2
rbenv rehash

# インストールしているrubyの確認
rbenv versions

# rubyの切り替え
rbenv global 2.7.0

# bundlerのインストール(-vでバージョン指定)
gem install bundler -v 1.16.5

【JDK】Remote host closed connection during handshakeのエラー【apache】

Remote host closed connection during handshake ってエラーがでて四苦八苦した備忘録です。

背景

サーバにて表題のエラーがでて困ったの。

結論

IBM JDK は TLSv1.0 で通信するそうな。
根本的な要因は Apache の設定で TLSv1.1 / TLSv1.2 以外のプロトコルを許可していなかったため。

↓ Apache のパラメータを下記のように書き換え

# TLSv1.0 含め許可
====================================
SSLProtocol ALL -SSLv2 -SSLv3
====================================

# Apache リロード
systemctl reload httpd

下記サイトで許可している SSLProtocol を確認できます。
https://www.cman.jp/network/support/ssl.html

【LINE Notify】シェルスクリプトでWEBスクレイピングしてみた【bash】

よくチェックするサイトに新着記事が投降されたら、LINE NotifyのAPIを叩いて通知するコードを書いてみました。

#!/bin/bash

# LINEのTOKENを定義
TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# WEBページの情報を取得
MSG=`curl https://www.megamouth.info/archive | grep "<a class=\"entry-title-link\"" | sed -e 's/<[^>]*>//g' | nl`
# 作業ディレクトリの定義
OPE_DIR=/opt/SCRAPING/megamouth
# 実行時のWEBページ情報を記録するファイルを定義
INFO_FILE=$OPE_DIR/info.txt
# 前回実行時のWEBページ情報を記録するファイルを定義
PRE_INFO_FILE=$OPE_DIR/pre_info.txt

if [ ! -e $OPE_DIR ]; then
        # ディレクトリの作成
        mkdir -p $OPE_DIR
fi

# 前回実行時の記録ファイルが無ければ作成し通知
if [ ! -e $PRE_INFO_FILE ]; then
        curl https://www.megamouth.info/archive | grep "<a class=\"entry-title-link\"" | sed -e 's/<[^>]*>//g' | nl > $PRE_INFO_FILE
        curl -X POST -H "Authorization: Bearer ${TOKEN}" -F "message=${MSG}" https://notify-api.line.me/api/notify
        exit 0
fi

# 今回取得した情報を変数に代入
curl https://www.megamouth.info/archive | grep "<a class=\"entry-title-link\"" | sed -e 's/<[^>]*>//g' | nl > $INFO_FILE

# 前回と今回取得した情報を比較
diff -s $INFO_FILE $PRE_INFO_FILE

# DIFFの実行結果を変数に代入
DIFF_STATUS=$?

if [ ${DIFF_STATUS} -eq 1 ]; then
        # 前回取得した情報と今回取得した情報に差異があれば通知
        curl -X POST -H "Authorization: Bearer ${TOKEN}" -F "message=${MSG}" https://notify-api.line.me/api/notify
elif [ ${DIFF_STATUS} -eq 2 ]; then
        # diff実行時にエラーが発生すればエラーを通知
        curl -X POST -H "Authorization: Bearer ${TOKEN}" -F "message=error" https://notify-api.line.me/api/notify
fi

# 情報ファイルを更新
cp -fp $INFO_FILE $PRE_INFO_FILE

なんだかもっといろいろと使えそうですね。
アラート通知とか。。
↓皆さんもぜひ
LINE Notify

【Ansible】踏み台経由で Ansible を実行する インベントリファイルの書き方

[stg_web]
test-stg-web01 ansible_host=192.168.33.10 ansible_ssh_common_args='-o ProxyCommand="ssh -i ~/.ssh/test.pem -W %h:%p -q bastion-user@192.168.33.30"'

[prod_web]
test-prod-web01 ansible_host=192.168.33.20 ansible_ssh_common_args='-o ProxyCommand="ssh -i ~/.ssh/test.pem -W %h:%p -q bastion-user@192.168.33.30"'

【チラ裏】SSL(TLS)証明書関連【openssl】

SSL(というかTLS)証明書関連ですね。
いつもどおりのチラ裏です。

証明書の整合性確認

# 秘密鍵
openssl rsa -noout -text -in test.com.key
# 証明書
openssl x509 -noout -text -in test.com.crt
# 中間証明書
openssl x509 -noout -text -in test.com.ca

・秘密鍵、証明書の「Modules」の値が同じであること
・証明書のIssuer:CNと中間証明書のSubject:CNが同じであること

秘密鍵、CSR作成

# 秘密鍵作成
openssl genrsa -aes256 2048 > test.com.key

# CSR作成
openssl req -new -key test.com.key -out test.com.csr
=========================================================
Country Name:
ISOによる2文字の国名の符号
→JP

State or Province Name:
都道府県名
→Osaka

Locality Name:
市区町村名
→Osaka

Organization Name:
組織名
→test,Co.,Ltd.

Organizational Unit Name:
部署名
→test

Common Name:
→test.com

その他便利なopenssl

# CSRの内容確認
openssl req -noout -text -in test.com.csr

# Keyファイルの確認
openssl rsa -noout -check -in test.com.key
RSA key ok

# CSRとKeyファイルの比較
openssl rsa -in test.com.key -text
openssl req -in test.com.csr -text
⇒mosulusの値を比較し、差異が無いことを確認

# 秘密鍵のパスフレーズを解除
openssl rsa -in test.com.key -out test.com.key
openssl rsa -in test.com -text

【Nginx】Nginx での リバースproxy 設定

Nginx のリバースproxy 設定に関して纏めておきます。

例1

server {
    listen       443 ssl;
    server_name  test.com www.test.com;

    client_max_body_size 12m;
    proxy_set_header Host $http_host;

    ssl_certificate /etc/certs/test.com/current/test.com.pem;
    ssl_certificate_key /etc/certs/test.com/current/test.com.key;

    location / {
        proxy_pass http://localhost:8008;
        proxy_set_header Host              $host;
        proxy_set_header X-Forwarded-HTTPS on;
        proxy_set_header X-Forwarded-SSL   on;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Nginx の443で受けてlocalの 8008 に渡す感じですね。
Nginx で ssl終端させる感じですね。

例2

server {
    listen       443 ssl;
    server_name  www.test.com;
    client_max_body_size 12m;

    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-HTTPS on;
    proxy_set_header X-Forwarded-SSL   on;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_buffer_size 32k;
    proxy_buffers 50 32k;
    proxy_busy_buffers_size 32k;

    ssl_certificate /etc/certs/test.com/current/www.test.com.pem;
    ssl_certificate_key /etc/certs/test.com/current/www.test.com.key;

    location / {
        proxy_pass https://192.168.33.10:443;
    }

include /etc/nginx/proxy_virtual/www.test.com/*.conf;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

     location ~ (\/api)(\/.*)?{
        proxy_pass https://localhost:8008;
    }

/api でのアクセスであれば local の 8008 に proxy する設定ですね。
それ以外は https://192.168.33.10:443 に proxy させます。
※ L7 での proxy ですね。

【Ansible】Master/Slave構成の JMeter をデプロイする【IaC】

Ansible で Master/Slave 構成の JMeter をデプロイする Playbook を作ってみました。
https://github.com/keisukesanuki/jmeter-MS.git
※ 詳細は GitHub の README を参照くださいませ。

ディレクトリ構成

.
├── README.md
├── ansible.cfg
├── group_vars
│   ├── all.yml
│   └── all.yml.example
├── hosts.example
├── roles
│   ├── common
│   │   ├── README.md
│   │   └── tasks
│   │       ├── etckeeper_commit.yml
│   │       ├── host_change.yml
│   │       └── main.yml
│   ├── dummy
│   │   ├── README.md
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── dummy.txt
│   ├── jmeter51
│   │   ├── README.md
│   │   ├── files
│   │   │   ├── jmeter.properties
│   │   │   └── start-controller_cui.sh
│   │   └── tasks
│   │       └── main.yml
│   ├── jmeter53
│   │   ├── README.md
│   │   ├── files
│   │   │   ├── jmeter.properties
│   │   │   └── start-controller_cui.sh
│   │   └── tasks
│   │       └── main.yml
│   ├── jmeter54
│   │   ├── README.md
│   │   ├── files
│   │   │   ├── jmeter.properties
│   │   │   └── start-controller_cui.sh
│   │   └── tasks
│   │       └── main.yml
│   ├── jmeter55
│   │   ├── README.md
│   │   ├── files
│   │   │   ├── jmeter.properties
│   │   │   └── start-controller_cui.sh
│   │   └── tasks
│   │       └── main.yml
│   ├── minimum
│   │   └── httpd
│   │       ├── README.md
│   │       ├── handlers
│   │       │   └── main.yml
│   │       ├── tasks
│   │       │   ├── main.yml
│   │       │   └── security.yml
│   │       └── templates
│   │           ├── mpm.conf
│   │           └── security.conf
│   ├── python-scripts
│   │   ├── README.md
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       ├── csv2gspread.py
│   │       ├── sacred-drive.json
│   │       └── start-controller_cui.sh
│   ├── reboot
│   │   └── tasks
│   │       └── main.yml
│   ├── slave-jmeter
│   │   ├── README.md
│   │   ├── files
│   │   │   ├── jmeter-node.service
│   │   │   └── jmeter-server.sh
│   │   └── tasks
│   │       └── main.yml
│   └── tigervnc
│       ├── README.md
│       ├── files
│       │   ├── vncpasswd.sh
│       │   └── vncserver@.service_root
│       └── tasks
│           └── main.yml
├── scenario
│   └── example
│       └── zabbix
│           └── zabbix_load_scenario.jmx
├── target.yml
└── target.yml.example

38 directories, 51 files

【チラ裏】ディスク拡張【growpart】

自分用のメモになります。

yum whatprovides growpart
yum install cloud-utils-growpart-0.29-5.el7.noarch
lsblk
growpart /dev/sdb 1
lsblk
df -h
xfs_growfs /dev/sdb1
lsblk
df -h

パーティション拡張( growpart ) ⇒ ファイルシステム拡張( xfs_growfs )ですね。
ext4 の場合は、resize2fs ですね。

※ 追記↓

【AWS】ダウンタイムなくルートパーティションを拡張してみる【ディスク拡張】

【Ansible】アクセス制限用のモジュールを作成する【シェルスクリプト】

Ansible の 標準モジュールに良さげなモジュールがなかったんで、んじゃいっそ作ってみようかと。

ディレクトリ構造

├── README.md
├── ansible.cfg
├── block.yml
├── group_vars
│   └── main.yml
├── hosts
├── library
│   └── block_ip.sh
└── roles
    └── block_ip
        ├── README.md
        ├── tasks
        │   └── main.yml
        └── vars
            └── main.yml

モジュール

root@DESKTOP-MOGIJIA:/opt/playbook/ip_block# cat library/block_ip.sh
#!/bin/sh
source `dirname $0`/args

if [ $state == "absent" ] ; then
        route del -host $target reject
        if [ $? -eq 0 ] ; then
                echo '{ "rc": 0, "changed": true }'
        else
                echo '{ "rc": 0 }'
        fi
elif [ $state == "present" ] ; then
        route add -host $target reject
        if [ $? -eq 0 ] ; then
                echo '{ "rc": 0, "changed": true }'
        else
                echo '{ "rc": 0 }'
        fi
else
        echo '{ "failed": true, "rc": 0 }'
fi

library ディレクトリ以下にモジュールを作成します。
それと、playbook の中で target (対象IPアドレス)、 state (状態)を定義できるようにしておきます。

Role

root@DESKTOP-MOGIJIA:/opt/playbook/ip_block# cat roles/block_ip/tasks/main.yml
---
# tasks file for block_ip
- name: block_ip
  block_ip:
    target: "{{ item }}"
    state: absent
  with_items:
    - "{{ block_target }}"

- name: accept_ip
  block_ip:
    target: "{{ item }}"
    state: absent
  with_items:
    - "{{ accept_target }}"

「state」が「present」⇒アクセスを制限
「state」が「absent」⇒アクセス制限を解除
としておきます。
root@DESKTOP-MOGIJIA:/opt/playbook/ip_block# cat group_vars/main.yml
---
   block_target:
     - 192.168.99.99
     - 192.168.99.98
     - 192.168.99.97
     - 192.168.99.96
     - 192.168.99.95
     - 192.168.99.94
     - 192.168.99.93
     - 192.168.99.00

   accept_target:
     - 127.0.0.1

playbook

root@DESKTOP-MOGIJIA:/opt/playbook/ip_block# cat block.yml
---
# Main Playbook
- name: apply master configuration to master nodes
  hosts: all
  vars_files:
   - ./group_vars/main.yml
  remote_user: vagrant
#  remote_user: centos
  become: yes
  roles:
    - block_ip

実行

root@DESKTOP-MOGIJIA:/opt/playbook/ip_block# ansible-playbook block.yml --ask-pass
SSH password:

PLAY [apply master configuration to master nodes] **********************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.33.10]

TASK [block_ip : block_ip] *********************************************************************************************
changed: [192.168.33.10] => (item=192.168.99.99)
changed: [192.168.33.10] => (item=192.168.99.98)
changed: [192.168.33.10] => (item=192.168.99.97)
changed: [192.168.33.10] => (item=192.168.99.96)
changed: [192.168.33.10] => (item=192.168.99.95)
changed: [192.168.33.10] => (item=192.168.99.94)
changed: [192.168.33.10] => (item=192.168.99.93)
changed: [192.168.33.10] => (item=192.168.99.00)

TASK [block_ip : accept_ip] ********************************************************************************************
ok: [192.168.33.10] => (item=127.0.0.1)

PLAY RECAP *************************************************************************************************************
192.168.33.10              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

結果

[root@ansible-dev ~]# ip route show
default via 10.0.2.2 dev eth0 proto dhcp metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100
192.168.33.0/24 dev eth1 proto kernel scope link src 192.168.33.10 metric 101
unreachable 192.168.99.0 scope host
unreachable 192.168.99.93 scope host
unreachable 192.168.99.94 scope host
unreachable 192.168.99.95 scope host
unreachable 192.168.99.96 scope host
unreachable 192.168.99.97 scope host
unreachable 192.168.99.98 scope host
unreachable 192.168.99.99 scope host
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1

うん、大丈夫ですね。