• Ansible批量安装clickhouse集群(shell脚本方式)


    准备工作

    下载安装包:
    java:
    jdk-8u333-linux-aarch64.tar.gz
    下载链接:
    https://www.oracle.com/java/technologies/downloads/#java8
    zookeeper:
    zookeeper-3.4.13.tar.gz
    下载链接:
    https://zookeeper.apache.org/releases.html#download
    http://archive.apache.org/dist/zookeeper/
    clickhouse:
    clickhouse-common-static-22.7.3.5-arm64.tgz
    clickhouse-common-static-dbg-22.7.3.5-arm64.tgz
    clickhouse-server-22.7.3.5-arm64.tgz
    clickhouse-client-22.7.3.5-arm64.tgz
    下载链接:
    https://packages.clickhouse.com/tgz/stable/

    ansible配置文件:
    hosts_40_1:

    1. [hosts_40]
    2. ***.***.***.*** shard_id=1 myid=1
    3. ***.***.***.*** shard_id=2 myid=2
    4. ***.***.***.*** shard_id=3 myid=3
    5. ***.***.***.*** shard_id=4 myid=4
    6. ***.***.***.*** shard_id=5 myid=5
    7. ***.***.***.*** shard_id=6 myid=6
    8. ***.***.***.*** shard_id=7 myid=7
    9. ***.***.***.*** shard_id=8 myid=8
    10. ***.***.***.*** shard_id=9 myid=9
    11. ***.***.***.*** shard_id=10 myid=10
    12. [hosts_40:vars]
    13. ansible_ssh_user=xxx
    14. ansible_ssh_pass='***'
    15. ansible_sudo_pass='***'

    一.磁盘挂载

    1.创建/data目录

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls /data"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls /data"

    2.格式化磁盘

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkfs.xfs -f /dev/sdb"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkfs.xfs -f /dev/sdb"

    3.记录

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "cat << EOF >> /etc/fstab
    2. /dev/sdb /data xfs defaults 0 0
    3. EOF"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "cat << EOF >> /etc/fstab
    5. /dev/sdb /data xfs defaults 0 0
    6. EOF"
    7. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "cat /etc/fstab"
    8. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "cat /etc/fstab"

    4.挂载

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mount -a"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mount -a"

    5.查看磁盘挂载是否成功

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "df -h"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "df -h"

    二.防火墙

    1.查看防火墙

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "systemctl status firewalld.service"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "systemctl status firewalld.service"

    2.如果没关防火墙需要关闭防火墙

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "systemctl stop firewalld.service"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "systemctl stop firewalld.service"

    三.添加hosts

    1.查看ip

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "hostname -i | cut -d ' ' -f3"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "hostname -i | cut -d ' ' -f3"

    2.查询hosts

    get_hosts.sh脚本:

    1. #!/bin/bash
    2. host=`hostname`
    3. ip=`hostname -i | cut -d ' ' -f3`
    4. echo $ip $host
    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m script -a '/home/deployer/get_hosts.sh'
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m script -a '/home/deployer/get_hosts.sh'

    3.整理好hosts后将hosts分发

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/home/deployer/hosts.txt dest=/home/deployer/ owner=deployer group=deployer mode=0777"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/home/deployer/hosts.txt dest=/home/deployer/ owner=deployer group=deployer mode=0777"

    4.检查是否分发成功

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /home/deployer"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /home/deployer"

    5.将分发后的hosts添加到每台机器

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "cat /home/deployer/hosts.txt >> /etc/hosts"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "cat /home/deployer/hosts.txt >> /etc/hosts"

    6.检查是否添加成功

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "cat /etc/hosts"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "cat /etc/hosts"

    四.创建目录

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/clickhouse"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/clickhouse"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/clickhouse/package"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/clickhouse/package"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/clickhouse/data"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/clickhouse/data"
    7. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/clickhouse/data/tmp"
    8. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/clickhouse/data/tmp"
    9. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chmod -R 777 /data/clickhouse"
    10. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chmod -R 777 /data/clickhouse"
    11. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chown -R deployer:deployer /data/clickhouse"
    12. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chown -R deployer:deployer /data/clickhouse"
    13. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/zookeeper"
    14. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/zookeeper"
    15. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/zookeeper/package"
    16. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/zookeeper/package"
    17. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/zookeeper/data"
    18. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/zookeeper/data"
    19. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/zookeeper/log"
    20. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/zookeeper/log"
    21. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chmod -R 777 /data/zookeeper"
    22. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chmod -R 777 /data/zookeeper"
    23. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chown -R deployer:deployer /data/zookeeper"
    24. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chown -R deployer:deployer /data/zookeeper"
    25. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/java"
    26. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/java"
    27. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mkdir /data/java/package"
    28. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mkdir /data/java/package"
    29. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/java/package"
    30. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/java/package"
    31. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chmod -R 777 /data/java"
    32. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chmod -R 777 /data/java"
    33. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "chown -R deployer:deployer /data/java"
    34. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "chown -R deployer:deployer /data/java"

    五.传文件

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/data/zookeeper/package/ dest=/data/zookeeper/package/ owner=deployer group=deployer mode=0777"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/data/zookeeper/package/ dest=/data/zookeeper/package/ owner=deployer group=deployer mode=0777"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/zookeeper/package"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/zookeeper/package"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/data/clickhouse/package/ dest=/data/clickhouse/package/ owner=deployer group=deployer mode=0777"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/data/clickhouse/package/ dest=/data/clickhouse/package/ owner=deployer group=deployer mode=0777"
    7. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/clickhouse/package"
    8. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/clickhouse/package"
    9. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/data/java/package/jdk-8u333-linux-aarch64.tar.gz dest=/data/java/package/jdk-8u333-linux-aarch64.tar.gz owner=deployer group=deployer mode=0777"
    10. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/data/java/package/jdk-8u333-linux-aarch64.tar.gz dest=/data/java/package/jdk-8u333-linux-aarch64.tar.gz owner=deployer group=deployer mode=0777"
    11. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/java/package"
    12. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/java/package"

    六.安装java

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "tar -zxvf /data/java/package/jdk-8u333-linux-aarch64.tar.gz -C /data/java/package/"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "tar -zxvf /data/java/package/jdk-8u333-linux-aarch64.tar.gz -C /data/java/package/"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "mv /data/java/package/jdk1.8.0_333 /data/java/package/jdk1.8.0_141"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "mv /data/java/package/jdk1.8.0_333 /data/java/package/jdk1.8.0_141"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/java/package"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/java/package"
    7. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "echo 'export JAVA_HOME=/data/java/package/jdk1.8.0_141' >> /etc/profile"
    8. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "echo 'export JAVA_HOME=/data/java/package/jdk1.8.0_141' >> /etc/profile"
    9. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "echo 'export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar' >> /etc/profile"
    10. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "echo 'export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar' >> /etc/profile"
    11. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$JAVA_HOME/bin' >> /etc/profile"
    12. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$JAVA_HOME/bin' >> /etc/profile"
    13. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "more /etc/profile"
    14. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "more /etc/profile"
    15. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "source /etc/profile"
    16. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "source /etc/profile"
    17. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "java -version"
    18. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "java -version"

    七.安装zookeeper

    1.解压zookeeper

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "tar -zxvf /data/zookeeper/package/zookeeper-3.4.13.tar.gz -C /data/zookeeper/package"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "tar -zxvf /data/zookeeper/package/zookeeper-3.4.13.tar.gz -C /data/zookeeper/package"
    3. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "ls -al /data/zookeeper/package/zookeeper-3.4.13"
    4. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "ls -al /data/zookeeper/package/zookeeper-3.4.13"

    2.创建myid myid值在hosts文件中预先设置好

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "touch /data/zookeeper/data/myid"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "touch /data/zookeeper/data/myid"
    3. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "echo -e '{{myid}}' >> /data/zookeeper/data/myid"
    4. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "echo -e '{{myid}}' >> /data/zookeeper/data/myid"
    5. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "cat /data/zookeeper/data/myid"
    6. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "cat /data/zookeeper/data/myid"

    3.修改配置文件

    3.1查看2181端口是否占用

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "netstat -tunlp|grep 2181"
    2. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "netstat -tunlp|grep 2181"

    将/data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg配置文件进行两处修改
    3.2将dataDir位置修改为
    /data/zookeeper/data
    3.3在配置文件zoo.cfg最后添加集群信息

    1. ###############cluster###############
    2. server.1=***.***.***.***:2888:3888
    3. server.2=***.***.***.***:2888:3888
    4. server.3=***.***.***.***:2888:3888
    5. server.4=***.***.***.***:2888:3888
    6. server.5=***.***.***.***:2888:3888
    7. server.6=***.***.***.***:2888:3888
    8. server.7=***.***.***.***:2888:3888
    9. server.8=***.***.***.***:2888:3888
    10. server.9=***.***.***.***:2888:3888

    3.4将zoo.cfg配置文件分发到集群中其他机器

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m copy -a "src=/data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg dest=/data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg owner=deployer group=deployer mode=0777"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m copy -a "src=/data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg dest=/data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg owner=deployer group=deployer mode=0777"

    3.5查看通过是否成功

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "tail -n 100 /data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "tail -n 100 /data/zookeeper/package/zookeeper-3.4.13/conf/zoo.cfg"

    4.添加环境变量并刷新配置

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "echo 'export ZOOKEEPER=/data/zookeeper/package/zookeeper-3.4.13' >> /etc/profile"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "echo 'export ZOOKEEPER=/data/zookeeper/package/zookeeper-3.4.13' >> /etc/profile"
    3. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$ZOOKEEPER/bin' >> /etc/profile"
    4. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$ZOOKEEPER/bin' >> /etc/profile"
    5. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$ZOOKEEPER/bin' >> /home/deployer/.bash_profile"
    6. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "echo -e 'export PATH=\$PATH:\$ZOOKEEPER/bin' >> /home/deployer/.bash_profile"
    7. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "source /etc/profile"
    8. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "source /etc/profile"

    5.启动zookeeper集群
    启动

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "source /etc/profile && zkServer.sh start"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "source /etc/profile && zkServer.sh start"

    停止

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "source /etc/profile && zkServer.sh stop"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "source /etc/profile && zkServer.sh stop"

    查看节点状态

    1. ansible -i /etc/ansible/hosts_40_zk1 hosts_40 -m shell -a "source /etc/profile && zkServer.sh status"
    2. ansible -i /etc/ansible/hosts_40_zk2 hosts_40 -m shell -a "source /etc/profile && zkServer.sh status"

    注意:
    zookeeper相关的目录及目录下的文件都必须属于deployer,不然会出现运行失败的情况。

    八.安装clickhouse

    1.安装clickhouse-common-static

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-common-static-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-common-static-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-common-static-22.7.3.5/install"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-common-static-22.7.3.5/install"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-common-static-22.7.3.5/install/doinst.sh"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-common-static-22.7.3.5/install/doinst.sh"

    2.安装clickhouse-common-static-dbg

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5/install"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5/install"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5/install/doinst.sh"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-common-static-dbg-22.7.3.5/install/doinst.sh"

    3.安装clickhouse-server

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-server-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-server-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-server-22.7.3.5/install"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-server-22.7.3.5/install"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-server-22.7.3.5/install/doinst.sh"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-server-22.7.3.5/install/doinst.sh"

    4.安装clickhouse-client

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-client-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "tar -zxvf /data/clickhouse/package/clickhouse-client-22.7.3.5-arm64.tgz -C /data/clickhouse/package"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-client-22.7.3.5/install"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ls -al /data/clickhouse/package/clickhouse-client-22.7.3.5/install"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-client-22.7.3.5/install/doinst.sh"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "sh /data/clickhouse/package/clickhouse-client-22.7.3.5/install/doinst.sh"

    5.修改配置文件

    5.1修改/etc/clickhouse-server/config.xml

    1. 0.0.0.0
    2. /data/clickhouse/data/
    3. /data/clickhouse/data/tmp/
    4. 添加
    5. "clickhouse_remote_servers" />
    6. "zookeeper-servers" optional="true" />
    7. "macros" optional="true" />
    8. "clickhouse_compression" optional="true" />
    9. 注释

    5.2查看端口是否占用-8123、9000、9004、9005、9009

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "netstat -tunlp|grep 8123"
    2. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "netstat -tunlp|grep 8123"

    5.3修改/etc/clickhouse-server/config.d/data-paths.xml文件

    1. /data/clickhouse/data
    2. /data/clickhouse/data/tmp

    5.4添加/etc/metrika.xml

            其中/etc/metrika.xml中的因为每台机器不一样,因此配置如下模板,其它配置信息每台机器都一样:

    1. shard_id
    2. macros_ip

    5.5修改/etc/clickhouse-server/users.xml中default用户的密码:

    1. echo -n password | openssl dgst -sha256
    2. 生成加密的password
    3. (stdin)= newpassword
    4. 修改/etc/clickhouse-server/users.xml
    5. newpassword

    5.6修改配置文件后统一分发:

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/etc/clickhouse-server/users.xml dest=/etc/clickhouse-server/users.xml owner=clickhouse group=clickhouse mode=0777"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/etc/clickhouse-server/users.xml dest=/etc/clickhouse-server/users.xml owner=clickhouse group=clickhouse mode=0777"
    3. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/etc/clickhouse-server/config.d/data-paths.xml dest=/etc/clickhouse-server/config.d/data-paths.xml owner=clickhouse group=clickhouse mode=0777"
    4. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/etc/clickhouse-server/config.d/data-paths.xml dest=/etc/clickhouse-server/config.d/data-paths.xml owner=clickhouse group=clickhouse mode=0777"
    5. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/etc/clickhouse-server/config.xml dest=/etc/clickhouse-server/config.xml owner=clickhouse group=clickhouse mode=0777"
    6. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/etc/clickhouse-server/config.xml dest=/etc/clickhouse-server/config.xml owner=clickhouse group=clickhouse mode=0777"
    7. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m copy -a "src=/etc/metrika.xml dest=/etc/metrika.xml owner=clickhouse group=clickhouse mode=0777"
    8. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m copy -a "src=/etc/metrika.xml dest=/etc/metrika.xml owner=clickhouse group=clickhouse mode=0777"

    5.7查看ip

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "hostname -i | cut -d ' ' -f3"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "hostname -i | cut -d ' ' -f3"

    5.8替换/etc/metrika.xml中的shard_id

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "sed -i 's/shard_id/{{shard_id}}/g' /etc/metrika.xml"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "sed -i 's/shard_id/{{shard_id}}/g' /etc/metrika.xml"

    5.9替换/etc/metrika.xml中的macros_ip
    update_file.sh脚本:

    1. #!/bin/bash
    2. ip=`hostname -i | cut -d ' ' -f3`
    3. sed -i 's/macros_ip/'$ip'/g' /etc/metrika.xml
    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m script -a '/data/clickhouse/update_file.sh'
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m script -a '/data/clickhouse/update_file.sh'

    6.启动服务

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "clickhouse start"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "clickhouse start"

    7.查看进程

    1. ansible -i /etc/ansible/hosts_40_1 hosts_40 -m shell -a "ps -ef | grep clickhouse"
    2. ansible -i /etc/ansible/hosts_40_2 hosts_40 -m shell -a "ps -ef | grep clickhouse"

    8.效果

    页面查询
    http://***.***.***.***:8123/play

    通过系统表查询remote_servers的配置是否生效

    SELECT * FROM system.clusters;

    查看path是否配置正确

    1. SELECT
    2. name,
    3. path,
    4. formatReadableSize(free_space) AS free,
    5. formatReadableSize(total_space) AS total,
    6. formatReadableSize(keep_free_space) AS reserved
    7. FROM system.disks;

    测试单表:

    1. CREATE TABLE default.test (
    2. id Int16,
    3. name String
    4. ) ENGINE = MergeTree()
    5. ORDER BY id;

    测试分布式表:

    1. #先创建本地表
    2. CREATE TABLE default.test_local on cluster perftest_20shards_2replicas (
    3. id Int16,
    4. name String
    5. ) ENGINE = MergeTree()
    6. ORDER BY id;
    7. #创建分布式表
    8. CREATE TABLE IF NOT EXISTS default.test_distribute ON CLUSTER perftest_20shards_2replicas AS default.test_local
    9. ENGINE = Distributed(perftest_20shards_2replicas, default, test_local, rand());

    欢迎关注“程序杂货铺”公众号,里面有精彩内容,欢迎大家收看^_^

     

  • 相关阅读:
    Transwarp Inceptor中的对象
    微信小程序中实现录音功能及其功效
    2024-5-18——找出可整除性得分最大的整数
    【C语言】归并排序
    使用Docker安装和部署RabbitMQ
    Python:json数据与Python对象之间的转换
    DNS压测工具-dnsperf的安装和使用(centos)
    使用水泥路肩培土两用机进行施工一次制作成型
    [Java]SPI扩展功能
    华为云云耀云服务器L实例评测|基于华为云云耀云服务器L实例搭建EMQX大规模分布式 MQTT 消息服务器场景体验
  • 原文地址:https://blog.csdn.net/u013473512/article/details/126661679