程序员

Cassandra基础(1)—— Casasndra集群搭建

2018-12-25  本文已影响36人  黄宝玲_1003

Cassandra基础(1)—— Casasndra集群搭建
Cassandra基础(2)—— Casasndra使用及其数据类型

本文将介绍Cassandra的搭建和使用

单节点Cassandra

Cassandra最大的特点就是分布式,所以启动单节点Cassandra不论是开发还是实战都没有意义,都是多节点。单节点最大的作用就是安装Cassandra,可以提供cqlsh命令行工具,能够作为客户端去连接Cassandra。

安装

方式一:按官网教程安装,提供Debian和RPM两种包.
方式二:在mac上,可以使用brew工具安装,命令如下:

brew install cassandra 
/*
安装后的目录:  
安装目录:/usr/local/Cellar/cassandra/  
Properties: /usr/local/etc/cassandra  
Logs: /usr/local/var/log/cassandra  
Data: /usr/local/var/lib/cassandra/data
*/

多节点Cassandra

Docker 搭建

官方docker镜像默认不启用任何安全验证,且未提供相应的env variables。
有两种方式去开启认证:
方式一:在官方镜像的基础上制作自己的镜像,继承官方镜像,并用RUN命令开启相关配置,再build得到开启了认证的新镜像。
编写Dockerfile如下:

FROM cassandra:3.11.2

# Authentication backend, implementing IAuthenticator; used to identify users
# Out of the box, Cassandra provides org.apache.cassandra.auth.{AllowAllAuthenticator,
# PasswordAuthenticator}.
#
# - AllowAllAuthenticator performs no checks - set it to disable authentication.
# - PasswordAuthenticator relies on username/password pairs to authenticate
#   users. It keeps usernames and hashed passwords in system_auth.roles table.
#   Please increase system_auth keyspace replication factor if you use this authenticator.
#   If using PasswordAuthenticator, CassandraRoleManager must also be used (see below)
RUN echo "authenticator: PasswordAuthenticator" >> /etc/cassandra/cassandra.yaml

# Authorization backend, implementing IAuthorizer; used to limit access/provide permissions
# Out of the box, Cassandra provides org.apache.cassandra.auth.{AllowAllAuthorizer,
# CassandraAuthorizer}.
#
# - AllowAllAuthorizer allows any action to any user - set it to disable authorization.
# - CassandraAuthorizer stores permissions in system_auth.role_permissions table. Please
#   increase system_auth keyspace replication factor if you use this authorizer.
RUN echo "authorizer: CassandraAuthorizer" >> /etc/cassandra/cassandra.yaml

# Part of the Authentication & Authorization backend, implementing IRoleManager; used
# to maintain grants and memberships between roles.
# Out of the box, Cassandra provides org.apache.cassandra.auth.CassandraRoleManager,
# which stores role information in the system_auth keyspace. Most functions of the
# IRoleManager require an authenticated login, so unless the configured IAuthenticator
# actually implements authentication, most of this functionality will be unavailable.
#
# - CassandraRoleManager stores role data in the system_auth keyspace. Please
#   increase system_auth keyspace replication factor if you use this role manager.
RUN echo "role_manager: CassandraRoleManager" >> /etc/cassandra/cassandra.yaml

然后build,得到加入开启认证相关环境变量的cassandra镜像。

docker build -t cassandra:3.11.2_auth .

用该镜像启动的容易的cassandra.yaml文件内有我们设置的环境变量。

接下来就可以进行集群搭建了:

// 先启动种子节点
docker run --name cassandra_node1 -d -v /var/docker-volume/cassandra:/var/lib/cassandra -e CASSANDRA_CLUSTER_NAME="test_cassandra_cluster" -e CASSANDRA_BROADCAST_ADDRESS=172.31.18.97 -p 7000:7000 -p 9160:9160 -p 9042:9042 docker.sunseaiot.cn/cassandra:3.11.2_auth
// 再启动非种子节点
docker run --name cassandra_node2 -d -v /var/docker-volume/cassandra:/var/lib/cassandra -e CASSANDRA_CLUSTER_NAME="test_cassandra_cluster" -e CASSANDRA_BROADCAST_ADDRESS=172.31.19.18 -e CASSANDRA_SEEDS=172.31.18.97 -p 7000:7000 -p 9160:9160 -p 9042:9042 docker.sunseaiot.cn/cassandra:3.11.2_auth
docker run --name cassandra_node3 -d -v /var/docker-volume/cassandra:/var/lib/cassandra -e CASSANDRA_CLUSTER_NAME="test_cassandra_cluster" -e CASSANDRA_BROADCAST_ADDRESS=172.31.16.140 -e CASSANDRA_SEEDS=172.31.18.97 -p 7000:7000 -p 9160:9160 -p 9042:9042 docker.sunseaiot.cn/cassandra:3.11.2_auth

// 查看集群状态
nodetool status 

// 集群搭好后,可用默认的超级用户Cassandra(密码cassandra)登陆
cqlsh 172.31.18.97 -u cassandra -p cassandra

// 然后提升`system_auth`keyspace的复制因子(通常为节点数)
ALTER KEYSPACE system_auth WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};

// 创建一个新的超级用户替代默认的超级用户cassandra(密码cassandra)
CREATE USER sunseaiot WITH PASSWORD 'sunseaiot' SUPERUSER ;

// 最后切换用户登陆,删除默认超级用户
DROP USER cassandra;

更多细节参考文档

上一篇下一篇

猜你喜欢

热点阅读