Git+Jenkins學習之路(十四)之自動化指令碼部署實踐
阿新 • • 發佈:2018-11-03
一、環境說明和準備
1、環境說明
主機名 | IP地址 | 角色 | 系統 |
---|---|---|---|
deploy-server | 192.168.56.12 | 釋出 | Centos 7.4 |
web | 192.168.56.13 | web伺服器,nfs伺服器 | Centos 7.4 |
2、伺服器準備工作
(1)釋出機前期準備
a.增加普通使用者並配置密碼
[[email protected] ~]# useradd www
[[email protected] ~]# id www
uid=1000(www) gid=1000(www) groups=1000(www)
[[email protected] ~]# passwd www
[[email protected] ~]# yum install -y git tree
b.建立部署需要的目錄並初始化git目錄
[[email protected] ~]# mkdir -pv /deploy/{code/{www,jxs,wap,gys,glzx,yyzx},config,tar,tmp}
[[email protected] ~]# chown -R www.www /deploy
[ [email protected] ~]$ tree /deploy
/deploy
├── code
│ ├── glzx
│ ├── gys
│ ├── jxs
│ ├── wap
│ ├── www
│ └── yyzx
├── config
├── tar
└── tmp
[[email protected] ~]$ cd /deploy/code/www && git init
c.建立配置檔案config.php
[ [email protected] ~]$ vim /deploy/config/config.php
this is config.php
[[email protected] ~]$ tree /deploy
/deploy
├── code
│ ├── glzx
│ ├── gys
│ ├── jxs
│ ├── wap
│ ├── www
│ └── yyzx
├── config
│ └── config.php
├── tar
└── tmp
d.配置釋出機和目標機的ssh通訊
[[email protected] deploy]$ cat /home/www/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDzM3AW6/X+djvKJTsdFbY3ik+mlradxpD3COoTP5h6x509unksuCdduV7awPjEGHvK2GVjJmvckxdvLkMc23p7bsctHlturPN2VozJTrYwXMAbmxf97cKE/fpKhjPXG8HlWBLpEaTM8PITgvdcyaeAUaIN+/h5VrA8TZKFAgbxDLxtgwqPzYIG9nqCO7MMCgzhJxI6PDQ6KVU9rHal/p4XKTIy4Rq4FzZTav2tS4zNJ7kX9+e6EO0JTooPanJXBTltLOJJsKxnlA7tc20rq6+0XVqbUBKYahL/8ZzkxZozNrNq7wtIuuJo0WTDFzDdPcJyAGlRWLuwct7y4p4UApVz [email protected]
[[email protected] ~]$ ssh-copy-id [email protected]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/www/.ssh/id_rsa.pub"
The authenticity of host '192.168.56.13 (192.168.56.13)' can't be established.
ECDSA key fingerprint is SHA256:ahG6dBy/Z1nUIUWhQQrylsiwBlnDKC/jz8rnaPU2eF0.
ECDSA key fingerprint is MD5:6e:58:0b:02:1c:a4:41:51:e8:7d:33:4d:46:bb:a0:68.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]$ ssh 192.168.56.13
Last login: Fri Nov 2 18:01:02 2018
[[email protected] ~]$
e.將ssh公鑰複製到github或gitlab
(2)web伺服器準備
a.部署NFS伺服器
[[email protected] ~]# yum install -y nfs-utils rpcbind
[[email protected] ~]# vim /etc/exports
/nas/www 192.168.56.0/24(rw,sync,no_root_squash)
/nas/jxs 192.168.56.0/24(rw,sync,no_root_squash)
/nas/wap 192.168.56.0/24(rw,sync,no_root_squash)
/nas/glzx 192.168.56.0/24(rw,sync,no_root_squash)
/nas/yyzx 192.168.56.0/24(rw,sync,no_root_squash)
/nas/gys 192.168.56.0/24(rw,sync,no_root_squash)
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# showmount -e
Export list for web:
/nas/gys 192.168.56.0/24
/nas/yyzx 192.168.56.0/24
/nas/glzx 192.168.56.0/24
/nas/wap 192.168.56.0/24
/nas/jxs 192.168.56.0/24
/nas/www 192.168.56.0/24
b.掛載共享目錄
[[email protected] ~]# mkdir /webroot/{www,jxs,wap,gys,glzx,yyzx}
[[email protected] ~]# chown -R www.www /webroot
[[email protected] ~]# mount -f nfs 192.168.56.13:/nas/www /webroot/www
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 1.7G 16G 10% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 497M 171M 326M 35% /boot
tmpfs 99M 0 99M 0% /run/user/0
192.168.56.13:/nas/www 18G 1.7G 16G 10% /webroot/www
c.部署nginx+php
[[email protected] ~]# useradd www
[[email protected] ~]# passwd www
[[email protected] ~]# yum install -y nginx php php-fpm
[[email protected] ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
[[email protected] ~]# mkdir /data/web -pv && chown -R www.www /data
[[email protected] ~]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
root /data/web/www;
index index.php index.htm index.html;
client_max_body_size 20m;
location / {
proxy_read_timeout 150;
try_files $uri $uri/ /index.php;
}
location ~* .*\.php$ {
#try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# vim /etc/php-fpm.d/www.conf
user www
group www
....
[[email protected] ~]# php-fpm -t
[03-Nov-2018 10:16:18] NOTICE: configuration file /etc/php-fpm.conf test is successful
[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sat, 03 Nov 2018 02:16:36 GMT
Content-Type: text/html
Content-Length: 35
Last-Modified: Sat, 03 Nov 2018 02:07:00 GMT
Connection: keep-alive
ETag: "5bdd02c4-23"
Accept-Ranges: bytes
二、釋出指令碼編寫
1、自動化部署流程設計
- 釋出機獲取程式碼(直接拉取),從svn或git倉庫進行拉取
- 釋出機編譯程式碼(可選,java語言需要編譯,PHP無需編譯)
- 將配置檔案拷貝到程式碼檔案(由於配置檔案有資料庫等機密資訊,需要獨立處理)
- 程式碼打包(將程式碼進行壓縮打包)
- SCP到目標伺服器目錄
- 將目標伺服器移除叢集
- 在目標伺服器上進行解壓傳過來的程式碼
- 解壓後,對程式碼根目錄進行軟連結到剛才的目錄檔案,實現版本連結
- 重啟(可選,tomcat需要重啟)
- 測試
- 加入叢集
2、自動化部署指令碼編寫
#!/bin/bash
#deploy-server create dir as root
#[ -d /deploy ] && mkdir -pv /deploy/{code/web-demo,config,tar,tmp} && chown -R www.www /deploy
#node-server create dir as root
#[ -d /data ] && mkdir -pv /data/web && chown -R www.www /data
#nfs dir /nas/www mount to /webroot
#Node List
NODE="192.168.56.13"
#Shell ENV
SHELL_NAME="deploy.sh"
SHELL_DIR="/home/www"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
#Code ENV
PRO_NAME="www"
CODE_DIR="/deploy/code/www"
CONFIG_DIR="/deploy/config"
TMP_DIR="/deploy/tmp"
TAR_DIR="deploy/tar"
LOCK_FILE="/tmp/deploy.lock"
#Date/Time ENV
LOG_DATE='date "+%Y-%m-%d"'
LOG_TIME='date "+%H-%M-%S"'
CDATE=$(date "+%Y-%m-%d")
CTIME=$(date "+%H-%M-%S")
useage(){
echo $"Useage: $0 { deploy |rollback [ list | version ] }"
}
url_test(){
URL=$1
curl -s --head $URL |grep '200 OK'
if [ $? -ne 0 ];then
shell_unlock;
writelog "test error" && exit;
fi
}
writelog(){
LOGINFO=$1
echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO} " >> ${SHELL_LOG}
}
code_get(){
writelog "code_get";
cd ${CODE_DIR} && git pull [email protected]:kin08200/learngit.git
cp -r ${CODE_DIR} ${TMP_DIR}
API_VERL=$(git show |grep commit |cut -d ' ' -f2 )
API_VER=$(echo ${API_VERL:0:6})
echo $API_VER
}
code_config(){
writelog "code_config"
/bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/${PRO_NAME}
PKG_NAME=${PRO_NAME}_"${API_VER}"_"${CDATE}-${CTIME}"
cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
code_tar(){
writelog "code_tar"
cd ${TMP_DIR} && tar -czf ${PKG_NAME}.tar.gz ${PKG_NAME}
writelog "${PKG_NAME}.tar.gz"
}
code_scp(){
writelog "code_scp"
scp ${TMP_DIR}/${PKG_NAME}.tar.gz $NODE:/webroot/www
}
code_deploy(){
writelog "code_deploy"
ssh $NODE "cd /webroot/www && tar -zxf ${PKG_NAME}.tar.gz"
ssh $NODE "rm -rf /data/web/www && ln -s /webroot/www/${PKG_NAME} /data/web/www"
}
code_test(){
url_test "http://192.168.56.13"
}
rollback_fun(){
ssh $NODE "rm -rf /data/web/www && ln -s /webroot/www/$1 /data/web/www"
}
rollback(){
if [ -z $1 ];then
shell_unlock;
echo "Please input rollback version." && exit;
fi
case $1 in
list)
ls -l /deploy/tmp/*.tar.gz
;;
*)
rollback_fun $1
;;
esac
}
shell_lock(){
touch ${LOCK_FILE}
}
shell_unlock(){
rm -f ${LOCK_FILE}
}
main(){
if [ -f ${LOCK_FILE} ];then
echo "Deploy is running" && exit
fi
DEPLOY_METHOD=$1
ROLLBACK_VER=$2
case $DEPLOY_METHOD in
deploy)
shell_lock;
code_get;
code_config;
code_tar;
code_scp;
code_deploy;
code_test;
shell_unlock;
;;
rollback)
shell_lock;
rollback $ROLLBACK_VER;
shell_unlock;
;;
*)
useage;
;;
esac
}
main $1 $2
三、釋出測試
1、開發機和github新增ssh信任
[[email protected] ~]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+ta7tkljnIgqvgEugQvIfVxH/a+geX250ZhOpe+14Q8EBQSI+qGyXCeihln+0aQpzIERvNBZ7JjWCg5XeQlgPBgCmqoQKNTWl/NUBT+uY/NY9fIGdRCVBvVcDC554Be48zB57mtapKQEkqm/8kmq7sPRQDv98l5wvFvYOPxocmjnioDZr3GeYmgdFPNJ5WGg6yY29IHXgh2v3eCXLwX2Z2eUdKCpV1LS42wdAN8TqHFCEmthREIq2r86ZKPOovD6Micq7wa2yJqtA/hkv+DvEhRzOIVznfW5EptOyKYcittGu63JGMSbCr1uCdW7PLUQ8aIWDDlip+/EcIt0KkuJJ [email protected]
2、克隆專案到開發機進行開發測試
[[email protected] ~]$ mkdir dev && cd dev
[[email protected] dev]$ git clone [email protected]:kin08200/learngit.git
[[email protected] dev]$ ll
total 4
drwxrwxr-x 3 www www 4096 Nov 2 22:02 learngit
[[email protected] dev]$ cd learngit
3、修改index.html檔案
[[email protected] learngit]$ ll
total 8
-rw-rw-r-- 1 www www 0 Nov 2 04:31 123
-rw-rw-r-- 1 www www 35 Nov 2 22:02 index.html
-rw-rw-r-- 1 www www 80 Nov 2 04:31 readme.txt
[[email protected] learngit]$ echo "<h1> welcome to Beijing </h1>" > index.html
4、提交程式碼
[[email protected] learngit]$ git add .
[[email protected] learngit]$ git commit -m "modify index.html"
[[email protected] learngit]$ git push origin master
5、釋出程式碼
[[email protected] ~]$ ./deploy.sh deploy
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:kin08200/learngit
* branch HEAD -> FETCH_HEAD
Updating 9a781b2..683a440
Fast-forward
index.html | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
683a44
www_683a44_2018-11-03-10-06-56.tar.gz 100% 22KB 9.3MB/s 00:00
HTTP/1.1 200 OK
6、測試訪問
[[email protected] ~]$ curl 192.168.56.13
<h1>
welcome to Beijing!!!!
</h1>
7、回滾測試
[[email protected] ~]# ll /data/web/
total 0
lrwxrwxrwx 1 www www 43 Nov 3 10:46 www -> /webroot/www/www_683a44_2018-11-03-10-06-56
測試訪問如下,下面進行檢視版本列表,並選擇回滾到www_9a781b_2018-11-03-09-59-44版本
[[email protected] ~]$ ./deploy.sh rollback list
-rw-rw-r-- 1 www www 22686 Nov 3 10:07 /deploy/tmp/www_683a44_2018-11-03-10-06-56.tar.gz
-rw-rw-r-- 1 www www 22685 Nov 3 10:44 /deploy/tmp/www_683a44_2018-11-03-10-43-55.tar.gz
-rw-rw-r-- 1 www www 22222 Nov 3 09:48 /deploy/tmp/www_9a781b_2018-11-03-09-48-36.tar.gz
-rw-rw-r-- 1 www www 22231 Nov 3 09:52 /deploy/tmp/www_9a781b_2018-11-03-09-52-32.tar.gz
-rw-rw-r-- 1 www www 22228 Nov 3 09:58 /deploy/tmp/www_9a781b_2018-11-03-09-58-15.tar.gz
-rw-rw-r-- 1 www www 22234 Nov 3 09:59 /deploy/tmp/www_9a781b_2018-11-03-09-59-44.tar.gz
[[email protected] ~]$ ./deploy.sh rollback www_9a781b_2018-11-03-09-59-44
[[email protected] ~]# ll /data/web/
total 0
lrwxrwxrwx 1 www www 43 Nov 3 10:48 www -> /webroot/www/www_9a781b_2018-11-03-09-59-44
再進行訪問測試: