鸿 网 互 联 www.68idc.cn

快速上手10.0 Ansible Docker

来源:互联网 作者:佚名 时间:2022-07-19 11:14
概览 Ansible内置一些专用的Docker模块,帮助我们更好的管理容器相关环境 环境信息 Ansible?2.9.16CentOS?Linux?release?7.9.2009?(Core)Docker?version?18.09.9Python?2.7.5Docker?python?module?docker-4.4.1 依赖

概览

Ansible内置一些专用的Docker模块,帮助我们更好的管理容器相关环境

环境信息

Ansible?2.9.16 CentOS?Linux?release?7.9.2009?(Core) Docker?version?18.09.9 Python?2.7.5 Docker?python?module?docker-4.4.1

依赖安装

#1?安装docker https://blog.csdn.net/xys2015/article/details/109370082 #2?开启EPEL?repository https://blog.csdn.net/xys2015/article/details/109378741 #3?安装pip yum?install?python-pip #4?安装docker?python?module pip?install?docker

这些依赖当然也可以通过Ansible来安装

快速上手

话不多说,环境配置好后,我们先看一个示例

目标

  • 在本机基于Dockerfile构建镜像
  • 启动构建好的镜像

目录结构

[root@192_168_1_237?ysansible]#?tree?docker/ docker/ ├──?main.yml ├──?mybusybox │???└──?Dockerfile └──?README.md #完整内容参见?https://gitee.com/as4k/ysansible/tree/master/docker/main1.yml

关于Docker本身的一些知识,本文不多做介绍

重点提要

????-?name:?Ensure?Docker?image?is?built?from?the?test?Dockerfile. ??????docker_image:?#相当于docker?build相关功能 ????????name:?mybusybox:v3?#这里是构建后的镜像名称,如果需要重新构建镜像,这里的版本号需要更改 ????????source:?build ????????build: ??????????pull:?yes??#自动拉取构建需要的镜像,我们这里即自动拉取busybox镜像 ??????????path:?mybusybox?#在?./mybusybox?找?Dockerfile?文件 ????????state:?present ????-?name:?Ensure?the?test?container?is?running. ??????docker_container:?#相当于docker?run相关功能 ????????image:?mybusybox:v3?#运行这个镜像 ????????name:?mybusybox?????#镜像的名称 ????????state:?started

执行结果验证

#?docker?ps?-a CONTAINER?ID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES f80619da371e????????mybusybox:v3????????"tail?-F?/tmp/tmp.txt"???36?minutes?ago??????Up?36?minutes???????????????????????????mybusybox #?docker?exec?mybusybox?hostname f80619da371e

Docker命令与Ansible状态对应关系

Docker CommandAnsible state value备注docker runstarted
docker runpresent保持原样,如果容器已经是停止状态,将不会启动docker stopstopped
docker rmabsent

在远程机器进行build

1?远程机器依赖安装

参考上文,当然也可以使用Ansible安装

2?改动playbook

????-?name:?copy?Dockerfile?to?target?machine ??????copy:? ????????src:?mybusybox ????????dest:?/tmp ????-?name:?Ensure?Docker?image?is?built?from?the?test?Dockerfile. ??????docker_image: ????????name:?mybusybox:v3 ????????source:?build ????????build: ??????????pull:?yes ??????????path:?/tmp/mybusybox ????????state:?present ????-?name:?Ensure?the?test?container?is?running. ??????docker_container: ????????image:?mybusybox:v3 ????????name:?mybusybox ????????state:?started

完整内容参考?https://gitee.com/as4k/ysansible/blob/master/docker/main2.yml

3?执行

ansible-playbook?main2.yml?--limit?192.168.31.101

docker_image 模块

如果我们熟悉docker image xxx相关操作,docker_image基本上跟其对应,可以使用ansible-doc docker_image查看文档,或者查看其官方文档?https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#ansible-collections-community-docker-docker-image-module

下面列举一些示例

#1?下载镜像 -?name:?Pull?an?image ??community.docker.docker_image: ????name:?pacur/centos-7 ????source:?pull #2?给镜像打标签 -?name:?Add?tag?latest?to?image ??community.docker.docker_image: ????name:?myimage:7.1.2???#原镜像名称 ????repository:?myimage:latest?#新打的镜像名称 ????force_tag:?yes?#如果标签重复,强制覆盖 ????source:?local #3?删除一个镜像 -?name:?Remove?image ??community.docker.docker_image: ????state:?absent ????name:?registry.ansible.com/chouseknecht/sinatra ????tag:?v1 #4?导出镜像为tar包 -?name:?Archive?image ??community.docker.docker_image: ????name:?registry.ansible.com/chouseknecht/sinatra ????tag:?v1 ????archive_path:?my_sinatra.tar ????source:?local

docker_container 模块

同类,这个模块实现docker container xxx相关功能,在线文档?https://docs.ansible.com/ansible/latest/collections/community/docker/docker_container_module.html#ansible-collections-community-docker-docker-container-module

一些参考示例如下:

#1?启动一个容器 -?name:?Ensure?the?test?container?is?running. ??docker_container: ????image:?mybusybox:v3 ????pull:?yes??#无论镜像是否存在,都进行拉取,这种情况可以规避镜像被更新,但是标签没变的情况 ????name:?mybusybox ????state:?started #2?启动镜像并挂载本地存储 -?name:?Finer?container?restart/update?control ??community.docker.docker_container: ????name:?test ????image:?ubuntu:18.04 ????env: ??????arg1:?"true" ??????arg2:?"whatever" ????volumes: ??????-?/tmp:/tmp #3?启动镜像并附带健康检查 -?name:?Start?container?with?healthstatus ??community.docker.docker_container: ????name:?nginx-proxy ????image:?nginx:1.13 ????state:?started ????healthcheck: ??????test:?["CMD",?"curl",?"--fail",?"http://nginx.host.com"] ??????interval:?1m30s ??????timeout:?10s ??????retries:?3 ??????start_period:?30s

报错处理

#1?没有安装docker?python?module ================== fatal:?[localhost]:?FAILED!?=>?{"changed":?false,?"msg":?"Failed?to?import?the?required?Python?library?(Docker?SDK?for?Python:?docker?(Python?>=?2.7)?or?docker-py?(Python?2.6))?on?192_168_1_237's?Python?/usr/bin/python2.?Please?read?module?documentation?and?install?in?the?appropriate?location.?If?the?required?library?is?installed,?but?Ansible?is?using?the?wrong?Python?interpreter,?please?consult?the?documentation?on?ansible_python_interpreter,?for?example?via?`pip?install?docker`?or?`pip?install?docker-py`?(Python?2.6).?The?error?was:?No?module?named?requests.exceptions"} #2?没有安装docker或者docker未启动 ================== fatal:?[localhost]:?FAILED!?=>?{"changed":?false,?"msg":?"Error?connecting:?Error?while?fetching?server?API?version:?('Connection?aborted.',?error(111,?'Connection?refused'))"}

参考资料

https://docs.ansible.com/ansible/latest/scenario_guides/guide_docker.html https://docs.ansible.com/ansible/latest/collections/index_module.html https://docs.ansible.com/ansible/latest/collections/community/docker/docker_image_module.html#ansible-collections-community-docker-docker-image-module

全部支持的docker模块截图如下

快速上手10.0 Ansible Docker_Ansible



上一篇:基于Kubernetes部署Jenkins动态构建环境
下一篇:没有了
网友评论
<