基於docker創建ansible以及管理容器節點

基於docker創建ansible以及管理容器節點


場景:在學習條件有限情況下,如果通過一臺VM來完成docker和ansible的學習

解決:先創建自定義鏡像-->構建多個ansible容器。

當然此法適用於其他類似場景。

VM環境:

  • OS:centos7
    Docker version 1.12.3, build 6b644ec
    docker-compose version 1.8.1, build 878cff1

 

關鍵點:

  • Dockerfile 編寫優化

  • Docker-compose.yml 編寫

  • ansible-ssh 免密鑰登錄

  • 容器間22端口互通


wKiom1hFxObAJLKAAACCFILFBfc468.png-wh_50


y準備工作

創建文件夾

mkdir -p /root/docker/ansible-demo && /root/docker/ansible-demo/volume2 && cd ~/docker/ansible-demo


創建dockerfile、docker-compose

Dockerfile 文件

# Set the base p_w_picpath to centos
FROM centos:latest
MAINTAINER osbing [email protected]
#mount volume
VOLUME ["/root/docker/ansible-demo/volume2"]
################## BEGIN INSTALLATION ######################
#install EPEL
RUN rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
&& rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 \
&& yum install -y yum-priorities
# Install
#RUN yum clean all
RUN yum install -y sudo
RUN yum install -y \
net-tools \
openssh-clients \
openssh-server \
ansible \
vim
################## END INSTALLATION ######################
# 將sshd的UsePAM參數設置成no
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
# 修改root用戶密碼
RUN echo "root:benny"|chpasswd
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# 啓動sshd服務並且暴露22端口
RUN mkdir /var/run/sshd
EXPOSE 22
ENTRYPOINT ["/usr/sbin/sshd","-D"]
# no cache創建鏡像
#ddocker build --no-cache  -t osbing/centos_sshd:0.2 .
# 創建容器。特權模式--privileged=true
docker run -d -p 9021:22 --privileged=true --name ansible-controller1 osbing/centos_sshd:0.2
docker run -d -p 9021:22 --privileged=true --name ansible-controller osbing/centos_sshd:0.2
docker run -d -p 9022:22 --privileged=true --name ansible-node2 osbing/centos_sshd:0.2
docker run -d -p 9023:22 --privileged=true --name ansible-node3 osbing/centos_sshd:0.2

 

或者使用Docker-compose文件創建容器

ansible-controller:
   p_w_picpath: osbing/centos_sshd:0.2
   ports:
     - "9021:22"
   environment:
     HOSTNAME:ansible-controller
ansible-node2:
   p_w_picpath: osbing/centos_sshd:0.2
   ports:
     - "9022:22"
   environment:
     HOSTNAME:ansible-node2
 
ansible-node3:
   p_w_picpath: osbing/centos_sshd:0.2
   ports:
     - "9023:22"
   environment:
      HOSTNAME:ansible-node3


 

ans ible -node2  ansible-node2  ansibIe-node2  ans ible -node3  ansible-node3  ans ible -node3  1  ansible-controller  ansible-control 1er  ansible-controller

 

ssh連接到ansible-controller進行配置和管理節點

# ssh連接到ansible-controller 進行修改
ssh [email protected] -p 9021
vim /etc/ansible/hosts

 

[test-servers]  172.17.6.12  172.17.0.13  172.17.e.14

# 生成公鑰
ssh-keygen

wKiom1hFxVLhz33BAAF8kZr8-9Y643.png-wh_50

 

拷貝公鑰到被管理節點的主機上

# ssh-copy-id 拷貝公鑰到被管理節點的主機上
ssh-copy-id -i [email protected]
ssh-copy-id -i [email protected]
ssh-copy-id -i [email protected]

 

[root@c9db9b7e94bO ssh-copy-id -i root@172.17.0.12  The authenticity of host '172.17.0.12 (172.17.0.12)' can't be established.  RSA key fingerprint is  Are you sure you want to continue connecting (yes/no)? yes  /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed  if you are prompted now it is to install the new keys  root@172.17.0.12's password:  Number of key(s) added: 1  Now try logging into the machine,  with:  "ssh 'root@172.17.O.12'  and check to make sure that only  the key(s) you wanted were added.  [root@c9db9b7e94b0 ssh-copy-id -i root@172.17.0.13  The authenticity of host '172.17.0.13 (172.17.0.13)' can't be established.  RSA key fingerprint is  Are you sure you want to continue connecting (yes/no)? yes  /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed  if you are prompted now it is to install the new keys  root@172.17.O.13's password:  Number of key(s) added: 1  Now try logging into the machine,  with:  "ssh 'root@172.17.O.13"  and check to make sure that only  the key(s) you wanted were added.  [root@c9db9b7e94b0 ssh-copy-id -i root@172.17.0.14  /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed  if you are prompted now it is to install the new keys  root@172.17.O.14's password:  Number of key(s) added: 1  Now try logging into the machine,  and check to make sure that only  with:  "ssh 'root@172.17.O.14"  the key(s) you wanted were added.

 

嘗試在Ansible服務端運行命令


例子1:檢查Ansible節點的運行時間(uptime

#ping測試
ansible -m ping "test-servers"

 

[root@c9db9b7e94b8 ansible  172.17.a.14 SUCCESS  'Ichangedll: false,  'ping” :  'pongll  172.17.a.12 SUCCESS  'Ichangedll: false,  'ping” :  'pongll  172.17.a.13 SUCCESS  'Ichangedll: false,  'ping” :  'pongll  -m png  " test-servers "

 

#獲取系統運行時間
ansible 'test-servers' -m command -a "uptime"

 

[root@c9db9b7e94b0  ansible 'test-servers  -m command  -a  uptime "  172.  172.  172.  17.@.14  up  17.@.12  up  17.@.13  up  SUCCESS  2:46,  SUCCESS  2:46,  SUCCESS  2:46,  -Load average:  -Load average:  -Load average:

 

例子2:檢查節點的內核版本

#獲取內核版本
ansible 'test-ser

wKioL1hFyLGQBVoFAABiykyvU8M162.png-wh_50

例子3:給節點增加用戶

#增加用戶
ansible "test-servers" -m command -a "useradd mark"
ansible "test-servers" -m command -a "grep mark /etc/passwd"

wKiom1hFyJ_CnccSAACnnAwSeKg689.png-wh_50

例子4:重定向輸出到文件中

[root@c9db9b7e94b0 ansible]# ansible "test-servers" -m command -a "df -Th" > /tmp/command-output.txt
[root@c9db9b7e94b0 ansible]# cat /tmp/command-output.txt

wKioL1hFyG-DQWMFAAEvkaUEhEg906.png-wh_50

END

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章