Automatically package and deploy docker and PHP environments based on Jenkins, ansible deployment ------- Day 86 of learning operation and maintenance from novice to master

The fourth stage of improvement

Time: August 23, 2023

Participants: All members of the class

Contents:

Deploy docker and PHP environments based on Jenkins

Table of contents

1. Environment deployment

(1) Experimental environment and server settings

(2) Turn off the firewall and selinux on all hosts, and modify the host name

(3) Configure git host

(4) Configure the jenkins host

(5) Jenkins configuration

2. Automatically package and deploy docker environment based on Jenkins

Tomcat host operation:

Result verification:

3. Automatically deploy PHP environment based on Jenkins

(1) Git server operation:

(2) Tomcat side operation:

(3) Jenkins operation:

Web access test:

4. Deploy PHP environment based on ansible

Jenkins operation:

Result test:


1. Environment deployment

(1) Experimental environment and server settings

IP address

CPU name

Role

192.168.100.131

git

git server

192.168.100.132

Jenkins git client

Ansible

jenkins server

192.168.100.133

tomcat,docker,httpd

web server

(2) Turn off the firewall and selinux on all hosts , and modify the host name

git host

Jenkins host

web host

(3) Configure git host

1. # rpm installation installation dependencies: both 1 and 2 are installed

[root@gitclient ~]# yum -y install curl-devel expat-devel gettext-devel  openssl-devel zlib-devel

2. # Compile and install—server and client, visit https://mirrors.edge.kernel.org/pub/software/scm/git/ to download the required version

[root@gitclient ~]# tar xf git-2.22.0.tar.gz -C /usr/src/

[root@gitclient ~]# cd /usr/src/git-2.22.0/

[root@git git-2.22.0]# make configure

[root@git git-2.22.0]# ./configure --prefix=/usr/local/git && make && make install

        Ps Note: As shown in the picture above, /bin/sh: autoconf: command not found make: *** [configure] error 127, what should I do?

Follow the prompts to install autoconf

[root@git git-2.22.0]# yum -y install autoconf

Then reconfigure, compile and install!

[root@git git-2.22.0]# ln -sf /usr/local/git/bin/git /usr/bin/

[root@git git-2.22.0]# git --version

3. #Configure users on the git server

[root@git ~]# useradd git

[root@git ~]# echo "123456" | passwd --stdin git

4. Create a local warehouse probe

[root@git ~]# su - git

[git@git ~]$ mkdir probe.git

[git@git ~]$ cd probe.git

[git@git probe.git]$ git --bare init

[git@git probe.git]$ exit

5. Clone the project code and synchronize it to the warehouse you created.

[root@git ~]# rz  #上传psi-probe.tar.gz

[root@git ~]# tar xf psi-probe.tar.gz

[root@git ~]# git clone [email protected]:/home/git/probe.git

[root@git ~]# cp -rf psi-probe/* probe/

[root@git ~]# cd probe/

[root@git probe]# git add .

[root@git probe]# git config --global user.email "[email protected]"

[root@git probe]# git config --global user.name "huyang"

[root@git probe]# git config --list

[root@git probe]# git commit -m "all probe"

[root@git probe]# git push origin master

(4) Configure the jenkins host

1. Install jenkins from source code

[root@jenkins ~]#rm -rf /usr/bin/java

[ root@jenkins ~]#tar xf jdk-17_linux-x64_bin.tar.gz

[ root@jenkins ~]#mv jdk-17.0.8 /usr/local/java

[root@jenkins ~]#vim /etc/profile

[root@jenkins ~]#source /etc/profile

[root@jenkins ~]#java -version

[root@jenkins ~]# rpm -ivh jenkins-2.401-1.1.noarch.rpm

2. Start jenkins

[root@jenkins ~]# jenkins start (keep it open)

The password is as shown in the picture:

[root@jenkins ~]# netstat -lnpt | grep :8080

3. Web configuration

Browser access test: http://192.168. 1 00.1 32 :8080

Enter the secret key:

Unlock Jenkins, password obtained from command line

 Enter the authorization password and click Next

Choose to install recommended plug-ins

Automatically download plug-ins (status is all green, next step)

The status is red, please restart and install again.

The status is red, and you can enter after restarting. However, there are plug-in installation errors, which will affect

