One-click automated deployment (gray-release) Practice

 

 

In the wave of DevOps in the past few years, automation, continuous integration of these two concepts has long been popular (Internet technician). Mr. Bill Gates once have said: "The first rule of any technology used in a business is that automation applied to an efficient operation will magnify the efficiency of automation is the second application to an inefficient operation on. , magnify the inefficiency. "

Automated deployment is becoming the direction of small and medium enterprises to pursue, then, migrant workers brother today on automated deployment overview, automated deployment tools, process automation deployment, automated deployment practices four aspects, together with you to discuss, share what the issue of SMEs automatic deployment.

 

1, automated deployment overview

1.1 What is Automated Deployment

A simple phrase summed up: all of the deployment process is fully automated operation, without human manual intervention.

1.2 Benefits of automatic deployment

The traditional deployment as follows:

  • Operation and maintenance personnel manual use Scp, Xftp other ways to transmit data

  • Login server manually execute git pull, svn update commands to update the code of operation

  • Developers manual compiler package, and then to the operation and maintenance personnel through the network transmission

  • Operation and maintenance personnel uploaded by rz are uploaded to the target server, and then perform the renaming of the original package, copy the new package to the destination directory, then execute the command to restart the service application to complete the deployment process

It appears to be very simple, not a lot of trouble, but once the project is more frequently deployed, in which case it will greatly reduce efficiency. Colombian migrant workers before work had such experience, the company's activities up to 100+ projects, many of which are on-line and off the assembly line requires fast, or update, manual deployment really tired.

The traditional deployment has the following disadvantages:

  • The whole process requires personnel involved, take up a lot of time, inefficient

  • On the line, update, slow rollback

  • There is a certain confusion of management, increasing the chances of human errors

So, the advantages of automated deployment began to unravel in this comparison! !

 

2, automated deployment tools

The concept of automatic dynamic deployment, we need to deploy automated tools, today to introduce some aspects of this tool to everyone, how to use? How to use? We determined according to actual needs, not everything needs to be given the tools, processes and methods are bullying.

2.1 Jenkins 

Jenkins is an open source software project, continuous integration tool is based on a Java development for continuous monitoring duplication of work, it aims to provide an open and easy to use software platform that enables continuous integration software becomes possible. It should be said Jenkins continuous integration tool is one of the best use of its plug-in is very large, the installation is also very convenient, quite powerful, and flexible, the biggest drawback is the high cost of learning.

ElectricFlow 2.2 
ElectricFlow is a release automation tool that provides free community version, you can run on VirtualBox. ElecticFlow support a large number of plug-ins and Groovy-based DSL, CLI, APIs.
 
2.3 Microsoft Visual Studio
one of the cornerstones of Microsoft DevOps product is Visual Studio. Visual Studio allows user-defined version definition, automated operation, track versions, and so on.
 
Octopus Deploy 2.4
Octopus Deploy purpose is to create automated deployment of .NET applications. You can install or make Azure instances in a single server.
 
IBM UrbanCode 2.5
2013 was acquired by IBM company, to automate the deployment UrbanCode local or cloud environments.
 
AWS CodeDeploy 2.6
Amazon automated deployment tools CodeDeploy, has an impressive list of clients, platform-independent and language.
 
DeployBot 2.7
DeployBot can link any Git repository, and allows either manually or automatically deployed to multiple environments. DeployBot provide a lot of integration, including the ability to deploy by Slack.
 
Shippable 2.8
shippable container based on the provisions of the Docker their own "DevOps pillars" and their own CI platform, run relying called minions of.
 
TeamCity 2.9
TeamCity is a CI server from Jet Brains's. TeamCity intelligent configuration capabilities and has an official Docker mirror server and agent.
 
Bamboo 2.10
Bamboo Server is CI, provided by people from the Atlassian, they are Jira and Confluence maker. Bamboo released "integrations that matter" and provide a "small teams" package, donated to Room to Read charity.
 
CODAR 2.11
CODAR is a continuous deployment of the HP solutions. Deployment using Jenkins trigger.
 
CircleCI 2.12
CircleCI is a CI solution, emphasizing flexibility, reliability and speed. CircleCI provided from resources to create solutions to deploy and supports a number of languages and applications.
 
Gradle 2.13
Gradle is being used by some of the industry's most well-known such as LinkedIn, Netflix, Adobe and creation tools. Gradle use Groovy to create scripts, build the framework convention, and that at the same time as a tool to build a universal tool of Apache Ant is.
 
Automic 2.14
Automic try to apply the principle of DevOps give some back-end applications, allowing them to have, based on the same practical web applications benefit from the last few years a lot of front-end.
 
Distelli 2.15
Distelli specially deployed Kubernetes cluster anywhere, except can be used on any cloud or physical servers. According to TechCrunch article, Distelli in December 2015 received $ 2.8 million in funding by the former AWS staff Rahul Singh founded.
 
2.16 XL Deploy
applications from XebiaLabs XL Deploy is a publishing automation tools, support a large number of plug-ins and the environment, using agentless architecture.
 
