kubernetes云平台管理实战:jenkins自动化构建docker镜像并部署到k8s集群(二十三)

一、集群规划

集群规划

集群规划

二、安装gitlab

安装gitlab

wget https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm
yum localinstall gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm -y

配置gitlab

vim /etc/gitlab/gitlab.rb
13行 external_url 'http://192.168.118.20'
1535行 prometheus_monitoring['enable'] = false

启动gitlab

gitlab-ctl reconfigure

访问gitlab

三、gitlab创建仓库并上传代码

创建下载项目代码

[root@node02 ~]# mkdir /opt/xiaoniao -p
[root@node02 ~]# mv xiaoniaofeifei.zip /opt/xiaoniao/
[root@node02 ~]# cd /opt/xiaoniao/

[root@node02 xiaoniao]# unzip xiaoniaofeifei.zip 
Archive: xiaoniaofeifei.zip
inflating: sound1.mp3 
creating: img/
inflating: img/bg1.jpg 
inflating: img/bg2.jpg 
inflating: img/number1.png 
inflating: img/number2.png 
inflating: img/s1.png 
inflating: img/s2.png 
inflating: 21.js 
inflating: 2000.png 
inflating: icon.png 
inflating: index.html 
[root@node02 xiaoniao]# ls
2000.png 21.js icon.png img index.html sound1.mp3 xiaoniaofeifei.zip
[root@node02 xiaoniao]# rm -rf xiaoniaofeifei.zip

上传代码

[root@node02 xiaoniao]# git config --global user.name "Administrator"
[root@node02 xiaoniao]# git config --global user.email "admin@example.com"
[root@node02 xiaoniao]# git init
Initialized empty Git repository in /opt/xiaoniao/.git/
[root@node02 xiaoniao]# git remote add origin http://192.168.118.20/root/xiaoniao.git
[root@node02 xiaoniao]# git add .
[root@node02 xiaoniao]# git commit -m "Initial commit"
[master (root-commit) 8d74802] Initial commit
11 files changed, 184 insertions(+)
create mode 100644 2000.png
create mode 100644 21.js
create mode 100644 icon.png
create mode 100644 img/bg1.jpg
create mode 100644 img/bg2.jpg
create mode 100644 img/number1.png
create mode 100644 img/number2.png
create mode 100644 img/s1.png
create mode 100644 img/s2.png
create mode 100644 index.html
create mode 100644 sound1.mp3
[root@node02 xiaoniao]# git push -u origin master
Username for 'http://192.168.118.20': root
Password for 'http://root@192.168.118.20': 
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (14/14), 88.15 KiB | 0 bytes/s, done.
Total 14 (delta 0), reused 0 (delta 0)
To http://192.168.118.20/root/xiaoniao.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

四、安装jenkins

1、安装

[root@node01 opt]# cd jenkins-k8s/
[root@node01 jenkins-k8s]# ll
total 593696
-rw-r--r-- 1 root root 9128610 May 15 17:30 apache-tomcat-8.0.27.tar.gz
-rw-r--r-- 1 root root 265814016 May 15 17:31 gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm
-rw-r--r-- 1 root root 166044032 May 15 17:31 jdk-8u102-linux-x64.rpm
-rw-r--r-- 1 root root 89566714 May 15 17:31 jenkin-data.tar.gz
-rw-r--r-- 1 root root 77289987 May 15 17:31 jenkins.war
-rw-r--r-- 1 root root 91014 May 15 17:31 xiaoniaofeifei.zip

安装jdk

[root@node01 jenkins-k8s]# rpm -ivh jdk-8u102-linux-x64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8.0_102-2000:1.8.0_102-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...

安装tomcat

[root@node01 jenkins-k8s]# mkdir /app
[root@node01 jenkins-k8s]# ls
apache-tomcat-8.0.27.tar.gz gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm jdk-8u102-linux-x64.rpm jenkin-data.tar.gz jenkins.war xiaoniaofeifei.zip
[root@node01 jenkins-k8s]# tar xf apache-tomcat-8.0.27.tar.gz -C /app
[root@node01 jenkins-k8s]# ls
apache-tomcat-8.0.27.tar.gz gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm jdk-8u102-linux-x64.rpm jenkin-data.tar.gz jenkins.war xiaoniaofeifei.zip

安装jenkins

[root@node01 jenkins-k8s]# mv jenkins.war /app/apache-tomcat-8.0.27/webapps/ROOT.war

