본문 바로가기
▶ Back-End/Java

Cassandra 설치 방법과 간단한 Node 분산 Test

by 오늘도 코딩 2023. 8. 8.
728x90
반응형

Docker를 이용해 Cassandra를 간단하게 설치할 수 있다.

*단일 노드 / 다중 노드(Cluster) 설치방법 중 선택해서 사용

*단일 노드 설치방법을 진행했다면 모든 과정을 초기화하고 진행

*다중 노드(Cluster)  설치 방법은 단일 호스트에서 진행

*docker-compose를 사용해도 됨

*자세한 설명 생략 

 

 

▷ 바로이동

    ① 다중 호스트에서 다중 노드 설치 방법

    ② 단일 노드 설치 방법

    ③ 다중 노드 설치 방법

    ④ Node 분산 Test

 

 

▷ 다중 호스트에서 다중 노드 설치 방법

*Docker Cluster(Swarm 모드)구성

▶ Node 1 
	💡docker run --name cassandra-node1 -v cassandra-node1:/var/lib/cassandra -p 9042:9042 -p 7000:7000 -p 7001:7001 -e CASSANDRA_CLUSTER_NAME=cassandra-cluster -e CASSANDRA_BROADCAST_ADDRESS=192.168.xxx.x1 -d cassandra:latest
	
▶ Node 2 
	💡docker run --name cassandra-node2 -v cassandra-node2:/var/lib/cassandra -p 9042:9042 -p 7000:7000 -p 7001:7001 -e CASSANDRA_CLUSTER_NAME=cassandra-cluster -e CASSANDRA_BROADCAST_ADDRESS=192.168.xxx.x2 -e CASSANDRA_SEEDS=192.168.xxx.x1 -d cassandra:latest

 

 

▷ 단일 노드 설치 방법

 

 Cassandra Image Pull

 

docker pull casandra
💡C:\Windows\system32>docker pull cassandra
Using default tag: latest
latest: Pulling from library/cassandra
7007490126ef: Pull complete
80386a9626c5: Pull complete
04860b6b47ca: Pull complete
1a09e3439ecd: Pull complete
82b989d172b7: Pull complete
c6101fe253bc: Pull complete
fe14d5ddcfc9: Pull complete
2736db7df4cb: Pull complete
1d96342d8b56: Pull complete
Digest: sha256:c2f86d94675a9f7f3d6287d25b83bec03229b4e050fbe91e3bf3e26771bc8794
Status: Downloaded newer image for cassandra:latest
docker.io/library/cassandra:latest

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview cassandra

💡C:\Windows\system32>docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
cassandra    latest    36dc7162221b   5 days ago   354MB

 

 Docker Volume Create

*Volume을 설정하지 않으면 Container 삭제 시 데이터도 함께 삭제

 

docker volume create cassandra-node1
💡C:\Windows\system32>docker volume ls
DRIVER    VOLUME NAME

💡C:\Windows\system32>docker volume create cassandra-node1
cassandra-node1

💡C:\Windows\system32>docker volume ls
DRIVER    VOLUME NAME
local     cassandra-node1

*생성된 Volume 경로(Windows 10) : \\wsl.localhost\docker-desktop-data\data\docker\volumes\cassandra-node1\_data

 

③ Cassandra Docker Container Run

 

docker container run 
	--name cassandra 
	-v cassandra-node1:/var/lib/cassandra 
	-p 9042:9042 
	-d 
	cassandra:latest

*--name : 컨테이너 명
*-v : 마운트 할 볼륨명 : 컨테이너 내의 경로
*-p : 포트 설정
*-d : background 실행
*cassandra:latest : 위에서 받은 Cassandra Image
💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

💡C:\Windows\system32>docker container run --name cassandra -v cassandra-node1:/var/lib/cassandra -p 9042:9042 -d cassandra:latest
d9bbb52b7267b4a0e05446450ed81732a32a10ba944d59d2b1c96069a5271dc1

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED         STATUS         PORTS                                                       NAMES
d9bbb52b7267   cassandra:latest   "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra

 

Cassandra Docker Container  확인

 

*Cassandra Container 진입방법 2가지

*Container bash shell
docker exec -it cassandra bin/bash

*Cassandra cql in the Container
docker exec -it cassandra cqlsh
💡C:\Windows\system32>docker exec -it cassandra bin/bash
💡root@059d9de84e40:/# cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>
💡C:\Windows\system32>docker exec -it cassandra cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>

 

