设为首页 收藏本站
查看: 845|回复: 0

[经验分享] ansible自动部署集群服务

[复制链接]

尚未签到

发表于 2018-7-30 08:18:44 | 显示全部楼层 |阅读模式
DSC0000.png

  上面的思路大致是:
  首先配置yum仓库,之后搭建http+php,之后搭建数据库,其次搭建nginx反代,最后设置keepalived自动化安装。
  [root@localhost ansible]# pwd
  /etc/ansible
  [root@localhost 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主配置文件
  [root@localhost ansible]# vim hosts
  只留一下部分:
  [keepnginx]
  172.16.1.3 hhname=kepnx1.zou.com state=MASTER pri=100
  172.16.1.5 hhname=kepnx2.zou.com state=BACKUP pri=98
  [httphp]
  172.16.1.11 hhname=hp1.zou.com
  172.16.1.8 hhname=hp2.zou.com
  [db]
  172.16.1.12 hhname=db.zou.com
  base
  ├── files
  │   ├── mage6.repo
  │   └── mage7.repo
  └── tasks
  └── main.yml
  [root@localhost roles]# 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
  [root@localhost roles]# vim http+php/handlers/main.yml
  - name: restart httpd
  service: name=httpd state=restarted
  [root@localhost roles]# 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
  [root@localhost roles]# vim http+php/templates/httpd.conf6.j2
  修改:
  Listen ` htport `
  DocumentRoot "` htdocumentroot `"
  <Directory "` htdocumentroot `">
  ErrorLog ` htdocumentroot `/error_log
  CustomLog ` htdocumentroot `/access_log combined
  [root@localhost roles]# 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
  [root@localhost roles]# 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
  [root@localhost db]# vim files/my6.cnf
  [mysqld]
  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
  [mysqld_safe]
  log-error=/var/log/mysqld.log
  pid-file=/var/run/mysqld/mysqld.pid
  [root@localhost db]# vim files/my7.cnf
  [mysqld]
  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
  [mysqld_safe]
  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
  [root@localhost db]# 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
  [root@localhost roles]# 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
  [root@localhost roles]# vim webdata/files/index.html
  web form ` hhname ` the version is ` ansible_distribution_major_version `;
  [root@localhost roles]# 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
  [root@localhost nginx]# 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
  [root@localhost nginx]# cat handlers/main.yml
  - name: restart nginx
  service: name=nginx state=restarted
  [root@localhost nginx]# cat vars/main.yml
  username: nginx
  [root@localhost 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_connections  1024;
  }
  http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$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.log  main;
  sendfile        on;
  keepalive_timeout  65;
  gzip  on;
  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
  [root@localhost keepalived]# 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
  [root@localhost keepalived]# vim handlers/main.yml
  - name: restart keepalived
  service: name=keepalived state=restarted
[root@localhost keepalived]# 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
  [root@localhost myansible]# cat base.yml
  - hosts: all
  remote_user: root
  roles:
  - base
  [root@localhost myansible]# cat http+php.yml
  - hosts: httphp
  remote_user: root
  roles:
  - http+php
  [root@localhost myansible]# cat db.yml
  - hosts: db
  remote_user: root
  roles:
  - db
  [root@localhost myansible]# cat hp+webdata.yml
  - hosts: httphp
  remote_user: root
  roles:
  - webdata
  [root@localhost myansible]# cat keng.yml
  - hosts: keepnginx
  remote_user: root
  roles:
  - keepalived
  - { role: nginx, username: nginx, when: "ansible_distribution_major_version == '7'" }
  ansible是不同启动的,安装完毕,配置好hosts文件即可使用,这就是安装了一个命令
  [root@localhost myansible]# ansible-playbook base.yml --check
  [root@localhost myansible]# ansible-playbook base.yml
  [root@localhost myansible]# ansible-playbook http+php.yml --check
  [root@localhost myansible]# ansible-playbook http+php.yml
  [root@localhost myansible]# ansible-playbook db.yml --check
  [root@localhost myansible]# ansible-playbook db.yml
  [root@localhost myansible]# ansible-playbook hp+webdata.yml --check
  [root@localhost myansible]# ansible-playbook hp+webdata.yml
  [root@localhost myansible]# ansible-playbook keng.yml --check
  [root@localhost myansible]# ansible-playbook keng.yml

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-543307-1-1.html 上篇帖子: Centos6.5利用RubyGems的fpm制作zabbix_agent的rpm包,并使用ansible批量部署 下篇帖子: Ansible-playbook自动部署mysql的主从复制读写分离
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表