In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to construct a freely expandable Jenkins with Kubernetes". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to construct a freely expandable Jenkins by Kubernetes".
Stand-alone Jenkins server
In this mode, there is only one Jenkins server responsible for all the build tasks and deployed to the remote server using a TCP connection. This is also the easiest way, you don't have to worry about other variables at all.
Master-slave strategy
Running Jenkins in stand-alone mode has some disadvantages.
Although you don't need to worry about multiple servers and nodes in stand-alone mode, the server may be overloaded when a large number of build tasks are running at the same time. You may consider increasing the number of build tasks that nodes can perform concurrently, but you will soon encounter performance bottlenecks.
To solve this problem, you can distribute some of the tasks to other machines, that is, Jenkins slave nodes. The Jenkins slave node will run a program to communicate with the master node to determine that there are enough executable build tasks. Once the Jenkins master node has scheduled the build task, it is distributed to the corresponding slave node. So have we solved our problem? Keep looking down.
Scalable Jenkins
Let's take a closer look at how Jenkins works. When CI is not yet established on your team, you may not need multiple static servers to perform Jenkins tasks.
When you do not need to run 7: 24, your server may be idle, resulting in a waste of resources.
But if you are using container technologies such as Kubernetes, you can make the running architecture of Jenkins more advanced. To put it simply, let the master node dispatch the build task and distribute the task to the slave node for execution, but you do not need to generate the corresponding slave node in advance-when the slave node needs to be used, it will emerge as the times require.
This operational architecture can solve the following problems
Jenkins server performance is no longer an issue
When you run Jenkins in a Kubernetes cluster, Jenkins can be adjusted and started according to the cluster resource usage. Because many applications share a cluster, it saves resources effectively-in this case Jenkins does not run at the peak of resource usage.
If you deploy clusters in the cloud, such as Google Cloud Platform,Jenkins, it will be easier and more flexible to run. GKE can not only adjust itself according to the situation of the container, but also add or remove nodes according to the load of the cluster, so it has the ability to expand indefinitely.
Run build tasks in parallel
You no longer need to think carefully about the number of tasks executed concurrently, Jenkins will have a slave node ready to run related tasks.
Load balancing
Kubernetes can also achieve good load balancing, it will let the Jenkins slave node run on the most suitable server, because of this, the build task will run faster and more efficiently.
Self-repair
If you have a problem with your build task or slave node, you don't have to worry at all. Jenkins will automatically remove the problematic slave node and start a new slave node.
This saves a lot of troubleshooting time because each slave node does not have to exist, and if a slave node encounters an obstacle, Jenkins asks Kubernetes to remove it and start a new slave node. It's that simple.
Which operating architecture do we choose?
At first, we may use stand-alone mode to use Jenkins, there may be no problem in half a year, when more and more people use it and the load is getting higher and higher, eventually we all have to face the problem of load out of range.
When we are not using Kubernetes, the solution may be to build a master-slave architecture or add CPU and memory to the stand-alone server. We may choose the latter approach, because it is the most effective and direct way. However, this approach does not completely solve our problems.
How to solve this problem
After many attempts, our company CTO proposed cloud deployment, and we decided to deploy Jenkins to the cloud and combine it with the existing Kubernetes cluster. In addition, we also migrated other tools to the cloud. Isn't it easy? Let's check it out.
We have two plans. One is to run the master node in the stand-alone mode in the container, and the other is to use the Jenkins to run in the Kubernetes cluster. For the latter, we propose the following two-layer topology:
The Jenkins master node acts as a controller to manage user login and schedule construction tasks.
Use Kubernetes to start additional Jenkins slave node containers, where the user will trigger the task and run it. When the task is successfully executed, the container will be removed.
Let's take a look at how to configure it.
Create a Jenkins master node instance image
The Dockerfile used to create a master node instance image is as follows:
Version: "3" FROM jenkins/jenkins:centos # # Distributed Builds pluginsRUN / usr/local/bin/install-plugins.sh ssh-slaves # # install Notifications and Publishing pluginsRUN / usr/local/bin/install-plugins.sh email-extRUN / usr/local/bin/install-plugins.sh mailerRUN / usr/local/bin/install-plugins.sh slack # # ArtifactsRUN / usr/local/bin/install-plugins.sh htmlpublisher # # UIRUN / usr/local/bin/install-plugins.sh greenballsRUN / usr/ Local/bin/install-plugins.sh simple-theme-plugin # # ScalingRUN / usr/local/bin/install-plugins.sh kubernetes # # install MavenUSER rootRUN yum update-y & & yum install- y mavenUSER jenkins
Build an image
Docker build-t / jenkins-master:0.0.1.
Push the image to the central warehouse
Docker push / jenkins-master:0.0.1
This Dockerfile is simple and does not require any software installation, because the master node does not have to run any build tasks, but only manages each node. You can modify it according to your own needs.
Self-starting Jenkins master node to run the instance
Next, use the Kubernetes mainfest file to start the running instance of the Jenkins master node, with the following contents:
ApiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: jenkins name: jenkins namespace: default apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: jenkins-crb roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects:-kind: ServiceAccount name: jenkins namespace: default apiVersion: V1 kind: PersistentVolumeClaim metadata: name: jenkins-pv-claim labels: app: jenkins spec: AccessModes:-ReadWriteOnce resources: requests: storage: 30Gi apiVersion: apps/v1 kind: Deployment metadata: name: jenkins-deployment labels: app: jenkins spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: serviceAccountName: jenkins volumes:-name: jenkins-pv -storage persistentVolumeClaim: claimName: jenkins-pv-claim containers:-name: jenkins image: / jenkins-master:0.0.1 env:-name: JAVA_OPTS value:-Djenkins.install.runSetupWizard=false imagePullPolicy: Always ports:-containerPort: 8080-containerPort: 50000 volumeMounts: -mountPath: "/ var/jenkins_home" name: jenkins-pv-storage securityContext: privileged: true imagePullSecrets:-name: regcred initContainers:-name: volume-mount-data-log image: busybox imagePullPolicy: Always command: ["sh" "- c" VolumeMounts:-mountPath: "/ var/jenkins_home" name: jenkins-pv-storage apiVersion: v1 kind: Service metadata: name: jenkins-service labels: app: jenkins-svc spec: ports:-port: 8080 targetPort: 8080 protocol: TCP name: app-port: 50000 targetPort: 50000 protocol : TCP name: jnlp selector: app: jenkins apiVersion: extensions/v1beta1 kind: Ingress metadata: name: jenkins-ingress annotations: nginx.ingress.kubernetes.io/add-base-url: "true" nginx.ingress.kubernetes.io/proxy-body-size: "0" spec: rules:-host: jenkins.example.com http: paths:-path: / Backend: serviceName: jenkins-service servicePort: 8080-host: jenkinsmaster.example.com http: paths:-path: / backend: serviceName: jenkins-service servicePort: 50000
Manifest file mainly defines the following:
Add a service named Jenkins to the default namespace
Jenkins-crb, which binds the jenkins service to the cluster administrator cluster-admin, which allows the Jenkins master node to communicate with the Kubernetes cluster and perform some cluster management tasks such as starting or stopping pods
Jenkins-pv-claim, as a persistent data volume, can save Jenkins data of 30GB.
After the running instance of Jenkins master node is started, the exposed ports of Jenkins service and jenkins-pv-claim data volume are 8080 and 50000, respectively.
Explain the IP of the Jenkins master node in the cluster
Set the URL provided by Jenkins
After the above configuration takes effect, you can choose the load balancing method or the slave node operation mode, or you can change it as needed, and then use kubectl apply-f to make the file take effect. You can then access Jenkins in your browser via Jenkins.example.com (or self-defined url).
Build to run instance image from node
Unlike master nodes, slave node images do not manage your build tasks but simply execute them. Therefore, we need to ensure that the slave node meets all the requirements for operation.
Since the applications we are building are micro-services running on containers, we need to install docker on Jenkins's slave node server so that we can run docker on the slave node.
The Dockerfile used is as follows:
FROM jenkins/jnlp-slave # # install MavenUSER rootRUN apt update-yRUN apt install-y apt-transport-https ca-certificates curl gnupg2 software-properties-commonRUN curl-fsSL https://apt.dockerproject.org/gpg | apt-key add- RUN apt-add-repository "deb https://apt.dockerproject.org/repo debian-$ (lsb_release-cs) main" RUN apt-get updateRUN apt-cache policy docker-engineRUN apt-get install-y docker-engine=1.13.1-0~debian-stretchVOLUME / root/.dockerRUN Cd / usr/bin & & curl-LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/amd64/kubectlRUN chmod + x / usr/bin/kubectlCOPY config / root/.kube/RUN wget-O / root/helm.tar.gz https://get.helm.sh/helm-v2.15.2-linux-amd64.tar.gzRUN cd / root & & tar-zxvf helm.tar.gzRUN cd / root & & mv / root/linux-amd64/helm / usr / bin/helmRUN chmod + x / usr/bin/helmRUN cp-a / usr/bin/helm / usr/local/bin/helmRUN helm init-- upgradeUSER root
Build an image
Docker build-t / jenkins-slave.
Push the image to the central warehouse
Docker push / jenkins-slave
Configure Kubernetes clusters on Jenkins
After the running image of the slave node has been pushed to the central repository, the next step is to set up on the Jenkins how to start a running container with Kunernetes once a build task is triggered. The main steps are divided into the following two steps:
Get URL or Kunernetes API via kubectl cluster-info | grep master
The configuration in Jenkins's cloud configure interface [your_jenkins_url] / configureClouds/) is as follows:
Finally, remember to save the above settings.
Disable the primary node actuator
To ensure that the primary node only acts as a controller and does not perform specific build tasks, we need to set the executors of the primary node to 0. 0.
Visit [your_jenkins_url] / computer/ (master) / configure, and follow the figure below to instruct executors to be set to 0 and save
Test whether the configuration is in effect
Create a free-style task, name it job-1, and configure it according to the following steps
Create a task that is exactly the same as job-1, named job-2
Start two tasks at the same time, and you will notice that both build tasks generate a new pod in kubernetes and execute it at the same time.
View the output of two tasks in the console:
At this point, I believe you have a deeper understanding of "Kubernetes how to construct a freely expandable Jenkins". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.