Ansible部署系列:(五)安装Cassandra

5. 安装Cassandra

5.1. 说明

安装在192.168.0.130这台机里,使用ansible进行安装和启动。脚本支持集群安装,在ansible的hosts文件里面[cassandraservers]进行配置,但目前我们只需要一台,以后有需要再进行扩展。

5.2. yml脚本

---
- hosts: cassandraservers
  remote_user: root
  vars_files:
    - ../vars.yml

  tasks:
    - name: mkdir jdk directory  #创建安装目录
      file: path={{ INSTALL_DIR }} state=directory mode=0755

    - name: Create cassandra group
      group: name=cassandra state=present

    - name: Create cassandra user
      user: name=cassandra group=cassandra

    - name: copy and unzip cassandra
      unarchive:
        src: "{{ PLAYBOOK_DIR }}/files/apache-cassandra-3.11.6-bin.tar.gz"
        dest: "{{ INSTALL_DIR }}"

    - name: install configuration file for cassandra
      template:
        src: "{{ PLAYBOOK_DIR }}/cassandra/templates/cassandra.yaml.j2"
        dest: "{{ INSTALL_DIR }}/apache-cassandra-3.11.6/conf/cassandra.yaml"

    - name: install rackdc configuration file for cassandra
      template:
        src: "{{ PLAYBOOK_DIR }}/cassandra/templates/cassandra-rackdc.properties.j2"
        dest: "{{ INSTALL_DIR }}/apache-cassandra-3.11.6/conf/cassandra-rackdc.properties"

    - name: mkdir ditectory for cassandra data
      file:
        dest: "{{ CASSANDRA_DATA_PATH }}"
        mode: 0755
        state: directory
        owner: cassandra
        group: cassandra

    - name: change owner and group
      file:
        path: "{{ INSTALL_DIR }}/apache-cassandra-3.11.6"
        owner: cassandra
        group: cassandra
        recurse: yes
      
    - name: make cassandra permission
      file:
        path: "{{ INSTALL_DIR }}/apache-cassandra-3.11.6/bin"
        mode: "a+x"
        recurse: yes

    - name: firewarld add 9042
      firewalld:
        port: 9042/tcp
        permanent: true
        immediate: true
        zone: public
        state: enabled
    - name: firewarld add 9160
      firewalld:
        port: 9160/tcp
        permanent: true
        immediate: true
        zone: public
        state: enabled
    - name: firewarld add 7000
      firewalld:
        port: 7000/tcp
        permanent: true
        immediate: true
        zone: public
        state: enabled

    - name: set env
      lineinfile:
        dest: ~/.bashrc
        insertafter: "{{ item.position }}"
        line: "{{ item.value }}"
        state: present
      with_items:
        - { position: EOF, value: "export CASSANDRA_HOME={{ INSTALL_DIR }}/apache-cassandra-3.11.6" }

    - name: enforce env
      shell: source ~/.bashrc

    - name: copy service for cassandra
      template:
        src: "{{ PLAYBOOK_DIR }}/cassandra/templates/cassandra.service.j2"
        dest: "/usr/lib/systemd/system/cassandra.service"

    - name: reload systemctl
      shell: systemctl daemon-reload
      
    - name: mkdir ditectory for cassandra password
      file:
        dest: /etc/cassandra
        mode: 0755
        state: directory
        owner: cassandra
        group: cassandra

    - name: copy jmx password file for cassandra
      template:
        src: "{{ PLAYBOOK_DIR }}/cassandra/templates/jmxremote.password.j2"
        dest: "/etc/cassandra/jmxremote.password"
        mode: 0755
        owner: cassandra
        group: cassandra
        
    - name: start cassandra
      service:
        name: cassandra.service
        state: started
      tags:
        - start cassandra

5.3. j2模板

脚本里面会使用到几个j2模板对Cassandra进行配置,

5.3.1 cassandra.yaml.j2

主要需进行配置的文件是apache-cassandra-3.11.6/conf/cassandra.yaml,修改的有以下属性:

  • cluster_name
  • hints_directory
  • data_file_directories
  • commitlog_directory
  • saved_caches_directory
  • seed_provider
  • listen_address
  • broadcast_rpc_address

找到原始的配置文件cassandra.yaml修改后缀加上.j2,根据上面提及的属性进行如下修改:

# The name of the cluster. This is mainly used to prevent machines in
# one logical cluster from joining another.
cluster_name: {{ CASSANDRA_CLUSTER_NAME }}

# Directory where Cassandra should store hints.
# If not set, the default directory is $CASSANDRA_HOME/data/hints.
hints_directory: {{ CASSANDRA_DATA_PATH }}/hints

