天天看點

Akka-CQRS(2)- 安裝部署cassandra cluster,ubuntu-16.04.1-LTS and MacOS mojave

 對于akka-cluster這樣的分布式軟體系統來說,選擇配套的資料庫類型也是比較講究的,最好也是分布式的,如cassandra,能保證良好的HA特性。前面的例子裡示範akka-persistence時已經使用了cassandra作為journal和snapshot-store。一直以來基本上都在一部macbookpro上開發、測試akka-cluster相關軟體。這次在騰訊雲上租了兩台8G,50G的伺服器,安裝了ubuntu 16.04.1 LTS作業系統,想着可以在一個真正的環境下試試cassandra cluster的安裝部署和實際使用。先是試着在ubuntu上安裝部署:

在ubuntu上安裝cassandra,跟着下面的步驟做:

echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list

curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -

sudo apt-get update

sudo apt-get install cassandra      

我安裝的是cassandra v3.11.3版本,是以用了debian 311x main來注明。安裝完畢後可以用status看看cassandra是不是已經啟動,start,stop cassandra可以用下面的指令:

sudo service cassandra status    //檢查運作狀态

sudo service cassandra start     //啟動cassandra

sudo service cassandra stop      //停止cassandra      

現在我們可以用 sudo service cassandra start  啟動cassandra

然後開啟cqlsh, 輸入:

use system;    

describe table local

注意顯示的system.local表列名:

CREATE TABLE system.local (
    key text PRIMARY KEY,
    bootstrapped text,
    broadcast_address inet,
    cluster_name text,
    cql_version text,
    data_center text,
    gossip_generation int,
    host_id uuid,
    listen_address inet,
    native_protocol_version text,
    partitioner text,
    rack text,
    release_version text,
    rpc_address inet,
    schema_version uuid,
    thrift_version text,
    tokens set<text>,
    truncated_at map<uuid, blob>
...      

列名裡包括了配置檔案cassandra.yaml中的許多配置如cluster_name,listen_address,rpc_address等。在安裝cassandra時已經存放了cassandra.yaml的初始值。是以必須記住如果修改cassandra.yaml裡涉及這些配置後必須把所有system表删掉讓cassandra自己根據新的.yaml檔案配置重新建立這些system表。

我嘗試建一個兩個節點node的cluster:

配置cluster:

server1   172.27.0.8
server2   172.27.0.7      

用server1做seednode。配置cluster需要修改cassandra.yaml檔案,具體路徑如下:

sudo nano /etc/cassandra/cassandra.yaml

需要修改檔案裡的配置參數:

cluster_name  : 統一叢集名稱
seed_provider : seed節點位址清單(以,号分割) 
listen_address : 叢集節點之間使用gossip協定通訊位址
rpc_address : 用戶端連接配接位址
endpoint_snitch : 節點所屬資料中心、機架      

在修改cassandra.yaml檔案之前先停了cassandra: sudo service cassandra stop

下面是server1的設定:

cluster_name: 'TestPOS Cluster'
listen_address: 172.27.0.8
rpc_address: 172.27.0.8
- seeds: 172.27.0.8
endpoint_snitch: SimpleSnitch      

切記!!!修改完畢在啟動cassandra之前必須首先删除cassandra的系統資料表system*:

sudo rm -rf /var/lib/cassandra/data/system/*      

然後啟動cassandra:  sudo service cassandra start

好了,現在可以用nodetool指令來檢查這個節點的啟動狀态:sudo nodetool status

結果顯示server1已經成功啟動了。

下面開始配置server2:

在修改cassandra.yaml檔案之前先停了cassandra: sudo service cassandra stop

cluster_name: 'TestPOS Cluster'
listen_address: 172.27.0.7
rpc_address: 172.27.0.7
- seeds: 172.27.0.8
endpoint_snitch: SimpleSnitch      

删除cassandra的系統資料表system*:

sudo rm -rf /var/lib/cassandra/data/system/*

然後啟動:  sudo service cassandra start

現在可以用nodetool指令來檢查這個叢集中所有節點的啟動狀态:sudo nodetool status

很遺憾,隻能看到server2一個節點。

這種現象說明server1,server2之間沒有溝通。它們應該是通過各自的7000端口交流的,估計是租賃的虛拟伺服器沒有開啟這個端口。在server1上用 nc -vc 172.27.0.7 7000  得到證明。嘗試用iptables, ufw等防火牆指令都無法解決問題,看來要留給網絡管理部門了。

做了些調研,下面是cassandra需要使用的端口說明:

7199 JMX monitoring port
1024 - 65355 Random port required by JMX. Starting with Java 7u4 a specific port can be specified using the com.sun.management.jmxremote.rmi.port property.
7000 Inter-node cluster
7001 SSL inter-node cluster
9042 CQL Native Transport Port
9160 Thrift      

另外,如果需要完整解除安裝cassandra, 可以用 : sudo apt-get purge cassandra

。。。

再試試用兩部macbookpro來建構一個2-node-cluster:

手頭剛好有兩部macbookpro,可以試試在mac上安裝部署cassandra cluster。 

用homebrew下載下傳和安裝cassandra 特别容易:

brew update
brew install cassandra      

brew info cassandra可以擷取cassandra安裝情況如版本等

直接用 nodetool status來檢查cassandra是否已經啟動

start,stop指令如下:

brew services start cassandra
brew services stop cassandra

或者

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist      

兩部macbookpro IP位址: 用mac1當作seednode

mac1   192.168.1.30
mac2   192.168.1.24      

下面是brew安裝後cassandra的一些重要檔案路徑:

  • Properties: 

    /usr/local/etc/cassandra

  • Logs: 

    /usr/local/var/log/cassandra

  • Data: 

    /usr/local/var/lib/cassandra/data

配置mac1:

brew services stop cassandra

sudo nano /usr/local/etc/cassandra/cassandra.yaml

cluster_name: 'TestPOS Cluster'
listen_address: 192.168.1.30
rpc_address: 192.168.1.30
- seeds: 192.168.1.30
endpoint_snitch: SimpleSnitch      

同樣,謹記要清除system表: sudo rm -rf /usr/local/var/lib/cassandra/data/system*

brew services start cassandra

nodetool status    顯示節點mac1已經啟動

配置mac2:

brew services stop cassandra

sudo nano /usr/local/etc/cassandra/cassandra.yaml

cluster_name: 'TestPOS Cluster'
listen_address: 192.168.1.24
rpc_address: 192.168.1.24
- seeds: 192.168.1.30
endpoint_snitch: SimpleSnitch      

同樣,謹記要清除system表: sudo rm -rf /usr/local/var/lib/cassandra/data/system*

brew services start cassandra

用: nc -vc 192.168.1.30 7000 檢查mac1的7000端口,果然是開啟的

nodetool status    顯示mac1,mac2兩個節點都已經啟動了

目前的endpoint_snitch使用了SimpleSnitch。但在生産環節裡需要配置:

endpoint_snitch: GossipingPropertyFileSnitch

然後在/usr/local/etc/cassandra/cassandra-rackdc.properties 檔案裡定義本節點的實體位置(資料中心,機架)

最後還要删除/usr/local/etc/cassandra/cassandra-topology.properties 檔案