2、在node01上生成秘钥对

[root@node01 jenkins-k8s]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PSdt0eZrMWCaJse9Ez8E4uHhtOXVrIj8HfbT3XmLdwY root@node01
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|             . o |
|          = * + o|
|         O # O . |
|        S ^ O O  |
|         + * BE*=|
|            + *+*|
|             o.o*|
|             ..o.|
+----[SHA256]-----+
[root@node01 jenkins-k8s]# cd    
[root@node01 ~]# cat .ssh/
authorized_keys  id_rsa           id_rsa.pub  

3、在gitlab配置jenkins公钥

[root@node01 ~]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD6vIj+MF0W4Z8byD8rjMK/K3TVUUx7YvmSAv9nwUZdVp7WqAj4bIqadr99U/BBQtmFslRB6cNlw8MMmw0Pdbb+Gpc9YyR7BG56XSprEs3dCgXmXX0uoAXjhfhcvhWC1Re0+8v2oAtuQ6+TgGXs7bRUntF2Cn8sQF3e8d+dIHSF4o6tBBvW3OM5vlWVdgVkZeXs95b1NMfdCOzvxCDHsR5YEk+/ioqIEGjDO0iUK7sIIgF3plZH1dmllqMZzSHqQQdR9BWEPdMiqCyx+zrZSubmItHY7detPzGDrgZPkc8aXjJQ9Je6y1G0Ia5uloyGCe19A5jg5WVf81mITOiG1lhV 

4、在jenkins web界面配置凭证(node01私匙)

[root@node01 ~]# cat /root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA+ryI/jBdFuGfG8g/K4zCvyt01VFMe2L5kgL/Z8FGXVae1qgI
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qw5IM1N0sHqCUvaxWkrljqAJwxJTdf
EP21zJ7f96LJXLZg7vg1Dc2iCpxiqrVYVVNACRKII1trQCTHGGdWabpres0zTpCX
pKGUeFOoaI2rpimg6nWBhQKBgQDmjQ8P3Vbm+E+RIAJ/qiFldPMK7nmapGjr+IgB
9wgcJW8ug9pji99ZERbuQJCHymN4XU/lXj8C7jvEBWyEaLR1EZ2K18nC2B08wyv2
WFRtNtSduY/JepfNiu6nTXiVTH4kjZbkhLAnhsy57X55VEI3rQv59QIu5qptn1m1
FAgFLwKBgQCScV0jBpcAWp+T0JwKiS1TBfASYyTl+EpAlzqIGsJOxapS+U07weaD
Zf3U76O7YdJiZfB/MWVbeg5MrqCCbhc5pj4mn/+WGBwCxFjYohzTfgpfWMKHBlXE
Fnx9gyNxUwSSPJAUgOYJlVqKH0e9IQiqNcGwj2SvtMUW6gt8GSE9HA==
-----END RSA PRIVATE KEY-----

二、创建xiaoniao项目并拉去代码测试

1、创建项目

2、项目配置

1、源码管理配置

构建配置

3、测试

测试截图

 测试代码:

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao
using credential gitlab
Cloning the remote Git repository
Cloning repository git@192.168.118.20:root/xiaoniao.git
 > git init /root/.jenkins/workspace/xiaoniao # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8d74802e2217b3269f8bec29ce59d5664c4f58cf (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 8d74802e2217b3269f8bec29ce59d5664c4f58cf # timeout=10
Commit message: "Initial commit"
First time build. Skipping changelog.
[xiaoniao] $ /bin/sh -xe /app/apache-tomcat-8.0.27/temp/jenkins3978014288218779777.sh
+ echo test
test
Finished: SUCCESS

jenkins目录查看是否拉取成功

[root@node01 ~]# ls /root/.jenkins/workspace/
xiaoniao  xiaoniao@tmp
[root@node01 ~]# ls /root/.jenkins/workspace/xiaoniao
2000.png  21.js  icon.png  img  index.html  sound1.mp3

三、遇到的坑

坑一:没有SSH Username with private key

没有下图:SSH Username with private key选项

解决方案:

下载:2.237版本

wget http://mirror.serverion.com/jenkins/war/2.237/jenkins.war

坑二:无法配置Repository URL地址

 解决方案:node1安装git

[root@node01 ~]# yum install -y git
[root@node01 ~]# git --version
posted @ 2020-05-26 22:35  活的潇洒80  阅读(1531)  评论(0编辑  收藏  举报