(Project Description:)

1. Create a job chain of Job 1, Job 2, Job 3 and Job 4 using build pipeline plugin in Jenkins

1.使用Jenkins中的构建管道插件创建作业1,作业2,作业3和作业4的作业链

2. Job 1: Pull the Github repo automatically when some developers push the repo to Github.

2.工作1:当一些开发人员将仓库推送到Github时,自动拉出Github仓库。

3. Job 2 :

3.工作2:

  • By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
  • Expose your pod so that testing team could perform the testing on the pod
  • Make the data to remain persistent ( If server collects some data like logs, other user information )

4. Job 3: Test your app if it is working or not.

4.工作3:测试您的应用程序是否正常运行。

5. Job 4: if the app is not working, then send email to the developer with error messages and redeploy the application after code is being edited by the developer

5.工作4:如果应用程序无法正常工作,则将错误消息发送给开发人员,并在开发人员编辑代码后重新部署应用程序

(Pre-requisites for the practical :)

  1. Git Installed on Your System
    安装在系统上的Git
  2. A Github Repository should be there to commit the code or cloned in the local system.
  3. Create a post-commit hook in .git/hooks folder in Git for git push so that if a developer commits code then it automatically pushes to Github.
    在Git的.git / hooks文件夹中为git push创建一个提交后钩子 ,以便开发人员提交代码后自动将其推送到Github。
  4. Configure Kubernetes in your Rhel OS.
    在Rhel操作系统中配置Kubernetes
  5. Configure Jenkins in your Rhel OS.
    在Rhel OS中配置Jenkins

Note: Preferably, Perform the tasks on Linux Machine for smooth functioning and seamless installations ( I am using RHEL-8).

注意:最好在Linux Machine上执行任务,以实现平稳运行和无缝安装(我正在使用RHEL-8)。

(Stepwise Implementation:)

STEP 0: Load your Jenkins server and restart the services

步骤0:加载Jenkins服务器并重新启动服务

$ systemctl restart jenkins

STEP 1: Create a job chain of Job 1, Job 2, Job 3 and Job 4 using build pipeline plugin in Jenkins

步骤1:使用Jenkins中的构建管道插件创建Job 1,Job 2,Job 3和Job 4的工作链

Install Build plugin in Jenkins by going to Manage Jenkins → Manage plugins → Available → Search for build plugin, install and restart

通过转到管理Jenkins→管理插件→可用→在Jenkins中安装Build插件,搜索Build插件,安装并重新启动




jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_php


STEP 2: Create Job 1 which will pull the Github repo automatically when some developers push the repo to Github.

步骤2:创建作业1,当一些开发人员将仓库推送到Github时,它将自动拉出Github仓库。

  1. Select SCM as Git and provide the URL of GitHub repo which will help this job to pull the code from GitHub repo


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_php_02


2. In Build Trigger, select GitHub hook trigger which makes the job pull the code only when there are some changes made in GitHub repo

2.在“构建触发器”中,选择“ GitHub钩子触发器” ,这使作业仅在GitHub存储库中进行了一些更改时才拉代码


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_03


3. In Build, select Execute shell to perform following commands which will create a directory in the container and copy all the files from the GitHub repo to this directory

3.在Build中,选择Execute shell执行以下命令,这些命令将在容器中创建一个目录并将所有文件从GitHub存储库复制到此目录


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_04


STEP 3: Create a Job 2 which by looking at the code or program file automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes

步骤3:创建作业2,该作业通过查看代码或程序文件自动启动相应的语言解释器安装的图像容器,以在Kubernetes上部署代码

  1. In Build Triggers, select Build after other projects are built and write the name of job1 which makes the job2 to run only if job1 ran successfully


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_html_05


2. In Build, select Execute shell for performing this script which will automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes with persistent storage and also exposing the pod for testing

2.在Build中,选择Execute shell以执行此脚本,这将自动启动相应的语言解释器安装的图像容器,以在具有持久性存储的Kubernetes顶部部署代码,并公开Pod进行测试


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_06


3. Code for our HTML language interpreter installed pod which will launch a pod with persistent storage volume and also it will expose the pod

3.安装我们HTML语言解释器的Pod的代码,它将启动具有持久存储量的Pod,并且还将公开Pod

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: html-pvc
    labels:
        name: html-pvc
spec:
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            storage: 500Mi
---
apiVersion: v1
kind: Pod
metadata:
    name: html-pod
    labels:
        env: testing
        region: IN
        app: html
spec:
    containers:
    - name: myhtml-con
      image: vimal13/apache-webserver
      volumeMounts:
      - name: html-vol
        mountPath: /var/www/html
    volumes:
    - name: html-vol
      persistentVolumeClaim:
        claimName: html-pvc
---
apiVersion: v1
kind: Service
metadata: 
  name: html-svc
  labels:
    app: html
