天天看點

基于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'
           
  • 準備ansible安裝腳本
    • https://gitee.com/pi4k8s/ansible-exmaples.git
    • https://gitee.com/cuiyingfeng/jenkins-ansible-examples.git
  • 系統配置調優
  • 安裝docker
  • 安裝python
  • 安裝redis叢集

系統配置調優

最小化安裝的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

參考