⑤ Node 상태 확인

 

nodetool status
💡root@d9bbb52b7267:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load        Tokens  Owns (effective)  Host ID                               Rack
UN  172.17.0.2  104.33 KiB  16      100.0%            d181a29d-9057-4b75-a05f-8fd66269f130  rack1

 

 

▷ 다중 노드 설치 방법

*단일 노드 설치 방법과 겹치는 부분 생략

*① Cassandra Image Pull 생략

*④ Cassandra Docker Container  확인 생략

 

 Docker Volume Create

*Volume을 설정하지 않으면 Container 삭제 시 데이터도 함께 삭제

*Node를 2개 생성하고, 각각 다른 Volume을 사용

 

docker volume create cassandra-node1
docker volume create cassandra-node2
💡C:\Windows\system32>docker volume ls
DRIVER    VOLUME NAME

💡C:\Windows\system32>docker volume create cassandra-node1
cassandra-node1

💡C:\Windows\system32>docker volume create cassandra-node2
cassandra-node2

💡C:\Windows\system32>docker volume ls
DRIVER    VOLUME NAME
local     cassandra-node1
local     cassandra-node2

*생성된 Volume 경로(Windows 10) : \\wsl.localhost\docker-desktop-data\data\docker\volumes\cassandra-node1\_data

*생성된 Volume 경로(Windows 10) : \\wsl.localhost\docker-desktop-data\data\docker\volumes\cassandra-node2\_data

 

③ Docker Network 설정

*Docker Container 끼리 서로 통신을 하기 위함

 

docker network create cassandra_network
💡C:\Windows\system32>docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
45af0bb071c8   bridge    bridge    local
d12ccaf4be1e   host      host      local
119c1a6a9916   none      null      local

💡C:\Windows\system32>docker network create cassandra_network
15fb0dc9e88a447c9a3ec86110c1315202b14c9b2c4336dba94268461810699b

💡C:\Windows\system32>docker network ls
NETWORK ID     NAME                DRIVER    SCOPE
45af0bb071c8   bridge              bridge    local
15fb0dc9e88a   cassandra_network   bridge    local
d12ccaf4be1e   host                host      local
119c1a6a9916   none                null      local

 

④ Cassandra Docker Container Run

 

docker run 
	--name cassandra-node1 
	--net=cassandra_network 
	-v cassandra-node1:/var/lib/cassandra 
	-p 9042:9042 
	-e CASSANDRA_CLUSTER_NAME=cassandra-cluster 
	-d 
	cassandra:latest


docker run 
	--name cassandra-node2 
	--net=cassandra_network 
	-v cassandra-node2:/var/lib/cassandra 
	-p 9043:9042 
	-e CASSANDRA_CLUSTER_NAME=cassandra-cluster 
	-e CASSANDRA_SEEDS=cassandra-node1 
	-d 
	cassandra:latest



*--name : 컨테이너 명
*--net : Docker Network 설정
*-v : 마운트 할 볼륨명 : 컨테이너 내의 경로
*-p : 포트 설정
*-e : 환경 변수 설정
*-d : background 실행
*cassandra:latest : 위에서 받은 Cassandra Image
💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

💡C:\Windows\system32>docker run --name cassandra-node1 --net=cassandra_network -v cassandra-node1:/var/lib/cassandra -p 9042:9042 -e CASSANDRA_CLUSTER_NAME=cassandra-cluster -d cassandra:latest
c98a2cb2bf54703093e8d889a1f913b96ab97ec428e5cee5322655c07ae41e9c

💡C:\Windows\system32>docker run --name cassandra-node2 --net=cassandra_network -v cassandra-node2:/var/lib/cassandra -p 9043:9042 -e CASSANDRA_CLUSTER_NAME=cassandra-cluster -e CASSANDRA_SEEDS=cassandra-node1 -d cassandra:latest
2a7787377e8e9127caf55cb4c19903143bdadf074e079868417215d4ec6e5ef1

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED          STATUS          PORTS                                                       NAMES
2a7787377e8e   cassandra:latest   "docker-entrypoint.s…"   16 seconds ago   Up 15 seconds   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9043->9042/tcp   cassandra-node2
c98a2cb2bf54   cassandra:latest   "docker-entrypoint.s…"   20 seconds ago   Up 19 seconds   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra-node1

 

④ Node 상태 확인

*아래 Node 분산 Test 참고

 

💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load        Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  109.38 KiB  16      100.0%            4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  109.3 KiB   16      100.0%            77b25610-b64f-4d62-8525-688204ba79d9  rack1