spec:
  type: NodePort
  ports:
  - name: http
    nodePort: 30002
    port: 81
    targetPort: 80
  selector:
    app: html
---

4. Code for our PHP language interpreter installed pod which will launch a pod with persistent storage volume and also it will expose the pod

4.安装了PHP语言解释器的Pod的代码,它将启动具有持久存储量的Pod,并且还将公开Pod

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: php-pvc
    labels:
        name: php-pvc
spec:
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            storage: 500Mi
---
apiVersion: v1
kind: Pod
metadata:
    name: php-pod
    labels:
        env: testing
        region: IN
        app: php
spec:
    containers:
    - name: myphp-con
      image: saurabh43800/apache-webserver-php:v4
      volumeMounts:
      - name: php-vol
        mountPath: /var/www/html
    volumes:
    - name: php-vol
      persistentVolumeClaim:
        claimName: php-pvc
---
apiVersion: v1
kind: Service
metadata: 
  name: php-svc
  labels:
    app: php
spec:
  type: NodePort
  ports:
  - name: http
    nodePort: 30001
    port: 81
    targetPort: 80
  selector:
    app: php
---

5. After job2 ran successfully we can see two pods launched and running with two persistent volume and also two services with NodePort Type

5.在job2成功运行之后,我们可以看到启动了两个Pod,并运行了两个持久卷,以及两个具有NodePort Type的服务。

$ kubectl get all


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_php_07


STEP 4: Create a Job 3 which will test your app if it is working or not and if the app is not working, then send email to the developer with error messages

步骤4:创建作业3,该作业将测试您的应用是否正常运行,以及应用是否正常运行,然后将包含错误消息的电子邮件发送给开发人员

  1. In Build Triggers, select Build after other projects are built and write the name of job2 which makes the job3 to run only if job2 ran successfully


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_08


2. In Build, select Execute shell for performing this script which will test the websites that they are running properly or not

2.在“构建”中,选择“执行外壳程序”以执行此脚本,该脚本将测试网站是否正常运行


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_html_09


3. In Post-build Actions, select E-mail notifications and write e-mail address to send mail whom you want to notify about the failure or any unstable build happened in the job while testing

3.在“构建后操作”中,选择“电子邮件通知”并编写电子邮件地址以发送您要通知其有关失败或测试期间作业中发生任何不稳定构建的邮件。


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_php_10


4. In Post-build Actions, select Editable E-mail notification to add extra functionalities in the e-mail like if you want to attach whole build log, send mail only for failure, unstable or always and add a subject, body, etc. in the mail

4.在“构建后操作”中,选择“可编辑的电子邮件通知”以在电子邮件中添加其他功能,例如,如果您要附加整个构建日志,仅针对失败,不稳定或始终发送邮件以及添加主题,正文等。 在邮件中

Note: Enable less secure apps option for your google account to send mail

注意:为您的Google帐户启用安全性较低的应用程序选项以发送邮件


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_11


STEP 5: Create a Job 4 which will monitor and redeploy the application after code is being edited by the developer if the application fails.

步骤5:创建作业4,如果应用程序失败,它将在开发人员编辑代码后监视并重新部署应用程序。

  1. In Build Triggers, select build after other projects are built so that this will run after job3 when it is successfully built and also select Build periodically so that it monitors the application every minute


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_12


2. In Build, select Execute shell to perform the following script so that it will continuously monitor if the pods are running or not and if fails it will make the job state as a failure

2.在“构建”中,选择“执行外壳程序”以执行以下脚本,以便它将连续监视Pod是否正在运行,如果失败,则它将使作业状态为失败


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_13


3. In Post-build Actions, select Trigger parameterized build on other projects so that if the current job fails or unstable then it will automatically run the job2 to redeploy the application

3.在构建后操作中,选择在其他项目上触发参数化构建,以便如果当前作业失败或不稳定,它将自动运行作业2以重新部署应用程序


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_html_14


STEP 6: Creating a Build Pipeline

步骤6:建立建立管道

  1. Create a new view with Build pipeline and give some name
  2. Select the initial job as Job1 (i.e devTask3_job1 in my case)


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_html_15


3. Run the build pipeline

3.运行构建管道


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_php_16


This is how Build pipeline looks like and all the jobs ran successfully!!

这就是构建管道的样子,所有作业都成功运行了!!

Final Output Pages from the Applications:

应用程序的最终输出页面:


jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_html_17

My HTML page from HTML Language Interpreter Pod 我HTML页面来自HTML Language Interpreter Pod

jenkins安装kubernetes插件总是不可用 jenkins kubernetes plugin_Jenkins_18

My PHP page from PHP Language Interpreter Pod 我PHP页面来自PHP Language Interpreter Pod

(GitHub Link:)

https://github.com/saurabhagarwal43800/devops_task3.git

https://github.com/saurabhagarwal43800/devops_task3.git


翻译自: https://medium.com/swlh/automating-kubernetes-with-jenkins-e6249bbcb3e