VM内存:8G
利用docker构建三节点两数据中心的Cassandra集群
1、拉取镜像
docker pull cassandra
2、创建第一个节点
docker run --name cas1 -p 9042:9042 -e CASSANDRA_CLUSTER_NAME=MyCluster -e CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch -e CASSANDRA_DC=datacenter1 -d cassandra
3、创建第二、三个节点
docker run --name cas2 -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' cas1)" -e CASSANDRA_CLUSTER_NAME=MyCluster -e CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch -e CASSANDRA_DC=datacenter1 -d cassandra
docker run --name cas3 -e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' cas1)" -e CASSANDRA_CLUSTER_NAME=MyCluster -e CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch -e CASSANDRA_DC=datacenter2 -d cassandra
4、检查集群状态
docker exec -it cas1 nodetool status
集群创建成功后如下显示,集群创建成功:

5、集群测试
在cas1节点上创建数据库,每个数据中心1份数据拷贝
CREATE KEYSPACE ks_test WITH replication = {'class': 'NetworkTopologyStrategy','datacenter1':1,'datacenter2':1};
nodetool status ks_test
查看数据分布:

在datacenter1中,数据均匀分布
创建表并插入数据:
use ks_test ;
CREATE TABLE t_test(patient_id int,id int,date timeuuid,details text,PRIMARY KEY (patient_id, id));
INSERT INTO t_test (patient_id,id,date,details) values (1,1,now(),'first exam patient 1');
INSERT INTO t_test (patient_id,id,date,details) values (1,2,now(),'second exam patient 1');
INSERT INTO t_test (patient_id,id,date,details) values (2,1,now(),'first exam patient 2');
INSERT INTO t_test (patient_id,id,date,details) values (3,1,now(),'first exam patient 3');
在节点2,3查看数据

参考:
https://gokhanatil.com/2018/02/build-a-cassandra-cluster-on-docker.html
https://developer.ibm.com/tutorials/ba-set-up-apache-cassandra-architecture/
来源:CSDN
作者:theseus_jg
链接:https://blog.csdn.net/theseus_jg/article/details/103757165