Codeship 2.17
Codeship is CI server hosting solutions with native support for custom Docker.
 
2.18 GoCD
a CD server, emphasizing the visual workflow, GoCD is an open source project, sponsored by the ThoughtWorks development.
 
Capistrano 2.19
Capistrano is an open source deployment tools, Ruby developers use. "API rational, expressive." Capistrano documents with scripting languages and
 
2.20 Travis CI
Travis CI can be synchronized to your GitHub account, allowing automated testing and deployment. Travis CI is a free open source project.
 
BuildBot 2.21 
BuildBot is an open source framework based on continuous set of Python, claiming "the frame contains a battery." BuildBot canned oriented solutions use cases, there is not flexible enough.

 

3, automated deployment process

Probably process steps are as follows:

    • Get the code

    • Compiler package

    • Remove the target server

    • Extract the files to the target directory

    • Copy difference file

    • Restart Service

    • test

    • Rejoins the cluster

    • Continue with the next node or set of nodes

 

 

If a problem occurs during the test, you need to roll back to the previous stable version.

Generally you will need to roll back to the version listed first, and then remove the existing soft link files, re on a version of the source file to generate a soft link to the target directory, and then restart the service, automated testing, and ultimately join the cluster .

 

4, automated deployment practices

Having a bunch of theoretical stuff, the next operation is the need to practice, and also wrote an automated script before I, as shown below:

 

 

Here are two examples, which examples are provided by the two flying ice Sign posting Simon, specific examples are as follows:

 

4.1 Use gray shell script to achieve java release

Scripting Environment:

1, the operating system: centos 6.5 64 bit

2, the use of the code management gitlab

3, the code on each tag via the control line

4, the front end load balancing using haproxy using haproxy socat implemented on a smooth RS downline

5, WEB container using tomcat achieve

6, the project built using maven

Using a script Caution:
1, publishing machine needs to be able to resolve the host name of the web server, and configure ssh communications
2, variable directory and user information need to create, create your own script does not make a judgment. I am here is to use the ansible web server for deployment, the relevant directories and users will be automatically created.
3, the deployment of code using the tag, but the update code to use flexible connections to control, switch to roll back on a soft link
4 because java is a compiled language, we use maven to compile, so you need to install maven environment.
5, on the environment configuration file: The configuration file for their own maintenance manual, every time delete the git repository pull down the configuration file, copy the code file corresponding to the build environment directory is compiled.

Probably script code, follow these steps:

#!/bin/bash

