iT邦幫忙

2023 iThome 鐵人賽

DAY 3
0
DevOps

通过Jenkins 实现 CI&CD系列 第 3

在 Kubernetes 上部署 Jenkins

  • 分享至 

  • xImage
  •  

部署 Jenkins 的方式有很多种,我这里就演示一种,在 Kubernetes 上部署 Jenkins。

为 Jenkins 创建一个 NamSpace

root@master:~# kubectl create namespace jenkins

准备 yaml 文件。关于 Jenkins 的版本,我这里使用的是 lts 版本,而不是最新的版本。并且我们将 NFS 服务器的 /share/jenkins 目录挂载到了 Jenkins 的 /var/jenkins_vol 目录。

root@master:~# vim jenkins.yaml 
root@master:~# cat jenkins.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        ports:
          - name: http-port
            containerPort: 8080
          - name: jnlp-port
            containerPort: 50000
        volumeMounts:
          - name: jenkins-vol
            mountPath: /var/jenkins_vol
      volumes:
        - name: jenkins-vol
          nfs:
            server: work2
            path: /share/jenkins

在 Jenkins 的 NameSpace 下 apply Jenkins 的 yaml 文件。并确保最终状态(Status)为 Running。

root@master:~# kubectl apply -f jenkins.yaml 
deployment.apps/jenkins created
root@master:~# kubectl get pods -n jenkins
NAME                         READY   STATUS              RESTARTS   AGE
jenkins-844c5d459b-d6gq9     0/1     ContainerCreating   0          2s
root@master:~# kubectl get pods -n jenkins
NAME                         READY   STATUS    RESTARTS   AGE
jenkins-844c5d459b-d6gq9     1/1     Running   0          2s

为 Jenkins 的 service 准备 yaml 文件。我这里使用的 service 类型为 NodePort。Jenkins 使用了两个端口,所以我们需要为每个端口创建 service。由于 jnlp 端口不需要与外网通信,所以使用了 ClusterIP。

root@master:~# vim jenkins_service.yaml 
root@master:~# cat jenkins_service.yaml
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: jenkins
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30008
  selector:
    app: jenkins

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

同样使用 apply 的方式创建 service。

root@master:~# kubectl apply -f jenkins_service.yaml 
service/jenkins created
service/jenkins-jnlp created
root@master:~# kubectl get service -n jenkins
NAME                TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)          AGE
jenkins             NodePort    192.168.109.226   <none>        8080:30008/TCP   9s
jenkins-jnlp        ClusterIP   192.168.213.170   <none>        50000/TCP        9s

通过查看日志,查看 Jenkins 初始密码。 字段就是初始密码的字段。

root@master:~# kubectl get pods -n jenkins
NAME                         READY   STATUS    RESTARTS   AGE
jenkins-844c5d459b-d6gq9     1/1     Running   0          4m18s
root@master:~# kubectl logs jenkins-844c5d459b-d6gq9 -n jenkins
Running from: /usr/share/jenkins/jenkins.war
webroot: /var/jenkins_home/war
2023-09-03 02:00:21.082+0000 [id=1]	INFO	winstone.Logger#logInternal: Beginning extraction from war file
2023-09-03 02:00:22.923+0000 [id=1]	WARNING	o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2023-09-03 02:00:23.072+0000 [id=1]	INFO	org.eclipse.jetty.server.Server#doStart: jetty-10.0.15; built: 2023-04-11T17:25:14.480Z; git: 68017dbd00236bb7e187330d7585a059610f661d; jvm 11.0.20+8
2023-09-03 02:00:23.773+0000 [id=1]	INFO	o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
... ...
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/var/jenkins_home/war/WEB-INF/lib/groovy-all-2.4.21.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2023-09-03 02:00:30.839+0000 [id=31]	INFO	jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

<Password>

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2023-09-03 02:01:06.043+0000 [id=28]	INFO	jenkins.InitReactorRunner$1#onAttained: Completed initialization
2023-09-03 02:01:06.095+0000 [id=22]	INFO	hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
2023-09-03 02:01:06.839+0000 [id=44]	INFO	h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2023-09-03 02:01:06.839+0000 [id=44]	INFO	hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
... ...

现在,我们就可以通过 http://<Node_IP>:30008 来访问我们的 Jenkins 了。这里的端口号,是我们在 Jenkins 的 service 里面定义的 nodePort,如果你在 service 里面的定义与我定义的不一样,这样需要将它改成你配置的端口号。

输入前面 logs 里面的密码,并点击 Continue
https://ithelp.ithome.com.tw/upload/images/20230903/20099494p472gbXTGF.png

选择 Install suggested plugins
https://ithelp.ithome.com.tw/upload/images/20230903/20099494DgqSjk7R0n.png

正在安装插件
https://ithelp.ithome.com.tw/upload/images/20230903/20099494fuWh66VlPv.png

输入管理员用户信息,用户名,密码等待
https://ithelp.ithome.com.tw/upload/images/20230903/20099494o6JkZFXWLq.png

点击 Save and Finish,保存并完成配置
https://ithelp.ithome.com.tw/upload/images/20230903/20099494u2EkSQ4Fl8.png

点击 Start using Jenkins
https://ithelp.ithome.com.tw/upload/images/20230903/20099494ggqRPhEZE6.png

到这里,我们的 Jenkins 安装就算完成了。
https://ithelp.ithome.com.tw/upload/images/20230903/20099494hT0Wpf8Y3A.png

更多安装相关的内容,可以查看:https://www.jenkins.io/zh/doc/book/installing/


上一篇
CI/CD 概述
下一篇
First Project(1)
系列文
通过Jenkins 实现 CI&CD26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言