zl程序教程

您现在的位置是:首页 >  工具

当前栏目

a8.ansible 生产实战案例 -- docker基于镜像仓库 playbook

Docker案例镜像 基于 -- 实战 仓库 生产
2023-09-14 09:15:22 时间

docker基于镜像仓库 playbook

源码下载地址:https://github.com/raymond999999/ansible

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

[root@ansible-server docker]# 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]# vim vars.yml
DOCKER_VERSION: 20.10.11
DOCKER_MIRRORS: mirrors.cloud.tencent.com

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

  tasks:
    - name: add CentOS or Rocky docker mirror warehouse
      yum_repository:
        name: docker-ce
        description: docker-ce
        file: docker-ce
        baseurl: https://{{ DOCKER_MIRRORS }}/docker-ce/linux/centos/{{ ansible_distribution_major_version }}/x86_64/stable/
        gpgkey: https://{{ DOCKER_MIRRORS }}/docker-ce/linux/centos/gpg 
        gpgcheck: yes
      when:
        - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
    - name: install CentOS or Rocky docker
      yum:
        name: docker-ce-{{ DOCKER_VERSION }},docker-ce-cli-{{ DOCKER_VERSION }}
      when:
        - (ansible_distribution=="CentOS" or ansible_distribution=="Rocky")
    - name: delete lock files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /var/lib/dpkg/lock
        - /var/lib/apt/lists/lock
        - /var/cache/apt/archives/lock
      when:
        - ansible_distribution=="Ubuntu"
    - name: apt update
      apt:
        update_cache: yes 
        force: yes 
      when:
        - ansible_distribution=="Ubuntu"
    - name: install Ubuntu docker depend on the package
      apt:
        name: apt-transport-https,ca-certificates,curl,software-properties-common
        force: yes 
      when:
        - ansible_distribution=="Ubuntu"
    - name: import Ubuntu docker key
      apt_key:
        url: https://{{ DOCKER_MIRRORS }}/docker-ce/linux/ubuntu/gpg
      when:
        - ansible_distribution=="Ubuntu"
    - name: import Ubuntu docker installation source
      apt_repository:
        repo: "deb [arch=amd64] https://{{ DOCKER_MIRRORS }}/docker-ce/linux/ubuntu {{ ansible_distribution_release }} stable"
        filename: docker-ce
      when:
        - ansible_distribution=="Ubuntu"
    - name: delete lock files
      file:
        path: "{{ item }}"
        state: absent
      loop:
        - /var/lib/dpkg/lock
        - /var/lib/apt/lists/lock
        - /var/cache/apt/archives/lock
      when:
        - ansible_distribution=="Ubuntu"
    - name: apt update
      apt:
        update_cache: yes 
        force: yes 
      when:
        - ansible_distribution=="Ubuntu"
    - name: install Ubuntu docker
      apt:
        name: docker-ce=5:{{ DOCKER_VERSION }}~3-0~ubuntu-{{ ansible_distribution_release }},docker-ce-cli=5:{{ DOCKER_VERSION }}~3-0~ubuntu-{{ ansible_distribution_release }}
        force: yes
      when:
        - ansible_distribution=="Ubuntu"
    - name: mkdir /etc/docker
      file:
        path: /etc/docker
        state: directory
    - name: set mirror_accelerator
      copy:
        src: daemon.json
        dest: /etc/docker/
    - name: start docker
      systemd:
        name: docker
        state: started
        enabled: yes
        daemon_reload: yes
    - 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: 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]# cd ../../
[root@ansible-server ansible]# ansible-playbook playbook/docker/install_docker.yml 

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

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

TASK [add CentOS or Rocky docker mirror warehouse] ********************************************************************************************
skipping: [172.31.0.104]
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
changed: [172.31.0.101]

TASK [install CentOS or Rocky docker] *********************************************************************************************************
skipping: [172.31.0.104]
skipping: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
changed: [172.31.0.101]

