#云原生征文# 基于ansible远程docker环境安装redis集群 原创

cuiyingfeng
发布于 2022-6-6 07:12
浏览
0收藏

本文档主要介绍如何使用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

【本文正在参加云原生有奖征文活动】,活动链接:https://ost.51cto.com/posts/12598

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2022-6-18 22:02:38修改
1
收藏
回复
举报
回复
    相关推荐