Friday, March 25, 2016

How To Setup Multi Node Cassandra 2 Cluster

This is going to be step by step guide how to install multi node Cassandra 2 cluster on Ubuntu server.

In this guide we are going to setup 5 nodes cluster. Bellow is the list of my machines in the cluster and obviously you may have different IPs.
  • 10.64.200.47
  • 10.64.200.48
  • 10.64.200.49
  • 10.64.200.50
  • 10.64.200.51
➤ Step 1 is to have Java 7 or Java 8 installed in all nodes.
Let’s install Java 8 by adding custom apt repository.
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
java -version
sudo apt-get install oracle-java8-set-default
➤ Step 2 is to add system group and user for cassandra in all nodes.
sudo addgroup cassandra
sudo adduser --ingroup cassandra cassandra
sudo adduser cassandra sudo
➤ Step 3 is to download and extract cassandra package in all nodes.
sudo su - cassandra
wget http://mirrors.ae-online.de/apache/cassandra/2.1.3/apache-cassandra-2.1.3-bin.tar.gz
sudo tar -xvf /home/cassandra/apache-cassandra-2.1.3-bin.tar.gz -C /usr/local/
cd /usr/local/
sudo mv apache-cassandra-2.1.3 cassandra-2.1.3
sudo ln -s /usr/local/cassandra-2.1.3 /usr/local/cassandra
sudo chown -R cassandra:cassandra /usr/local/cassandra-2.1.3
➤ Step 4 is to configure environment variables in all nodes.
Just edit the .profile file vim ~/.profile and paste the following at the end of the file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
export CASSANDRA_INSTALL=/usr/local/cassandra
export CASSANDRA_HOME=$CASSANDRA_INSTALL
Afterwards source the .profile file source ~/.profile.
➤ Step 5 is to configure cassandra instances in all nodes
Just edit the $CASSANDRA_HOME/conf/cassandra.yaml file vim $CASSANDRA_HOME/conf/cassandra.yaml and replace the following keys with proper values:
cluster_name: 'MyFirstCluster'
seeds: "10.64.200.47, 10.64.200.48"
listen_address: 10.64.200.47
rpc_address: 10.64.200.47
* Note that each node should have it’s own listen_address and rpc_address corresponding to it’s IP address.
That’s it we are done with configuration!
➤ Step 6 is to start the cluster and verify it.
First seed nodes should be started so they can serve as seeds for others. To start the node simply run the following:
$CASSANDRA_HOME/bin/cassandra -f
To verify after all nodes are started run the following command:
$CASSANDRA_HOME/bin/nodetool status
You should see something like the following:
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns Host ID Rack
UN 10.64.200.47 122.04 KB 256 ? e66d39d0-80d3-492f-900a-5dd006d9a763 rack1
UN 10.64.200.48 142.04 KB 256 ? e97a6709-3216-43af-90de-5a3017761a46 rack1
UN 10.64.200.49 127.83 KB 256 ? 6f74e3f8-4f20-43d8-90ff-5439939f5159 rack1
UN 10.64.200.50 137.89 KB 256 ? 8bd7390d-dde8-4aaa-8a44-73981e698a05 rack1
UN 10.64.200.51 161.07 KB 256 ? cf967b8c-4187-46d3-a464-fff09c28bb0d rack1
➤ Step 7 is to execute simple CQL.
To connect to cassandra cluster run the following command from any machine which has cassandra binaries:
$CASSANDRA_HOME/bin/cqlsh 10.64.200.49 9042
And run the following examples:
CREATE KEYSPACE "testdb" WITH REPLICATION = { 'class' : 'SimpleStrategy' , 'replication_factor' :3 };
USE testdb;
CREATE TABLE employee (
employeeID int,
departmentID int,
firstName varchar,
lastName varchar,
PRIMARY KEY (employeeID, departmentID)
);
INSERT INTO employee (employeeID, departmentID, firstName, lastName)
VALUES (120, 10, 'John', 'Green');
INSERT INTO employee (employeeID, departmentID, firstName, lastName)
VALUES (125, 13, 'Arthur', 'Smith');
SELECT * FROM employee WHERE employeeID IN (120, 125);
SELECT COUNT(*) FROM employee;
CREATE INDEX employee_firstName_idx ON employee(firstName);
SELECT * FROM employee WHERE firstName = 'Arthur';

No comments:

Post a Comment