1. 配置minion 首先,移除原来的测试minion [[email protected] bin]# salt-key -d test230 The following keys are going to be deleted: Accepted Keys: test230 Proceed? [N/y] y Key for minion test230 deleted. 新增minion: 【svr205-2】 [[email protected] ~]# yum install salt-minion -y [[email protected] ~]# vim /etc/salt/minion master: 10.0.200.21 id: svr205-2 [[email protected] ~]# service salt-minion start Starting salt-minion daemon: [ OK ] 【svr205-2, 3, 4, 5类似】同上 2. 配置master和minion的key [[email protected] bin]# salt-key -L Accepted Keys: Unaccepted Keys: svr205-2 svr205-3 svr205-4 svr205-5 Rejected Keys: [[email protected] bin]# salt-key -a svr205* The following keys are going to be accepted: Unaccepted Keys: svr205-2 svr205-3 svr205-4 svr205-5 Proceed? [n/Y] y Key for minion svr205-2 accepted. Key for minion svr205-3 accepted. [[email protected] bin]# salt-key -L Accepted Keys: svr205-2 svr205-3 svr205-4 svr205-5 Unaccepted Keys: Rejected Keys: 测试: [[email protected] salt]# salt ‘svr205-*‘ test.ping svr205-3: True svr205-2: True svr205-5: True svr205-4: True 3. 配置管理-同步vim配置 [[email protected] salt]# pwd /srv/salt [[email protected] salt]# cat edit/vim.sls vim-enhanced: pkg.installed: [] /root/.vimrc: file.managed: - source: salt://edit/conf/vimrc - mode: 644 - uesr: root - group: root [[email protected] salt]# ls edit/conf/ vimrc [[email protected] salt]# salt ‘svr205-*‘ state.sls edit.vim ########################################################################## 略 ########################################################################## 4. 远程执行-在4台主机上执行脚本lvs-realsvr,并加入开机启动。 启动脚本: [[email protected] salt]# cat lvs/real/start.sls /etc/rc.d/init.d/lvs-realsvr: file.managed: - source: salt://lvs/bin/lvs-realsvr.sh - mode: 755 lvs-realsvr: cmd.run: - require: - file: /etc/rc.d/init.d/lvs-realsvr - name: service lvs-realsvr start && chkconfig lvs-realsvr on 关闭脚本: [[email protected] salt]# cat lvs/real/stop.sls /etc/rc.d/init.d/lvs-realsvr: file.managed: - source: salt://lvs/bin/lvs-realsvr.sh - mode: 755 lvs-realsvr: cmd.run: - require: - file: /etc/rc.d/init.d/lvs-realsvr - name: service lvs-realsvr stop && chkconfig lvs-realsvr off 控制脚本: [[email protected] salt]# cat lvs/bin/lvs-realsvr.sh #!/bin/bash # # 2015/2/28 # lvs real server # # chkconfig: - 85 15 # description: control vip on lvs realserver # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 lockfile="/var/lock/subsys/lvs-real" s_vip=‘10.0.205.100‘ start() { ifconfig lo:1 ${s_vip} netmask 255.255.255.255 broadcast ${s_vip} route add -host ${s_vip} dev lo:1 echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce ifconfig lo:1 down route del -host ${s_vip} retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } status() { ip a |grep inet |grep -v inet6 } case $1 in start) start status ;; stop) stop status ;; status) status ;; *) echo $"Usage: $0 {start|stop|status}" exit 2 esac 执行: [[email protected] salt]# salt ‘svr205-*‘ state.sls lvs.real.start ########################################################################## 略 ########################################################################## 查看minion的状态: 【svr205-2】 [[email protected] ~]# ll /etc/init.d/lvs-realsvr -rwxr-xr-x 1 root root 1252 Feb 28 05:31 /etc/init.d/lvs-realsvr [[email protected] ~]# ip a |grep inet |grep -v inet6 inet 127.0.0.1/8 scope host lo inet 10.0.205.100/32 brd 10.0.205.100 scope global lo:1 inet 10.0.205.2/8 brd 10.255.255.255 scope global eth0 [[email protected] salt]# salt ‘svr205-*‘ state.sls lvs.real.stop ########################################################################## 略 ########################################################################## 查看minion的状态: 【svr205-2】 [[email protected] ~]# ip a |grep inet |grep -v inet6 inet 127.0.0.1/8 scope host lo inet 10.0.205.2/8 brd 10.255.255.255 scope global eth0 5. 配置管理-安装nginx服务,并加入开机启动。 [[email protected] salt]# cat nginx/init.sls nginx: pkg.installed: [] service.running: - require: - pkg: nginx cmd.run: - require: - pkg: nginx - name: chkconfig nginx on [[email protected] salt]# salt ‘svr205-*‘ state.sls ########################################################################## 略 ########################################################################## 深入一步,使用模版部署自定义的index.html,更新nginx首页,显示当前web主机的hostname和ip信息。 [[email protected] salt]# cat nginx/init.sls nginx: pkg.installed: [] service.running: - watch: - file: /usr/share/nginx/html/index.html - require: - pkg: nginx cmd.run: - require: - pkg: nginx - name: chkconfig nginx on /usr/share/nginx/html/index.html: file.managed: - source: salt://nginx/conf/index.html - template: jinja [[email protected] salt]# cat nginx/conf/index.html <p> HOST: {{ grains[‘host‘] }} <br /> <br /><br /> {% for k,v in grains.get(‘ip4_interfaces‘).items() %} {{ k }}: {{ v.pop() }} <br /> {% endfor %} </p> [[email protected] salt]# salt ‘svr205-*‘ state.sls nginx ########################################################################## 略 ########################################################################## 查看文件是否有改变: 【svr205-2】 [[email protected] ~]# cat /usr/share/nginx/html/index.html <p> HOST: svr205-2 <br /> <br /><br /> lo: 10.0.205.100 <br /> eth0: 10.0.205.2 <br /> </p> 如上,得到了该主机的hostname和ip信息,符合预期。
时间: 2024-10-07 06:47:28