這篇文章將為大家詳細(xì)講解有關(guān)如何通過puppet管理遠(yuǎn)程docker容器并配置puppet和實(shí)現(xiàn)變更,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
公司主營(yíng)業(yè)務(wù):做網(wǎng)站、成都做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出武侯免費(fèi)做網(wǎng)站回饋大家。
前提準(zhǔn)備:
1.master和docker節(jié)點(diǎn)上分別安裝好puppet master和puppet agent;
2.docker節(jié)點(diǎn)上安裝好docker1.2.0、nsenter(被腳本用于連接容器),并pull一個(gè)鏡像:training/webapp
master上的準(zhǔn)備工作:
創(chuàng)建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節(jié)點(diǎn)的配置,增加這么一行:
import "nodes/docker.hzg.com.pp"
編輯/etc/puppet/fileserver.conf,授權(quán)docker對(duì)modules和files的訪問,添加內(nèi)容:
[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]那一段增加以下內(nèi)容(可選):
modulepath = /etc/puppet/modules
PS:因?yàn)槲沂褂胮uppet kick實(shí)現(xiàn)配置,要為agent做點(diǎn)配置工作:
agent上:
編輯puppet.conf,在[agent]那段增加以下內(nèi)容(可選):
listen = true
實(shí)現(xiàn)配置:
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
因?yàn)槲覜]有配置LDAP,所以有些警告內(nèi)容。
檢查docker節(jié)點(diǎn)上的信息:
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
可以看到相應(yīng)的東西都創(chuàng)建了。
關(guān)于“如何通過puppet管理遠(yuǎn)程docker容器并配置puppet和實(shí)現(xiàn)變更”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。