美文网首页单片机学习空间
Ansible部署系列:(五)安装Cassandra

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

作者: Anson_1f2a | 来源:发表于2021-06-10 14:30 被阅读0次

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

相关文章

网友评论

    本文标题:Ansible部署系列:(五)安装Cassandra

    本文链接:https://www.haomeiwen.com/subject/srnqeltx.html