k8s環境: 192.168.0.91 master 192.168.0.92 node
192.168.0.96 gitlab
192.168.0.98 harbor
k8s集羣安裝請參照:https://www.cnblogs.com/effortsing/p/10312081.html
gitlab安裝請參照:https://www.cnblogs.com/effortsing/p/10012158.html
arbor安裝請參照:https://www.cnblogs.com/effortsing/p/10061096.html
配置jenkins主機上的docker能夠登陸harbor倉庫而且上傳鏡像到harbor倉庫:參照:https://www.cnblogs.com/effortsing/p/10061096.htmlhtml
下面全部操做都是在node節點:192.168.0.92上進行 一、生成自定義Jenkins master鏡像 原始的Jenkins master鏡像並不符合咱們當前需求,因此在原始鏡像的基礎上作了一些改變,這一步並非必須的,主要是看具體需求 下載原始鏡像 連接:https://pan.baidu.com/s/14z5BnFAXYoMnDoXbiNgmuQ 提取碼:ecsq 導入鏡像 docker load < jenkinsci.tar 查看鏡像 [root@test2 ~]# docker images jenkinsci/jenkins latest b589aefe29ff 3 months ago 703 MB 編寫Dockerfile:
cat>/home/jenkins-dockerfile/Dockerfile <<EOF
FROM jenkinsci/jenkins
USER root
RUN apt-get update && apt-get install -y libltdl7.*
RUN apt-get install maven -yjava
RUN apt-get install git -y
ARG dockerGid=999
RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
RUN mkdir -p /opt/maven/repository
RUN mkdir -p /ceph/maven/repository
EOFnode
該Dockerfile所作的工做爲: 安裝Maven 配置Maven倉庫位置,以便啓動時掛載宿主機倉庫爲容器中Maven倉庫 設置啓動用戶爲root
把jenkins加入docker組,不然沒法使用宿主機的docker
安裝libltdl7.* 庫,不然沒法使用宿主機的docker 構建鏡像 docker build -t jenkinsci/jenkins:v1 /home/jenkins-dockerfile/ Jenkins啓動YAML配置文件 jenkins命令空間建立 cat >namespace-jenkins.yaml<<EOF apiVersion: v1 kind: Namespace metadata: name: jenkins labels: name: jenkins EOF Jenkins 權限配置 此處直接將jenkins-admin集成了cluster-admin權限,可根據本身具體須要進行權限的設置 cat>jenkins-account.yaml<<EOF apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: jenkins name: jenkins-admin namespace: jenkins --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: jenkins-admin labels: k8s-app: jenkins subjects: - kind: ServiceAccount name: jenkins-admin namespace: jenkins roleRef: kind: ClusterRole name: cluster-admin apiGroup: rbac.authorization.k8s.io EOF Jenkins Deployment配置 此處配置簡單明瞭,須要說明的地方是掛在卷,此處掛載了四個目錄,下面分別作出掛載緣由: /var/jenkins_home(容器) –> /ceph/jenkins_home(宿主機) 咱們須要將容器中的Jenkins源目錄掛載導本地宿主機,由於該目錄下保存了Jenkins產生的全部配置、咱們的自定義配置、任務配置及詳情等等信息,>因此須要持久化導宿主機,以便從新啓動Jenkins容器的時候可以找到相應數據,防止數據丟失。此處咱們使用的ceph,保證整個kubernetes集羣全部機 器可以共享同一個目錄。 /opt/maven/repository(容器) –> /ceph/maven/repository(宿主機) 這一對掛載目錄是Maven倉庫的掛載目錄,不論是Jenkins master容器或者是Jenkins slave目錄都須要掛載該目錄,以便容器中maven可以在下載編譯代 碼時可以從該倉庫中找到相應Jar包,同時也保證了數據的持久化。 /usr/bin/docker(容器) –> /usr/bin/docker(宿主機) /var/run/docker.sock(容器) –> /var/run/docker.sock(宿主機) 這兩對掛載目錄做用是可以在容器中操做宿主機docker,具體的用途是在slave容器中編輯maven代碼並生成jar以後,須要生成該代碼服務的docker鏡像 並上傳至本地私有倉庫。所以須要操做宿主機docker以便完成這一系列操做 --------------------- cat>jenkins-deployment.yaml<<EOF apiVersion: apps/v1beta2 kind: Deployment metadata: name: jenkins namespace: jenkins labels: k8s-app: jenkins spec: replicas: 1 selector: matchLabels: k8s-app: jenkins template: metadata: labels: k8s-app: jenkins spec: containers: - name: jenkins image: jenkinsci/jenkins:v1 imagePullPolicy: IfNotPresent volumeMounts: - name: jenkins-home mountPath: /var/jenkins_home - name: maven-repository mountPath: /opt/maven/repository - name: docker mountPath: /usr/bin/docker - name: docker-sock mountPath: /var/run/docker.sock ports: - containerPort: 8080 - containerPort: 50000 volumes: - name: jenkins-home hostPath: path: /ceph/jenkins_home - name: maven-repository hostPath: path: /ceph/maven/repository - name: docker hostPath: path: /usr/bin/docker - name: docker-sock hostPath: path: /var/run/docker.sock serviceAccountName: jenkins-admin EOF Jenkins Service配置 該Service配置做用是可以讓用戶訪問到Jenkins。此處開放並配置了8080、32000端口,這兩個端口在Deployment 中也應該開放。此處配置的宿主機開放端口分別爲:3188八、32000 cat>jenkins-service.yaml<<EOF kind: Service apiVersion: v1 metadata: labels: k8s-app: jenkins name: jenkins namespace: jenkins annotations: prometheus.io/scrape: 'true' spec: ports: - name: jenkins port: 8080 nodePort: 31888 targetPort: 8080 - name: jenkins-agent port: 50000 nodePort: 50000 targetPort: 50000 type: NodePort selector: k8s-app: jenkins EOF 啓動Jenkins鏡像容器 kubectl create -f namespace-jenkins.yaml kubectl apply -f jenkins-account.yaml kubectl apply -f jenkins-deployment.yaml kubectl apply -f jenkins-service.yaml
二、生成自定義Jenkins slave鏡像 須要使用官方鏡像cnych-jenkins,其餘的鏡像裏面都沒有kubectl工具,都試過。我這裏把官方鏡像存到網盤裏面了,方便之後使用 下載帶kubectl版jenkins鏡像
連接:https://pan.baidu.com/s/1RyxKgbHLFTLYqH6ndA_tAg
提取碼:7sy3
nginx
導入鏡像 docker load < cnych-jenkins-jnlp.tar 編寫Dockerfile:
cat>/home/jenkins-dockerfile/Dockerfile <<EOF
FROM cnych/jenkins:jnlp
USER root
RUN apt-get update && apt-get install -y libltdl7.*
RUN apt-get install maven -ygit
RUN apt-get install git -y
ARG dockerGid=999
RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
RUN mkdir -p /opt/maven/repository
RUN mkdir -p /ceph/maven/repository
EOFweb
該Dockerfile所作的工做爲: 安裝Maven 配置Maven倉庫位置,以便啓動時掛載宿主機倉庫爲容器中Maven倉庫 設置啓動用戶爲root 構建鏡像 docker build -t cnych/jenkins:v1 /home/jenkins-dockerfile/ 查看鏡像 [root@test2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE cnych/jenkins v1 969993fe2aa9 23 seconds ago 2.2 GB jenkinsci/jenkins v1 2114cb298e17 About an hour ago 1.41 GB cnych/jenkins jnlp a430a5795102 12 days ago 628 MB jenkinsci/jenkins latest b589aefe29ff 3 months ago 703 MB coredns/coredns 1.2.0 da1adafc0e78 7 months ago 34.2 MB infoblox/dnstools latest d0cee038721f 8 months ago 15.7 MB registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel v0.10.0-amd64 b949a39093d6 10 months ago 44.6 MB registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64 3.1 da86e6ba6ca1 14 months ago 742 kB 三、訪問並操做Jenkins-master 訪問jenkins http://192.168.0.92:31888 查看密碼 [root@test2 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6f8a62f8a0f7 2114cb298e17 "/sbin/tini -- /us..." About an hour ago Up About an hour k8s_jenkins_jenkins-7b46757695-4hx6f_jenkins_e8cb1035-3fe6-11e9-a258-000c2980fc47_0 docker exec 6f8a62f8a0f7 cat /var/jenkins_home/secrets/initialAdminPassword 471234cd0eb44ec3bfc4015fbacd599b 而後會要求安裝一些插件,這裏選擇默認安裝,不然下一步打開是空白頁 設置登陸用戶名密碼: admin/jenkins@123 這時候會跳轉到首頁, 此時Jenkins就能夠真正使用了: 對jenkins進行升級 重啓jenkins(有點慢,等5分鐘) 刷新網頁從新登陸 admin/471234cd0eb44ec3bfc4015fbacd599b 查看更新後的版本 重置admin密碼 進入首頁-》系統管理-》全局安全配置 把「啓用安全」勾上和把Jenkins專有用戶數據庫勾上、容許用戶註冊勾上-》保存 點擊右上角的admin-》設置-》修改裏面的密碼爲(jenkins@123)-》保存-》從新登陸-》輸入帳號密碼 須要安裝的插件 Kubernetes Cli Plugin:該插件可直接在Jenkins中使用kubernetes命令行進行操做。 Kubernetes plugin: 使用kubernetes則須要安裝該插件 Kubernetes Continuous Deploy Plugin:kubernetes部署deploymrnt.yaml 時候須要使用 進入首頁-》系統管理-》插件管理-》可選插件-》輸入kubernetes-》選中全部帶kubernetes的插件進行安裝-》安裝完返回首頁 查看全部帶kubernetes的插件是否安裝上 進入首頁-》系統管理-》插件管理-》已安裝-》輸入kubernetes-》 也可登陸該網站:https://plugins.jenkins.io/,查找須要的插件 增長一個kubernetes雲 點擊 系統管理->系統設置,往下拉可看到雲,點擊新增一個雲來新增一個kubernetes雲 配置jenkins鏈接kubernetes 請參照:https://www.cnblogs.com/effortsing/p/10013441.html 配置Kubernetes Pod Template 其實就是配置Jenkins的jnlp-slave 在該kubernetes雲下,新增Kubernetes Pod Template,配置一個模板容器配置,以下圖所示:
配置鏡像,下面裏面的鏡像必定要寫對,不然寫成別的鏡像,到最後編譯時候就一直報錯沒有mvn
配置卷:就是deployment.yaml 裏面的掛載路徑
全局配置(非必須) 點擊 系統管理->系統設置,下拉找到全局屬性,可根據須要配置Java環境變量、Maven環境變量 全局工具配置 點擊 系統管理->全局工具配置,此處可配置配置一些經常使用的工具配置,好比java、ant、maven、docker 參照:https://www.cnblogs.com/effortsing/p/10375689.html 四、建立Pipeline測試任務 Pipeline任務採用流式的處理方法,步驟清晰,很是適合進行任務配置。點擊新建 建立一個Pipeline任務 建立完成後,會進入任務配置界面,下拉找到Pipeline(中文版爲:流水線),則可編寫Pipeline,進行任務配置
下面pipline裏面的簡單任務:查看slave鏡像裏面是否有java的家目錄、查看maven的版本等操做, def label = "jnlp-slave" podTemplate(label: label, cloud: 'kubernetes',containers: [ containerTemplate(name: 'jnlp-slave', image: 'cnych/jenkins:v1') ], volumes: [hostPathVolume(mounntPath:'/opt/maven/repository',hostPath:'/ceph/maven/repository'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:'/var/run/docker.sock',hostPath:'/var/run/docker.sock')]) { node(label) { stage('Get a Maven project') { container(label) { stage('wait for exec check'){ sh 'sleep 1'
sh 'echo $JAVA_HOME'
sh 'mvn -v' } stage('get maven env') { sh 'cat /etc/resolv.conf' sh 'cat /etc/issue' sh 'uname -a' sh 'env' } } } } }
查看pod個數:
[root@test2 ~]# kubectl get pod -n jenkins
NAME READY STATUS RESTARTS AGE
jenkins-7b46757695-5k5lg 1/1 Running 0 4hdocker
開始構建
查看pod個數:
發現多了一個pod
[root@test2 ~]# kubectl get pod -n jenkins
NAME READY STATUS RESTARTS AGE
jenkins-7b46757695-5k5lg 1/1 Running 0 4h
jnlp-slave-65srw 2/2 Running 0 23sshell
構建成功以下圖:
查看pod個數
[root@test2 ~]# kubectl get pod -n jenkins
NAME READY STATUS RESTARTS AGE
jenkins-7b46757695-5k5lg 1/1 Running 0 4h
jnlp-slave-65srw 2/2 Terminating 0 31s數據庫
再次查看pod個數:發現消失
從上邊的pod個數變化中,咱們能夠清晰的看到 Jenkins Slave 自動建立到註銷刪除的過程,整個過程是自動完成的,不須要人工干預。
[root@test2 ~]# kubectl get pod -n jenkins
NAME READY STATUS RESTARTS AGE
jenkins-7b46757695-5k5lg 1/1 Running 0 4hapache
4、配置容器版jenkins使用宿主機的kubectl命令 4.一、slave鏡像須要使用cnych/jenkins:jnlp,這個官方鏡像裏面有kubectl工具,其餘的沒有,都試過,上面就是用的這個鏡像,因此直接下一步 4.2、掛載kubectl工具 /root/.kube 目錄,咱們將這個目錄掛載到容器的 /home/jenkins/.kube 目錄下面這是爲了讓咱們可以在 Pod 的容器中可以使用 kubectl 工具來訪問咱們的 Kubernetes 集羣, 方便咱們後面在 Slave Pod 部署 Kubernetes 應用。添加一個掛在路徑,以下圖所示:
4.3、pipline腳本以下: 先試試是否能使用宿主機的kubectl命令,只查看一個pod狀況: def label = "jnlp-slave" podTemplate(label: label, cloud: 'kubernetes',containers: [ containerTemplate(name: 'jnlp-slave', image: 'cnych/jenkins:v1') ], volumes: [hostPathVolume(mounntPath:'/opt/maven/repository',hostPath:'/ceph/maven/repository'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:'/var/run/docker.sock',hostPath:'/var/run/docker.sock'), hostPathVolume(mounntPath:' /home/jenkins/.kube',hostPath:'/root/.kube')]) { node(label) { stage('Get a Maven project') { container(label) { stage('wait for exec check'){ sh 'kubectl get pod -n jenkins' } } } } } 4.4、點擊開始構建,以下圖所示:
4.五、查看控制檯輸出,以下圖所示:
五、使用jenkins-salve建立一個nignx項目pod 5.1、導入nginx鏡像 下載鏡像:連接:https://pan.baidu.com/s/1ZvjakBRYNWN3FFSIz0O78g 提取碼:8nfj rz nginx.tar docker load < nginx.tar 5.2、pipline腳本以下: def label = "jnlp-slave" podTemplate(label: label, cloud: 'kubernetes',containers: [ containerTemplate(name: 'jnlp-slave', image: 'cnych/jenkins:v1') ], volumes: [hostPathVolume(mounntPath:'/opt/maven/repository',hostPath:'/ceph/maven/repository'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:' /home/jenkins/.kube',hostPath:'/root/.kube')]) { node(label) { stage('create a pod') { container(label) { stage('cat the pod'){ sh 'kubectl get pod -n jenkins' } stage('create the deploy-nginx.yaml'){ echo 'create the deploy-nginx.yaml' sh ''' cat >deploy-nginx.yaml<<EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: http-test-dm2 spec: replicas: 1 template: metadata: labels: name: http-test-dm2 spec: containers: - name: http-test-con image: nginx imagePullPolicy: Never ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: http-nginx-ser spec: type: NodePort ports: - port: 80 nodePort: 31000 targetPort: 80 selector: name: http-test-dm2 --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nginx spec: rules: - host: www.nginx2.com http: paths: - path: / backend: serviceName: http-nginx-ser servicePort: 80 EOF ''' } stage('deploy to k8s'){ sh 'kubectl create -f deploy-nginx.yaml' } } } } } 5.3、點擊開始構建,以下圖所示:
5.4、查看構建好的pod,pod默認會放在在上面第3步中配置的命名空間下, [root@test2 ~]# kubectl get pod -n jenkins NAME READY STATUS RESTARTS AGE http-test-dm2-b9776b68c-8b7tk 1/1 Running 0 18s jenkins-7b46757695-5k5lg 1/1 Running 6 13d jnlp-slave-pzwtc 2/2 Terminating 0 31s [root@test2 ~]# kubectl get pod -n jenkins NAME READY STATUS RESTARTS AGE http-test-dm2-b9776b68c-8b7tk 1/1 Running 0 1m jenkins-7b46757695-5k5lg 1/1 Running 6 13d 從上面看到:http-test-dm2-b9776b68c-8b7tk 這就是剛建立的nginx容器 5.4、測試訪問nginx,以下圖所示: http://192.168.0.92:31000
六、使用jenkins-salve建立一個solo開源博客項目pod(看視頻是使用下面方式部署的,) 參照:https://www.cnblogs.com/effortsing/p/10468840.html 6.一、配置jenkins-slave主機節點能夠免密登陸harbor倉庫 參照:https://www.cnblogs.com/effortsing/p/10060748.html 6.二、配置jenkins-slave主機節點上的docker能夠登陸harbor倉庫而且上傳鏡像到harbor倉庫 參照:https://www.cnblogs.com/effortsing/p/10061096.html 6.3、配置jenkins全局工具:添加git工具 進入jenkins界面的全局工具配置裏面選擇自動安裝git工具,目的是拉取gitlab代碼 6.四、配置jenkins全局工具:配置jdk、maven家目錄,參照:https://www.cnblogs.com/effortsing/p/10480972.html 6.五、配置jenkins全局工具:添加settings.xml,Maven Configuration -> Settings file in filesystem -> File Path /usr/share/maven/conf/settings.xml
setting.xml須要進到容器裏面查看所在的位置,添加操做以下圖所示:
6.6、 安裝Pipeline Maven Integration 插件 6.七、下載solo源碼包、修改代碼訪問地址、推送到gitlab代碼倉庫中,參照:https://www.cnblogs.com/effortsing/p/10468840.html 6.八、配置jenkins-master、jenkins-slave主機上的git祕鑰到gitlab上實現拉取代碼功能, 參照:https://www.cnblogs.com/effortsing/p/10482792.html 6.9、生成pipeline能夠用的git連接地址,寫到pipline腳本中,這樣在pipline腳本才能夠從gitlab上拉取代碼 參照:https://www.cnblogs.com/effortsing/p/10468840.html 裏面的步驟 6.10、編寫pipline腳本以下:
注意下面pipline腳本里面的 maven build步驟,裏面的JAVA_HOME和mvn工具 都是jenkins-slave裏面的,而不是jenkins-master裏面的工具,
如何查看jenkins-slave裏面JAVA_HOME位置:只有經過構建的時候,在pipline裏面寫shell命令進行查看,由於製做jenkins-slave鏡像的基礎
鏡像是從官方鏡像拉取的,而這個官方jenkins-slave鏡像是不能獨立啓動的,試過,用docker沒法啓動,只能當slave使用。可是爲何還要用這個
鏡像,就是由於這個基礎鏡像裏面包含kubectl工具, 本身以前嘗試往jenkins-master鏡像裏面添加kubectl工具,可是失敗,
注意:下面piplene裏面的第三個stage步驟裏面的JAVA_HOME路徑是第一個stage步驟mvn -v 獲得的結果有java路徑,要填寫這個,不要填寫echo $JAVA_HOME獲得的結果
先編譯構建一下,而後獲得mvn -v 結果後,及時暫停,而後把java路徑填寫到第三個stage步驟裏面,重新編譯打包 def label = "jnlp-slave" podTemplate(label: label, cloud: 'kubernetes',containers: [ containerTemplate(name: 'jnlp-slave', image: 'cnych/jenkins:v1') ], volumes: [hostPathVolume(mounntPath:'/opt/maven/repository',hostPath:'/ceph/maven/repository'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:'/usr/bin/docker',hostPath:'/usr/bin/docker'), hostPathVolume(mounntPath:' /home/jenkins/.kube',hostPath:'/root/.kube')]) { node(label) { stage('create a pod') { container(label) { stage('cat the pod'){ sh 'kubectl get pod -n jenkins'
sh 'echo $JAVA_HOME'
sh 'mvn -v' } stage('git checkout'){ echo 'git clone' checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c2ca4523-96d0-4fdc-a427-bfefc36a3aa5', url: 'http://192.168.0.96:8081/root/hello.git']]]) } stage('maven build'){ echo 'maven build' sh ''' export JAVA_HOME=/usr/local/newhope/java1.8 /usr/bin/mvn clean package -Dmaven.test.skip=true ''' } stage('docker build and push images'){ echo 'docker build and push images' sh ''' REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag} cat >Dockerfile<<EOF FROM 192.168.0.98:5000/library/tomcat-85:latest RUN rm -rf /usr/local/tomcat/webapps/ROOT/ COPY target/*.war /usr/local/tomcat/webapps/ROOT.war WORKDIR /usr/local/tomcat EXPOSE 8080 CMD ["./bin/catalina.sh", "run"] EOF docker build -t $REPOSITORY . docker login -u admin -p Harbor12345 192.168.0.98:5000 docker push $REPOSITORY ''' } stage('create the deploy-solo.yaml'){ echo 'create the deploy-solo.yaml' sh ''' REPOSITORY=192.168.0.98:5000/library/solo/solo:${Tag} cat >deploy-solo.yaml<<EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: http-solo-dm2 spec: replicas: 1 template: metadata: labels: name: http-solo-dm2 spec: containers: - name: http-solo-con image: $REPOSITORY imagePullPolicy: Never ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: http-solo-ser spec: type: NodePort ports: - port: 8080 nodePort: 33580 targetPort: 8080 selector: name: http-solo-dm2 --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: solo spec: rules: - host: www.solo.com http: paths: - path: / backend: serviceName: http-solo-ser servicePort: 8080 EOF ''' } stage('deploy to k8s'){ sh 'kubectl create -f deploy-solo.yaml' } } } } }
上面pipline腳本作的事情以下:
查看JAVA_HOME
查看maven版本
拉取代碼:git checkout
編譯打包:maven build
構建並推送鏡像:docker build and push images
建立yaml文件:create the deploy-solo.yaml
部署到k8s集羣裏面:deploy to k8s
6.十一、啓用Tag標籤,參照:https://www.cnblogs.com/effortsing/p/10468840.html 裏面的步驟 6.十二、開始構建,參照:https://www.cnblogs.com/effortsing/p/10468840.html 裏面的步驟
6.1三、構建成功以下圖所示:
6.1四、查看harbor倉庫,多出來個鏡像,以下圖所示
http://192.168.0.98:5000
6.1五、查看pod,多出來個pod,但不是Running狀態
[root@test2 ~]# kubectl get pod -n jenkins
NAME READY STATUS RESTARTS AGE
http-solo-dm2-76676d9cfc-vh6gg 0/1 CrashLoopBackOff 6 7m
jenkins-7b46757695-tjh2j 1/1 Running 2 11h
查看報錯日誌以下:
kubectl logs -f pods/http-solo-dm2-76676d9cfc-vh6gg -n jenkins
[ERROR]-[2019-03-25 13:25:04]-[org.b3log.latke.Latkes:836]: Read skin [Jane]'s configuration failed: null
[ERROR]-[2019-03-25 13:25:04]-[org.b3log.solo.SoloServletListener:302]: Can't load the default skins, please make sure skin [Jane] is under skins directory and structure correctly
分析:多是代碼問題,多是項目推送到gitlab上的位置不正確,這個須要結合研發來解決,作到這裏已經不錯了。本身搞很難的,這個暫時先放下,
6.1七、查看svc,多出來個svc
[root@test2 ~]# kubectl get svc -n jenkins
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
http-solo-ser NodePort 10.100.197.60 <none> 8080:33580/TCP 7m
jenkins NodePort 10.97.49.156 <none> 8080:31888/TCP,50000:50000/TCP 11h
報錯解決:
報錯找不到mvn工具,錯誤以下所示: /home/jenkins/workspace/pipline-test@tmp/durable-33237322/script.sh: 4: /home/jenkins/workspace/pipline-test@tmp/durable-33237322/script.sh: /usr/bin/mvn: not found
解決:以前編譯一直顯示找不到mvn工具,是由於下圖中配置jenkins-slave鏡像時候寫錯了,沒有填寫成編譯好的cnych/jenkins:v1 鏡像,
參照:容器版jenkins實現CI/CD:
https://www.cnblogs.com/effortsing/p/10468840.html
製做jenkins鏡像參照: https://blog.csdn.net/a632189007/article/details/79311795
上面連接這篇文檔的核心內容以下:
一、生成自定義Jenkins master鏡像
Dockerfile:
FROM 192.168.1.184:5000/jenkins/jenkins:2.89.3
ENV MAVEN_VERSION 3.0.5
ENV JAVA_HOME /usr/java/jdk1.8.0_121
ENV MAVEN_HOME /opt/maven/apache-maven-${MAVEN_VERSION}
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# build java
COPY ./jdk1.8.0_121 ${JAVA_HOME}
COPY ./libltdl.so.7 /usr/lib/libltdl.so.7
# build maven
COPY apache-maven-${MAVEN_VERSION}-bin.tar.gz /tmp/maven/apache-maven-${MAVEN_VERSION}-bin.tar.gz
COPY settings.xml /tmp/maven/settings.xml
USER root:root
RUN mkdir -p /opt/maven/repository \
&& cd /opt/maven \
&& tar -zxvf /tmp/maven/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& cp /tmp/maven/settings.xml ${MAVEN_HOME}/conf/settings.xml \
&& rm -rf /tmp/maven
ENV PATH ${JAVA_HOME}/bin:${MAVEN_HOME}/bin:${PATH}
該Dockerfile所作的工做爲:
1. 從新安裝Java環境並配置環境變量;
2. 安裝Maven並配置環境變量;
3. 配置Maven倉庫位置,以便啓動時掛載宿主機倉庫爲容器中Maven倉庫;
4. 設置啓動用戶爲root。
二、生成自定義Jenkins slave鏡像
Dockerfile:
FROM jenkinsci/jnlp-slave:latest
ENV MAVEN_VERSION 3.0.5
ENV JAVA_HOME /usr/java/jdk1.8.0_121
ENV MAVEN_HOME /opt/maven/apache-maven-${MAVEN_VERSION}
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# build java
COPY ./jdk1.8.0_121 ${JAVA_HOME}
COPY ./libltdl.so.7 /usr/lib/libltdl.so.7
# build maven
COPY apache-maven-${MAVEN_VERSION}-bin.tar.gz /tmp/maven/apache-maven-${MAVEN_VERSION}-bin.tar.gz
COPY settings.xml /tmp/maven/settings.xml
USER root:root
RUN mkdir -p /opt/maven/repository \
&& cd /opt/maven \
&& tar -zxvf /tmp/maven/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& cp /tmp/maven/settings.xml ${MAVEN_HOME}/conf/settings.xml \
&& rm -rf /tmp/maven \
&& apt-get -yq update \
&& apt-get -yq --no-install-recommends --no-install-suggests install sshpass \
&& apt-get clean -y
ENV PATH ${MAVEN_HOME}/bin:${PATH}
該Dockerfile操做與Jenkins master的Dockerfile基本一致。不過該鏡像中缺乏libltdl.so.7文件,須要從宿主機中拷貝進去,該文件在slave節點容器中使用docker時會用到,所以十分重要
容器板jenkins使用宿主機的kubectl參照:http://ju.outofmemory.cn/entry/363089