*Replica 수가 2로 설정되어 동일한 데이터의 Replica를 사용할 수 있으므로  두 개의 Node가 Owns 100.0%

*Defult로 N개의 Node가 N개의 Replica를 생성하는 모양(데이터 분산, Replica 분산 100%)

 

*Keyspace에서 Replica 설정 가능

CREATE KEYSPACE testDB WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'datacenter1': 1};

/** Test를 위한 ColumeFamily 생성 */
USE testDB;
CREATE TABLE users ( user_id int PRIMARY KEY, fname text, lname text );
INSERT INTO users (user_id,  fname, lname) VALUES (1745, 'john', 'smith');
SELECT * FROM users;
💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> CREATE KEYSPACE testDB WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'datacenter1': 1};
💡cqlsh> quit
💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  99.21 KiB  16      48.8%             4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  109.3 KiB  16      51.2%             77b25610-b64f-4d62-8525-688204ba79d9  rack1

*Replica 1로 설정 후 Owns 상태

*이 경우 cassandra-node1이 종료되면 cassandra-node2는 정상적으로 데이터를 조회할 수 없음

 

 

▷ Node 분산 Test

*Test Data는 위에서 생성

 

○ Node 2개가 모두 구동 중일 때 ( Replica1 )

 

💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  99.21 KiB  16      48.8%             4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  109.3 KiB  16      51.2%             77b25610-b64f-4d62-8525-688204ba79d9  rack1

💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> USE testDB;
💡cqlsh:testdb> SELECT * FROM users;

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith

 

○ Node 1개만 구동 중일 때 ( Replica1 )

*cassandra-node2 중지

*해당 케이스 오류 발생

 

💡C:\Windows\system32>docker stop cassandra-node2

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED          STATUS          PORTS                                                       NAMES
c98a2cb2bf54   cassandra:latest   "docker-entrypoint.s…"   24 minutes ago   Up 24 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra-node1


💡C:\Windows\system32>docker exec -it cassandra-node1 bin/bash

💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  80.73 KiB  16      48.8%             4d416bda-a4d2-4508-b203-52133b7470b0  rack1
DN  172.19.0.3  80.66 KiB  16      51.2%             77b25610-b64f-4d62-8525-688204ba79d9  rack1

💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> use testdb;
💡cqlsh:testdb> select * from users;
NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: 127.0.0.1:9042 datacenter1>: Unavailable('Error from server: code=1000 [Unavailable exception] message="Cannot achieve consistency level ONE" info={\'consistency\': \'ONE\', \'required_replicas\': 1, \'alive_replicas\': 0}')})

 

○ 멈춘 Node 재 구동 ( Replica1 )

*다시 구동하는 약간의 시간은 필요

 

💡C:\Windows\system32>docker start cassandra-node2

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED          STATUS          PORTS                                                       NAMES
2a7787377e8e   cassandra:latest   "docker-entrypoint.s…"   39 minutes ago   Up 1 second     7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9043->9042/tcp   cassandra-node2
c98a2cb2bf54   cassandra:latest   "docker-entrypoint.s…"   40 minutes ago   Up 40 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra-node1

💡C:\Windows\system32>docker exec -it cassandra-node1 bin/bash

💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  80.73 KiB  16      48.8%             4d416bda-a4d2-4508-b203-52133b7470b0  rack1
DN  172.19.0.3  80.66 KiB  16      51.2%             77b25610-b64f-4d62-8525-688204ba79d9  rack1

💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> use testdb;
💡cqlsh:testdb> select * from users;

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith

 

○ Node 2개가 모두 구동 중일 때 ( Replica2 )

*Keyspace를 삭제 후 다시 생성

 

💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> drop keyspace  testdb;
💡cqlsh> quit

💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load        Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  287.11 KiB  16      100.0%            4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  188.4 KiB   16      100.0%            77b25610-b64f-4d62-8525-688204ba79d9  rack1

💡root@c98a2cb2bf54:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> CREATE KEYSPACE testDB WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'datacenter1': 2};
💡cqlsh> USE testDB;
💡cqlsh:testdb> CREATE TABLE users ( user_id int PRIMARY KEY, fname text, lname text );
💡cqlsh:testdb> INSERT INTO users (user_id,  fname, lname) VALUES (1745, 'john', 'smith');
💡cqlsh:testdb> SELECT * FROM users;

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith
    
