1. 程式人生 > >Jenkins + docker + docker-compsoe + svn 持續整合

Jenkins + docker + docker-compsoe + svn 持續整合

開發十年,就只剩下這套架構體系了! >>>   

一  架構規劃

實驗環境是通過SVN提交程式碼,使用Jenkins一鍵釋出到測試環境進行映象build和站點安裝測試,將DOCKER映象上傳到 私有映象倉庫,最終通過jenkins一鍵釋出到正式環境

環境規劃:

192.168.157.130  Jenkins  svn  registry

192.168.157.131  測試節點  docker

192.168.157.132  正式節點  docker

二 192.168.157.130 上Jenkins SVN registry安裝

1 svn安裝

yum install -y subversion
mkdir /home/svn
#建立倉庫目錄
svnadmin create /home/svn/repos

vi /home/svn/repos/conf/svnserve.conf
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz

vi /home/svn/repos/conf/passwd
	svnuser = 123456

vi /home/svn/repos/conf/authz
	[repos:/]
svnuser = rw

svnserve -d -r /home/svn
ss -anltup | grep svn
tcp    LISTEN     0      7         *:3690                  *:*                   users:(("svnserve",pid=10888,fd=3))
倉庫地址:svn://192.168.157.130/repos

使用客戶端連線並上傳一個測試的index.html檔案,檔案內容為 hello world!

2 安裝Docker

2.1 Docker安裝(所有節點)

cd /etc/yum.repos.d
wget https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable docker

2.2 私有映象倉庫安裝

docker run -d \
-v /opt/registry:/var/lib/registry \
-p 5000:5000 \
--restart=always \
--name registry \
Registry

2.3 所有節點 修改配置檔案使之Pull映象到我們的私有倉庫

在部署節點配置Docker可信任私有倉庫:

vi /etc/docker/daemon.json
{"registry-mirrors": ["http://04be47cf.m.daocloud.io"],"insecure-registries":["192.168.157.130:5000"]}


systemctl daemon-reload 
systemctl restart docker  

2.4測試 是否可以訪問

curl http://192.168.157.130:5000/v2/_catalog

{"repositories":[]} 說明成功

3 安裝 Jenkins

3.1安裝包下載地址:

http://mirrors.jenkins.io/war-stable/
http://maven.apache.org/download.cgi
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

3.2 安裝jdk 和 maven

tar xf jdk-8u45-linux-x64.tar.gz
tar xf apache-maven-3.5.0-bin.tar.gz
mv jdk1.8.0_45/ /usr/local/jdk1.8
mv apache-maven-3.5.0 /usr/local/maven3.5

vim /etc/profile
JAVA_HOME=/usr/local/jdk1.8
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
MAVEN_HOME=/usr/local/maven3.5
PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
export JAVA_HOME CLASSPATH MAVEN_HOME PATH

source /etc/profile

檢視java版本
java -version

3.3安裝啟動Tomcat

tar zxvf apache-tomcat-8.0.46.tar.gz
cd apache-tomcat-8.0.46/webapps
rm -rf  ROOT/*
unzip /root/jenkins.war -d ROOT

#啟動tomcat
../bin/startup.sh
Using CATALINA_BASE:   /root/apache-tomcat-8.0.46
Using CATALINA_HOME:   /root/apache-tomcat-8.0.46
Using CATALINA_TMPDIR: /root/apache-tomcat-8.0.46/temp
Using JRE_HOME:        /usr/local/jdk1.8
Using CLASSPATH:       /root/apache-tomcat-8.0.46/bin/bootstrap.jar:/root/apache-tomcat-8.0.46/bin/tomcat-juli.jar
Tomcat started. 
說明Tomcat已經啟動

3.4 配置Jenkins

Jenkins 訪問地址:

http://192.168.157.130:8080

檢視密碼

cat /root/.jenkins/secrets/initialAdminPassword
4a5be2f79ecd4dd5b2e713db64a2a834

登陸進去後 選擇自定義安裝外掛

增加管理員賬號

Jenkins 配置

系統管理->系統設定:主要配置workspace目錄,全域性環境變數,郵件通知,其他外掛配置等。

系統管理->Global Tool Configuration:主要配置JDK、Maven等工具。

在系統設定裡面先配置好SSH連線各個部署節點資訊,在建立專案中使用。

建立 ssh的連線憑據: 作用 釋出版本需元遠端連線 伺服器 是ssh的使用者和密碼

系統設定

全域性工具配置

三 私有映象製作

製作nginx 映象 和 php 映象並上傳到私有倉庫,mysql映象使用官方映象即可

Nginx 和 php 的相關Dockerfile 檔案和 軟體包,配置檔案放在一個名為Dockerfile-lnmp的資料夾中

目錄結構

1 Nginx映象製作

1.1 Nginx 的Dockerfile檔案內容:

cat Dockerfile 
FROM centos:6
MAINTAINER zzzz
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel && \
    yum clean all
ADD nginx-1.12.1.tar.gz /tmp
RUN cd /tmp/nginx-1.12.1 && \
    ./configure --prefix=/usr/local/nginx && \
    make -j 2 && make install && \
    rm -rf /tmp/nginx-1.12.1
COPY nginx.conf /usr/local/nginx/conf

EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

1.2 Nginx 配置檔案設定

cat nginx.conf 

user  root; 
worker_processes  auto; 
error_log  logs/error.log  info;
pid        logs/nginx.pid; 

events {
    use epoll; 
}
http {

    include       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 logs/access.log main;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 80;
        server_name localhost;
        root html;
        index index.php index.html;

        location ~ \.php$ {
            root html;
            fastcgi_pass php-cgi:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
            include fastcgi_params;
        }
    }
}

1.3 build nginx映象

docker build -t 192.168.157.130:5000/lnmp-nginx:base . 

1.4 上傳映象到私有倉庫

docker push 192.168.157.130:5000/lnmp-nginx:base

2 php映象製作

2.1 Php 的Dockerfile檔案內容:

cat Dockerfile 

FROM centos:6
MAINTAINER zzzz
RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel && \
    yum clean all
ADD php-5.6.31.tar.gz /tmp/
RUN cd /tmp/php-5.6.31 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-mysql --with-mysqli \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-iconv \
    --enable-fpm --enable-zip --enable-mbstring && \
    make -j 4 && make install && \
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
    sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf && \
    cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
    chmod +x /etc/init.d/php-fpm && \
    rm -rf /tmp/php-5.6.31 
COPY php.ini /usr/local/php/etc

EXPOSE 9000
CMD /etc/init.d/php-fpm start && tail -F /var/log/messages

2.2 php.ini 檔案內容都是預設的設定未做改變(可以直接從其他的php伺服器中拷貝)

2.3 build php私有映象

docker build -t 192.168.157.130:5000/lnmp-php:base

2.4 上傳映象到私有倉庫

docker push 192.168.157.130:5000/lnmp-php:base

四 在測試節點上和正式節點上部署Docker-Compose

1 安裝Docker-compose

curl -L https://github.com/docker/compose/releases/download/1.22.0-rc2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose

docker-compose -version
docker-compose version 1.22.0-rc2, build 6817b533