其他分享
首页 > 其他分享> > a10.ansible 生产实战案例 --docker基于二进制 playbook

a10.ansible 生产实战案例 --docker基于二进制 playbook

作者:互联网

docker基于二进制 playbook

[root@ansible-server ansible]# mkdir -p playbook/docker-binary
[root@ansible-server ansible]# cd playbook/docker-binary/

[root@ansible-server docker-binary]# wget https://mirrors.cloud.tencent.com/docker-ce/linux/static/stable/x86_64/docker-20.10.9.tgz -P files/

[root@ansible-server docker-binary]# vim vars.yml
DOCKER_VERSION: 20.10.9

[root@ansible-server docker-binary]# vim files/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H unix://var/run/docker.sock
ExecReload=/bin/kill -s HUP \$MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

[root@ansible-server docker-binary]# vim files/daemon.json
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}

[root@ansible-server docker-binary]# vim install_docker.yml
---
- hosts: all
  vars_files:
    vars.yml

  tasks:
    - name: unarchive  docker package
      unarchive:
        src: "docker-{{ DOCKER_VERSION }}.tgz"
        dest: /usr/local/src
    - name: move docker files
      shell:
        cmd: mv /usr/local/src/docker/* /usr/bin/
    - name: copy docker.service file
      copy:
        src: docker.service
        dest: /lib/systemd/system/docker.service
    - name: mkdir /etc/docker
      file:
        path: /etc/docker
        state: directory
    - name: set mirror_accelerator
      copy:
        src: daemon.json
        dest: /etc/docker/
    - name: set docker alias
      lineinfile:
        path: ~/.bashrc
        line: "{{ item }}"
      loop:
        - "alias rmi=\"docker images -qa|xargs docker rmi -f\""
        - "alias rmc=\"docker ps -qa|xargs docker rm -f\""
    - name: start docker
      systemd:
        name: docker
        state: started
        enabled: yes
        daemon_reload: yes
    - name: set WARNING No swap limit support
      replace:
        path: /etc/default/grub
        regexp: '^(GRUB_CMDLINE_LINUX=.*)\"$'
        replace: '\1 swapaccount=1"'
      when:
        - ansible_distribution=="Ubuntu"
    - name: update-grub
      shell:
        cmd: update-grub
      when:
        - ansible_distribution=="Ubuntu"
    - name: reboot Ubuntu system
      reboot:
      when:
        - ansible_distribution=="Ubuntu"

[root@ansible-server docker-binary]# cd../../
[root@ansible-server ansible]# ansible-playbook playbook/docker-binary/install_docker.yml 

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.31.0.103]
ok: [172.31.0.104]
ok: [172.31.0.101]
ok: [172.31.0.102]
ok: [172.31.0.105]

TASK [unarchive  docker package] **************************************************************************************************************
changed: [172.31.0.102]
changed: [172.31.0.101]
changed: [172.31.0.104]
changed: [172.31.0.103]
changed: [172.31.0.105]

TASK [move docker files] **********************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.101]
changed: [172.31.0.102]

TASK [copy docker.service file] ***************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]

TASK [mkdir /etc/docker] **********************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]

TASK [set mirror_accelerator] *****************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]
changed: [172.31.0.102]
changed: [172.31.0.101]

TASK [set docker alias] ***********************************************************************************************************************
changed: [172.31.0.103] => (item=alias rmi="docker images -qa|xargs docker rmi -f")
changed: [172.31.0.104] => (item=alias rmi="docker images -qa|xargs docker rmi -f")
changed: [172.31.0.105] => (item=alias rmi="docker images -qa|xargs docker rmi -f")
changed: [172.31.0.102] => (item=alias rmi="docker images -qa|xargs docker rmi -f")
changed: [172.31.0.101] => (item=alias rmi="docker images -qa|xargs docker rmi -f")
changed: [172.31.0.103] => (item=alias rmc="docker ps -qa|xargs docker rm -f")
changed: [172.31.0.104] => (item=alias rmc="docker ps -qa|xargs docker rm -f")
changed: [172.31.0.105] => (item=alias rmc="docker ps -qa|xargs docker rm -f")
changed: [172.31.0.101] => (item=alias rmc="docker ps -qa|xargs docker rm -f")
changed: [172.31.0.102] => (item=alias rmc="docker ps -qa|xargs docker rm -f")

TASK [start docker] ***************************************************************************************************************************
changed: [172.31.0.103]
changed: [172.31.0.101]
changed: [172.31.0.102]
changed: [172.31.0.104]
changed: [172.31.0.105]

TASK [set WARNING No swap limit support] ******************************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]

TASK [update-grub] ****************************************************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]

TASK [reboot Ubuntu system] *******************************************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.104]
changed: [172.31.0.105]

PLAY RECAP ************************************************************************************************************************************
172.31.0.101               : ok=8    changed=7    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
172.31.0.102               : ok=8    changed=7    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
172.31.0.103               : ok=8    changed=7    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
172.31.0.104               : ok=11   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.31.0.105               : ok=11   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0    

[root@rocky8-client ~]# docker --version
Docker version 20.10.9, build c2ea9bc
[root@centos8-client ~]# docker --version
Docker version 20.10.9, build c2ea9bc
[root@centos7-client ~]# docker --version
Docker version 20.10.9, build c2ea9bc
root@ubuntu1804-client:~# docker --version
Docker version 20.10.9, build c2ea9bc
root@ubuntu2004-client:~# docker --version
Docker version 20.10.9, build c2ea9bc

[root@rocky8-client ~]# cat /etc/docker/daemon.json 
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}
[root@centos8-client ~]# cat /etc/docker/daemon.json 
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}
[root@centos7-client ~]# cat /etc/docker/daemon.json 
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}
root@ubuntu1804-client:~# cat /etc/docker/daemon.json 
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}
root@ubuntu2004-client:~# cat /etc/docker/daemon.json 
{
    "registry-mirrors": [
        "https://hzw5xiv7.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com",
        "https://dockerhub.azk8s.cn",
        "https://reg-mirror.qiniu.com",
        "https://hub-mirror.c.163.com",
        "https://mirror.ccs.tencentyun.com"
    ]
}

[root@rocky8-client ~]# grep -E "alias rm(i|c)" .bashrc
alias rmi="docker images -qa|xargs docker rmi -f"
alias rmc="docker ps -qa|xargs docker rm -f"
[root@centos8-client ~]# grep -E "alias rm(i|c)" .bashrc
alias rmi="docker images -qa|xargs docker rmi -f"
alias rmc="docker ps -qa|xargs docker rm -f"
[root@centos7-client ~]# grep -E "alias rm(i|c)" .bashrc
alias rmi="docker images -qa|xargs docker rmi -f"
alias rmc="docker ps -qa|xargs docker rm -f"
root@ubuntu1804-client:~# grep -E "alias rm(i|c)" .bashrc
alias rmi="docker images -qa|xargs docker rmi -f"
alias rmc="docker ps -qa|xargs docker rm -f"
root@ubuntu2004-client:~# grep -E "alias rm(i|c)" .bashrc
alias rmi="docker images -qa|xargs docker rmi -f"
alias rmc="docker ps -qa|xargs docker rm -f"

root@ubuntu1804-client:~# grep "^GRUB_CMDLINE_LINUX=" /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1"
root@ubuntu2004-client:~# grep "^GRUB_CMDLINE_LINUX=" /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1"

root@ubuntu1804-client:~# docker info
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.9
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
 runc version: v1.0.2-0-g52b36a2d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 4.15.0-156-generic
 Operating System: Ubuntu 18.04.6 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.924GiB
 Name: ubuntu1804-client.example.local
 ID: 2WIK:3ZBS:VG6P:E3PY:AAE6:U73I:OXGM:ORCI:3D7H:46YX:2C2O:BQ45
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://hzw5xiv7.mirror.aliyuncs.com/
  https://docker.mirrors.ustc.edu.cn/
  http://f1361db2.m.daocloud.io/
  https://registry.docker-cn.com/
  https://dockerhub.azk8s.cn/
  https://reg-mirror.qiniu.com/
  https://hub-mirror.c.163.com/
  https://mirror.ccs.tencentyun.com/
 Live Restore Enabled: false
 Product License: Community Engine
 
root@ubuntu2004-client:~# docker info
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.9
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
 runc version: v1.0.2-0-g52b36a2d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-89-generic
 Operating System: Ubuntu 20.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.913GiB
 Name: ubuntu2004-client.example.local
 ID: SAIP:6OFU:OW3H:CQRZ:M665:3OIL:3RWF:35PG:YJ63:7FKS:T3CU:VGYF
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://hzw5xiv7.mirror.aliyuncs.com/
  https://docker.mirrors.ustc.edu.cn/
  http://f1361db2.m.daocloud.io/
  https://registry.docker-cn.com/
  https://dockerhub.azk8s.cn/
  https://reg-mirror.qiniu.com/
  https://hub-mirror.c.163.com/
  https://mirror.ccs.tencentyun.com/
 Live Restore Enabled: false
 Product License: Community Engine

标签:a10,--,changed,ansible,https,mirror,docker,com,172.31
来源: https://blog.csdn.net/qq_25599925/article/details/122175740