After turning green, do the following:

Create an administrator user and choose to use the admin account to continue!

Instance configuration, save and done!

Click to get started

Ready to use!

(5) Jenkins configuration

1. Install the necessary plugins for Jenkins

On the Jenkins homepage, click Manage Jenkins>>Manage Plugins>>Optional plug-ins on the left, enter the Publish Over SSH  and Maven Integration plug-ins to be installed in the filter search box, and check the check box on the left, and click "Direct installation" can start the plug-in installation operation.

Check:

Bottom of the page:

2. Add verification credentials

You can add credentials here or not. This test does not involve credentials.

Operation as shown:

Operation as shown:

Operation as shown:

Operation as shown:

Operation as shown:

In the credentials page, click "system" to jump to the "System" page. Click "Add Domain" in the left navigation bar.

Jump to the "Add Domain" page. Create a domain name of "web" on this page and click "ok" to complete the configuration.

"Add Credentials"

Click "+Add Credentials"

After filling in the above data, click "OK" to view the newly added remote web host account, as shown below:

3. Add Publish Over SSH remote host

Click "Manage Jenkins" -> "Configure System" -> "Publish over SSH" -> "SSH Servers" -> "Add" option button on the Jenkins homepage to add an SSH remote host:

As shown in the figure, after entering necessary information such as Name, Hostname, Username, etc., click the " Advanced" option button -> Check the "Use Password authentication, or use a different key" option -> Enter the "Remote host login password" -> " Test Configuration" to test the remote host configuration. After successfully testing the remote host configuration, click the "Save" button.

4. Configure Maven, JDK, and Git environments

On the Jenkins homepage, click "Manage Jenkins" -> "Global Tool Configuration" -> "JDK" -> Add "JDK" and set the JDK alias to "JDK17" . Remove the "Install automatically" option and set "JAVA_HOME" to the actual installation path of the JDK in this case.

/usr/local/java

5. Install maven in Jenkins

[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz

[root@jenkins ~]# mv apache-maven-3.5.0 /usr/local/maven-3.5.0

6. Change the Alibaba Cloud mirror station for maven

[root@jenkins ~]# vim /usr/local/maven-3.5.0/conf/settings.xml

#Delete everything in the two <mirrors>, about 12 lines, and add the following Alibaba Cloud mirror station to the two <mirrors>

    <mirror>

        <id>nexus-aliyun</id>

        <mirrorOf>central</mirrorOf>

        <name>Nexus aliyun</name>

<url>http://maven.aliyun.com/nexus/content/groups/public</url>

</mirror>

final effect

Find the Maven configuration option in the "Global Tool Configuration" configuration interface, then click "Add Maven" and set the alias to " maven3.5.0 " .

7. Git configuration

[root@jenkins ~]# which git

/usr/bin/git

After the above global basic configuration is completed, click Save to complete.

2. Automatically package and deploy docker environment based on Jenkins

Tomcat host operation:

1. Install docker-ce

[root@tomcat ~]# mkdir /data

On the 192.168.100.133 machine, build the tomcat base image. Docker and JDK need to be installed before building the base image.

[root@tomcat ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@tomcat ~]# yum -y install yum-utils device-mapper-persistent-data lvm2

[root@tomcat ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@tomcat ~]# ls /etc/yum.repos.d/

[root@tomcat ~]# yum -y install docker-ce

[root@tomcat ~]# systemctl start docker

[root@tomcat ~]# systemctl enable docker

[root@tomcat ~]# docker version

2. Alibaba Cloud Image Accelerator

[root@tomcat ~]# cat << END > /etc/docker/daemon.json

{

        "registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ]

}

END

[root@tomcat ~]# systemctl daemon-reload

[root@tomcat ~]# systemctl restart docker

Download centos:7 image

[root@tomcat ~]# docker pull centos7

[root@tomcat ~]# docker images

3. Build tomcat base image 

[root@tomcat ~]# mkdir docker-tomcat

[root@tomcat ~]# cd docker-tomcat

[root@tomcat docker-tomcat]# ls

[root@tomcat docker-tomcat]# cat Dockerfile

[root@tomcat docker-tomcat]# docker build -t tomcat:v1  .

[root@tomcat docker-tomcat]# docker images

4. Build a Maven project

After the above configuration is completed, return to the Jenkins homepage, select " New task item ", then enter a task name " probe-docker ", select the " Maven project " configuration item, and click the "OK" button at the bottom of the current page.

After clicking the " OK " button, select " Source Code Management " and select " Git ", and set the " Repository URL " address.

[email protected]:/home/git/probe.git

Select " Build " -> clean package -Dmaven.test.skip=true

Select the " Send build artifacts over SSH " option in " Post-Build Actions " after the previous step and proceed.

Then enter the following:

name:web

Exec command

scp 192.168.100.132:/root/.jenkins/workspace/probe-docker/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

After all the above configurations are completed, click Save. Then click on the engineering task " probe-docker " -> " Build new " just created until the task is built. You can click on the progress bar to view the build process.

        You can see that this engineering task has been built successfully, and after the build, the command to create a Docker Web environment for the docker project has also been executed successfully.

Result verification:

[root@tomcat ~]# ls /data/

[root@tomcat ~]# docker ps -a

Web access test:

http://192.168.100.133:8090/probe/

3. Automatically deploy PHP environment based on Jenkins

(1) Git server operation:

1. Prepare git:

[root@git ~]# su - git

[git@git ~]$ mkdir php.git

[git@git ~]$ cd php.git

[git@git php.git]$ git --bare init

[git@git php.git]$ exit

2. Upload the code to the warehouse

[root@git ~]# git clone [email protected]:/home/git/php.git

3. Configure the test page

[root@git ~]# cd php/

[root@git php]# cat << EOF > index.php

<?php

phpinfo();

?>

EOF

4. Submit local and remote warehouses

[root@git php]# git add .

[root@git php]# git commit -m "all"

[root@git php]# git push origin master

[email protected]'s password:

(2) Tomcat side operation:

1. Deploy the web hosting environment

[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@web ~]# systemctl start httpd

[root@web ~]# systemctl start mariadb

Access test:

192.168.100.133:80

(3) Jenkins operation:

1. The Jenkins host publishes the key to the web host

[root@jenkins ~]# su -s /bin/bash jenkins

bash-4.2$ ssh-keygen

bash-4.2$ ssh-copy-id [email protected]

2. Deployment based on rsync

Create a Freestyle project

Select: git-->Enter Repository URL

[email protected]:/home/git/php.git

Enter the following in the build steps and click OK!

rsync -avz --delete * [email protected].100.133:/var/www/html/

Click build now and view the console output

View console output

Result verification:

[root@web ~]# ls /var/www/html/

[root@web ~]# cat /var/www/html/index.php

Web access test:

http://192.168.100.133

4. Deploy PHP environment based on ansible

Jenkins operation:

1. Download ansible software

[root@jenkins ~]# rpm -ivh epel-release-latest-7.noarch.rpm

[root@jenkins ~]# yum -y install ansible

[root@jenkins ~]# vim /etc/ansible/hosts

2. Modify the jenkins running user

[root@jenkins ~]# vim /etc/sysconfig/jenkins

[root@jenkins ~]# killall -9 java

[root@jenkins ~]# jenkins start

3. Add Ansible plug-in

Select manage jenkins-->plugins

Search and install ansible plugin

4. Create a new task and select item-->freestyle project

Named php-ansible

5. Send the key to the git repository and web client

[root@jenkins ~]# ssh-keygen

[root@jenkins ~]# ssh-copy-id [email protected]

[root@jenkins ~]# ssh-copy-id [email protected].100.133

Select: git-->Enter Repository URL

[email protected]:/home/git/php.git

Input content:

Host patten:web

Inventory : select file or host list

Enter: /etc/ansible/hosts

Modulesynchronize

Module arguments or command to execute
    src=${WORKSPACE} dest=/var/www/html rsync_opts=--exclude=.git

Finally, click "Save"

Click build now and view the console output

View console output

Result test:

[root@tomcat ~]# ls /var/www/html/

[root@tomcat ~]# ls /var/www/html/php-ansible/

[root@tomcat ~]#

cat    /var/www/html/php-ansible/index.php

Web access test:

http://192.168.100.133/php-ansible/

Guess you like

Origin blog.csdn.net/2302_77582029/article/details/132455079