TASK [delete lock files] **********************************************************************************************************************
skipping: [172.31.0.101] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.101] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.102] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.103] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.101] => (item=/var/cache/apt/archives/lock) 
skipping: [172.31.0.103] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.103] => (item=/var/cache/apt/archives/lock) 
skipping: [172.31.0.102] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.102] => (item=/var/cache/apt/archives/lock) 
changed: [172.31.0.104] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.105] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.104] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.105] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.104] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.105] => (item=/var/cache/apt/archives/lock)

TASK [apt update] *****************************************************************************************************************************
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 [install Ubuntu docker depend on the package] ********************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]

TASK [import Ubuntu docker key] ***************************************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]

TASK [import Ubuntu docker installation source] ***********************************************************************************************
skipping: [172.31.0.101]
skipping: [172.31.0.102]
skipping: [172.31.0.103]
changed: [172.31.0.105]
changed: [172.31.0.104]

TASK [delete lock files] **********************************************************************************************************************
skipping: [172.31.0.101] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.101] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.101] => (item=/var/cache/apt/archives/lock) 
skipping: [172.31.0.102] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.102] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.102] => (item=/var/cache/apt/archives/lock) 
skipping: [172.31.0.103] => (item=/var/lib/dpkg/lock) 
skipping: [172.31.0.103] => (item=/var/lib/apt/lists/lock) 
skipping: [172.31.0.103] => (item=/var/cache/apt/archives/lock) 
changed: [172.31.0.104] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.105] => (item=/var/lib/dpkg/lock)
changed: [172.31.0.105] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.104] => (item=/var/lib/apt/lists/lock)
changed: [172.31.0.105] => (item=/var/cache/apt/archives/lock)
changed: [172.31.0.104] => (item=/var/cache/apt/archives/lock)

TASK [apt update] *****************************************************************************************************************************
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 [install Ubuntu docker] ******************************************************************************************************************
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 [mkdir /etc/docker] **********************************************************************************************************************
ok: [172.31.0.104]
ok: [172.31.0.105]
changed: [172.31.0.103]
changed: [172.31.0.102]
changed: [172.31.0.101]

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

TASK [start docker] ***************************************************************************************************************************
ok: [172.31.0.104]
ok: [172.31.0.105]
changed: [172.31.0.103]
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.105] => (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.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.105] => (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.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 [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.105]
changed: [172.31.0.104]

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

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=7    changed=6    unreachable=0    failed=0    skipped=11   rescued=0    ignored=0   
172.31.0.102               : ok=7    changed=6    unreachable=0    failed=0    skipped=11   rescued=0    ignored=0   
172.31.0.103               : ok=7    changed=6    unreachable=0    failed=0    skipped=11   rescued=0    ignored=0   
172.31.0.104               : ok=16   changed=13   unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
172.31.0.105               : ok=16   changed=13   unreachable=0    failed=0    skipped=2    rescued=0    ignored=0 

[root@rocky8-client ~]# cat /etc/yum.repos.d/docker-ce.repo 
[docker-ce]
baseurl = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck = 1
gpgkey = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/gpg
name = docker-ce
[root@centos8-client ~]# cat /etc/yum.repos.d/docker-ce.repo
[docker-ce]
baseurl = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck = 1
gpgkey = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/gpg
name = docker-ce
[root@centos7-client ~]# cat /etc/yum.repos.d/docker-ce.repo 
[docker-ce]
baseurl = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/7/x86_64/stable/
gpgcheck = 1
gpgkey = https://mirrors.cloud.tencent.com/docker-ce/linux/centos/gpg
name = docker-ce
root@ubuntu1804-client:~# cat /etc/apt/sources.list.d/docker-ce.list 
deb [arch=amd64] https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu bionic stable
root@ubuntu2004-client:~# cat /etc/apt/sources.list.d/docker-ce.list 
deb [arch=amd64] https://mirrors.cloud.tencent.com/docker-ce/linux/ubuntu focal stable

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

[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
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.12.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.11
 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: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 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: LPXG:AQQO:XDQH:VQDG:SF5U:DPWK:AYY3:FRML:2XD2:VMYC:LKKZ:6VJY
 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

root@ubuntu2004-client:~# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.12.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.11
 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: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 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: K6CS:PAV6:KGWR:MFLV:I57N:D3LW:CS2T:Z7HB:TGI6:3QRK:43WV:WTFQ
 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