5

基于ansible在docker环境安装redis集群(3主3从)

 1 year ago
source link: https://blog.51cto.com/cuiyingfeng/5359437
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于ansible在docker环境安装redis集群(3主3从)

原创

本文档主要介绍如何使用ansible在远程centos服务器docker环境快速安装redis集群(3主3从),目标服务器为一台本地虚拟机,安装操作系统为官方的Centos7.9-64,语言环境选择简体中文,安装类型选择最小化安装,不预装任何软件。

  • 一台虚机:4c8g40g
  • 一台可以连接该虚机的mac pro,预装ansible和git,其中ansible版本为2.12.4
服务器类型 OS 内网IP 备注
目标服务器 Centos7.9 10.0.1.121 root/root1234
主控端 mac pro
  • 准备hosts文件
#redis-cluster-hosts
server1 ansible_ssh_user=root ansible_ssh_host=10.0.1.121 ansible_ssh_pass='root1234'

系统配置调优

最小化安装的centos7.9一般需要做一些才能充分发挥系统性能,ansible安装脚本里有roles可以用来修改ulimit、关闭防火墙和关闭selinux。

cuiyingfeng@bogon ~ % git clone https://gitee.com/pi4k8s/ansible-exmaples.git
cuiyingfeng@bogon ~ % cd ansible-exmaples

# 用准备好的redis-cluster-hosts文件替换下载的hosts
cuiyingfeng@MacBook-Pro-2 ansible-exmaples % cat hosts
server1 ansible_ssh_user=root ansible_ssh_host=10.0.1.121 ansible_ssh_pass='root1234'
cuiyingfeng@MacBook-Pro-2 ansible-exmaples %  ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook base-centos7.9/playbook/install-os-enforce.yaml -i hosts -e env_hosts=server1

安装docker

cuiyingfeng@bogon ~ % git clone https://gitee.com/pi4k8s/ansible-exmaples.git
cuiyingfeng@bogon ~ % cd ansible-exmaples

# 用准备好的redis-cluster-hosts文件替换下载的hosts
cuiyingfeng@MacBook-Pro-2 ansible-exmaples % cat hosts
server1 ansible_ssh_user=root ansible_ssh_host=10.0.1.121 ansible_ssh_pass='root1234'
cuiyingfeng@MacBook-Pro-2 ansible-exmaples %  ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook common-centos7.9/playbook/install-docker.yaml -i hosts -e env_hosts=server1
cuiyingfeng@MacBook-Pro-2 ansible-exmaples % ansible -m shell -a 'docker --version' -o -i hosts server1 
server1 | CHANGED | rc=0 | (stdout) Docker version 20.10.16, build aa7e414

安装python3

目标服务器安装的Centos7.9操作系统默认安装的python版本比较低,其实并不支持ansible的docker容器相关指令;这里我们需要先在目标服务器远程安装一个高版本的python,后续再通过指定python路径的方式去执行ansible脚本就可以成功操作docker容器了。

本步骤中我们可以直接通过yum方式安装pyhon3(centos7.9默认安装的版本是3.6.8),然后再用pip3安装docker,后续就可以正常启动docker容器了,只是需要指定ansible_python_interpreter为/usr/bin/python3。

另外要注意一下主控端的ansible版本号,本实践使用的ansible版本为2.12.4(2.9.27版本也可以通过),如果ansible版本过低可能会导致基于本实践的安装python3失败。

# 在安装docker的步骤中我们已经同步下载了安装python3的ansible脚本,可以直接执行如下
cuiyingfeng@MacBook-Pro-2 ansible-exmaples %  ansible-playbook common-centos7.9/playbook/install-python3.yaml -i hosts -e env_hosts=server1
cuiyingfeng@MacBook-Pro-2 ansible-exmaples % ansible -m shell -a 'python3 --version' -o -i hosts server1
server1 | CHANGED | rc=0 | (stdout) Python 3.6.8

安装redis集群

cuiyingfeng@MacBook-Pro-2 ~ % git clone https://gitee.com/cuiyingfeng/jenkins-ansible-examples.git
cuiyingfeng@MacBook-Pro-2 ~ % cd jenkins-ansible-examples/redis-cluster
# 用准备好的redis-cluster-hosts文件替换下载的redis-cluster-hosts
cuiyingfeng@MacBook-Pro-2 redis-cluster % cat redis-cluster-hosts 
server1 ansible_ssh_user=root ansible_ssh_host=10.0.1.121 ansible_ssh_pass='root1234'
cuiyingfeng@MacBook-Pro-2 redis-cluster % ansible-playbook playbook/install-redis-cluster.yaml -i redis-cluster-hosts -e env_hosts=server1
cuiyingfeng@MacBook-Pro-2 redis-cluster % ansible -m shell -a 'docker exec redis-6379  redis-cli -a 1234 cluster info' -i redis-cluster-hosts server1 
server1 | CHANGED | rc=0 >>
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:252
cluster_stats_messages_pong_sent:242
cluster_stats_messages_sent:494
cluster_stats_messages_ping_received:242
cluster_stats_messages_pong_received:248
cluster_stats_messages_received:490Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

pipeline

 https://gitee.com/cuiyingfeng/jenkins-ansible-examples 下有一枚彩蛋redis-cluster/install-redis-cluster-from-scratch.groovy,熟悉jenkins的同学可以上车了。

 Docker 搭建 Redis Cluster 集群环境

 Scaling with Redis Cluster

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK