使用动态jenkins从节点和kubernetes进行实时滚动更新的Web服务器

The main motive of this article is to solve real-world problems. Can you imagine the loss of Instagram, not only loss but also hate of a million users it receives if it is even down by a few seconds, so nobody would want that, right?

本文的主要目的是解决现实世界中的问题。 您能想象Instagram丢失,不仅丢失,而且即使它下降了几秒钟,也会收到一百万用户的仇恨,所以没人会想要的,对吗?

It can be solved by using automatic rollout of updates using Kubernetes and managing the build triggers by Jenkins.

可以通过使用Kubernetes自动发布更新并由Jenkins管理构建触发器来解决。

So let us get going, on how to solve this major issue in this easy way.

因此,让我们开始,以简单的方式解决这个重大问题。

我们的系统要求: (Our system requirements:)

  • 2 RHEL 8 VM’s (1 as docker server with Jenkins configured and the other as docker client)

    2个RHEL 8 VM (1个为配置Jenkins的Docker服务器,另一个为Docker客户端)

  • Jenkins(it should be updated as some plugins might not work with the outdated version)should have the following plugins installed

    Jenkins (应进行更新,因为某些插件可能不适用于过时的版本)应安装以下插件

1. Build Pipeline Plugin

1.构建管道插件

2. Docker Plugin

2. Docker插件

3.GitHub Plugin

3.GitHub插件

  • We need to create a Dockerfile with kubectl configured so that this image will run as a slave for Jenkins as well as SSH configured

    我们需要创建一个Dockerfile与配置,以便kubectl这个图像将作为詹金斯从属运行以及配置SSH

  • Minikube configured and it is running( to start do minikube start )

    配置了Minikube并且它正在运行(要开始执行minikube start)

First of all, we need to set up socket binding so as to allow the docker services of this VM to be used by others also. For this, we need to enable TCP support also. This is done by adding -H tcp://0.0.0.0:1234 in /usr/lib/systemd/system/docker.service file in the docker server.

首先,我们需要设置套接字绑定 ,以允许其他人也使用此VM的docker服务。 为此,我们还需要启用TCP支持。 这是通过在Docker服务器的/usr/lib/systemd/system/docker.service文件中添加-H tcp://0.0.0.0:1234来完成的。

vim /usr/lib/systemd/system/docker.service
change configuration of the docker services 更改Docker服务的配置

Now restart your docker services using:

现在使用以下命令重新启动您的docker服务:

systemctl daemon-reload
systemctl restart docker

Now, a container image is created that has Linux and other basic configuration required to run Slave for Jenkins.

现在,将创建一个容器映像,该容器映像具有运行Slave for Jenkins所需的Linux和其他基本配置。

Step by step explanation of the Dockerfile are:

Dockerfile的分步说明如下:

  1. Use centos as the BaseOS

    使用centos作为BaseOS
  2. Install java

    安装java
  3. Install ssh

    安装ssh
  4. Install sudo command so as there are no issues in permissions

    安装sudo命令,以便权限中没有问题
  5. Set both the password and username as root, which will help in connect using ssh

    将密码和用户名都设置为root,这将有助于使用ssh进行连接
  6. Install the kubectl command

    安装kubectl命令
  7. Make the kubectl command executable

    使kubectl命令可执行
  8. Move the kubectl command to the /usr/local/bin location,so as to make it accessible from anywhere

    将kubectl命令移动到/ usr / local / bin位置,以便可以从任何地方访问它
  9. Create .kube directory in the /root folder

    在/ root文件夹中创建.kube目录
  10. Copy the config file, client.key,client.crt,ca.crt file to the /root/.kube directory. (The permission files are those of kubectl configured of your HostOS)

    将配置文件client.key,client.crt,ca.crt文件复制到/root/.kube目录。 (许可文件是您的HostOS配置的kubectl的许可文件)
kubernetes config file kubernetes配置文件

11. Create /root/yml and copy the deployment.yml file in it.

11.创建/ root / yml并在其中复制deployment.yml文件。

deployment.yml Deployment.yml

12. Generate the SSH key in the container and expose 22 port.

12.在容器中生成SSH密钥,并公开22端口。

Dockerfile with ssh and kubectl configured | shirsha30/sshd_cont:v12 配置了ssh和kubectl的Dockerfile | shirsha30 / sshd_cont:v12

The image can also be used from the docker hub:

该图像也可以从docker hub使用:

We also need to configure our cloud (Jenkins Master-Slave)

我们还需要配置我们的云(Jenkins Master-Slave)

To set up Jenkins as a Cloud server go to Jenkins>Manage Jenkins> Manage Nodes and Clouds> Configure Clouds. Now add Docker host details as follows:

要将Jenkins设置为云服务器,请转到Jenkins > 管理Jenkins>管理节点和云>配置云。 现在添加Docker主机详细信息,如下所示:

docker cloud 码头工人云
docker agent template docker代理模板
docker agent template configured 配置的Docker代理模板

So let us begin with Job1 and what all it will do:

因此,让我们从Job1开始,它会做什么:

  1. Jenkins will pull code from the GitHub repository and store it in its workspace using Poll SCM (set at intervals of 1 minute). Instead of using Poll SCM, you can also do the same using GitHub hook triggers and ngrok, to get a brief about how to do, refer to this article:

    Jenkins将从GitHub存储库中提取代码,并使用Poll SCM(以1分钟为间隔设置)将其存储在其工作区中。 除了使用Poll SCM之外,您还可以使用GitHub钩子触发器和ngrok进行相同的操作,以获取有关操作方法的简要信息,请参阅本文:

2. The developer has also pushed the Dockerfile with the image(shirsha30/httpd_server:latest) dynamically for the application and copy the application code(index.html) into that corresponding docker image.

2.开发人员还为应用程序动态推送了带有映像( shirsha30 / httpd_server:latest )的Dockerfile ,并将应用程序代码(index.html)复制到相应的Docker映像中。

shirsha30/httpd_server:latest / httpd_server:最新的Dockerfile
job1 configuration 1.0 job1配置1.0
job1 configuration 1.1 job1配置1.1
job1 configuration 1.2 job1配置1.2

The output of Job1:

Job1的输出:

Job1 output Job1输出

Job2:

职位2:

  1. Job 2 will run on the successful completion of Job1.

    作业2将在作业1成功完成后运行。
  2. This job deploys Webserver on top of the Kubernetes cluster on the Dynamic Jenkins Slave node.

    该作业将Web服务器部署在Dynamic Jenkins从节点上的Kubernetes集群的顶部。
Job2 configuration 1.0 Job2配置1.0
Job2 configuration 1.1 Job2配置1.1
Job2 configuration 1.2 Job2配置1.2

You might get this error:

您可能会收到此错误:

Possible error 可能的错误
  • This error is if your plugins or Jenkins are out of date.

    如果您的插件或Jenkins已过时,则会出现此错误。
  • Minikube should be running in the base system. To check if it is running, run the command:

    Minikube应该在基本系统中运行。 要检查它是否正在运行,请运行以下命令:
minikube ip

If it is not running, then start the services using

如果未运行,请使用以下命令启动服务

minikube start
  • Or it may be because there is an error in your config file of Kubernetes. (check the cluster information by launching and going manually inside the docker container). The file name should be “config”. To check run the below command:

    或者可能是因为您的Kubernetes配置文件中存在错误。 (通过在docker容器内启动和手动检查集群信息)。 文件名应为“ config”。 要检查,请运行以下命令:
kubectl config view

Now all the errors are debugged.

现在,所有错误都已调试。

Initially when the job starts building it shows

最初,当作业开始构建时,它会显示

pending- ‘Jenkins’ doesn’t have label docker-agent ->docker agent offline->docker agent launching

待处理-'Jenkins'没有标签docker-agent-> docker agent offline-> docker agent launching

Jenkins’ doesn’t have label docker-agent Jenkins没有标签docker-agent
->docker agent offline ->码头代理离线
->docker agent launching ->启动docker代理

The output of job2:

job2的输出:

The output showing container getting created, and then the port exposed. 输出显示正在创建容器,然后暴露了端口。
After 1st build 第一次构建后
The output showing rolling updates 输出显示滚动更新

Site updated using rollouts:

网站使用推广更新:

After rolling updates 滚动更新后

Build-pipeline view:

构建管道视图:

Build-pipeline view 构建管道视图

Refer the git repository for more information:

请参考git仓库以获取更多信息:

That’s all for everybody. Thank you!

这就是每个人的全部。 谢谢!

翻译自: https://medium.com/swlh/web-server-with-live-rolling-updates-using-dynamic-jenkins-slave-node-and-kubernetes-28b13b04731e

你可能感兴趣的:(java,web,运维,mysql,leetcode)