环境准备:
安装centos7.1系统,最小化安装
设置好网络和防火墙 网络需要能访问外网,
ip 192.168.1.7
下面关闭防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/g' /etc/selinux/config
设置yum源
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install epel-release
安装应用:docker+git+maven+jenkins
安装docker
yum install docker -y
修改docker配置
vi /etc/sysconfig/docker
#OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
#if [ -z "${DOCKER_CERT_PATH}" ]; then
# DOCKER_CERT_PATH=/etc/docker
#fi
OPTIONS='--insecure-registry=192.168.1.7:5000'
启动docker
systemctl start docker
systemctl enable docker
创建镜像仓库
docker pull registry
docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --name registry registry
docker pull tomcat:7.0.70-jre7-alpine
安装git和maven
yum install git -y
yum install maven -y
配置git用户
cd ~
touch .git-credentials
vi .git-credentials
http://root:[email protected]
http://admin:[email protected]
http://admin:[email protected]
git config --global credential.helper store
git config --global user.name root
git config --global user.email [email protected]
git config --list
建立git项目mybank-demo-maven
cd /opt/ #新建初始化项目
git init --bare mybank-demo-maven.git
然后
cd /tmp/
git clone [email protected]:/opt/mybank-demo-maven.git #注意用户密码
cd mybank-demo-maven
git clone https://github.com/nichochen/mybank-demo-maven #下载GitHub上的项目
mv mybank-demo-maven/* .
rm -rf mybank-demo-maven
创建Dockerfile
vi Dockerfile
FROM tomcat:7.0.70-jre7-alpine
ADD ./target/ROOT.war /usr/local/tomcat/webapps/mybank.war
[root@gluster01 mybank-demo-maven]# ll 总用量 16 -rw-r--r--. 1 root root 90 1月 4 00:33 Dockerfile -rw-r--r--. 1 root root 830 1月 3 19:53 pom.xml -rw-r--r--. 1 root root 181 1月 3 19:53 README.md drwxr-xr-x. 3 root root 4096 1月 3 19:53 src
将文件提交到git
git add pom.xml
git add README.md
git add src
git add Dockerfile
git commit -m 'first commit'
git push origin master
安装jenkins
cd /etc/yum.repos.d/
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins -y
yum install java -y
yum install net-tools
修改端口为8081
vi /etc/sysconfig/jenkins
JENKINS_PORT="8081"
启动jenkins
/etc/init.d/jenkins start
在浏览器中输入:http://192.168.1.7:8081,效果如下
按照提示执行
cat /var/lib/jenkins/secrets/initialAdminPassword 得到Administrator password
接着设置账号密码
比如 admin:admin root:123456
进入主页面
登陆jenkins, 进入系统管理-插件管理-可选插件 搜索ssh plugin 和 Maven Integration plugin 然后安装, 安装界面选择安装好后重启jenkins
接着进入系统管理--系统设置
在系统设置中设置ssh
在上步中Credentials add 添加用户密码 root :123456
并接着添加jenkins的url配置
系统设置的其他项可以保持不动,保存上面的修改
下面进行全局工具配置
配置其中的 jdk git maven
yum安装的java
用命令查找路径
跟踪软连接
ll /usr/bin/javac
ll /etc/alternatives/javac
ll /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/bin/javac
jdk的home就是/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/
git的是命令路径
maven和docker
用命令 mvn -v 查看home路径
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"
新构建maven项目 名称为mybank
接着
下面git源码和add密码 root:123456
增加构建后操作
其中的num镜像数要多一个,要多tag一个有5000/mybank-tomcat辨识的镜像,第一次打包,要tag一个,以便于打包后是准确的v1版本
cd /var/lib/jenkins/workspace/mybank/
num=`docker images|grep "5000/mybank-tomcat"|wc -l`
docker build -t mybank-tomcat:v$num .
docker tag mybank-tomcat:v$num 192.168.1.7:5000/mybank-tomcat:v$num
docker push 192.168.1.7:5000/mybank-tomcat:v$num
docker run -d -p 8080:8080 mybank-tomcat:v$num
配置完成后保存
执行构建;
执行构建前,去服务器修改maven仓库路径 最好阿里云的镜像,增加如下配置,飞速下载
vi /etc/maven/settings.xml
alimaven central aliyun maven http://maven.aliyun.com/nexus/content/repositories/central/
构建成功并做成镜像后上传到docker仓库,最后启动镜像服务,日志截图
在服务器上查看生成情况
[root@gluster01 conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.1.7:5000/mybank-tomcat v1 783463c71415 5 hours ago 150 MB
mybank-tomcat v1 783463c71415 5 hours ago 150 MB
[root@gluster01 conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50769a5283c8 mybank-tomcat:v1 "catalina.sh run" 5 hours ago Up 5 hours 0.0.0.0:8080->8080/tcp sleepy_kowalevski
8fb688b7453d registry:latest "/entrypoint.sh /etc/" 2 days ago Up 6 hours 0.0.0.0:5000->5000/tcp registry
镜像服务正常,用浏览器访问服务正常
到此 一般的基础流程已经完成
如有兴趣可在git中添加秘钥认证