侧边栏壁纸
博主头像
fastjrun博主等级

前大厂工程师,长期从事 Java 开发,架构设计,容器化等相关工作,精通java,熟练使用maven、jenkins等devops相关工具链,擅长容器化方案规划、设计和落地。

  • 累计撰写 70 篇文章
  • 累计创建 47 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

基于ansible在centos服务器安装docker

fastjrun
2022-05-07 / 0 评论 / 0 点赞 / 600 阅读 / 936 字 / 正在检测是否收录...

本文档主要介绍如何使用ansible在centos中快速安装时新版docker,目标服务器为一台阿里云ECS,安装操作系统为阿里云提供的公共镜像Centos7.9-64。

安装实践分为本地安装、远程安装两种,每次安装docker之前都会对ECS重装操作系统,且不预装任何服务和软件。

准备

硬件

  • 一台阿里云ECS:1c2g40g
  • 一台可以连接阿里云的linux服务器,预装ansible和git

基础环境

服务器类型 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

[root@myhost ~]# yum install -y ansible
[root@myhost ~]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[root@myhost ~]# 

安装git

[root@myhost ~]# yum install -y git
[root@myhost ~]# git version
git version 1.8.3.1
下载脚本
[root@myhost ~]# git clone https://gitee.com/pi4k8s/ansible-exmaples.git
安装docker
[root@myhost ~]# cd ansible-examples/aliyun-cento79/
[root@myhost aliyun-cento79]# sh install-docker.sh localhost
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'

PLAY [localhost] *****************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]

TASK [docker : install required packages] ****************************************************************
changed: [localhost]

TASK [Add docker repositories] ***************************************************************************
changed: [localhost]

TASK [install docker] ************************************************************************************
changed: [localhost]

TASK [docker : swap off is fstab] ************************************************************************
ok: [localhost]

TASK [docker : swap is off] ******************************************************************************
changed: [localhost]

TASK [mkdir /etc/docker] *********************************************************************************
changed: [localhost]

TASK [docker etc is file] ********************************************************************************
changed: [localhost]

TASK [restart docker] ************************************************************************************
changed: [localhost]

RUNNING HANDLER [docker : yum-makecache] *****************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because
yum is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [localhost]

RUNNING HANDLER [restart docker] *************************************************************************
changed: [localhost]

PLAY RECAP ***********************************************************************************************
localhost                  : ok=11   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
验证
[root@myhost aliyun-cento79]# docker version
Client: Docker Engine - Community
 Version:           20.10.15
 API version:       1.41
 Go version:        go1.17.9
 Git commit:        fd82621
 Built:             Thu May  5 13:16:58 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.15
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.9
  Git commit:       4433bf6
  Built:            Thu May  5 13:15:18 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

远程安装

本地安装指ssh到主控端,然后通过ansible将docker安装到目标服务器

下载脚本
[root@myhost ~]# git clone https://gitee.com/pi4k8s/ansible-exmaples.git
安装docker
[root@server240 ansible-examples]# cd ..
[root@server240 output]# cd ansible-examples/aliyun-cento79/
[root@server240 aliyun-cento79]# ANSIBLE_HOST_KEY_CHECKING=false sh install-docker.sh myhost

PLAY [myhost] ********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************
ok: [myhost]

TASK [docker : install required packages] ****************************************************************
changed: [myhost]

TASK [docker : Add docker repositories] ******************************************************************
changed: [myhost]

TASK [docker : install docker] ***************************************************************************
changed: [myhost]

TASK [docker : swap off is fstab] ************************************************************************
ok: [myhost]

TASK [docker : swap is off] ******************************************************************************
changed: [myhost]

TASK [docker : mkdir /etc/docker] ************************************************************************
changed: [myhost]

TASK [docker : docker etc is file] ***********************************************************************
changed: [myhost]

TASK [docker : restart docker] ***************************************************************************
changed: [myhost]

RUNNING HANDLER [docker : yum-makecache] *****************************************************************
 [WARNING]: Consider using yum module rather than running yum
changed: [myhost]

RUNNING HANDLER [docker : restart docker] ****************************************************************
changed: [myhost]

PLAY RECAP ***********************************************************************************************
myhost                     : ok=11   changed=9    unreachable=0    failed=0
验证
[root@server240 aliyun-cento79]# ansible -m shell -a 'docker version' -i hosts myhost
myhost | SUCCESS | rc=0 >>
Client: Docker Engine - Community
 Version:           20.10.15
 API version:       1.41
 Go version:        go1.17.9
 Git commit:        fd82621
 Built:             Thu May  5 13:16:58 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.15
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.9
  Git commit:       4433bf6
  Built:            Thu May  5 13:15:18 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

0

评论区