2017年12月24日 星期日

Redis 4.0.6 Cluster on CentOS 7

Redis Cluster

建立 vm instances

建立 2 個 instances: rdc-01(10.128.0.6) 與 rdc-02(10.128.0.12), 設定如下:
由於 rdc-01 與 rdc-02 為 cluster, 以下步驟請重複 rdc-01 與 rdc-02
  • Boot disk
    • CentOS 7
  • Firewall
    • Allow HTTP traffic
    • Allow HTTPS traffic

更新套件管理包

SSH 進入 GCE, 更新套件管理包
# sudo su
# yum update -y

安裝相依套件

# yum -y install ruby ruby-devel rubygems rpm-build gcc tcl wget

抓取 redis 安裝包

# cd /home/lucars
# wget http://download.redis.io/releases/redis-4.0.6.tar.gz

解壓縮

# tar zxvf redis-4.0.6.tar.gz

編譯 Redis

# cd redis-4.0.6
# make

測試(可忽略)

# make test

安裝

# make install

建立目錄

# mkdir /etc/redis
# cd /etc/redis
# mkdir 7000 7001 7002

複製 config 檔案

以下步驟, 重複處理 7000 / 7001 / 7002
# cp /home/lucars/redis-4.0.6/redis.conf /etc/redis/7000/redis.conf

修改 config 檔案

以下步驟, 重複處理 7000 / 7001 / 7002
# vi /etc/redis/7000/redis.conf
修改內容如下
port 依序修改成 7000 / 7001 / 7002
bind 的 ip 請以內部 ip 為主
port 7000
bind 10.128.0.6
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000

啟動服務

# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7000/redis.conf
# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7001/redis.conf
# /home/lucars/redis-4.0.6/src/redis-server /etc/redis/7002/redis.conf

建立 Cluster

當 rdc-01 與 rdc-02 的 redis services 都啟動之後, 在 rdc-01 上啟動 cluster
以下步驟只需在 rdc-01 下執行
# gem install redis
# /home/lucars/redis-4.0.6/src/redis-trib.rb create --replicas 1 10.128.0.6:7000 10.128.0.6:7001 10.128.0.6:7002 10.128.0.12:7000 10.128.0.12:7001 10.128.0.12:7002
輸出內容如下
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.128.0.6:7000
10.128.0.12:7000
10.128.0.6:7001
Adding replica 10.128.0.12:7001 to 10.128.0.6:7000
Adding replica 10.128.0.6:7002 to 10.128.0.12:7000
Adding replica 10.128.0.12:7002 to 10.128.0.6:7001
M: 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2 10.128.0.6:7000
   slots:0-5460 (5461 slots) master
M: 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665 10.128.0.6:7001
   slots:10923-16383 (5461 slots) master
S: b5eb0947604ae617d32c542dc702ec1e623a7790 10.128.0.6:7002
   replicates dfa016eae87db5cf7a3347a700a4852888ce6f58
M: dfa016eae87db5cf7a3347a700a4852888ce6f58 10.128.0.12:7000
   slots:5461-10922 (5462 slots) master
S: 3bb319b44ff4934bcb9b8a1a4fc05f26cf57b5a5 10.128.0.12:7001
   replicates 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2
S: b560f9dab350d42f516e14db1141ad359607e628 10.128.0.12:7002
   replicates 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 10.128.0.6:7000)
M: 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2 10.128.0.6:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: b560f9dab350d42f516e14db1141ad359607e628 10.128.0.12:7002
   slots: (0 slots) slave
   replicates 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665
M: 40e3140fb9cb5bc6d2214cc3cd120cda39e2d665 10.128.0.6:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3bb319b44ff4934bcb9b8a1a4fc05f26cf57b5a5 10.128.0.12:7001
   slots: (0 slots) slave
   replicates 66c1e4c34cfa9fb5f0c1ff128f11414afa332bc2
M: dfa016eae87db5cf7a3347a700a4852888ce6f58 10.128.0.12:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b5eb0947604ae617d32c542dc702ec1e623a7790 10.128.0.6:7002
   slots: (0 slots) slave
   replicates dfa016eae87db5cf7a3347a700a4852888ce6f58
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

測試連線到 cluster

# /home/lucars/redis-4.0.6/src/redis-cli -h 10.128.0.6 -p 7000 -c

額外補充

redis-trib.rb 執行失敗: 找不到 redis

