Jenkins+Git+Gitlab+Ansible實現持續集成自動化部署動態網站(二)--技術流ken

 

項目前言

 

在上一篇博客《Jenkins+Git+Gitlab+Ansible實現持續化集成一鍵部署靜態網站(一)--技術流ken》中已經詳細講解了如何使用這四個工具來持續集成自動化部署一個靜態的網站。php

若是你們能夠熟練掌握以上內容,勢必會在工做中減輕不小的工做量。html

本篇博客將再次使用這四個工具結合freestyle和pipeline來完成動態網站的部署。node

爲了拓寬知識點,本篇博客將使用jenkins的兩種經常使用方法來進行部署,若是你對pipeline還不熟悉,請參考我以前的博客《Jenkins憑證及任務演示-pipeline(二)--技術流ken》。mysql

雖然說freestyle也可完成相同的操做,並且操做簡單,可是freestyle只是適合服務器量不多的場景50-100臺左右。若是你管理了百臺、千臺服務器勢必會用到pipeline進行批量化操做。linux

因此pipeline來仍是應該深刻去研究一下的,雖然說確實有困難,可是由於它的格式相對固定單一,也不是可望而不可及。多學多練多總結。git

有關持續集成自動化部署系列差很少寫了有十篇左右了。本篇博客將是這一系列的最後一篇,接下來會更新docker及k8s系列。敬請期待。程序員

 

項目需求

 

需求一.、使用gitlab建立項目web

需求2、 使用ansible的roles實現一鍵化部署wordpresssql

               每次部署須要備份以前的網站數據docker

               使用roles

               使用templates

               腳本對網站監控檢測          

需求3、 完成以後項目上傳至gitlab

需求4、使用jenkins實現項目的持續集成

 

項目環境準備

 

centos7

Gitlab服務器: 10.220.5.137

Ansible服務器: 10.220.5.139

wordpress服務器1: 10.220.5.138

防火牆以及selinux關閉狀態

 

Gitlab建立項目

 

有關項目的建立我這裏就再也不贅述,你們能夠參考我以前的博客《Gitlab在linux/windows中免密使用(二)--技術流ken

我就直接拿我以前建立好的項目進行演示

Git下載遠程倉庫內容

 

 在ansible服務器10.220.5.139上面操做

第一步:建立目錄並clone遠程倉庫內容

[root@ken ~]# mkdir t
[root@ken ~]# cd t
[root@ken t]# git clone http://10.220.5.137/webg1/wordpress.git
Cloning into 'wordpress'...
Username for 'http://10.220.5.137': root
Password for 'http://root@10.220.5.137': 
remote: Counting objects: 1062, done.
remote: Compressing objects: 100% (969/969), done.
remote: Total 1062 (delta 68), reused 1042 (delta 68)
Receiving objects: 100% (1062/1062), 4.15 MiB | 0 bytes/s, done.
Resolving deltas: 100% (68/68), done.
[root@ken t]# ls
wordpress

 

Ansible劇本的編寫

 

第一步:建立相關目錄

在ansible服務器端操做

[root@ken ~]# mkdir /project/roles/wordpress/{vars,tasks,files,templates} -pv

 

第二步:編寫templates模版

