Jenkins+k8s CI(持续集成)/CD(持续部署)

1、安装Jenkins

官网地址:https://www.jenkins.io/download/

wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo --no-check-certificate
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install fontconfig java-11-openjdk
 yum install jenkins

1.1、启动jenkins并测试

修改配置:(不然执行shell会报错,没有权限)

vi /etc/sysconfig/jenkins

JENKINS_USER="root"
JENKINS_GROUP="root"

vi /usr/lib/systemd/system/jenkins.service

User=root
Group=root

systemctl daemon-reload
systemctl restart jenkins
systemctl start jenkins && systemctl enable jenkins

ps -ef | grep jenkins
systemctl stop firewalld #关闭防火墙
systemctl disable firewalld

访问地址: http://192.168.83.11:8080/

1.2、Jenkins初始化

cat /var/lib/jenkins/secrets/initialAdminPassword #获取密码

Jenkins+k8s CI(持续集成)/CD(持续部署)_第1张图片

后面设置账号密码及配置访问url就完成了 

1.3、jenkins配置

步骤1、配置tools

Jenkins+k8s CI(持续集成)/CD(持续部署)_第2张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第3张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第4张图片

Jenkins+k8s CI(持续集成)/CD(持续部署)_第5张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第6张图片

 步骤2:安装插件

(maven ) 为了能构建maven项目

Jenkins+k8s CI(持续集成)/CD(持续部署)_第7张图片

ssh ) 远程执行shell

 

步骤3:系统配置

Jenkins+k8s CI(持续集成)/CD(持续部署)_第8张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第9张图片

Jenkins+k8s CI(持续集成)/CD(持续部署)_第10张图片

步骤4:新建项目

Jenkins+k8s CI(持续集成)/CD(持续部署)_第11张图片

步骤5:安装docker,helm(推送镜像用,过程略)

2、安装harbor

2.1、安装docker

yum install -y yum-utils


yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin


systemctl enable docker && systemctl start docker

2.2、安装docker-compose

 curl -SL https://github.com/docker/compose/releases/download/v2.10.2/docker-compose
-linux-x86_64 -o /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
chmod 777 /usr/local/bin/docker-compose
docker compose version

2.3、安装harbor

下载地址:https://github.com/goharbor/harbor/releases

tar -zxvf harbor.....tar.gz

修改配置文件:

cd /harbor && vi harbor.yml

Jenkins+k8s CI(持续集成)/CD(持续部署)_第12张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第13张图片

 

Jenkins+k8s CI(持续集成)/CD(持续部署)_第14张图片

./install.sh --with-chartmuseum #安装并添加helm仓库支持


服务器重启后:
cd /harbor && docker-compose start

访问地址:http://192.168.83.12/harbor/projects

推送测试:

步骤1:修改docker配置

vi /etc/docker/daemon.json

添加:"insecure-registries":["192.168.83.12"]

或域名
添加:"insecure-registries":["harbor.fj.com"]

额外步骤(域名方式)

Jenkins+k8s CI(持续集成)/CD(持续部署)_第15张图片

  步骤2:登入

docker login 192.168.83.12
docker login harbor.fj.com

 步骤3:查看推送镜像命令

Jenkins+k8s CI(持续集成)/CD(持续部署)_第16张图片

 步骤4:推送

docker tag sp:1 192.168.83.12/test/sp:1
docker push 192.168.83.12/test/sp:1


docker tag sp:1 harbor.fj.com/test/sp:1
docker push harbor.fj.com/test/sp:1

3、安装gitlab

安装地址:https://gitlab.cn/install/

vi /etc/gitlab/gitlab.rb #修改配置文件

external_url 'http://192.168.83.13'
nginx['listen_port']=80
gitlab_rails['initial_root_password'] = "root"
systemctl stop firewalld.service && systemctl disable firewalld.service

 配置git

gitlab-ctl reconfigure

启动并查看状态 

gitlab-ctl start
gitlab-ctl status

访问地址: http://192.168.83.13

注意:正常情况下会自动创建root用户并在/etc/gitlab/initial_root_password里面有密码,但是这次实际的情况并没有创建用户出来,所以需要在登入页面注册一个账号,然后执行以下命令:

gitlab-rails console #等等等

User.where(id:1).first
user.state = "active"
user.access_level = 50
user.save!

3.1、新建项目并推送工程

Jenkins+k8s CI(持续集成)/CD(持续部署)_第17张图片

 推送过程略,新建过后有说明,按说明操作即可

4、CI/CD实战

4.1、新建maven工程提交到git(略)

git init --initial-branch=master
git remote add origin http://192.168.83.13/jasmine/helm.git
git add .
git commit -m "Initial commit"
git push -u origin master

4.2、配置工程jenkins

配置丢弃策略

Jenkins+k8s CI(持续集成)/CD(持续部署)_第18张图片

配置git仓库地址

Jenkins+k8s CI(持续集成)/CD(持续部署)_第19张图片

 配置环境

Jenkins+k8s CI(持续集成)/CD(持续部署)_第20张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第21张图片

 maven构建完成时动作:

Jenkins+k8s CI(持续集成)/CD(持续部署)_第22张图片

 构建完成后操作:

Jenkins+k8s CI(持续集成)/CD(持续部署)_第23张图片

5、CI/CD helm实战

5.1、安装helm push插件

下载地址:https://github.com/chartmuseum/helm-push/releases/download/v0.10.1/helm-push_0.10.1_linux_amd64.tar.gz

[root@k8s-ctrl plugins]# helm env | grep HELM_PLUGINS
HELM_PLUGINS="/root/.local/share/helm/plugins"
[root@k8s-ctrl plugins]# cd /root/.local/share/helm/plugins    
[root@k8s-ctrl plugins]# mkdir helm-push
[root@k8s-ctrl plugins]# cp /helm-push_0.10.1_linux_amd64.tar.gz helm-push
[root@k8s-ctrl plugins]# cd helm-push/
[root@k8s-ctrl helm-push]# tar -zxvf helm-push_0.10.1_linux_amd64.tar.gz 
LICENSE
plugin.yaml
bin/helm-cm-push
[root@k8s-ctrl helm-push]# helm plugin list
NAME    VERSION DESCRIPTION                      
cm-push 0.10.1  Push chart package to ChartMuseum
[root@k8s-ctrl helm-push]# 

添加仓库地址 

# 添加仓库地址到本地列表(其中 myharbor 为这个仓库地址在 helm 本地的名称,连接是仓库URL,后面是登录 harbor 的用户名和密码)
# URL格式:http(s)://{harbor域名或iP:端口(如果默认443或80可不加)}/chartrepo/{yourHarborProjectName}

helm repo add myharbor http://192.168.83.12/chartrepo/test --username admin --password admin

helm repo add myharbor http://harbor.fj.com/chartrepo/test --username admin --password admin

查看是否添加成功:helm repo list

推送:

helm cm-push spboot-1.0.0.tgz myharbor

拉取安装验证:

helm repo update
helm install spsp myharbor/spboot

5.2、创建helm工程

Jenkins+k8s CI(持续集成)/CD(持续部署)_第24张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第25张图片

 初始提交到git

Jenkins+k8s CI(持续集成)/CD(持续部署)_第26张图片

 5.3、Jenkins配置

Jenkins+k8s CI(持续集成)/CD(持续部署)_第27张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第28张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第29张图片

 Jenkins+k8s CI(持续集成)/CD(持续部署)_第30张图片

你可能感兴趣的:(运维,ci/cd,jenkins,运维)