# Directories where Cassandra should store data on disk.  Cassandra
# will spread data evenly across them, subject to the granularity of
# the configured compaction strategy.
# If not set, the default directory is $CASSANDRA_HOME/data/data.
data_file_directories:
     - {{ CASSANDRA_DATA_PATH }}/data

# commit log.  when running on magnetic HDD, this should be a
# separate spindle than the data directories.
# If not set, the default directory is $CASSANDRA_HOME/data/commitlog.
commitlog_directory: {{ CASSANDRA_DATA_PATH }}/commitlog

# saved caches
# If not set, the default directory is $CASSANDRA_HOME/data/saved_caches.
saved_caches_directory: {{ CASSANDRA_DATA_PATH }}/saved_caches

# any class that implements the SeedProvider interface and has a
# constructor that takes a Map<String, String> of parameters will do.
seed_provider:
    # Addresses of hosts that are deemed contact points. 
    # Cassandra nodes use this list of hosts to find each other and learn
    # the topology of the ring.  You must change this if you are running
    # multiple nodes!
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
      parameters:
          # seeds is actually a comma-delimited list of addresses.
          # Ex: "<ip1>,<ip2>,<ip3>"
          - seeds: "{% set comma = joiner(",") %}
{% for item in groups['cassandraservers'] -%}
    {{ comma() }}{{ hostvars[item].inventory_hostname }}
{%- endfor %}"

# Address or interface to bind to and tell other Cassandra nodes to connect to.
# You _must_ change this if you want multiple nodes to be able to communicate!
#
# Set listen_address OR listen_interface, not both.
#
# Leaving it blank leaves it up to InetAddress.getLocalHost(). This
# will always do the Right Thing _if_ the node is properly configured
# (hostname, name resolution, etc), and the Right Thing is to use the
# address associated with the hostname (it might not be).
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: {{ ansible_hostname }}

# RPC address to broadcast to drivers and other Cassandra nodes. This cannot
# be set to 0.0.0.0. If left blank, this will be set to the value of
# rpc_address. If rpc_address is set to 0.0.0.0, broadcast_rpc_address must
# be set.
broadcast_rpc_address: {{ ansible_default_ipv4.address }}

seeds: 是集群的节点
broadcast_rpc_address: 广播访问地址

5.3.2 cassandra-rackdc.properties.j2

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# These properties are used with GossipingPropertyFileSnitch and will
# indicate the rack and dc for this node
dc={{ datacenter }}
rack={{ rack }}

# Add a suffix to a datacenter name. Used by the Ec2Snitch and Ec2MultiRegionSnitch
# to append a string to the EC2 region name.
#dc_suffix=

# Uncomment the following line to make this snitch prefer the internal ip when possible, as the Ec2MultiRegionSnitch does.
# prefer_local=true

5.3.4 cassandra.service.j2

启动服务文件

monitorRole QED
controlRole R&D
anson {{ CASSANDRA_PWD }}

5.3.3 cassandra.service.j2

jmx登录密码

[Unit]
Description=Cassandra
Requires=network.service
After=network.service

[Service]
Type=forking
WorkingDirectory={{ INSTALL_DIR }}/apache-cassandra-3.11.6
Environment=JAVA_HOME={{ JDK_DIR }}/jdk1.8.0_212
Environment=LOCAL_JMX=no
ExecStart={{ INSTALL_DIR }}/apache-cassandra-3.11.6/bin/cassandra

User=cassandra
Group=cassandra
LimitNOFILE=65536
LimitNPROC=65536
LimitMEMLOCK=infinity
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

5.4. 运行ansible-playbook

ansible-playbook -i /etc/ansible/hosts cassandra/main.yml

5.5. 安装成功并查看启动状态

image.png

5.6. 修改登陆权限

进入安装目录

a. 进入cqlsh,默认的用户名密码都是cassandra

./apache-cassandra-3.11.6/bin/cqlsh -u cassandra -p cassandra

b. 创建超级用户

CREATE USER anson WITH PASSWORD '123456' SUPERUSER;

c. 退出

exit

d. 修改用户cassandra权限

使用刚创建的超级用户登陆,重复步骤a

./apache-cassandra-3.11.6/bin/cqlsh -u anson -p 123456

修改权限

ALTER ROLE cassandra WITH SUPERUSER = false AND LOGIN = false;

成功后即可退出,重复步骤c

5.7. 查看节点状态

./apache-cassandra-3.11.6/bin/nodetool -u anson status

根据提示输入密码后即可看到如下图所示

image.png
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容