複製代碼
  [root@ken ~]# cp /etc/httpd/conf/httpd.conf /project/roles/wordpress/templates/httpd.conf.j2
  [root@ken ~]# grep -v -E '^#|^$| +#' /project/roles/wordpress/templates/httpd.conf.j2
  ServerRoot "/etc/httpd"
  Listen {{ port }}    #定義成變量
  Include conf.modules.d/*.conf
  User {{ user }}       #定義成變量
  Group apache
  ServerAdmin root@localhost
  ServerName {{ ansible_eth0.ipv4.address }}  #引用內置變量
  DocumentRoot {{ root }}               #定義成變量
複製代碼

 

第三步:編輯hosts配置文件

[root@ken project]# vim /etc/ansible/hosts
[devser]
10.220.5.139

 

第四步:編寫測試文件

[root@ken project]# vim roles/wordpress/files/index.php
<?php
phpinfo();
?>

 

第五步:編寫網站健康監測腳本

複製代碼
[root@ken project]# cd roles/wordpress/files/
[root@ken files]# ls
[root@ken files]# vim check.sh 
[root@ken files]# cat check.sh 
#!/bin/bash
URL=$1
PORT=$2
curl -I http://$1:$2/index.php | grep "200 OK" &>/dev/null
if [ $? -eq 0 ];then
    echo "$1 status is ok"
else
    echo "$1 status is not ok"
fi
複製代碼

 

第六步:編寫tasks下的文件

複製代碼
[root@ken wordpress]# cat tasks/main.yml
######## 安裝 httpd php mysql ###############
- name: install httpd
  yum: name=httpd state=present
- name: install mysql
  yum: name=mariadb-server state=present
- name: install php
  shell: yum install php php-mysql -y
######## 配置httpd #########################
- name: make configrantion file
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- name: install test page
  copy: src=index.php dest={{ root }}
######## 啓動服務 ####################
- name: start httpd
  service: name=httpd state=restarted
- name: start mysql
  service: name=mariadb state=restarted
######### 檢查部署結果是否正常 ##########
- name: make health check
  shell: sh roles/wordpress/files/check.sh {{ ansible_eth0.ipv4.address }} {{ port }}
  delegate_to: localhost
  register: health_status
- debug: msg="{{ health_status.stdout }}"
########## 從gitlab拉取代碼 #################
- name: backup old files
  shell: mv {{ root }} {{ backup_to }}
- name: close ssl authtication
  shell: git config --global http.sslVerify false
- name: git clone wordpress from gitlab
  git: "repo=http://{{ gitlab_user }}:{{ gitlab_pass }}@10.220.5.137/webg1/wordpress.git dest={{ root }} version=master"
複製代碼

 

第七步:編寫vars下的文件

[root@ken ~] # cat /project/roles/wordpress/vars/main.yml
port: 82
user: apache
root: /var/www
gitlab_user: root
gitlab_pass: 12345678

 

第八步:編輯劇本

劇本必定要和roles在同一個目錄之中

執行劇本的時候也要在roles同級目錄下執行

[root@ken ~]# vim /project/wordpress.yaml
- hosts: all
  vars:
     backup_to: "{{ root }}_{{ ansible_date_time.epoch }}"
  roles:
   - wordpress

 

第九步:一鍵部署wordpress

[root@ken ~]# cd /project/
[root@ken project]# ansible-playbook -i inventory/dev dev.yaml

 

第十步:查看執行結果

能夠發現沒有報錯

[root@ken project]# ansible-playbook -i inventory/den dev.yaml 
 [WARNING]: Found variable using reserved name: port


PLAY [all] ***********************************************************************

TASK [Gathering Facts] ***********************************************************
ok: [10.220.5.138]

TASK [wordpress : install httpd] *************************************************
ok: [10.220.5.138]

TASK [wordpress : install mysql] *************************************************
ok: [10.220.5.138]

TASK [wordpress : install php] ***************************************************
 [WARNING]: Consider using the yum module rather than running yum.  If you need
to use command because yum is insufficient you can add warn=False to this command
task or set command_warnings=False in ansible.cfg to get rid of this message.

changed: [10.220.5.138]

TASK [wordpress : make configrantion file] ***************************************
ok: [10.220.5.138]

TASK [wordpress : install test page] *********************************************
ok: [10.220.5.138]

TASK [wordpress : start httpd] ***************************************************
changed: [10.220.5.138]

TASK [wordpress : start mysql] ***************************************************
ok: [10.220.5.138]

TASK [wordpress : make health check] *********************************************
changed: [10.220.5.138 -> localhost]

TASK [wordpress : debug] *********************************************************
ok: [10.220.5.138] => {
    "msg": "10.220.5.138 status is ok"
}

TASK [wordpress : backup old files] **********************************************
changed: [10.220.5.138]

TASK [wordpress : close ssl authtication] ****************************************
changed: [10.220.5.138]

TASK [wordpress : git clone wordpress from gitlab] *******************************
changed: [10.220.5.138]

PLAY RECAP ***********************************************************************
10.220.5.138               : ok=13   changed=6    unreachable=0    failed=0   

 

數據上傳至Gitlab

 

第一步:上傳因此數據至遠程gitlab

[root@ken project]# cd ..
[root@ken wordpress]# ls
da  project  wordpress
[root@ken wordpress]# ls -a
.  ..  da  .git  project  wordpress
[root@ken wordpress]# git add .
[root@ken wordpress]# git commit -m "v3"
[master e533bc1] v3
 2 files changed, 3 insertions(+)
 create mode 100644 project/dev.retry
 create mode 100644 project/inventory/den
[root@ken wordpress]# git push

 

第二步:web端查看

能夠看到如今已經有了咱們剛纔已經上傳的v3版本了

 

Jenkins端實現持續集成部署--freestyle

 

第一步:建立一個freestyle的任務

 

第二步:填寫gitlab倉庫信息

 

第三步:構建shell

填寫以下信息並保存

這裏之因此要先cd到工做目錄下的project是由於要找到dev.yaml劇本

 

 

 

第四步:開始構建

能夠發現已經部署完成

 

 

Jenkins端實現持續集成部署--pipeline

 

第一步:建立一個流水線即pipeline任務

 

第二步:編寫pipeline

 若是你是複製的下面的代碼請務必更改你的git路徑以及指定你的憑證ID

#!groovy

pipeline {
 agent {node {label 'master'}}

 environment {
  PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
 }

 parameters {
  choice(
   choices: 'dev\nrprod',
   description: 'Choose deploy environment',
   name: 'deploy_env'
  )
  string (name: 'branch', defaultValue: 'master', description: 'Fill in your ansible repo branch')
 }

 stages {
  stage ("Pull deploy code") {
   steps{
    sh 'git config --global http.sslVerify false'
    dir ("${env.WORKSPACE}"){
     git branch: 'master', credentialsId: '527d840a-ad47-4a16-bfa1-c6eab88a3563', url: 'http://10.220.5.137/webg1/wordpress.git'
    }
   }

  }

  stage ("Anisble deployment") {
   steps {
    dir("${env.WORKSPACE}"){
     echo "[INFO] Start deployment"
     sh """
     set +x
     ansible-playbook -i inventory/$env ./dev.yaml -e branch=$branch -e env=$deploy_env
     set -x
     """
     echo "[INFO] Deployment finished..."
    }
   }
  }

 }

}

 

第三步:查看執行結果

發現執行成功

之後程序員更改代碼以後就能夠本身測試上線了

 

第四步:數據庫受權

[root@ken ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> grant all on *.* to ken@'localhost' identified by '123';
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> exit
Bye

 

第五步:填寫上面數據庫配置的信息

 

第六步:配置成功頁面

注意訪問的時候IP地址以後須要加上:82端口號,由於咱們上面的劇本中指定了變量port爲82號端口

 

相關文章
相關標籤/搜索