本文檔主要介紹如何使用ansible在遠端centos伺服器docker環境快速安裝redis6.0.8,目标伺服器為一台阿裡雲ECS,安裝作業系統為阿裡雲提供的公共鏡像Centos7.9-64。且已經預裝docker環境。
準備
硬體
- 一台阿裡雲ECS:1c2g40g
- 預裝docker環境 參考 基于ansible在centos伺服器安裝docker
- 一台可以連接配接阿裡雲的linux伺服器,預裝ansible和git,其中ansible版本為2.9.27
基礎環境
伺服器類型 | OS | 公網IP | 内網IP | 備注 |
---|---|---|---|---|
目标伺服器 | Centos7.9(阿裡雲自帶的公共鏡像) | 123.23.12.21 | 172.22.3.13 | 阿裡雲ECS(root/Epay@1002) |
主要端 | Centos7.9 | 192.168.99.240 | 本地LInux伺服器 |
安裝
本地安裝指ssh到主要端,然後通過ansible将redis容器在目标伺服器上啟動并運作。
下載下傳腳本
[root@myhost ~]# git clone https://gitee.com/pi4k8s/ansible-exmaples.git
安裝python3
目标伺服器安裝的Centos7.9作業系統預設安裝的python版本比較低,其實并不支援ansible的docker容器相關指令;這裡我們需要先在目标伺服器遠端安裝一個高版本的python,後續再通過指定python路徑的方式去執行ansible腳本就可以成功操作docker容器了。
前面一個步驟中下載下傳的腳本目前是把python3編譯安裝在/usr/soft/python3目錄下,後續啟動docker容器均需要指定ansible_python_interpreter為/usr/soft/python3。
[root@server240 ansible-examples]# cd ..
[root@server240 output]# cd ansible-examples/aliyun-cento79/
[root@server240 aliyun-centos7.9]# ANSIBLE_HOST_KEY_CHECKING=false sh install-python.sh myhost
PLAY [myhost] ********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [myhost]
TASK [python3 : install required packages] ***************************************************************
ok: [myhost]
TASK [python3 : get python sources] **********************************************************************
ok: [myhost]
TASK [remove python3] ************************************************************************************
ok: [myhost]
TASK [remove python3-compile] ****************************************************************************
ok: [myhost]
TASK [cp python3] ****************************************************************************************
changed: [myhost]
TASK [compile python3] ***********************************************************************************
changed: [myhost]
TASK [install python3] ***********************************************************************************
changed: [myhost]
TASK [python3 : install pip docker-py] *******************************************************************
changed: [myhost]
PLAY RECAP ***********************************************************************************************
myhost : ok=9 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@server240 aliyun-centos7.9]# ansible -m shell -a '/usr/soft/python3/bin/python3 --version' -i hosts myhost
myhost | CHANGED | rc=0 >>
Python 3.6.13
安裝redis6.0.8
[root@server240 ansible-examples]# cd ..
[root@server240 output]# cd ansible-examples/aliyun-cento79/
[root@server240 aliyun-centos7.9]# ANSIBLE_HOST_KEY_CHECKING=false sh install-redis.sh myhost
PLAY [myhost] ********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [myhost]
TASK [redis : Allow Overcommit Memory] *******************************************************************
changed: [myhost]
TASK [redis : Disable THP support scripts added to rc.local] *********************************************
changed: [myhost]
TASK [redis : disable transparent_hugepage] **************************************************************
changed: [myhost]
TASK [deploy redis docker container] *********************************************************************
changed: [myhost]
TASK [redis : get ip] ************************************************************************************
changed: [myhost]
TASK [redis : debug] *************************************************************************************
ok: [myhost] => {
"containerIp": {
"changed": true,
"cmd": [
"docker",
"inspect",
"--format",
"{{ .NetworkSettings.IPAddress }}",
"redis"
],
"delta": "0:00:00.039010",
"end": "2022-05-07 21:58:23.130803",
"failed": false,
"rc": 0,
"start": "2022-05-07 21:58:23.091793",
"stderr": "",
"stderr_lines": [],
"stdout": "172.17.0.2",
"stdout_lines": [
"172.17.0.2"
]
}
}
RUNNING HANDLER [redis : wait for container to become active] ********************************************
ok: [myhost]
PLAY RECAP ***********************************************************************************************
myhost : ok=8 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@server240 aliyun-centos7.9]# ansible -m shell -a 'docker exec redis redis-cli -v' -i hosts myhost
myhost | CHANGED | rc=0 >>
redis-cli 6.0.8