共計 6708 個字符,預計需要花費 17 分鐘才能閱讀完成。
這篇文章將為大家詳細講解有關如何通過 puppet 管理遠程 docker 容器并配置 puppet 和實現變更,丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
前提準備:
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
read -p What you want to do?try input help to get some tips(please input the words in ): what
if [ $what = help ];
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 ];
echo The running containers are:\n
docker ps;
if [ $what = statusa ];
then
echo All containers s status:\n
docker ps -a;
if [ $what = statusl ];
echo The latest infomation about containers:\n
docker ps -l;
if [ $what = access ];
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;
if
[ $what = manage ];
while true
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;
if [ $name2 = status ];
then
echo Running container:
docker ps;continue;
if [ $name2 = exit ];
then
echo Exiting
break;
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;
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
編輯 /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 和實現變更”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。