💡cqlsh:testdb> quit
💡root@c98a2cb2bf54:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load        Tokens  Owns (effective)  Host ID                               Rack
UN  172.19.0.2  231.15 KiB  16      100.0%            4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  250.83 KiB  16      100.0%            77b25610-b64f-4d62-8525-688204ba79d9  rack1

 

○ Node 1개만 구동 중일 때 ( Replica2 )

*cassandra-node1 중지

 

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED             STATUS             PORTS                                                       NAMES
2a7787377e8e   cassandra:latest   "docker-entrypoint.s…"   About an hour ago   Up 44 minutes      7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9043->9042/tcp   cassandra-node2
c98a2cb2bf54   cassandra:latest   "docker-entrypoint.s…"   About an hour ago   Up About an hour   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   cassandra-node1

💡C:\Windows\system32>docker stop cassandra-node1
cassandra-node1

💡C:\Windows\system32>docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED             STATUS          PORTS                                                       NAMES
2a7787377e8e   cassandra:latest   "docker-entrypoint.s…"   About an hour ago   Up 45 minutes   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9043->9042/tcp   cassandra-node2

💡C:\Windows\system32>docker exec -it cassandra-node2 bin/bash
root@2a7787377e8e:/# nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load        Tokens  Owns (effective)  Host ID                               Rack
DN  172.19.0.2  231.15 KiB  16      100.0%            4d416bda-a4d2-4508-b203-52133b7470b0  rack1
UN  172.19.0.3  250.83 KiB  16      100.0%            77b25610-b64f-4d62-8525-688204ba79d9  rack1

💡root@2a7787377e8e:/# cqlsh
Connected to cassandra-cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
💡cqlsh> use testdb;
💡cqlsh:testdb> select * from users;

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith

 

 

▷ ETC 

*Volumn Mount 확인

 

 

 

▷ 참고

 

How to add some nodes to existing cluster on Cassandra in Docker

I'm using latest default image of Cassandra in Docker. In Cassandra I have one keyspace with my data. What I want to do is to add some nodes and change replication factor. How can I achieve this?

stackoverflow.com

 

Is it acceptable to have 100% ownership on every Cassandra node?

I am working on a Cassandra cluster with 3 nodes and this is the current ownership rate: 19:36:30 root@node01:~# nodetool status foo Datacenter: Foo ===================== Status=Up/Down |/ State=N...

stackoverflow.com

 

 

▷ 관련 글

 

Cassandra 기본 개념

Apache Cassandra의 기본 개념을 간단히 소개 *자세한 설명 생략 ▷ Cassandra 란? *현재 Facebook과 twitter 등 에서 사용 - Apache Cassandra는 대용량 데이터를 관리하기 위해 설계된 시스템으로 분산형 오픈소

coding-today.tistory.com

 

SpringBoot + Cassandra 연동(Single Node)

Docker에 설치한 Cassandra(Single Node)와 SpringBoot 연동 *spring-boot-starter-parent 2.3.10.RELEASE 기준 *자세한 설명 생략 ▷ 이슈 사항 확인 !! 이슈 → Local Host에서 Multi Node를 연동할 때 Docker Network 문제 발생 *Spr

coding-today.tistory.com

 

SpringBoot + Cassandra 연동(Multi Node)

Docker에 설치한 Cassandra(Multi Node)와 SpringBoot 연동 *spring-boot-starter-parent 2.3.10.RELEASE 기준 *Docker에 Application을 올리는 과정 *자세한 설명 생략 ▷ 이슈 사항 확인 !! 이슈 → Local Host에서 Multi Node를 연

coding-today.tistory.com

 

Docker Cluster(Swarm 모드)구성 방법

두 개의 HOST를 이용한 Docker Cluster 구성 *Docker Desktop(Windows 10)은 Docker Swarm을 지원하지 않는다.(단일 Cluster만 가능) *Ubuntu에서 진행 *A Host : Manager Node *B Host : Worker Node *Manager Node : Worker Node를 관리하

coding-today.tistory.com

 

 

728x90
728x90

'▶ Back-End > Java' 카테고리의 다른 글

SpringBoot + Cassandra 연동(Multi Node)  (0) 2023.08.18
SpringBoot + Cassandra 연동(Single Node)  (0) 2023.08.09
SLF4J 기본 사용 방법  (0) 2023.08.07
Custom Annotation 생성 Example  (0) 2023.07.26
SpringBoot Maria DB + MyBatis 설정  (0) 2022.12.06

댓글