安裝前說明
Gitlab,Jenkins,Ansible部署在一臺服務(wù)器上
- Gitlab通過默認的80端口訪問
- Jenkins通過設(shè)置后的8090端口訪問
安裝前裝備
# 查看操作系統(tǒng)版本
[root@gitlab ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
# 查看已經(jīng)安裝的gitlab版本
[root@gitlab ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
11.11.0
# 查看磁盤空間
[root@gitlab ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 42G 8.6G 83% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 12K 1.9G 1% /dev/shm
tmpfs 1.9G 195M 1.7G 11% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/xvda1 1014M 336M 679M 34% /boot
/dev/mapper/centos-home 46G 33M 46G 1% /home
tmpfs 377M 0 377M 0% /run/user/0
安裝最新版的Ansible
# 下載Centos7的EPEL
[root@gitlab ~]# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@gitlab ~]# yum clean all && yum makecache
[root@gitlab ~]# yum install ansible
[root@gitlab ~]# ansible --version
ansible 2.9.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
# 如果有這個文件,則刪除
[root@gitlab ansible]# rm -f /etc/ansible/.hosts.swp
[root@gitlab ~]# vim /etc/ansible/hosts
在這里插入圖片描述
測試Ansible是否安裝好,是否可以ping通客戶機
[root@gitlab ansible]# ansible webservers -m ping
future@xx.xx.xx.xx | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
安裝JDK
# 下載jdk安裝包
[root@gitlab ~]# wget https://download.oracle.com/otn/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz
# 安裝jdk
[root@gitlab ~]# tar zxvf jdk-8u45-linux-x64.tar.gz
[root@gitlab ~]# mv jdk1.8.0_45 /usr/local/
[root@gitlab ~]# mv jdk-8u45-linux-x64.tar.gz /usr/local/src/
#添加到系統(tǒng)的環(huán)境變量
[root@gitlab ~]# cat >>/etc/profile << EOF
> export JAVA_HOME=/usr/local/jdk1.8.0_45
> export JRE_HOME=\${JAVA_HOME}/jre
> export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib
> export PATH=\${JAVA_HOME}/bin:\$PATH
> EOF
#使profile文件生效,并查看java版本
[root@gitlab ~]# source /etc/profile
[root@gitlab ~]# java -version
java version "1.8.0_45"
安裝Tomcat
# 下載tomcat安裝包
[root@gitlab ~]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.4/bin/apache-tomcat-8.5.4.tar.gz
[root@gitlab ~]# tar zxvf apache-tomcat-8.5.4.tar.gz
[root@gitlab ~]# mv apache-tomcat-8.5.4 /usr/local/jenkins
[root@gitlab ~]# rm -rf /usr/local/jenkins/webapps/*
# 修改tomcat字符集及端口,端口其實可以不用改,加入字符集
[root@gitlab ~]# vim /usr/local/jenkins/conf/server.xml
<Connector port="8080" **URIEncoding="UTF-8"** protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
安裝GitLab
# 配置yum源
[root@gitlab ~]# cat > /etc/yum.repos.d/gitlab-ce.repo << EOF
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el\$releasever/
gpgcheck=0
enabled=1
EOF
#更新yum緩存
[root@gitlab ~]# yum clean all
[root@gitlab ~]# yum makecache
#安裝GitLab依賴包
[root@gitlab ~]# yum install -y curl policycoreutils-python openssh-server iptables-services
#安裝GitLab,安裝過程中會出現(xiàn)下圖的圖案
[root@gitlab ~]# sudo yum -y install gitlab-ce
在這里插入圖片描述
配置GitLab
# 編輯配置文件
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb
# 修改訪問地址
external_url 'http://xx.xx.xx.xx'
# 開啟備份
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab_rails['backup_keep_time'] = 604800
# 初始化配置&啟動服務(wù)
[root@gitlab ~]# gitlab-ctl reconfigure
# 查看狀態(tài)
[root@gitlab ~]# gitlab-ctl status
# 查看gitlab版本號
[root@gitlab ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
11.1.2
# 開啟防火墻
[root@gitlab ~]# systemctl stop firewalld
[root@gitlab ~]# systemctl disable firewalld
[root@gitlab ~]# systemctl start iptables
[root@gitlab ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@gitlab ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
# 訪問gitlab, 第一次登陸會要求設(shè)置密碼
瀏覽器打開 http://xx.xx.xx.xx
在這里插入圖片描述
安裝Jenkins
# 獲取最新版jenkins安裝包
[root@gitlab ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
[root@gitlab ~]# sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
# 安裝jenkins
[root@gitlab ~]# yum install jenkins
# 查看jenkins所在的地方
[root@gitlab ~]# whereis jenkins
jenkins: /usr/lib/jenkins /usr/local/jenkins
[root@gitlab ~]# find / -name jenkins
find: ‘/proc/53884’: No such file or directory
/run/lock/subsys/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins
/etc/rc.d/init.d/jenkins
/var/lib/yum/repos/x86_64/7/jenkins
/var/lib/jenkins
/var/log/jenkins
/var/cache/yum/x86_64/7/jenkins
/var/cache/jenkins
更改Jenkins端口
# 查看當前所有已經(jīng)使用的端口情況
[root@gitlab ~]# netstat -nultp
# 發(fā)現(xiàn)jenkins默認的8080端口已經(jīng)被占用,則更改Jenkins端口,并將對應(yīng)端口開放,此處以8090為例
[root@gitlab ~]# vim /etc/sysconfig/jenkins
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
# 配置防火墻
> 如果沒有安裝iptables,在使用service iptables save會有問題,需要先按照如下步驟安裝
# 1、安裝或更新服務(wù)
[root@deploy ~]# yum install iptables-services
# 2、啟用iptables
[root@deploy ~]# systemctl enable iptables
# 3、啟動iptables
[root@deploy ~]# systemctl start iptables 打開iptables
[root@deploy ~]# iptables -I INPUT -p tcp --dport 8090 -j ACCEPT
[root@deploy ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
啟動Jenkins
[root@deploy ~]# service jenkins start
Starting jenkins (via systemctl): [ OK ]
訪問Jenkins
瀏覽器輸入http://ip:8090
# 通過以下密碼獲取解鎖密碼
[root@deploy ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
Gitlab和Jenkins整合開始
在GitLab中新建一個用于測試GitLab和Jenkins是否打通的項目Ansible
在這里插入圖片描述
在服務(wù)器上初始化GitLab上的測試項目Ansible
[root@gitlab ~]# mkdir -p /data/Ansible/
[root@gitlab ~]# cd /data/Ansible/
[root@gitlab Ansible]# git config --global http.sslVerify false
[root@gitlab Ansible]# git config --global user.name "Meiyan"
[root@gitlab Ansible]# git config --global user.email "nancygong@cuhk.edu.com"
[root@gitlab Ansible]# git clone git@xx.xx.xx.xx:Meiyan/Ansible.git code
Cloning into 'code'...
warning: You appear to have cloned an empty repository.
[root@gitlab Ansible]# cd code/
[root@gitlab code]# echo "##### gitlab connect to jenkins test" > README.md
[root@gitlab code]# git add .
[root@gitlab code]# git commit -m 'add readme'
[root@gitlab code]# git push -u origin master
為root用戶生成SSH免密訪問webservers客戶機的權(quán)限
[root@gitlab ~]# ssh-keygen
[root@gitlab ~]# ssh-copy-id -i /var/lib/jenkins/.ssh/id_rsa.pub future@xx.xx.xx.xx
[root@gitlab ~]# cat .ssh/id_rsa.pub
將公鑰id_rsa.pub上傳到gitlab
在這里插入圖片描述
為jenkins用戶生成SSH免密訪問webservers客戶機的權(quán)限
在Jenkins中默認是使用jenkins用戶訪問客戶機的,所以需要先設(shè)置jenkins用戶執(zhí)行playbook需要的SSH免密訪問權(quán)限
# 切換到j(luò)enkins用戶
[root@gitlab ~]# su jenkins
bash-4.2$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bdHpy9bQAVfatZTvyb4lBw0FHDi8gAK++Fq2KnkeHSQ jenkins@gitlab.laweb.cuhk.edu.cn
The key's randomart image is:
+---[RSA 2048]----+
| .. . ..o+==|
| . . . ..++o=o|
| E o . ..oo+.o|
| + . . o.. +.|
| . o S o o +.o|
| o . . . + +.|
| .. = + + o|
|o .= . . +.|
| ++.. ..|
+----[SHA256]-----+
bash-4.2$ ssh-copy-id -i /var/lib/jenkins/.ssh/id_rsa.pub future@xx.xx.xx.xx
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/jenkins/.ssh/id_rsa.pub"
The authenticity of host 'xx.xx.xx.xx)' can't be established.
在GitLab中配置Jenkins相關(guān)屬性
創(chuàng)建個人訪問令牌
在這里插入圖片描述
在這里插入圖片描述
# 復(fù)制
Gitlab倉庫地址
GitLab中剛剛生成的access token
備用
在Jenkins中安裝GitLab相關(guān)插件
# 系統(tǒng)管理->管理插件->可選擇插件->搜索 gitlab
在這里插入圖片描述
在這里插入圖片描述
配置GitLab plugin
在這里插入圖片描述
配置 GitLab
`# 系統(tǒng)管理>系統(tǒng)配置>Gitlab`
在這里插入圖片描述
配置Jenkins訪問GitLab的全局憑證(點擊【添加】后出現(xiàn)的此界面)
在這里插入圖片描述
測試:在Jenkins中是否通過API token連通GitLab
在這里插入圖片描述
新建一個用于測試GitLab+ Jenkins是否打通的任務(wù)gitlab_test_task
在這里插入圖片描述
# 點擊【添加】
在這里插入圖片描述
創(chuàng)建用于訪問GitLab中的項目全局憑證
在這里插入圖片描述
配置Jenkins中測試任務(wù)git_test_task的【源碼管理】部分
在這里插入圖片描述
配置Jenkins中測試任務(wù)git_test_task的【構(gòu)建觸發(fā)器】部分
在這里插入圖片描述
在這里插入圖片描述
在GitLab中創(chuàng)建測試項目Ansible的Web鉤子
在這里插入圖片描述
如果出現(xiàn)以下報錯

在這里插入圖片描述
解決報錯的方法如下:開啟訪問本地網(wǎng)絡(luò)服務(wù)
在這里插入圖片描述
測試剛創(chuàng)建的Web鉤子
在這里插入圖片描述
測試push代碼時,Jenkins中測試任務(wù)gitlab_test_task是否會自動構(gòu)建
[root@gitlab ~]# cd /data/Ansible/code/
[root@gitlab code]# echo "# test auto build in jenkins" > README.md
[root@gitlab code]# git add .
[root@gitlab code]# git commit -m 'add readme'
[root@gitlab code]# git push origin master
在GitLab上查看Web鉤子最近調(diào)用的情況
在這里插入圖片描述
在這里插入圖片描述
也可以在Jenkins上查看最近自動構(gòu)建的情況
在這里插入圖片描述
Gitlab整合Jenkins完成
Jenkins整合Ansible開始
新建ansible playbook用于Jenkins中的測試任務(wù)
[root@gitlab ~]# mkdir -p /data/Ansible-playbook
[root@gitlab ~]# vim /data/Ansible-playbook/jenkins-mkdir.yml
---
- hosts: webservers
gather_facts: no
tasks:
- name: mkdir /home/future/data/jenkins-test-dir
file: path=/home/future/data/jenkins-test-dir state=directory
在Jenkins中安裝Ansible相關(guān)插件
# 插件下載的鏡像地址:http://mirror.xmission.com/jenkins/plugins/
在這里插入圖片描述
新建一個用于測試GitLab+ Jenkins+Ansible是否打通的任務(wù)ansible-mkdir-task
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在Jenkins中執(zhí)行這個測試任務(wù)
在這里插入圖片描述
查看執(zhí)行結(jié)果
在這里插入圖片描述
也可以在ansoble的服務(wù)器上測試playbook是否能成功執(zhí)行(可選操作)
[root@gitlab Ansible-playbook]# ansible-playbook /data/Ansible-playbook/jenkins-mkdir.yml -f 5
PLAY [webservers] **************************************************************
TASK [mkdir /home/future/data2/jenkins-test-dir] *******************************
changed: [future@xx.xx.xx.xx]
PLAY RECAP *********************************************************************
future@xx.xx.xx.xx : ok=1 changed=1 unreachable=0 failed=0 s
查看在客戶機執(zhí)行的Gitlab+Jenkins+Ansible自動操作結(jié)果
# 確實自動新建了jenkins-test-dir目錄
future@future:~$ tree data
data
└── jenkins-test-dir
Jenkins整合Ansible完成
番外篇-安裝過程中有可能遇到的問題 & 參考操作的知識點
解決jenkins插件問題
# 如下圖所屬,部分插件沒有安裝成功
在這里插入圖片描述
# 從以下地址將所有未安裝的插件下載到本地
http://mirror.xmission.com/jenkins/plugins/
在這里插入圖片描述
# 手動安裝插件
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
# 重啟jenkins命令
[root@gitlab ~]# systemctl restart jenkins.service
# 查看jenkins狀態(tài)
[root@gitlab ~]# systemctl status jenkins.service
其他可選參考操作(沒有影響)
# 關(guān)閉防火墻
# 停止firewall
[root@deploy ~]# systemctl stop firewalld.service
# 禁止firewall開機啟動
[root@deploy ~]# systemctl disable firewalld.service
# 查看默認防火墻狀態(tài)(關(guān)閉后顯示not running,開啟后顯示running)
[root@deploy ~]# firewall-cmd --state
以上用的都是http的訪問方式
因為在jenkins中配置gitlab是老師出現(xiàn) NSS error -8156 (SEC_ERROR_CA_CERT_INVALID)Issuer certificate is invalid.報錯,多次嘗試加自簽名證書都未成功,所以目前判斷:自簽名證書在此是不行的。所以,為了先把整個流程配通,將gitlab的訪問方式從https改成了http。
# cat /etc/gitlab/gitlab.rb
## GitLab URL
將
external_url 'https://10.20.xxx.xx'
nginx['redirect_http_to_https'] = true
改成了
# external_url 'https://10.20.217.17'
# nginx['redirect_http_to_https'] = true
external_url 'http://10.20.xxx.xx'
nginx['redirect_http_to_https'] = false
# 修改配置文件后,需要重啟GitLab,輸入以下命令,這里也會等很長時間
gitlab-ctl reconfigure
gitlab-ctl restart
# 需要重啟Jenkins
systemctl restart jenkins.service
卸載舊版Ansible
[root@gitlab ~]# yum remove ansible
安裝最新版的Ansible
# 下載Centos7的EPEL
[root@gitlab ~]# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@gitlab ~]# yum clean all && yum makecache
[root@gitlab ~]# yum install ansible
[root@gitlab ~]# ansible --version
ansible 2.9.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 9 2019, 14:30:50) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
# 如果有這個文件,則刪除
[root@gitlab ansible]# rm -f /etc/ansible/.hosts.swp
[root@gitlab ~]# vim /etc/ansible/hosts
在這里插入圖片描述
測試Ansible是否安裝好,是否可以ping通客戶機
[root@gitlab ansible]# ansible webservers -m ping
future@xx.xx.xx.xx | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
在Jenkins中配置Ansible時報錯Failed to connect to the host via ssh: Host key verification failed
https://blog.csdn.net/GongMeiyan/article/details/104046497
報錯:There was an issue creating /data as requested: [Errno 13] Permission denied: b'/data'", "path": "/data/jenkin-test-dir"
[root@gitlab Ansible-playbook]# ansible-playbook /data/Ansible-playbook/jenkins-mkdir.yml -f 5
PLAY [webservers] ******************************************************************************************************************************************************************************
TASK [mkdir /data/jenkins-test-dir] ************************************************************************************************************************************************************
fatal: [future@xx.xx.xx.xx]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "msg": "There was an issue creating /data as requested: [Errno 13] Permission denied: b'/data'", "path": "/data/jenkin-test-dir"}
解決方法:路徑不是絕對路徑,所以訪問不了,要改成:/home/future/data/jenkin-test-dir
運行yum系統(tǒng)提示Existing lock /var/run/yum.pid: another copy is running as pid
原因:就是yum在自動更新,所以再運行yum就會報這個錯。
解決方法:關(guān)掉正在執(zhí)行的yum更新就可以了就可以了,停止正在運行的yum進程,或者禁用開機自動啟動yum進程。
# 停止yum進程
rm -f /var/run/yum.pid
# yum自動更新機制自動激活了yum進程,用下面的命令停止自動更新:
/etc/init.d/yum-updatesd stop
# 如果我們不需要自動更新服務(wù),可以直接刪除它。
yum remove yum-updatesd
# 不過刪除該服務(wù)之后,以后的更新要用下面的命令手工來完成了。
yum update
報錯:
[root@gitlab ~]# ansible all -m command -a "uptime"
xx.xx.xx.xx | FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 127
}
我的解決方法是:卸載當前的ansible2.4版本的,然后重裝最新版ansible2.9后就成功了
網(wǎng)上提供的解決辦法是:[https://www.jeffgeerling.com/blog/2018/fixing-unreachable-ssh-error-when-running-ansible-playbooks-against-ubuntu-1804-or-1604](https://www.jeffgeerling.com/blog/2018/fixing-unreachable-ssh-error-when-running-ansible-playbooks-against-ubuntu-1804-or-1604)
但是我從網(wǎng)上的方法反反復(fù)復(fù)嘗試了很多都不能解決
Centos7 安裝 python2.7.5
# 安裝相關(guān)包
yum install gcc openssl-devel bzip2-devel
# 安裝wget;
yum -y install wget
cd /usr/src
wget https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar -zxvf Python-2.7.5.tgz
# 安裝 python 2.7
cd Python-2.7.5
./configure --enable-optimizations
make altinstall
# 查看安裝版本
python -V
# 安裝 pip
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python2.7 get-pip.py
CentOS7安裝EPEL的兩種方式
http://www.itdecent.cn/p/1882cd3b2295
Centos7 yum安裝、卸載、升級軟件等命令
http://www.itdecent.cn/p/164d46da187e
Ansible官網(wǎng)安裝手冊
https://ansible-tran.readthedocs.io/en/latest/docs/intro_installation.html#what-version
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-rhel-centos-or-fedora