这篇文章将为大家详细讲解有关如何通过puppet管理远程docker容器并配置puppet和实现变更,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
前提准备:
1.master和docker节点上分别安装好puppet master和puppet agent;
2.docker节点上安装好docker1.2.0、nsenter(被脚本用于连接容器),并pull一个镜像:training/webapp
master上的准备工作:
创建docker模块:
mkdir -p /etc/puppet/modules/docker/{manifests,files,templates}
vi /etc/puppet/modules/docker/manifests/init.pp
#编写docker类
class docker {
exec { "dockerlaunch" :
command => "/usr/bin/docker run -d -p 1000:5000 --name webbase training/webapp python app.py && /usr/bin/docker run -d -p 2000:5000 --name web1 --link webbase:webbase training/webapp python app.py",
}
exec { "dockerlogs" :
command => "/bin/mkdir -p /var/log/dockerlaunch && /usr/bin/docker inspect webbase >> /var/log/dockerlaunch/webbase.log && /usr/bin/docker inspect web1 >> /var/log/dockerlaunch/web1.log",
}
file { "/root/status.log" :
ensure => file,
mode => '740',
content => "docker container is running:webbase and web1 please use broswer access the ip address of docker.hzg.com and the 1000 or the 2000 port.You can use the control.sh script help you to manage the container",
}
file { "/root/control.sh" :
ensure => file,
mode => '1777',
source => "puppet:///modules/docker/control.sh",
}
notify { "Docker container is running on node $fqdn !": }
}
编写管理脚本,并放置到/etc/puppet/modules/docker/files目录中:
vi control.sh
#脚本如下
#!/bin/bash
#used for access the specific container
#written by Hochikong
while true
do
{
read -p "What you want to do?try input 'help' to get some tips(please input the words in ''): " what
if [ $what = 'help' ];
then
echo "################################################################################################################################";
echo " The helping information about this script ";
echo "################################################################################################################################";
echo "COMMAND INFO ";
echo "################################################################################################################################";
echo "'status' get the info about the running containers. ";
echo "'access' access the specific contianer. ";
echo "'manage' manage the contianer,such as 'start','stop' and 'delete'. ";
echo "'exit' exit this script. ";
echo "'statusa' show the infomation about all containers. ";
echo "'statusl' show the latest infomation about container. ";
echo "################################################################################################################################";
echo "MAINCOMMAND SUBCOMMAND INFO ";
echo "################################################################################################################################";
echo "'manage' 'start' launch a exist contianer ";
echo "'manage' 'stop' stop a running container ";
echo "'manage' 'delete' detele a not-running container ";
echo "'manage' 'status' get the info about the running containers ";
echo "'manage' 'statusa' show the infomation about all containers. ";
echo "'manage' 'statusl' show the latest infomation about container. ";
echo "################################################################################################################################";
fi
if [ $what = 'status' ];
then
echo "The running containers are:\n";
docker ps;
fi
if [ $what = 'statusa' ];
then
echo "All containers's status:\n";
docker ps -a;
fi
if [ $what = 'statusl' ];
then
echo "The latest infomation about containers:\n";
docker ps -l;
fi
if [ $what = 'access' ];
then
read -p "Please input the specific container's name:" name;
CPID=$(docker inspect --format '{{.State.Pid}}' $name);
nsenter --target $CPID --mount --uts --ipc --net --pid;
fi
if
[ $what = 'manage' ];
then
while true
do
{
read -p "Please input the container name which you want to manage,or 'exit',or 'help'?: " name2;
if [ $name2 = 'help' ];
then
echo "#############################################################################################################";
echo " SUBCOMMAND INFO ";
echo "#############################################################################################################";
echo " 'start' launch a exist contianer ";
echo " 'stop' stop a running container ";
echo " 'delete' detele a not-running container ";
echo " 'status' get the info about the running containers ";
echo " 'statusa' show the infomation about all containers. ";
echo " 'statusl' show the latest infomation about container. ";
echo "#############################################################################################################";
break;
fi
if [ $name2 = 'status' ];
then
echo "Running container:";
docker ps;continue;
fi
if [ $name2 = 'exit' ];
then
echo "Exiting";
break;
fi
if [ $name2 = 'statusa' ];
then
echo "All infomation about containers:\n";
docker ps -a;continue;
elif [ $name2 = 'statusl' ];
then
echo "The latest infomation about containers:\n";
docker ps -l;continue;
fi
read -p "Do you want to 'start' or 'stop' or 'delete' your container?: " what2;
if [ $what2 = 'start' ];
then
echo "Notice:Please make sure this container is not running";
docker start $name2;continue
elif [ $what2 = 'stop' ];
then
echo "Notice:container is stopping";
docker stop $name2;continue;
elif [ $what2 = 'delete' ];
then
echo "Notice:You cannot delete a running container,if the container is running,please stop it first!";
docker rm $name2;continue;
else
echo "Error:Command Error,no such command!";continue;
fi
}
done
fi
if [ $what = 'exit' ];
then
exit;
fi
}
done
编辑/etc/puppet/manifests/nodes/docker.hzg.com.pp,加载docker类:
node 'docker.hzg.com' {
include docker
}
编辑/etc/puppet/manifests/site.pp,加载docker节点的配置,增加这么一行:
import "nodes/docker.hzg.com.pp"
编辑/etc/puppet/fileserver.conf,授权docker对modules和files的访问,添加内容:
[files]
path /etc/puppet/files
allow docker.hzg.com
# allow *.example.com
# deny *.evil.example.com
# allow 192.168.0.0/24
[files]
path /etc/puppet/modules
allow *.hzg.com
编辑/etc/puppet/puppet.conf,在[main]那一段增加以下内容(可选):
modulepath = /etc/puppet/modules
PS:因为我使用puppet kick实现配置,要为agent做点配置工作:
agent上:
编辑puppet.conf,在[agent]那段增加以下内容(可选):
listen = true
实现配置:
master上:
root@workgroup:~# puppet kick docker.hzg.com
Warning: Puppet kick is deprecated. See http://links.puppetlabs.com/puppet-kick-deprecation
Warning: Failed to load ruby LDAP library. LDAP functionality will not be available
Triggering docker.hzg.com
Getting status
status is success
docker.hzg.com finished with exit code 0
Finished
因为我没有配置LDAP,所以有些警告内容。
检查docker节点上的信息:
root@docker:~# ls
BACKUPDockerfile control.sh Dockerfile hzg.sh init.pp status.log test2.sh test.py util-linux-2.24
root@docker:~# cd /var/log/dockerlaunch/
root@docker:/var/log/dockerlaunch# ls
web1.log webbase.log
root@docker:/var/log/dockerlaunch# cd ~
root@docker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
050ebb07cf25 training/webapp:latest "python app.py" About a minute ago Up About a minute 0.0.0.0:2000->5000/tcp web1
0ef5d56e4c89 training/webapp:latest "python app.py" About a minute ago Up About a minute 0.0.0.0:1000->5000/tcp web1/webbase,webbase
可以看到相应的东西都创建了。
关于“如何通过puppet管理远程docker容器并配置puppet和实现变更”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。