准备开发测试环境

0.环境准备

在开始介绍PuppetOpenstack前,我们需要准备一台虚拟服务器用于接下来的练习。

读者可以通过使用虚拟化软件或者通过云平台创建一台虚拟机。

其规格如下:

  • 2 vCPU, 4G RAM, 30G Disk, 至少有一块NIC,操作系统为CentOS 7.1/7.2,可以访问Internet

在安装Puppet之前,需要为虚拟主机设置合适的主机名,域名,时间等。

  1. $ hostnamectl set-hostname learnpom
  2. $ echo "127.0.1.1 learnpom.example.in learnpom" >> /etc/hosts

1.了解Puppet

在安装Puppet前,首先需要了解Puppet的运行方式,当前Puppet支持两种运行方式:

  • Server/Client模式,需要安装Puppet agent和Puppet server软件包
  • Standalone模式,只需要安装Puppet agent软件包

在通常的开发场景下,笔者推荐使用Standalone模式,操作简单,定位问题容易;
在管理内部的测试/生产环境时,笔者建议须使用Server/Client模式,进行集中式管理。

本书中除个别场景外,默认以Standalone模式为主。

2.安装Puppet

Puppet由三个软件包构成:

  • puppet-agent: 用于安装Puppet,Ruby,Facter,Hiera和依赖包的软件包
  • puppetserver: 用于安装Puppet Server服务

注:本文所使用的Puppet版本是4.1x

打开虚拟机的终端,使用root权限在命令行下输入以下命令:

  1. $ cat << EOF >> install_puppet.sh
  2. # Script for installing puppet Based on CentOS 7.x
  3. set -e
  4. if [ -n "$DEBUG" ]; then
  5. set -x
  6. fi
  7. # set environment
  8. export SCRIPT_DIR=$(cd `dirname $0` && pwd -P)
  9. export PUPPET_VERSION=${PUPPET_VERSION:-4}
  10. export MANAGE_PUPPET_MODULES=${MANAGE_PUPPET_MODULES:-true}
  11. export MANAGE_REPOS=${MANAGE_REPOS:-true}
  12. export PUPPET_ARGS=${PUPPET_ARGS:-}
  13. export SCRIPT_DIR=$(cd `dirname $0` && pwd -P)
  14. if [ $PUPPET_VERSION == 4 ]; then
  15. export PATH=${PATH}:/opt/puppetlabs/bin
  16. export PUPPET_RELEASE_FILE=puppetlabs-release-pc1
  17. export PUPPET_BASE_PATH=/etc/puppetlabs/code
  18. export PUPPET_PKG=puppet-agent
  19. elif [ $PUPPET_MAJ_VERSION == 5 ]; then
  20. export PATH=${PATH}:/opt/puppetlabs/bin:/opt/puppetlabs/puppet/bin
  21. export PUPPET_RELEASE_FILE=puppet5-nightly-release
  22. export PUPPET_BASE_PATH=/etc/puppetlabs/code
  23. export PUPPET_PKG=${PUPPET_PKG:-puppet-agent}
  24. fi
  25. if [ $(id -u) != 0 ]; then
  26. # preserve environment so we can have ZUUL_* params
  27. SUDO='sudo -E'
  28. fi
  29. echo 'Setup (RedHat based)'
  30. sudo yum -y remove facter puppet rdo-release
  31. sudo yum -y install libxml2-devel libxslt-devel ruby-devel rubygems wget
  32. sudo yum -y groupinstall "Development Tools"
  33. echo 'Install Bundler'
  34. mkdir -p .bundled_gems
  35. export GEM_HOME=`pwd`/.bundled_gems
  36. gem install bundler --no-rdoc --no-ri --verbose
  37. echo 'Start install puppet'
  38. if rpm --quiet -q $PUPPET_RELEASE_FILE; then
  39. $SUDO rpm -e $PUPPET_RELEASE_FILE
  40. fi
  41. # EPEL does not work fine with RDO, we need to make sure EPEL is really disabled
  42. if rpm --quiet -q epel-release; then
  43. $SUDO rpm -e epel-release
  44. fi
  45. $SUDO rm -f /tmp/puppet.rpm
  46. wget http://yum.puppetlabs.com/${PUPPET_RELEASE_FILE}-el-7.noarch.rpm -O /tmp/puppet.rpm
  47. $SUDO rpm -ivh /tmp/puppet.rpm
  48. $SUDO yum install -y dstat ${PUPPET_PKG} setools setroubleshoot audit
  49. $SUDO service auditd start
  50. # SElinux in permissive mode so later we can catch alerts
  51. $SUDO setenforce 0
  52. EOF
  53. $ sudo bash install_puppet.sh

3.安装PuppetServer

Puppetserver的手动安装和配置部署比较繁杂,但是Puppet的目标不就是实现软件安装部署的自动化吗?

因此,我们可以使用puppet module安装用于部署Puppet Server的module,然后完成Puppetserver的一键安装。

在终端下执行以下命令:

  1. $ puppet module install theforeman-puppet
  2. $ cat > install.pp <<EOF
  3. class { '::puppet': server => true, server_foreman => false }
  4. EOF
  5. $ puppet apply install.pp -v