This is the third day of my participation in the August Text Challenge.More challenges in August

We read the world wrong and say that it deceives us. — Tagore “Stray Birds”

Summary of a.

Before we can learn how to use a tool, we need to know how to install it. This article focuses on installing and using Jenkins in K8S.

Open source tools describe The official documentation Official Installation Documentation Docker installation
jenkins Devops continuous integration tool Jenkins’s official website Jenkins Quick installation Docker installation

The table above lists the official installation address. If you need a quick experience, it is recommended to use Docker directly to install the application, and a single command can start the application:

docker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts-jdk11
Copy the code

This paper introduces the installation and use of Jenkins in K8S, mainly through two ways of installation practice:

  • Write your ownyamlFiles installed
  • Installation using helm

Installation environment

Minikube is used for the installation, which is basically the same in a K8S cluster

  • Minikube: v1.18.1
  • Helm: v3.5.3

2. CustomizationyamlFile installation Jenkins

Since Jenkins needs to persist data, we need to create PVC. It is recommended to use storageClass to create PVC dynamically. There is a default storageClass in minikube named: standard, which can be checked by using the following command:

# kubectl get sc
NAME                 PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
standard (default)   k8s.io/minikube-hostpath   Delete          Immediate           false                  50m
Copy the code

Use can view website: about storageClass kubernetes. IO/useful/docs/con…

Create the jenkins-deploy.yaml file with the following contents:

# # # # # # # # # # # # # # # use storageClass create PVC # # # # # # # # # # # # # # # # # # #
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-data-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  # specify the storageClass name, using the default minikube standard
  storageClassName: "standard"
  resources:
    requests:
      storage: 10Gi

# # # # # # # # # # # # # # # to create a ServiceAccount name: Jenkins - admin# # # # # # # # # # # # # # # # # # #
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin
  namespace: default
  labels:
    name: jenkins

# # # # # # # # # # # # # # # binding Jenkins - admin account for the cluster administrator role, in order to control permissions suggestion binding custom roles # # # # # # # # # # # # # # # # # # #
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins-admin
  labels:
    name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins-admin
    namespace: default
roleRef:
  kind: ClusterRole
  # cluster-admin is the default administrator role in a K8S cluster
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io


# # # # # # # # # # # # # # # in the default namespace to create deployment # # # # # # # # # # # # # # # # # # #
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      K8s 1.21.x serviceAccount is renamed serviceAccountName
      Enter the name of the serviceAccount created above
      serviceAccount: jenkins-admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts-jdk11
          imagePullPolicy: IfNotPresent
          env:
            - name: JAVA_OPTS
              value: -Duser.timezone=Asia/Shanghai
          ports:
            - containerPort: 8080
              name: web
              protocol: TCP
            - containerPort: 50000
              name: agent
              protocol: TCP
          resources:
            limits:
              cpu: 1000m
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          readinessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          volumeMounts:
            - name: jenkinshome
              mountPath: /var/jenkins_home
      volumes:
        - name: jenkinshome
          persistentVolumeClaim:
            claimName: jenkins-data-pvc

# # # # # # # # # # # # # # # in the default namespace to create service # # # # # # # # # # # # # # # # # # #
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: default
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: ClusterIP
  ports:
    - name: web
      port: 8080
      targetPort: 8080


---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-agent
  namespace: default
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: ClusterIP
  ports:
    - name: agent
      port: 50000
      targetPort: 50000

Copy the code

Deploy Jenkins using the following command:

# kubectl apply -f jenkins-deploy.yaml

persistentvolumeclaim/jenkins-data-pvc created
serviceaccount/jenkins-admin created
clusterrolebinding.rbac.authorization.k8s.io/jenkins-admin created
deployment.apps/jenkins created
service/jenkins created
service/jenkins-agent created
Copy the code

Temporarily expose the service port using the following command:

kubectl port-forward service/jenkins 8080:8080 -n default
Copy the code

Production environments recommend using ingress to expose services by domain name

After the service port is exposed, you can access http://localhost:8080

Use the following command to view the administrator password:

# kubectl get pod -n default
NAME                       READY   STATUS    RESTARTS   AGE
jenkins-68666b56fc-p8fvd   1/1     Running   0          8m28s

# kubectl exec jenkins-68666b56fc-p8fvd -- cat /var/jenkins_home/secrets/initialAdminPassword
b06be4420bcd4a02ab4968ab02838986
Copy the code

After a successful login, you need to install the plug-in:

The reason for not installing the recommended plug-in here is that we do not need many plug-ins, and the default plug-in download will be from abroad, which is relatively slow. Later, we will introduce the configuration of domestic download address.

Click Install to create the first administrator user:

You can continue using the admin account based on the actual situation.

Install Jenkins using helm

Go to the helm’s official package management repository to find the application to install.

Helm package management address: artifacthub. IO /

Search for Jenkins in Artifact Hub as shown below:

Follow Jenkins’ instructions for installation and the following installation steps are detailed:

Use the following command to add the repository where Jenkins is installed

$  helm repo add jenkins https://charts.jenkins.io
"jenkins" has been added to your repositories
Copy the code

Use the following command to view the helm repository that has been added:

$  helm repo list
NAME            URL
kong            https://charts.konghq.com
aliyun          https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
stable          https://charts.helm.sh/stable
kubeview        https://benc-uk.github.io/kubeview/charts
tscharts        https://technosophos.github.com/tscharts
bitnami         https://charts.bitnami.com/bitnami
apisix          https://charts.apiseven.com
jenkins         https://charts.jenkins.io
Copy the code

Update the HELM repository with the following command:

$  helm repo update
Copy the code

Jenkins in the repository can be searched using the following command:

$helm Search Repo Jenkins Aliyun/Jenkins 0.13.5 2.73 Opensource continuous integration server. It s...
bitnami/jenkins 8.0.8           2.289.3         The leading open sourceAutomation Server Jenkins/Jenkins 3.5.9 2.289.3 Jenkins - Build Great things at any Scale! The ... Stable/Jenkins 2.5.4 LTS DEPRECATED - Opensource continuous integration...
Copy the code

Use the following command to see what can be configured:

$  helm show values jenkins/jenkins
Copy the code

Download helm’s chart package locally using the following command:

$  helm pull jenkins/jenkins
Copy the code

You can run the tar -zxvf command to decompress the package:

$tar -zxvf Jenkins-3.5.9. tgz-rw-r --r-- 1 1049089 45006 Jul 28 23:36 changelog. md-rw-r --r-- 1 1049089 1287 Jul 28 23:36 Chart.yaml -rw-r--r-- 1 1049089 30809 Jul 28 23:36 README.md -rw-r--r-- 1 1049089 37647 Jul 28 23:36 VALUES_SUMMARY.md drwxr-xr-x 1 1049089 0 Aug 5 17:59 templates -rw-r--r-- 1 1049089 36203 Jul 28 23:36 values.yamlCopy the code

Modify the values.yaml file as required to customize the configuration. For quick experience, run the following command to install the values:

$  helm install jenkins ./jenkins
Copy the code

Use the following command to view the login username and password:

Check the login user name
$  kubectl exec jenkins-0 -- cat /run/secrets/chart-admin-username
Check the login password
$  kubectl exec jenkins-0 -- cat /run/secrets/chart-admin-password
Copy the code

Temporarily expose the service using the following command:

kubectl --namespace default port-forward svc/jenkins 8080:8080
Copy the code

Production environments recommend using ingress to expose services by domain name

After the service port is exposed, you can access: http://localhost:8080, as shown below: