Centos7安装jenkins+docker部署tomcat
环境:centos7.5
硬件:gitlab:cpu=1,内存=2G
Jenkins:cpu=1,内存=1G
服务器:
10.125.7.20 node1
10.125.7.21 node2
10.125.7.22 node3
1、安装docker(每台执行)
- 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
- 关闭selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && setenforce 0
- 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
- 安装docker
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce -y
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s https://i9iblr0h.mirror.aliyuncs.com
systemctl restart docker
systemctl enable docker
2、安装gitlab(10.125.7.21)
- 拉取gitlab镜像
docker pull gitlab/gitlab-ce
- 启动gitlab容器
docker run --detach --hostname node2 --publish 443:443 --publish 80:80 --publish 2222:22 --name gitlab --restart always --volume ~/gitlab/config:/etc/gitlab --volume ~/gitlab/logs:/var/log/gitlab --volume ~/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest
- 创建用户
修改及添加用户密码
- 用新创建的用户登录gitlab并创建项目
- gitlab创建用户,组,项目操作等
可参考 - 在本地安装开发者工具idea最新版(一定要下载企业版,然后再破解)
然后在idea上创建springboot项目简单的写一个jsp并push到gitlab上,具体操作可自行百度,如果需要本地打包需在本地安装jdk,git,maven等
3、安装jenkins(10.125.7.22)
-
部署git和maven
yum install git -y
在http://maven.apache.org/download.cgi下载maven包
tar xf apache-maven-3.6.3-bin.tar.gz mkdir /opt/app/maven -pv mv apache-maven-3.6.3/* /opt/app/maven mkdir /opt/java && cd /opt/java && tar xf jdk-8u251-linux-x64.tar.gz
配置环境变量vim /etc/profile
export JAVA_HOME=/opt/java/jdk1.8.0_251 export M2_HOME=/opt/app/maven export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin source /etc/profile
-
安装jenkins并启动
配置yum源
vim /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins-stable baseurl=http://pkg.jenkins.io/redhat-stable gpgcheck=1 ## 安装jenkins yum install jenkins -y
-
配置jenkins使用
vim /etc/sysconfig/jenkins #监听端口 JENKINS_PORT="8080" #修改配置 $JENKINS_USER="root" vim /etc/init.d/jenkins 在candidates="下面添加 /opt/java/jdk1.8.0_251/bin/java ## 启动jenkins systemctl start jenkins
-
登录jenkins并简单进行配置
4、安装harbor私有仓库(10.125.7.20)
-
安装docker-compose
- 下载二进制文件
curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- 赋予二进制文件可执行权限
chmod +x /usr/local/bin/docker-compose
- 根据自己的情况决定是否安装命令补全功能
yum install bash-completion curl -L https://raw.githubusercontent.com/docker/compose/1.16.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
- 测试是否安装成功
docker-compose --version
-
安装harbor
- 下载
wget -P /usr/local/src/ https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-online-installer-v1.2.0.tgz
- 解压
cd /usr/local/src && tar zxf harbor-online-installer-v1.2.0.tgz -C /usr/local/
- 修改配置文件
cd /usr/local/harbor/ vim /usr/local/harbor/harbor.cfg hostname = 192.168.68.134 ui_url_protocol = https ssl_cert = /etc/certs/ca.crt ssl_cert_key = /etc/certs/ca.key secretkey_path = /data
- 生成证书
mkdir /etc/certs openssl genrsa -out /etc/certs/ca.key 2048 openssl req -x509 -new -nodes -key /etc/certs/ca.key -subj "/CN=10.125.7.20" -days 5000 -out /etc/certs/ca.crt
- 客户端配置(harbor客户端,10.125.7.22和10.125.7.21)
mkdir -pv /etc/docker/certs.d/10.125.7.20 scp /etc/certs/ca.crt root@10.125.7.21:/etc/docker/certs.d/10.125.7.20/ scp /etc/certs/ca.crt root@10.125.7.22:/etc/docker/certs.d/10.125.7.20/
- 客户端配置加速器
vim /etc/docker/daemon.json { "registry-mirrors": ["http://hub-mirror.c.163.com"], "insecure-registries": ["10.125.7.20"] } systemctl restart docker
- 执行安装
cd /usr/local/harbor && ./prepare ./install.sh
- 访问https://10.125.7.20/harbor/sign-in
默认账号密码: admin / Harbor12345 登录后修改密码
如果harbor-adminserver一直Restarting状态
解决办法:1、docker-compose down
2、rm -rf /data/secretkey
3、cd /usr/local/harbor && ./prepare
4、docker-compose up -d- 登录验证
5、jenkins上构建maven项目
先在jenkins服务器上安装ansible并与所部属服务器免密登录
10.125.7.21 jenkins
10.125.7.22 tomcat
安装并配置ansible(10.125.7.22)
yum install ansible
vim /etc/ansible/hosts添加以下配置
[tomcat]
10.125.7.22
[tomcat:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456
配置免密登录
ssh-keygen -t rsa -p
ssh-copy-id -i ~/.ssh/id_rsa.pub “10.125.7.22”
验证