失敗原因
usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/lucars/redis-4.0.6/src/redis-trib.rb:25:in `<main>'
處理方式
# gem install redis

gem install redis 執行失敗: Ruby 版本低於 2.2.2

失敗原因
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.2.2.
更新到 ruby 到 2.2.4
# yum install gcc-c++ patch readline readline-devel zlib zlib-devel
# yum install libyaml-devel libffi-devel openssl-devel make
# yum install bzip2 autoconf automake libtool bison iconv-devel sqlite-devel
# curl -sSL https://rvm.io/mpapis.asc | gpg --import -
# curl -L get.rvm.io | bash -s stable
# source /etc/profile.d/rvm.sh
# rvm reload
# rvm requirements run
# rvm install 2.2.4
# rvm use 2.2.4 --default
# ruby --version

2017年9月14日 星期四

CentOS 7 安裝 RabbitMQ 3.6.12

CentOS 7 安裝 RabbitMQ 3.6.12

相關文件

安裝 Erlang

使用 yum 安裝 Erlang 是最簡單又方便的方式, 雖然不一定是最新版的, 不過已經符合 RabbitMQ 需求.
在 CentOS 7 上使用 Erlang 20.x
建立 repo 檔
$ vi /etc/yum.repos.d/rabbitmq-erlang.repo
# In /etc/yum.repos.d/rabbitmq-erlang.repo
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/20/el/7
gpgcheck=1
gpgkey=https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
安裝 Erlang
$ yum install erlang
測試 Erlang 是否安裝成功
$ erl

1> 123+456.
579
跳出 Erlang
2> halt().

安裝 RabbitMQ

下載 RabbitMQ 的 RPM
$ wget https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_12/rabbitmq-server-3.6.12-1.el7.noarch.rpm
安裝 RabbitMQ
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
yum install rabbitmq-server-3.6.12-1.noarch.rpm

執行 RabbitMQ

讓 RabbitMQ 在背景執行
$ rabbitmq-server -detached

結語

  • 安裝比起之前簡單很多
  • 建立帳號跟權限就跟以往一樣
  • 效能怎麼比我想像中差... Orz

2017年7月25日 星期二

CentOS 7 運行 Docker 錯誤 Failed to get D-Bus connection: Operation not permitted

Docker run 的 container 內的 root 只是一般使用者, 必須要給予真正的 root 權限, 才能執行 mount 或是在 docker 中啟動 docker.

主要原因

CentOS 7 權限不足的原因

解決辦法

docker run --name centos --privileged -d centos:latest /sbin/init
給予真正的 root 的權限

2017年7月4日 星期二

使用 Docker 建立 Cassandra Cluster

目標

建立 3 個 Cassandra Server 的 Cluster

取得 Cassandra Image

docker pull 取得最新的 cassandra image
執行
$ docker pull cassandra:latest
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker pull cassandra:latest
latest: Pulling from library/cassandra
9f0706ba7422: Pull complete
47bacf36113f: Pull complete
7c6842ab2e6f: Pull complete
b0970d2724fb: Pull complete
7dd1392a6ce5: Pull complete
43faf38e08a8: Pull complete
448a3a082d57: Pull complete
2c7ffe4dc3db: Pull complete
caac33302fe6: Pull complete
8ba01779bed9: Pull complete
1aa363d655b1: Pull complete
02a5c66a8b99: Pull complete
Digest: sha256:b25a30a85b5869257f560fb05296886136b809288f53f32ebefde8967e6e6ca5
Status: Downloaded newer image for cassandra:latest

查看 Images

docker images 查看所以已下載的 images
執行
$ docker images
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cassandra           latest              c82d9de5d478        5 days ago          386MB

建立 Docker Network

docker network create 建立一個 bridge 類型的網路
執行
$ docker network create --driver bridge apptier
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker network create --driver bridge apptier
c40c7ae8e9a1c6ad8eced493c0fa77d881cd63bc2c2a702af666d6cc4b1dcb96

查看 Docker Network

docker network ls 查看目前所有網路設定
執行
$ docker network ls
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c40c7ae8e9a1        apptier             bridge              local
de34dc3841de        bridge              bridge              local
fcde3e725906        host                host                local
e70d8c2c4afd        none                null                local

建立第 1 台 Cassandra Server

docker run: 建立 cassandra server 的 container
--name cassanda1: 設定 container 名稱為 cassandra1
--network apptier: 設定 network 為 apptier
-d cassandra: 背景執行 cassandra image
執行
$ docker run --name cassandra --network apptier -d cassandra
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker run --name cassandra1 --network apptier -d cassandra
33b7a234cef63f07ec62366aaf86035fa4552157a5cdf8645438a64975fbf056
NBAS055de-MacBook-Pro:~ nbas055$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                         NAMES
33b7a234cef6        cassandra           "/docker-entrypoin..."   3 seconds ago       Up 2 seconds        7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra1

查詢目前正在執行的 Containers

docker ps 列出目前正在執行的 containers
執行
$ docker ps
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                         NAMES
7f22277bf144        cassandra           "/docker-entrypoin..."   2 seconds ago       Up 1 second         7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra1

查詢 Containers 的執行狀況(CPU/Ram)

docker stats 查看所有 container 執行的狀況
執行
$ docker stats
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker stats
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
33b7a234cef6        0.10%               1.148GiB / 1.952GiB   58.83%              1.12kB / 0B         0B / 1.78MB         42

刪除 Container

若 container 設定錯誤
docker rm cassandra1: 指定刪除 cassandra1 的 container
-f: 強制刪除
P.S. 刪除之後再嘗試建立一個 container
執行
$ docker rm -f cassandra1
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker rm -f cassandra1
33b7a234cef6
NBAS055de-MacBook-Pro:~ nbas055$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

建立 Cassandra Cluster

設定 cassandra2 跟 cassandra3
執行 docker run 同時指定連結目標
-e CASSANDRA_SEEDS: 設定目標為 cassandra1
執行
$ docker run --name cassandra2 --network apptier -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.Networks.apptier.IPAddress }}' cassandra1)" -d cassandra
$ docker run --name cassandra3 --network apptier -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.Networks.apptier.IPAddress }}' cassandra1)" -d cassandra
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker run --name cassandra2 --network apptier -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.Networks.apptier.IPAddress }}' cassandra1)" -d cassandra
cf300680fa79787482da750fb7b7907c01b263889ca2e17434b9a829f56e3346
NBAS055de-MacBook-Pro:~ nbas055$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                         NAMES
cf300680fa79        cassandra           "/docker-entrypoin..."   12 seconds ago      Up 10 seconds       7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra2
7f22277bf144        cassandra           "/docker-entrypoin..."   45 minutes ago      Up 44 minutes       7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra1
NBAS055de-MacBook-Pro:~ nbas055$ docker run --name cassandra3 --network apptier -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.Networks.apptier.IPAddress }}' cassandra1)" -d cassandra
1b3070561d24662830117005ace7615ccab8e66d652b0c5401f852da93490160
NBAS055de-MacBook-Pro:~ nbas055$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                         NAMES
1b3070561d24        cassandra           "/docker-entrypoin..."   About a minute ago   Up About a minute   7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra3
a277482c88c3        cassandra           "/docker-entrypoin..."   3 minutes ago        Up 3 minutes        7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra2
fbc7888155bc        cassandra           "/docker-entrypoin..."   4 minutes ago        Up 4 minutes        7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   cassandra1
NBAS055de-MacBook-Pro:~ nbas055$ docker stats
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
1b3070561d24        223.78%             2.131GiB / 7.787GiB   27.36%              578B / 0B           0B / 692kB          41
a277482c88c3        0.58%               2.137GiB / 7.787GiB   27.45%              1.13kB / 0B         0B / 1.95MB         42
fbc7888155bc        0.45%               2.14GiB / 7.787GiB    27.48%              1.91kB / 0B         76MB / 1.95MB       42

查看 Node 運行狀況

docker exec -it 進入 cassandra1
nodetool status 查看 cassandra node server 運行狀態
執行
$ docker exec -it cassandra1 /bin/bash
$ nodetool status
結果
NBAS055de-MacBook-Pro:~ nbas055$ docker exec -it cassandra1 /bin/bash
root@687e14268025:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens       Owns (effective)  Host ID                               Rack
UN  172.18.0.2  108.67 KiB  256          61.8%             791e7d85-9b08-45e0-8804-c804a3538679  rack1
UN  172.18.0.3  103.21 KiB  256          67.9%             ecbe769c-0d54-4d4b-8019-7f5bfa51a7ed  rack1
UN  172.18.0.4  15.35 KiB  256          70.4%             634eb8c4-17ea-40dd-ac2e-33ab97ada0ef  rack1

注意事項

  • 每個 node server 在連接 cassandra1 時記得間隔幾秒, 避免連接失敗
  • docker info 可查看 docker 預設的記憶體大小, cassandra 每個 node 至少需要 2 GB 的記憶體, 所以 node 數 x 2 = docker 記憶體