1、jenkins+rancher+harbor 安装

  1. jenkins安装
    jenkins安装
    安装成功后要安装一些插件如maven ,git,svn 等
  2. rancher安装
    rancher安装说明
  3. harbor安装
    安装说明
    至此所需环境安装完毕
    jenkins构建项目然后向harbor推送镜像文件。
    rancher 创建服务 从harbor拉去镜像
    jenkins 构建项目的 脚本
#!/bin/bash
source /etc/profile

##set color##
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }
##set color##

# 设置变量
project="contract"
version=`date +%Y%m%d%H%M%S`
port=2001
harbor_user="admin"
harbor_pwd="Harbor12345"
harbor_host="192.168.3.194:8100"
echo ----------------------------------------------

# mvn打包
cd /var/lib/jenkins/workspace/contract1
mvn clean install -DskipTests=true
if [ $? -eq 0 ];then
    echoGreen "mvn package is success!"
else
    echoRed "mvn package is Failed!" && exit 1
fi    
sleep 2

# 进入target目录
 cd /var/lib/jenkins/workspace/contract1/target/ && mv com.logimis.ContractService-1.0-SNAPSHOT-exec.jar app.jar

# 创建docker镜像
cat > run.sh << EOF
#!/bin/bash
source /etc/profile
java -jar /opt/app.jar --server.port=${port}
EOF
chmod +x run.sh
cat >Dockerfile << EOF
#FROM 192.168.3.194:8100/public/jdk:1.8
FROM java:8
MAINTAINER wc <123@qq.com>
ENV LANG en_US.UTF-8
ADD   app.jar /opt
ADD   run.sh  /
EXPOSE 9001
ENTRYPOINT [ "sh", "-c", "/run.sh" ]
EOF



# 编译镜像
echoGreen "开始构建本次镜像!"
docker build -t $harbor_host/contract/$project:$version .
[ $? != 0 ] && echoRed "请注意,打镜像时出错,故而退出!" && exit 1

# 登录远程Harbor仓库
echoGreen "开始登录远程Harbor仓库!"
docker login -u $harbor_user -p $harbor_pwd $harbor_host
[ $? != 0 ] && echoRed "请注意,登录Harbor时出错,故而退出!" && exit 1

# 上传到docker私服
echoGreen "开始push新镜像到私服!"
docker push $harbor_host/contract/$project:$version
[ $? != 0 ] && echoRed "请注意,在执行push上传时出错,故而退出!" && exit 1
docker rmi $harbor_host/contract/$project:$version

# 更新镜像 这块还有点问题
echoGreen "开始将新镜像部署到远端!"
rancher kubectl set image deployment/$project $project=192.168.3.194:8100/contract/$project:$version -n contract
[ $? != 0 ] && echoRed "请注意,在执行镜像更新时出错,故而退出!" && exit 1
echoGreen "部署完成!"

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