Jenkins+Gitlab+Ansible自动化部署(六)

Jenkins+Gitlab+Ansible自动化部署(2021-08-12)

  • Pipeline Job实现Nginix+MySQL+PHP+Wordpress实现自动化部署交付

一、环境准备

  • 编写ansible playbook脚本实现Wordpress远程部署
  • 将wordpress源码与playbook部署脚本提交到gitlab仓库
  • 编写pipeline job脚本实现Jenkins流水线持续交付流程
  • Jenkins集成Ansible与gitlab实现wordpress的自动化部署

二、验证环境

1、登录 Jenkins主机

[root@jenkins ~]# su - deploy
Last login: Wed Aug 11 20:04:22 CST 2021 on pts/0
[deploy@jenkins ~]$ ansible --version
ansible [core 2.11.3]
  config file = None
  configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/deploy/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.9.1 (default, Aug 11 2021, 11:57:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.1
  libyaml = True
  
[deploy@jenkins ~]$ scp /etc/hosts root@192.168.200.160:/etc/hosts  
[deploy@jenkins ~]$ ssh root@test.example.com
Last login: Thu Aug 12 15:29:16 2021 from 192.168.200.158
[root@test ~]# yum -y install git
  

2、登录Jenkins web管理页

在这里插入图片描述

3、登录gitlab web管理页

在这里插入图片描述

三、ansible playbook 项目准备

1、定义配置文件

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~
$ cd ansible-playbook-repo/

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo (main)
$ ll
total 1
-rw-r--r-- 1 qiufeng.li 197121 12 Aug 12 10:41 ansible-playbook.txt
drwxr-xr-x 1 qiufeng.li 197121  0 Aug 12 10:45 nginx_playbooks/

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo (main)
$ mkdir wordpress_playbooks

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo (main)
$ cd wordpress_playbooks/

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ mkdir inventory

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ mkdir -p roles/wordpress/{files,tasks,templates}

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ ll
total 0
drwxr-xr-x 1 qiufeng.li 197121 0 Aug 12 13:49 inventory/
drwxr-xr-x 1 qiufeng.li 197121 0 Aug 12 13:49 roles/

2、配置deploy.yml

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ vim deploy.yml
- hosts: wordpress
  gather_facts: true
  remote_user: root
  vars:
      backup_to: "{
  {root}}_{
  {branch}}_{
  {ansible_date_time.epoch}}"
  roles:
    - wordpress

3、配置dev

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ vim inventory/dev
[wordpress]
test.example.com

[wordpress:vars]
server_name=test.example.com
port=8080
user=deploy
worker_processes=2
max_open_file=30000
root=/data/www
gitlab_user='root'
gitlab_pass='1234.com'

4、配置prod

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ vim inventory/prod
[wordpress]
test.example.com

[wordpress:vars]
server_name=test.example.com
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/data/www
gitlab_user='root'
gitlab_pass='1234.com'

5、配置health_check.sh和info.php

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$  vim roles/wordpress/files/health_check.sh
#!/bin/sh
URL=$1
PORT=$2
curl -Is http://$URL:$PORT/info.php > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ echo "<?php phpinfo(); ?>" >> roles/wordpress/files/info.php

6、配置www.conf

qiufeng.li@DESKTOP-JC7QDVB MINGW64 ~/ansible-playbook-repo/wordpress_playbooks (main)
$ vim roles/wordpress/files/www.conf

; Start a new pool named 'www'.
[www]

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = deploy
; RPM: Keep a group allowed to write in log dir.
group = deploy

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock


; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 511

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0660
listen.owner = deploy
listen.group = deploy
;listen.mode = 0660
; When POSIX Access Control Lists are supported you can set them using
; these options, value is a comma separated list of user/group names.
; When set, listen.owner and listen.group are ignored
;listen.acl_users =
;listen.acl_groups =

; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 127.0.0.1

; Specify the nice(2) priority to apply to the pool processes (only if set)
; The value can vary from -19 (highest priority) to 20 (lower priority)
; Note: - It will only work if the FPM master process is launched as root
;       - The pool processes will inherit the master process priority
;         unless it specified otherwise
; Default Value: no set
; process.priority = -19

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives. With this process management, there will be
;             always at least 1 children.
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
;  ondemand - no children are created at startup. Children will be forked when
;             new requests will connect. The following parameter are used:
;             pm.max_children           - the maximum number of children that
;                                         can be alive at the same time.
;             pm.process_idle_timeout   - The number of seconds after which
;                                         an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the orig
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值