vlei 发表于 2018-7-30 08:18:44

ansible自动部署集群服务


  上面的思路大致是:
  首先配置yum仓库,之后搭建http+php,之后搭建数据库,其次搭建nginx反代,最后设置keepalived自动化安装。
  # pwd
  /etc/ansible
  # tree -L 3 roles/
  roles/
  ├── base
  │   ├── files
  │   │   ├── mage6.repo
  │   │   └── mage7.repo
  │   └── tasks
  │       └── main.yml
  ├── db
  │   ├── files
  │   │   ├── my6.cnf
  │   │   └── my7.cnf
  │   ├── handlers
  │   │   └── main.yml
  │   └── tasks
  │       └── main.yml
  ├── http+php
  │   ├── handlers
  │   │   └── main.yml
  │   ├── tasks
  │   │   └── main.yml
  │   ├── templates
  │   │   ├── httpd.conf6.j2
  │   │   └── httpd.conf7.j2
  │   └── vars
  │       └── main.yml
  ├── keepalived
  │   ├── handlers
  │   │   └── main.yml
  │   ├── tasks
  │   │   └── main.yml
  │   └── templates
  │       └── keepalived.conf.j2
  ├── nginx
  │   ├── handlers
  │   │   └── main.yml
  │   ├── tasks
  │   │   └── main.yml
  │   ├── templates
  │   │   └── nginx.conf.j2
  │   └── vars
  │       └── main.yml
  └── webdata
  ├── files
  │   ├── index.html
  │   ├── index.php
  │   └── wordpress
  └── tasks
  └── main.yml
  最后建立完成要有这些文件
  首先修改ansible主配置文件
  # vim hosts
  只留一下部分:
  
  172.16.1.3 hhname=kepnx1.zou.com state=MASTER pri=100
  172.16.1.5 hhname=kepnx2.zou.com state=BACKUP pri=98
  
  172.16.1.11 hhname=hp1.zou.com
  172.16.1.8 hhname=hp2.zou.com
  
  172.16.1.12 hhname=db.zou.com
  base
  ├── files
  │   ├── mage6.repo
  │   └── mage7.repo
  └── tasks
  └── main.yml
  # vim base/tasks/main.yml
  - name: install repo-file
  copy: src=mage7.repo dest=/etc/yum.repos.d/
  when:ansible_distribution_major_version == "7"
  - name: install repo source for yum
  copy: src=mage6.repo dest=/etc/yum.repos.d/
  when:ansible_distribution_major_version == "6"
  - name: rm some file of repos
  shell: rm -rf /etc/yum.repos.d/C*
  - name: set hostname
  hostname: name=` hhname `
  tags: sethostname
  - name: install killall for ckeck servers's state
  yum: name=psmisc state=latest
  - name: install bash-completion
  yum: name=bash-completion state=latest
  之后准备好两个可以yum安装册仓库源设置好mage6.repo 和mage7.repo
  http+php/
  ├── handlers
  │   └── main.yml
  ├── tasks
  │   └── main.yml
  ├── templates
  │   ├── httpd.conf6.j2
  │   └── httpd.conf7.j2
  └── vars
  └── main.yml
  # vim http+php/handlers/main.yml
  - name: restart httpd
  service: name=httpd state=restarted
  # vim http+php/tasks/main.yml
  - name: install http
  yum: name=httpd state=latest
  - name: install php
  yum: name=php state=latest
  - name: install php-mysql
  yum: name=php-mysql state=latest
  - name: install php-gd
  yum: name=php-gd state=latest
  - name: install php-mbsting
  yum: name=php-mbstring state=latest
  when: ansible_distribution_major_version == "7"
  - name: install php-xml
  yum: name=php-xml state=latest
  - name: mkdir web' file
  file: path=` htdocumentroot ` state=directory
  - name: install httpd.conf
  template: src=httpd.conf6.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd
  tags: rehttpdconf
  when: ansible_distribution_major_version == "6"
  - name: install httpd.conf
  template: src=httpd.conf7.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart httpd
  tags: rehttpdconf
  when: ansible_distribution_major_version == "7"
  - name: start httpd
  service: name=httpd state=started
  # vim http+php/templates/httpd.conf6.j2
  修改:
  Listen ` htport `
  DocumentRoot "` htdocumentroot `"
  <Directory "` htdocumentroot `">
  ErrorLog ` htdocumentroot `/error_log
  CustomLog ` htdocumentroot `/access_log combined
  # vim http+php/templates/httpd.conf7.j2
  修改:
  Listen ` htport `
  User ` htuser `
  Group ` htgroup `
  ServerName ` hhname `:80
  DocumentRoot "` htdocumentroot `"
  <Directory "` htdocumentroot `">
  <Directory "` htdocumentroot `">
  ErrorLog "` htdocumentroot `/error_log"
  CustomLog "` htdocumentroot `/access_log" combined
  # vim http+php/vars/main.yml
  htuser: apache
  htgroup: apache
  htport: 80
  htdocumentroot: /data/www
  db
  ├── files
  │   ├── my6.cnf
  │   └── my7.cnf
  ├── handlers
  │   └── main.yml
  └── tasks
  └── main.yml
  # vim files/my6.cnf
  
  datadir=/data/db
  socket=/var/lib/mysql/mysql.sock
  user=mysql
  innodb_file_per_table=ON
  skip_name_resolve=ON
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
  
  log-error=/var/log/mysqld.log
  pid-file=/var/run/mysqld/mysqld.pid
  # vim files/my7.cnf
  
  datadir=/data/db
  socket=/var/lib/mysql/mysql.sock
  innodb_file_per_table=ON
  skip_name_resolve=ON
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0
  # Settings user and group are ignored when systemd is used.
  # If you need to run mysqld under a different user or group,
  # customize your systemd unit file for mariadb according to the
  # instructions in http://fedoraproject.org/wiki/Systemd
  
  log-error=/var/log/mariadb/mariadb.log
  pid-file=/var/run/mariadb/mariadb.pid
  #
  # include all files from the config directory
  #
  !includedir /etc/my.cnf.d
  vim handlers/main.yml
  - name: restart mariadb
  service: name=mariadb state=restarted
  - name: restart mysql
  service: name=mysqld state=restarted
  # vim tasks/main.yml
  - name: install mariadb-server
  yum: name=mariadb-server state=latest
  when: ansible_distribution_major_version == "7"
  - name: install mysql-server
  yum: name=mysql-server state=latest
  when: ansible_distribution_major_version == "6"
  - name: build data file
  file: path=/data/db owner=mysql group=mysql state=directory
  - name: install mariadb conf
  copy: src=my7.cnf dest=/etc/my.cnf
  notify: restart mariadb
  tags: remariadbconf
  when: ansible_distribution_major_version == "7"
  - name: install mysql conf
  copy: src=my6.cnf dest=/etc/my.cnf
  notify: restart mysql
  tags: remysqlconf
  when: ansible_distribution_major_version == "6"
  - name: start mariadb
  service: name=mariadb state=started
  when: ansible_distribution_major_version == "7"
  - name: start mysql
  service: name=mysqld state=started
  when: ansible_distribution_major_version == "6"
  webdata/
  ├── files
  │   ├── index.html
  │   ├── index.php
  │   └── wordpress
  └── tasks
  └── main.yml
  # vim webdata/tasks/main.yml
  - name: web of index.html for test
  copy: src=index.html dest=/data/www
  - name: web of index.php for test
  copy: src=index.php dest=/data/www
  - name: web of wordpress
  copy: src=wordpress dest=/data/www/
  tags: copywordpress
  # vim webdata/files/index.html
  web form ` hhname ` the version is ` ansible_distribution_major_version `;
  # vim webdata/files/index.ph
  <?php
  $conn=mysql_connect('172.16.1.12','zou','123.comer');
  if($conn)
  echo ok;
  echo the web from ` hhname `;
  else
  echo fault;
  mysql_close();
  phpinfo()
  ?>
  之后准备好wordpress网页压缩包解压缩只有放到这个响应的位置,并编辑好wp-config.php
  nginx
  ├── handlers
  │   └── main.yml
  ├── tasks
  │   └── main.yml
  ├── templates
  │   └── nginx.conf.j2
  └── vars
  └── main.yml
  # vim tasks/main.yml
  - name: install nginx package
  yum: name=nginx state=present
  - name: install conf file
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx
  tags: reinstallconf
  - name: start nginx
  service: name=nginx state=started enabled=true
  # cat handlers/main.yml
  - name: restart nginx
  service: name=nginx state=restarted
  # cat vars/main.yml
  username: nginx
  # grep -v '^[[:space:]]\+#' templates/nginx.conf.j2
  user` username `;
  worker_processes` ansible_processor_vcpus `;
  error_log/var/log/nginx/error.log warn;
  pid      /var/run/nginx.pid;
  events {
  worker_connections1024;
  }
  http {
  include       /etc/nginx/mime.types;
  default_typeapplication/octet-stream;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log/var/log/nginx/access.logmain;
  sendfile      on;
  keepalive_timeout65;
  gzipon;
  upstream backend {
  server 172.16.1.8;
  server 172.16.1.11 weight=2;
  }
  include /etc/nginx/conf.d/*.conf;
  #########################################################################
  #sorry nginx      #
  ###################
  server {
  listen       80;
  server_name` hhname `;
  #
  location / {
  proxy_pass http://backend;
  index index.html index.php;
  }
  error_page   500 502 503 504/50x.html;
  }
  }
  keepalived/
  ├── handlers
  │   └── main.yml
  ├── tasks
  │   └── main.yml
  └── templates
  └── keepalived.conf.j2
  # vim tasks/main.yml
  - name: install the keepalived
  yum: name=keepalived state=latest
  - name: install ntpdate
  yum: name=ntpdate state=latest
  - name: make time to equal
  shell: ntpdate 172.16.0.1
  - name: install the conf_file
  template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
  notify: restart keepalived
  tags: rekeepconf
  - name: start keepalived
  service: name=keepalived state=started enabled=true
  # vim handlers/main.yml
  - name: restart keepalived
  service: name=keepalived state=restarted
# cat templates/keepalived.conf.j2  global_defs {
  notification_email {
  root@localhost
  }
  notification_email_from keepalived@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id ` hhname `
  vrrp_mcast_group4 224.0.101.1
  }
  vrrp_script chk_nginx {
  script "killall -0 nginx && exit 0 || exit 1"
  interval 1
  weight -5
  }
  track_script {
  chk_nginx
  }
  vrrp_instance VI_1 {
  state ` state `
  interface eno16777736
  virtual_router_id 101
  priority ` pri `
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 123.com
  }
  virtual_ipaddress {
  172.16.1.4
  }
  track_script {
  chk_nginx
  }
  }
  基本定义完成角色,但是要想要生效还要调用角色,执行才能实现集群的部署
  /root/myansible/
  ├── base.yml
  ├── db.yml
  ├── hp+webdata.yml
  ├── http+php.yml
  └── keng.yml
  # cat base.yml
  - hosts: all
  remote_user: root
  roles:
  - base
  # cat http+php.yml
  - hosts: httphp
  remote_user: root
  roles:
  - http+php
  # cat db.yml
  - hosts: db
  remote_user: root
  roles:
  - db
  # cat hp+webdata.yml
  - hosts: httphp
  remote_user: root
  roles:
  - webdata
  # cat keng.yml
  - hosts: keepnginx
  remote_user: root
  roles:
  - keepalived
  - { role: nginx, username: nginx, when: "ansible_distribution_major_version == '7'" }
  ansible是不同启动的,安装完毕,配置好hosts文件即可使用,这就是安装了一个命令
  # ansible-playbook base.yml --check
  # ansible-playbook base.yml
  # ansible-playbook http+php.yml --check
  # ansible-playbook http+php.yml
  # ansible-playbook db.yml --check
  # ansible-playbook db.yml
  # ansible-playbook hp+webdata.yml --check
  # ansible-playbook hp+webdata.yml
  # ansible-playbook keng.yml --check
  # ansible-playbook keng.yml
页: [1]
查看完整版本: ansible自动部署集群服务