# 设置时间变量
CTIME=$(date "+%Y-%m-%d-%H-%M")
# 项目名称,建议和gitlab仓库名称一致
project=
# 本地代码目录(gitlab拉取代码后存放目录)
CODE_DIR=/data/gitlab/"$project"
# 临时代码目录,用来修改配置文件和编译打包代码
TMP_DIR=/data/tmp/"$project"
# 用来存放war包
WAR_DIR=/data/war/"$project"
# 对应环境配置文件
deploy_conf=/data/conf/pro/"$project"/*
# 代码中的配置文件路径
local_conf=$TMP_DIR/src/main/resources/config
# 远程主机名称
REMOTE_HOST="tomcat-01 tomcat-02"
# 远程主机代码目录
REMOTE_CODE_DIR=/data/webapps/"$project"
# 远程主机用户
REMOTE_USER=root
# 远程主机war包存放目录
REMOTE_WAR_DIR=/data/war/
# 代码临时目录
CODE_TMP=/data/code_tmp/
# 上线日志
DEPKOY_LOG=/data/log/pro_log.log

# 脚本使用帮助
usage(){
echo $"Usage: $0 [deploy tag | rollback_list | rollback_pro ver]"
}

# 拉取代码
git_pro(){
if [ $# -lt 1 ];then
echo "请传入tag"
exit 1
fi
tag=$1
cd $CODE_DIR && git checkout master && git pull && git checkout $1
if [ $? != 0 ];then
echo "拉取代码失败"
exit 10
fi
cd $CODE_DIR && git pull 2>/dev/null >/dev/null
# 推送代码到临时目录
rsync -avz --delete $CODE_DIR/ $TMP_DIR/ 2>/dev/null >/dev/null
}

# 设置代码的配置文件
config_pro(){
echo "设置代码配置文件"
rm -f $local_conf/config.properties
.........
}

# 打包代码
tar_pro(){
echo "本地打包代码"
cd $TMP_DIR && /usr/local/maven/bin/mvn clean compile war:war && cp target/"$project".war "$WAR_DIR"/"$project"_"$tag"_"$CTIME".war
}

# 推送war包到远端服务器
rsync_pro(){
echo "推送war包到远端服务器"
for host in $REMOTE_HOST;do
scp "$WAR_DIR"/"$project"_"$tag"_"$CTIME".war $REMOTE_USER@$host:$REMOTE_WAR_DIR
done
}

# 解压代码包
solution_pro(){
echo "解压代码包"
for host in $REMOTE_HOST;do
ssh $REMOTE_USER@$host "unzip "$REMOTE_WAR_DIR""$project"_"$tag"_"$CTIME".war -d "$CODE_TMP""$project"_"$tag"_"$CTIME"" 2>/dev/null >/dev/null
done
}

# api测试
test_pro(){
# 运行api测试脚本,如果api测试有问题,则退出部署
if [ $? != 0 ];then
echo "API测试存在问题,退出部署"
exit 10
fi
}


# 部署代码
deploy_pro(){
echo "部署代码"
...................
sleep 3
# 执行api测试
test_pro
ssh haproxy "echo "enable server $project/$host" | /usr/bin/socat /var/lib/haproxy/stats stdio"
done
}
# 列出可以回滚的版本
rollback_list(){
echo "------------可回滚版本-------------"
ssh $REMOTE_USER@$REMOTE_HOST "ls -r "$CODE_TMP" | grep -o $project.*"
}

# 回滚代码
rollback_pro(){
   echo "回滚中"
   for host in $REMOTE_HOST;do
   .............................
   sleep 3
   ssh haproxy "echo "enable server $project/$host" | /usr/bin/socat /var/lib/haproxy/stats stdio"
   done
}

# 记录日志
record_log(){
   echo "$CTIME 主机:$REMOTE_HOST 项目:$project tag:$1" >> $DEPKOY_LOG
}

# 代码执行选项设置
main(){
   case $1 in
    deploy)
    git_pro $2;
    config_pro;
    tar_pro;
    rsync_pro;
    solution_pro;
    deploy_pro;
    record_log $2;
    ;;
    rollback_list)
    rollback_list;
    ;;
    rollback_pro)
    rollback_pro $2;
    record_log;
    ;;
    *)
    usage;
    esac
}
main $1 $2

 

4.2 Use shell php code to achieve automatic release

Script adapt to the environment:

1, the operating system: centos 6.5 64 bit

2, the use of the code management gitlab

3, the code on each tag via the control line and the rollback

NOTE: If you need me this deployment in your enterprise, but also need to have appropriate environmental norms and git branch management practices.

Using a script Caution:
1, publishing machine needs to be able to resolve the host name of the web server, and configure ssh communications
2, variable directory and user information need to create, create your own script does not make a judgment. I am here is to use the ansible web server for deployment, the relevant directories and users will be automatically created.
3, the deployment of code using the tag, the principle is the rollback rollback to the previous version of the tag, so the deployment script itself is not a backup code.
4. If you need to filter some temporary log directory or catalog, can push the code in rsync options when using -exclude filter, filter the sample script .git directory and config.php file is not deployed.

#!/bin/bash

# 设置时间相关变量
CTIME=$(date "+%Y-%m-%d-%H-%M")
# 项目名称,建议和gitlab仓库名称一致
project=test
# 本地代码目录(gitlab拉取代码后存放目录)
CODE_DIR=/data/gitlab/pro/$project/
# 远程主机
REMOTE_HOST="LNMP-01.fblinux.com LNMP-02.fblinux.com"
# 远程主机代码目录
REMOTE_DIR=/data/www/fblinux/
# 远程主机用户
REMOTE_USER=root
# 远程主机代码执行用户
CODE_USER=php
# 上线日志
DEPKOY_LOG=/data/log/pro_log.log

#脚本使用帮助
usage(){
echo $"Usage: $0 [deploy tag]"
}

#拉取代码
git_pro(){
if [ $# -lt 1 ];then
echo "请传入tag"
exit 1
fi
echo "拉取代码"
cd $CODE_DIR && git checkout master && git pull && git checkout $1
if [ $? != 0 ];then
echo "拉取代码失败"
exit 10
fi
cd $CODE_DIR && git pull
}

#推送代码服务器
rsync_pro(){
for host in $REMOTE_HOST;do
echo "推送代码到服务器$host"
rsync -rPv -P --delete --exclude="config.php" --exclude=".git" $CODE_DIR -e 'ssh -p 22' $REMOTE_USER@$host:$REMOTE_DIR
if [ $? != 0 ];then
echo "推送代码失败"
exit 10
fi
echo "代码授权"
ssh $REMOTE_USER@$host "chown -R $CODE_USER $REMOTE_DIR"
if [ $? != 0 ];then
echo "代码授权失败"
exit 10
fi
done
}

#记录日志
record_log(){
echo "$CTIME 主机:$REMOTE_HOST 项目:$project tag:$1" >> $DEPKOY_LOG
}

main(){
case $1 in
deploy)
git_pro $2;
rsync_pro;
record_log $2;
;;
*)
usage;
esac
}
main $1 $2

These are two examples of actual production deployment environment configuration, codes and other precautions and explain. Readers need to complete code of the two instances are requested to  road workers technologies brother  replies, "No public backstage automated deployment " to get Download the complete script code.

References are as follows:

https://dzone.com/articles/21-automated-deployment-tools-you-should-know 

http://www.fblinux.com/?p=489    

http://www.fblinux.com/?p=476

 

Guess you like

Origin www.cnblogs.com/youkanyouxiao/p/11537511.html