目录
基于Jenkins自动打包并部署docker环境
1、安装docker-ce
2、阿里云镜像加速器
3、构建tomcat 基础镜像
4、构建一个Maven项目
基于Jenkins自动化部署PHP环境
基于rsync部署
基于Jenkins自动打包并部署docker环境
1、安装docker-ce
在192.168.2.123 机器上,构建 tomcat基础镜像。在构建基础镜像之前需要先安装 Docker 与 JDK。
[root@docker ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo[root@docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo[root@docker ~]# ls /etc/yum.repos.d/
backup CentOS-Base.repo CentOS-Media.repo docker-ce.repo[root@docker ~]# yum -y install docker-ce
[root@docker ~]# systemctl start docker
[root@docker ~]# systemctl enable docker
[root@docker ~]# docker versionClient: Docker Engine - CommunityVersion: 24.0.5API version: 1.43Go version: go1.20.6Git commit: ced0996Built: Fri Jul 21 20:39:02 2023OS/Arch: linux/amd64Context: defaultServer: Docker Engine - CommunityEngine:Version: 24.0.5API version: 1.43 (minimum version 1.12)Go version: go1.20.6Git commit: a61e2b4Built: Fri Jul 21 20:38:05 2023OS/Arch: linux/amd64Experimental: falsecontainerd:Version: 1.6.22GitCommit: 8165feabfdfe38c65b599c4993d227328c231fcarunc:Version: 1.1.8GitCommit: v1.1.8-0-g82f18fedocker-init:Version: 0.19.0GitCommit: de40ad0
2、阿里云镜像加速器
centos包提取链接:https://pan.baidu.com/s/1QXYUjocx14PHGg9VsK-Utg?pwd=r346
提取码:r346
[root@docker ~]# cat << END > /etc/docker/daemon.json
{"registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ]
}
END
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker[root@tomcat ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:976f9f3fda9623a99264a1a57df6a028672d90d6016d8d26ccfbea7d00c5b444[root@tomcat ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 976f9f3fda96 21 seconds ago 589MB
3、构建tomcat 基础镜像
[root@tomcat ~]# mkdir docker-tomcat[root@tomcat ~]# mv apache-tomcat-8.5.40.tar.gz docker-tomcat/[root@tomcat ~]# mv jdk-8u191-linux-x64.tar.gz docker-tomcat/[root@tomcat ~]# cd docker-tomcat/[root@tomcat docker-tomcat]# ls
apache-tomcat-8.5.40.tar.gz jdk-8u191-linux-x64.tar.gz[root@tomcat docker-tomcat]# vim DockerfileFROM centos:7
MAINTAINER from crushlinux <crushlinux@163.com>#copy jdk and tomcat into image
ADD ./apache-tomcat-8.5.40.tar.gz /usr/local/
ADD ./jdk-8u191-linux-x64.tar.gz /usr/local#set variable
ENV JAVA_HOME /usr/local/jdk1.8.0_191
ENV PATH $JAVA_HOME/bin:$PATH#container starts up
ENTRYPOINT /usr/local/apache-tomcat-8.5.40/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.40/logs/catalina.out[root@tomcat docker-tomcat]# docker build -t tomcat:v1 .[+] Building 4.8s (8/8) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 508B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [internal] load metadata for docker.io/library/centos:7 0.0s=> CACHED [1/3] FROM docker.io/library/centos:7 0.0s=> [internal] load build context 0.1s=> => transferring context: 9.69MB 0.1s=> [2/3] ADD ./apache-tomcat-8.5.40.tar.gz /usr/local/ 0.2s=> [3/3] ADD ./jdk-8u191-linux-x64.tar.gz /usr/local 3.1s=> exporting to image 1.2s=> => exporting layers 1.2s=> => writing image sha256:a2828f5c15bd893c5f428dbb46cc816827cd73ff2610a7f99944b9308bb74a9d 0.0s=> => naming to docker.io/library/tomcat:v1 [root@tomcat docker-tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat v1 a2828f5c15bd About a minute ago 1e+03MB
centos 7 976f9f3fda96 14 minutes ago 589MB
4、构建一个Maven项目
在以上配置完成后,回到 Jenkins 首页,选择“新建任务”,然后输入一个任务名称 “probe-docker”,并选择“Maven project”配置项,点击当前页面下方的“确定”按钮。
在点击“确定”按钮,选择“源码管理”并选中“Git”,设置“Repository URL”地址。
选择“Build”-> clean package -Dmaven.test.skip=true
在上一步后面选择“构建后操作”中的“Send build artfacts over SSH”选项并进行。
scp 192.168.2.122:/root/.jenkins/workspace/proble/psi-probe-web/target/probe.war /data/
docker run -itd --name tomcat-test -p 8090:8080 -v /data:/usr/local/apache-tomcat-8.5.40/webapps tomcat:v1
以上全部配置完成后,点击保存即可。然后点击刚才创建的工程任务“probe-docker“->” Build new”直至任务构建完成。开始构建过程中可以点击进度条查看
可以看到此工程任务已构建成功,并且在构建后,创建用于docker项目的 Docker Web 环境命令也执行成功。
[root@tomcat ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b411b42687eb tomcat:v1 "/bin/sh -c '/usr/lo…" 4 minutes ago Up 4 minutes 0.0.0.0:8090->8080/tcp, :::8090->8080/tcp tomcat-test
基于Jenkins自动化部署PHP环境
准备git仓库
[root@git ~]# su - git上一次登录:四 8月 24 11:32:12 CST 2023从 192.168.2.122pts/3 上[git@git ~]$ mkdir php.git[git@git ~]$ cd php.git[git@git php.git]$ git --bare init已初始化空的 Git 仓库于 /home/git/php.git/[git@git php.git]$ exit登出
上传代码到仓库
[root@git ~]# git clone git@192.168.2.121:/home/git/php.git正克隆到 'php'...
git@192.168.2.121's password: #密码是123456
warning: 您似乎克隆了一个空仓库。[root@git ~]# cd php/[root@git php]# cat << EOF > index.php
> <?php
> phpinfo();
> ?>
> EOF[root@git php]# git add .[root@git php]# git commit -m "all"[master(根提交) 677669d] all1 file changed, 3 insertions(+)create mode 100644 index.php[root@git php]# git push origin mastergit@192.168.2.121's password: 枚举对象: 3, 完成.
对象计数中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 215 bytes | 215.00 KiB/s, 完成.
总共 3 (差异 0),复用 0 (差异 0)
To 192.168.2.121:/home/git/php.git* [new branch] master -> master
部署web主机环境
[root@tomcat ~]# yum install -y httpd mariadb-server mariadb mariadb-devel php php-mbstring php-mysql php-bcmath php-gd php-xmlrpc php-ldap php-xml libevent libevent-devel net-snmp net-snmp-devel libxml2 libxml2-devel ntpdate[root@tomcat ~]# systemctl start httpd
[root@tomcat ~]# systemctl start mariadb
Jenkins主机将密钥发布到web主机
[root@jenkins ~]# su -s /bin/bash jenkinsbash-4.2$ ssh-keygenbash-4.2$ ssh-copy-id root@192.168.2.123/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/jenkins/.ssh/id_rsa.pub"
The authenticity of host '192.168.2.123 (192.168.2.123)' can't be established.
ECDSA key fingerprint is SHA256:Obx7t3j/yRda0ptEZ8jPqaf0aMWNgwYhxuCTS+UaXNY.
ECDSA key fingerprint is MD5:bc:8b:84:8d:18:89:43:3f:2b:b8:fe:3c:02:29:e2:6d.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.2.123's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.2.123'"
and check to make sure that only the key(s) you wanted were added.
基于rsync部署
创建一个Freestyle project
rsync -avz --delete * root@192.168.2.123:/var/www/html/
[root@tomcat ~]# ls /var/www/html/index.php[root@tomcat ~]# cat /var/www/html/index.php <?php
phpinfo();
?>
基于ansible部署
epel包提取链接:https://pan.baidu.com/s/1Ss3dN23LtKlmCP5LKrmRmw?pwd=t9w6
提取码:t9w6
[root@jenkins ~]# rpm -ivh epel-release-latest-7.noarch.rpm [root@jenkins ~]# yum -y install ansible[root@jenkins ~]# vim /etc/ansible/hosts
[webserver]
192.168.2.123
修改jenkins运行用户
[root@jenkins ~]# vim /usr/lib/systemd/system/jenkins.service 34 User=root[root@jenkins ~]# jenkins start
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# ssh-copy-id git@192.168.2.121
[root@jenkins ~]# ssh-copy-id root@192.168.2.123
src=${WORKSPACE} dest=/var/www/html rsync_opts=--exclude=.git
[root@tomcat ~]# cat /var/www/html/proble-ansible/index.php
<?php
phpinfo();
?>