首页 > 其他分享 >19. rs、deploy和Jenkins集成K8S实现CICD实战

19. rs、deploy和Jenkins集成K8S实现CICD实战

时间:2024-08-03 19:05:40浏览次数:15  
标签:oldboyedu master231 deploy 19 CICD apps xiuxian root

1.rs控制器

1.作用

可以实现Pod的副本控制。相比rc资源,其功能性更强且更加轻量级。

2.案例1-rs实现类似rc的功能

[root@master231 rs]# cat 01-rs-xiuxian-matchLabels.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-xiuxian
spec:
  # 指定Pod的副本数量
  replicas: 3
  # 定义如何关联Pod
  selector:
    # 基于标签关联Pod
    matchLabels:
      apps: xiuxian
  # 定义Pod的模板
  template:
    metadata:
      labels:
        apps: xiuxian
        version: v1
    spec:
       containers:
       - name: c1
         image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
[root@master231 rs]# 

3.案例2-rs实现优于rc的功能

	3.1 准备测试环境

[root@master231 ~]# kubectl run xiuxian01 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
pod/xiuxian01 created
[root@master231 ~]#
[root@master231 ~]# kubectl run xiuxian02 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
pod/xiuxian02 created
[root@master231 ~]#
[root@master231 ~]# kubectl run xiuxian03 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3
pod/xiuxian03 created
[root@master231 ~]#
[root@master231 ~]#
[root@master231 ~]# kubectl label pod xiuxian01 apps=v1
pod/xiuxian01 labeled
[root@master231 ~]#
[root@master231 ~]# kubectl label pod xiuxian02 apps=v2
pod/xiuxian02 labeled
[root@master231 ~]#
[root@master231 ~]# kubectl label pod xiuxian03 apps=v3
pod/xiuxian03 labeled
[root@master231 ~]#
[root@master231 ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian01 1/1 Running 0 75s apps=v1,run=xiuxian01
xiuxian02 1/1 Running 0 69s apps=v2,run=xiuxian02
xiuxian03 1/1 Running 0 63s apps=v3,run=xiuxian03
[root@master231 ~]#
[root@master231 ~]# kubectl run xiuxian04 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
pod/xiuxian04 created
[root@master231 ~]#
[root@master231 ~]# kubectl label pod xiuxian04 apps=v4
pod/xiuxian04 labeled
[root@master231 ~]#
[root@master231 ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
xiuxian01 1/1 Running 0 6m23s apps=v1,run=xiuxian01
xiuxian02 1/1 Running 0 6m17s apps=v2,run=xiuxian02
xiuxian03 1/1 Running 0 6m11s apps=v3,run=xiuxian03
xiuxian04 1/1 Running 0 28s apps=v4,run=xiuxian04
[root@master231 ~]#

		3.2 创建rs资源

[root@master231 rs]# cat 02-rs-xiuxian-matchExpressions.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-xiuxian-matchexpressions
spec:

指定Pod的副本数量

replicas: 5

定义如何关联Pod

selector:
# 基于标签表达式关联Pod
matchExpressions:
# 代表的是标签的key
- key: apps
# 代表的是标签的value
values:
- v1
- v2
- v3
# 指定key和value之间的关系: In, NotIn, Exists and DoesNotExist
# In:
# key的值必须在value的列表之中。
# NotIn:
# 和In相反。
# Exists:
# 只要存在key即可,value任意,因此可以省略value字段。
# DoesNotExist:
# 不存在指定的key,和Exists相反,因此可以省略value字段。
operator: In

定义Pod的模板

template:
metadata:
labels:
apps: v1
school: oldboyedu
class: linux92
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
[root@master231 rs]#
[root@master231 rs]# kubectl apply -f 02-rs-xiuxian-matchExpressions.yaml
replicaset.apps/rs-xiuxian-matchexpressions created
[root@master231 rs]#
[root@master231 rs]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rs-xiuxian-matchexpressions-6rgx2 1/1 Running 0 16s apps=v1,class=linux92,school=oldboyedu
rs-xiuxian-matchexpressions-md6jh 1/1 Running 0 16s apps=v1,class=linux92,school=oldboyedu
xiuxian01 1/1 Running 0 8m6s apps=v1,run=xiuxian01
xiuxian02 1/1 Running 0 8m apps=v2,run=xiuxian02
xiuxian03 1/1 Running 0 7m54s apps=v3,run=xiuxian03
xiuxian04 1/1 Running 0 2m11s apps=v4,run=xiuxian04
[root@master231 rs]#

  • deploy控制器
    1.作用
    用于部署服务,底层基于rs控制来控制Pod副本数量,并不会直接控制Pod。

相比于rc和rs而言, deploy支持声明式更新。

2.案例1-基于matchLabels匹配

[root@master231 deploy]# cat 01-deploy-xiuxian-matchLabels.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deloy-xiuxian
spec:

指定Pod的副本数量

replicas: 3

定义如何关联Pod

selector:
# 基于标签关联Pod
matchLabels:
apps: xiuxian

定义Pod的模板

template:
metadata:
labels:
apps: xiuxian
version: v1
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
[root@master231 deploy]#

2.案例2-基于matchLabels匹配

[root@master231 deploy]# cat 02-deloy-xiuxian-matchExpressions.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-xiuxian-matchexpressions
spec:

指定Pod的副本数量

replicas: 5

定义如何关联Pod

selector:
# 基于标签表达式关联Pod
matchExpressions:
# 代表的是标签的key
- key: apps
# 代表的是标签的value
values:
- v1
- v2
- v3
# 指定key和value之间的关系: In, NotIn, Exists and DoesNotExist
# In:
# key的值必须在value的列表之中。
# NotIn:
# 和In相反。
# Exists:
# 只要存在key即可,value任意,因此可以省略value字段。
# DoesNotExist:
# 不存在指定的key,和Exists相反,因此可以省略value字段。
operator: In

定义Pod的模板

template:
metadata:
labels:
apps: v1
school: oldboyedu
class: linux92
spec:
containers:
- name: c1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
[root@master231 deploy]#

3.测试案例

[root@master231 deploy]# kubectl get deploy,rs,pods
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deloy-xiuxian 3/3 3 3 3m8s
deployment.apps/deploy-xiuxian-matchexpressions 5/5 5 5 14s

NAME DESIRED CURRENT READY AGE
replicaset.apps/deloy-xiuxian-8676cbd54f 3 3 3 3m8s
replicaset.apps/deploy-xiuxian-matchexpressions-5d6d946f5b 5 5 5 14s

NAME READY STATUS RESTARTS AGE
pod/deloy-xiuxian-8676cbd54f-7zc4j 1/1 Running 0 3m8s
pod/deloy-xiuxian-8676cbd54f-jwxbl 1/1 Running 0 3m8s
pod/deloy-xiuxian-8676cbd54f-pfpvk 1/1 Running 0 3m8s
pod/deploy-xiuxian-matchexpressions-5d6d946f5b-7dx2j 1/1 Running 0 14s
pod/deploy-xiuxian-matchexpressions-5d6d946f5b-88rxj 1/1 Running 0 14s
pod/deploy-xiuxian-matchexpressions-5d6d946f5b-dkscx 1/1 Running 0 14s
pod/deploy-xiuxian-matchexpressions-5d6d946f5b-kzkkt 1/1 Running 0 14s
pod/deploy-xiuxian-matchexpressions-5d6d946f5b-sfch6 1/1 Running 0 14s
[root@master231 deploy]#

4.deployments的Recreate升级策略

[root@master231 deploy]# cat 03-deploy-update-Recreate.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deloy-xiuxian-update
spec:

升级策略

strategy:
# 指定升级的类型,"Recreate" or "RollingUpdate"
# Recreate:
# 代表的是删除所有旧的Pod,再去创建新的Pod,不推荐使用。
type: Recreate
replicas: 3
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
version: v1
spec:
containers:
- name: c1
# image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
# image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3
# 指定镜像的拉取策略,有效值为: Always, Never, IfNotPresent
# Never:
# 如果本地有镜像则尝试启动。
# 如果本地没有镜像则不会去远程仓库拉取镜像。
# IfNotPresent:
# 如果本地有镜像则尝试启动。
# 如果本地没有镜像则去远程仓库拉取镜像。
# Always:
# 如果本地有镜像,则会将本地的镜像和远程仓库的摘要信息进行对比,若一致则直接使用本地缓存镜像,若不一致则重新拉取。
# 如果本地没有镜像,则直接从远程仓库拉取镜像。
#
# 默认策略会根据镜像的标签来定:
# 如果你的tag是"latest",则默认策略为"Always";
# 若你的tag是非"latest",则默认策略为"IfNotPresent"
imagePullPolicy: Always


apiVersion: v1
kind: Service
metadata:
name: xiuxian-svc-deploy
spec:
type: ClusterIP
clusterIP: 10.200.0.80
selector:
apps: xiuxian
ports:

  • port: 80
    targetPort: 80

[root@master231 deploy]#

5.deployments的RollingUpdate升级策略

[root@master231 deploy]# cat 03-deploy-update-Recreate.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deloy-xiuxian-update
spec:

升级策略

strategy:
# 指定升级的类型,"Recreate" or "RollingUpdate"
# Recreate:
# 代表的是删除所有旧的Pod,再去创建新的Pod,不推荐使用。
# RollingUpdate:
# 代表滚动更新,会更新一部分Pod,逐步替换旧的Pod,默认就是这种类型。
type: RollingUpdate
# 定义滚动升级策略,前提是 "type: RollingUpdate"时该字段才有效。
rollingUpdate:
# 在升级的过程中,在原有副本数量基础之上,最多能够启动的Pod数量。
# 该字段支持百分比和数字,若不指定,则默认为:"25%"。
maxSurge: 2
# 在升级过程中,在原有副本数量基础之上,不可用的副本数量。
# 该字段支持百分比和数字,若不指定,则默认为:"25%"。
maxUnavailable: 1
replicas: 5
selector:
matchLabels:
apps: xiuxian
template:
metadata:
labels:
apps: xiuxian
version: v1
spec:
containers:
- name: c1
# image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
# image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3
# 指定镜像的拉取策略,有效值为: Always, Never, IfNotPresent
# Never:
# 如果本地有镜像则尝试启动。
# 如果本地没有镜像则不会去远程仓库拉取镜像。
# IfNotPresent:
# 如果本地有镜像则尝试启动。
# 如果本地没有镜像则去远程仓库拉取镜像。
# Always:
# 如果本地有镜像,则会将本地的镜像和远程仓库的摘要信息进行对比,若一致则直接使用本地缓存镜像,若不一致则重新拉取。
# 如果本地没有镜像,则直接从远程仓库拉取镜像。
#
# 默认策略会根据镜像的标签来定:
# 如果你的tag是"latest",则默认策略为"Always";
# 若你的tag是非"latest",则默认策略为"IfNotPresent"
imagePullPolicy: Always


apiVersion: v1
kind: Service
metadata:
name: xiuxian-svc-deploy
spec:
type: ClusterIP
clusterIP: 10.200.0.80
selector:
apps: xiuxian
ports:

  • port: 80
    targetPort: 80

[root@master231 deploy]#

[root@master231 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deloy-xiuxian-update-b8c754-4s474 1/1 Running 0 35s 10.100.2.87 worker233
deloy-xiuxian-update-b8c754-97bh5 1/1 Running 0 35s 10.100.1.134 worker232
deloy-xiuxian-update-b8c754-dgmzp 1/1 Running 0 35s 10.100.1.135 worker232
deloy-xiuxian-update-b8c754-kpx6p 1/1 Running 0 35s 10.100.2.89 worker233
deloy-xiuxian-update-b8c754-wbc9q 1/1 Running 0 35s 10.100.2.88 worker233
[root@master231 ~]#
[root@master231 ~]#
[root@master231 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deloy-xiuxian-update-5f77fddbd7-m4kd2 0/1 ContainerCreating 0 1s worker232
deloy-xiuxian-update-5f77fddbd7-plszj 0/1 ContainerCreating 0 1s worker233
deloy-xiuxian-update-5f77fddbd7-tz78m 0/1 ContainerCreating 0 1s worker232
deloy-xiuxian-update-b8c754-4s474 1/1 Running 0 54s 10.100.2.87 worker233
deloy-xiuxian-update-b8c754-97bh5 1/1 Running 0 54s 10.100.1.134 worker232
deloy-xiuxian-update-b8c754-dgmzp 1/1 Running 0 54s 10.100.1.135 worker232
deloy-xiuxian-update-b8c754-kpx6p 0/1 Terminating 0 54s 10.100.2.89 worker233
deloy-xiuxian-update-b8c754-wbc9q 1/1 Running 0 54s 10.100.2.88 worker233
[root@master231 ~]#
[root@master231 ~]#
[root@master231 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deloy-xiuxian-update-5f77fddbd7-6tgf9 0/1 ContainerCreating 0 0s worker232
deloy-xiuxian-update-5f77fddbd7-8tq8s 0/1 ContainerCreating 0 1s worker233
deloy-xiuxian-update-5f77fddbd7-m4kd2 0/1 ContainerCreating 0 3s worker232
deloy-xiuxian-update-5f77fddbd7-plszj 1/1 Running 0 3s 10.100.2.90 worker233
deloy-xiuxian-update-5f77fddbd7-tz78m 1/1 Running 0 3s 10.100.1.136 worker232
deloy-xiuxian-update-b8c754-4s474 1/1 Running 0 56s 10.100.2.87 worker233
deloy-xiuxian-update-b8c754-dgmzp 1/1 Running 0 56s 10.100.1.135 worker232
deloy-xiuxian-update-b8c754-wbc9q 1/1 Terminating 0 56s 10.100.2.88 worker233
[root@master231 ~]#
[root@master231 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deloy-xiuxian-update-5f77fddbd7-6tgf9 1/1 Running 0 105s 10.100.1.138 worker232
deloy-xiuxian-update-5f77fddbd7-8tq8s 1/1 Running 0 106s 10.100.2.91 worker233
deloy-xiuxian-update-5f77fddbd7-m4kd2 1/1 Running 0 108s 10.100.1.137 worker232
deloy-xiuxian-update-5f77fddbd7-plszj 1/1 Running 0 108s 10.100.2.90 worker233
deloy-xiuxian-update-5f77fddbd7-tz78m 1/1 Running 0 108s 10.100.1.136 worker232
[root@master231 ~]#

  • deploy实现响应式更新和回滚:
    1.响应式更新应用
    [root@master231 deploy]# kubectl set image deploy deloy-xiuxian-update-strategy c1=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v3
    deployment.apps/deloy-xiuxian-update-strategy image updated
    [root@master231 deploy]#
    [root@master231 deploy]#
    [root@master231 deploy]# kubectl set image deploy deloy-xiuxian-update-strategy c1=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
    deployment.apps/deloy-xiuxian-update-strategy image updated
    [root@master231 deploy]#

    2.查看应用发布的历史版本
    [root@master231 deploy]# kubectl rollout history deployment deloy-xiuxian-update-strategy
    deployment.apps/deloy-xiuxian-update-strategy
    REVISION CHANGE-CAUSE
    2
    3
    4

[root@master231 deploy]#

3.查看当前应用的部署状态

[root@master231 deploy]# kubectl rollout status deployment deloy-xiuxian-update-strategy
deployment "deloy-xiuxian-update-strategy" successfully rolled out
[root@master231 deploy]#

4.暂停更新

[root@master231 deploy]# kubectl rollout pause deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy paused
[root@master231 deploy]#

5.恢复更新

[root@master231 deploy]# kubectl rollout resume deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy resumed
[root@master231 deploy]#

6.回滚到上一个版本

[root@master231 deploy]# kubectl rollout undo deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy rolled back
[root@master231 deploy]#
[root@master231 deploy]# kubectl rollout history deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy
REVISION CHANGE-CAUSE
2
4
5

[root@master231 deploy]#
[root@master231 deploy]# kubectl rollout undo deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy rolled back
[root@master231 deploy]#
[root@master231 deploy]# kubectl rollout history deployment deloy-xiuxian-update-strategy
deployment.apps/deloy-xiuxian-update-strategy
REVISION CHANGE-CAUSE
2
5
6

[root@master231 deploy]#

7.回滚到指定版本

[root@master231 deploy]# kubectl rollout undo deployment deloy-xiuxian-update-strategy --to-revision=2
deployment.apps/deloy-xiuxian-update-strategy rolled back
[root@master231 deploy]#

apt-get install openjdk-17-jre # 在线安装较慢,建议离线安装

启动Jenkins报错:
java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null

温馨提示,如果遇到上述问题,说明你没有安装fontconfig 软件包。

[root@jenkins211 ~]# mkdir -pv /oldboyedu/softwares

[root@jenkins211 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/jdk-17_linux-x64_bin.tar.gz

[root@jenkins211 ~]# tar xf jdk-17_linux-x64_bin.tar.gz -C /oldboyedu/softwares/

[root@jenkins211 ~]# cat /etc/profile.d/jdk.sh

!/bin/bash

export JAVA_HOME=/oldboyedu/softwares/jdk-17.0.8
export PATH=$PATH:$JAVA_HOME/bin
[root@jenkins211 ~]#
[root@jenkins211 ~]# source /etc/profile.d/jdk.sh
[root@jenkins211 ~]#
[root@jenkins211 ~]#
[root@jenkins211 ~]# java --version
java 17.0.8 2023-07-18 LTS
Java(TM) SE Runtime Environment (build 17.0.8+9-LTS-211)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.8+9-LTS-211, mixed mode, sharing)
[root@jenkins211 ~]#

4 安装Jenkins环境

apt-get install jenkins # 这种在线方式安装较慢,不建议。

[root@jenkins211 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/jenkins_2.426.3_all.deb

[root@jenkins211 ~]# dpkg -i jenkins_2.426.3_all.deb

5 修改Jenkins的启动脚本并重启服务

[root@jenkins211 ~]# vim /lib/systemd/system/jenkins.service
......
User=root
Group=root

...
Environment="JENKINS_HOME=/var/lib/jenkins"

找到上面一行后添加如下一行

Environment="JAVA_HOME=/oldboyedu/softwares/jdk-17.0.8"

[root@jenkins211 ~]# systemctl daemon-reload
[root@jenkins211 ~]#
[root@jenkins211 ~]# systemctl restart jenkins

6 访问Jenkins的WebUI

[root@jenkins211 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword

7 选择插件来安装

见视频。

8 跳过插件安装

见视频。

9 继续使用admin账号

见视频。

10 配置访问Jenkins的URL

见视频。

10 开始使用Jenkins

见视频。

  • Jenkins的基础配置
    1 Jenkins的管理员密码和时区设置
    1.1 点击配置按钮
    见视频。

     1.2 修改密码和时区
    

见视频。

	1.3 使用新密码重新登录

见视频。

2 Jenkins的软件源配置
	2.1 修改插件国内镜像源地址

[root@jenkins211 ~]# sed -i.bak 's#updates.jenkins.io/download#mirror.tuna.tsinghua.edu.cn/jenkins#g' /var/lib/jenkins/updates/default.json

	2.2 修改搜索引擎地址

[root@jenkins211 ~]# sed -i 's#www.google.com#www.baidu.com#g' /var/lib/jenkins/updates/default.json

	2.3 将升级站点URL替换成国内镜像地址

修改地址:
https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

  • 安装Jenkins常用插件
    1 安装Jenkins中文插件,需重启

    2 安装git插件

    3 安装的webhook插件

    4 安装pipeline插件

    5 安装ansible插件

    6 安装email插件

    7 安装DingTalk插件

    8 安装微信插件

    9.安装git参数化构建
    Git Parameter

    10 一键导入插件

将我下载好的软件包导入到"/var/lib/jenkins/plugins"目录并重启Jenkins环境。
[root@jenkins211 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/oldboyedu-jenkins211-plugins.tar.gz

[root@jenkins211 ~]# tar xf oldboyedu-jenkins211-plugins.tar.gz -C /var/lib/jenkins/plugins/

[root@jenkins211 ~]# systemctl restart jenkins

  • 推送代码到gitee
    1.新建代码仓库
    略,见视频。

    2.模拟开发人员编写代码
    [root@worker233 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/oldboyedu-yiliao.zip
    [root@worker233 ~]#
    [root@worker233 ~]# mkdir oldboyedu-linux92
    [root@worker233 ~]#
    [root@worker233 ~]# apt -y install unzip
    [root@worker233 ~]#
    [root@worker233 ~]# unzip oldboyedu-yiliao.zip -d oldboyedu-linux92/
    [root@worker233 ~]#
    [root@worker233 ~]# cd oldboyedu-linux92/
    [root@worker233 oldboyedu-linux92]#
    [root@worker233 oldboyedu-linux92]# ll
    total 224
    drwxr-xr-x 5 root root 4096 Aug 2 16:22 ./
    drwx------ 10 root root 4096 Aug 2 16:21 ../
    -rw-r--r-- 1 root root 16458 Jun 13 2019 about.html
    -rw-r--r-- 1 root root 20149 Jun 13 2019 album.html
    -rw-r--r-- 1 root root 19662 Jun 13 2019 article_detail.html
    -rw-r--r-- 1 root root 18767 Jun 13 2019 article.html
    -rw-r--r-- 1 root root 18913 Jun 13 2019 comment.html
    -rw-r--r-- 1 root root 16465 Jun 13 2019 contact.html
    drwxr-xr-x 2 root root 4096 Sep 19 2022 css/
    drwxr-xr-x 5 root root 4096 Sep 19 2022 images/
    -rw-r--r-- 1 root root 29627 Jun 29 2019 index.html
    drwxr-xr-x 2 root root 4096 Sep 19 2022 js/
    -rw-r--r-- 1 root root 24893 Jun 13 2019 product_detail.html
    -rw-r--r-- 1 root root 20672 Jun 13 2019 product.html
    [root@worker233 oldboyedu-linux92]#

    3.编写Dockerfile
    [root@worker233 oldboyedu-linux92]# cat Dockerfile
    FROM registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1

MAINTAINER JasonYin

LABEL auther=jasonyin
school=oldboyedu
class=linux92

EXPOSE 80

WORKDIR /usr/share/nginx/html/

ADD . /usr/share/nginx/html/

[root@worker233 oldboyedu-linux92]#

4.Git 全局设置【写你的账号密码信息】

[root@worker233 oldboyedu-linux92]# git config --global user.name "jasonyin2020"
[root@worker233 oldboyedu-linux92]# git config --global user.email "[email protected]"
[root@worker233 oldboyedu-linux92]#

5.初始化git项目

[root@worker233 oldboyedu-linux92]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m
Initialized empty Git repository in /root/oldboyedu-linux92/.git/
[root@worker233 oldboyedu-linux92]#

6.推送代码到本地仓库

[root@worker233 oldboyedu-linux92]# git add .
[root@worker233 oldboyedu-linux92]# git commit -m 'add dockerfile'

7.添加远程仓库

[root@worker233 oldboyedu-linux92]# git remote add origin https://gitee.com/jasonyin2020/oldboyedu-linux92.git
[root@worker233 oldboyedu-linux92]#
[root@worker233 oldboyedu-linux92]# git remote -v
origin https://gitee.com/jasonyin2020/oldboyedu-linux92.git (fetch)
origin https://gitee.com/jasonyin2020/oldboyedu-linux92.git (push)
[root@worker233 oldboyedu-linux92]#

8.推送文件到远程gitee仓库 

[root@worker233 oldboyedu-linux92]# git push -u origin "master"
Username for 'https://gitee.com': jasonyin2020 # 写你自己的账号,我的账号是"jasonyin2020",你写你自己的账号。
Password for 'https://[email protected]': # 写你自己的密码
Enumerating objects: 91, done.
Counting objects: 100% (91/91), done.
Delta compression using up to 2 threads
Compressing objects: 100% (91/91), done.
Writing objects: 100% (91/91), 1.48 MiB | 3.27 MiB/s, done.
Total 91 (delta 12), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/jasonyin2020/oldboyedu-linux92.git

  • Jenkins和gitee的连通性测试
    1.新建Jenkins的自由风格项目
    略,见视频。

    2.设置gitee仓库信息
    略,见视频。

    3.添加测试命令
    pwd
    ls -l ./

    4.立即构建
    略,见视频。

    5.验证是否构建成功
    略,见视频。

  • Jenkins从gitee拉取代码并推送镜像
    1.创建harbor仓库
    略,见视频。

    2.修改Jenkins的项目配置的脚本内容
    docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v1 .
    docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v1

    3.Jenkins节点安装docker环境
    [root@jenkins211 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/oldboyedu-docker-compose-binary-install.tar.gz

[root@jenkins211 ~]# tar xf oldboyedu-docker-compose-binary-install.tar.gz

[root@jenkins211 ~]# ./install-docker.sh install

4.添加hosts解析,添加harbor主机的解析记录。

[root@jenkins211 ~]# echo 10.0.0.250 harbor.oldboyedu.com >> /etc/hosts

5.拷贝harbor的自建客户端证书

[root@jenkins211 ~]# mkdir -pv /etc/docker/certs.d/harbor.oldboyedu.com
mkdir: created directory '/etc/docker/certs.d'
mkdir: created directory '/etc/docker/certs.d/harbor.oldboyedu.com'
[root@jenkins211 ~]#
[root@jenkins211 ~]# scp harbor.oldboyedu.com:/oldboyedu/softwares/harbor/certs/docker-client/* /etc/docker/certs.d/harbor.oldboyedu.com
[root@jenkins211 ~]#
[root@jenkins211 ~]# ll /etc/docker/certs.d/harbor.oldboyedu.com
total 20
drwxr-xr-x 2 root root 4096 Aug 2 16:54 ./
drwxr-xr-x 3 root root 4096 Aug 2 16:53 ../
-rw-r--r-- 1 root root 2049 Aug 2 16:54 ca.crt
-rw-r--r-- 1 root root 2147 Aug 2 16:54 harbor.oldboyedu.com.cert
-rw------- 1 root root 3272 Aug 2 16:54 harbor.oldboyedu.com.key
[root@jenkins211 ~]#

6.登录harbor仓库

[root@jenkins211 ~]# docker login -u admin -p 1 harbor.oldboyedu.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@jenkins211 ~]#

7.开始构建镜像并推送harbor仓库

略,见视频。

8.检查harbor项目是否推送成功。

略。

  • Jenkins一键更新K8S应用
    1.K8S部署医疗项目
    [root@master231 01-yiliao]# cat 01-ns-yiliao.yaml
    apiVersion: v1
    kind: Namespace
    metadata:
    labels:
    school: oldboyedu
    class: linux92
    name: yiliao
    [root@master231 01-yiliao]#
    [root@master231 01-yiliao]# cat 02-deploy-yiliao.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: deploy-yiliao
    namespace: yiliao
    spec:
    replicas: 3
    selector:
    matchLabels:
    apps: yiliao
    template:
    metadata:
    labels:
    apps: yiliao
    spec:
    containers:
    - name: yiliao
    image: harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v1
    [root@master231 01-yiliao]#
    [root@master231 01-yiliao]#
    [root@master231 01-yiliao]# cat 03-svc-yiliao.yaml
    apiVersion: v1
    kind: Service
    metadata:
    name: yiliao-svc
    namespace: yiliao
    spec:
    type: NodePort
    selector:
    apps: yiliao
    ports:
    • port: 80
      targetPort: 80
      nodePort: 30080
      [root@master231 01-yiliao]#
      [root@master231 01-yiliao]# kubectl apply -f .
      namespace/yiliao created
      deployment.apps/deploy-yiliao created
      service/yiliao-svc created
      [root@master231 01-yiliao]#
      [root@master231 01-yiliao]# kubectl get svc,po -n yiliao
      NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
      service/yiliao-svc NodePort 10.200.4.151 80:30080/TCP 88s

NAME READY STATUS RESTARTS AGE
pod/deploy-yiliao-79c8886b59-6qlbf 1/1 Running 0 88s
pod/deploy-yiliao-79c8886b59-c7dbw 1/1 Running 0 88s
pod/deploy-yiliao-79c8886b59-n8r9p 1/1 Running 0 88s
[root@master231 01-yiliao]#

2.访问测试 

http://10.0.0.233:30080/

3.Jenkins更新K8S应用
	3.1 下载kubect的详细步骤

https://www.cnblogs.com/yinzhengjie/p/17981419#31-下载k8s二进制软件版

svip:
[root@jenkins211 ~]# wget http://192.168.16.253/Linux92/Kubernetes/day19-/softwares/jenkins/kubectl-1.23.17

	3.2 将kubectl放到PATH环境变量

[root@jenkins211 ~]# mv kubectl-1.23.17 /usr/local/sbin/kubectl
[root@jenkins211 ~]#
[root@jenkins211 ~]# chmod +x /usr/local/sbin/kubectl

	3.3 拷贝master节点的证书

[root@jenkins211 ~]# mkdir -pv ~/.kube/
[root@jenkins211 ~]#
[root@jenkins211 ~]# scp 10.0.0.231:/root/.kube/config ~/.kube/

	3.4 修改Jenkins脚本内容

docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v3 .
docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v3
kubectl -n yiliao set image deploy deploy-yiliao yiliao=harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v3

	3.5 测试验证

略。

  • Jenkins自定义文本参数进行版本构建
    1.添加文本参数变量
    略,见视频。

      2.脚本内容
    

docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$version .
docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$version
kubectl -n yiliao set image deploy deploy-yiliao yiliao=harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$version

  • Jenkins自定义选项参数进行版本构建
    1.添加选项参数变量
    略,见视频。

      2.脚本内容
    

docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$tag .
docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$tag
kubectl -n yiliao set image deploy deploy-yiliao yiliao=harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$tag

  • Jenkins基于git参数化构建
    1.添加git参数变量
    注意选择分支,参数类型是"修订",可以基于"标签",前提是你推过标签。

    2.脚本内容
    docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$release .
    docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$release
    kubectl -n yiliao set image deploy deploy-yiliao yiliao=harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:$release

  • Jenkins实现回滚
    略。

  • 今日内容回顾:

    • rs和rc的区别 *

    • deploy控制器 ****

      • 1.底层调用的是rs;
      • 2.支持声明式更新;
      • 3.默认有滚动更新策略;
    • Jenkins和K8S集成实现CI/CD *****

      • Jenkins的环境部署
      • 插件导入
      • 推送代码到gitee
      • Jenkins拉取代码编译推送到habor仓库
      • Jenkins更新K8S应用
      • Jenkins回滚
    • 镜像拉取策略:**
      Always
      IfNotPresent
      Never

周末作业:
- 完成课堂的所有练习并整理思维导图;
- 将Jenkins实现邮件,钉钉,企业微信告警功能,可以集成sonerQube,maven,...进行集成;

扩展:
- 1.使用pipeline语法改写自由风格的代码;
- 2.在网上找一个开发代码再跑一遍咱们的环境;

标签:oldboyedu,master231,deploy,19,CICD,apps,xiuxian,root
From: https://www.cnblogs.com/colorsound/p/18340911

相关文章

  • Aspose.Slides.NET 19.2 解析ppt内容保存svg 保存ppt内部图片
    //LoadPowerPointpresentationPresentationpresentation=newPresentation("D:\\work\\code\\demo\\WPFPPT\\test.ppt");varslide=presentation.Slides[3];//SaveslideasSVGvarstream=newFileStream(@"D:\图像{0}.svg",......
  • CF1946F Nobody is needed 题解
    Nobodyisneeded推销我的洛谷博客。题意多组数据。给定一个长度为\(n\)的排列\(a\),你需要回答\(q\)组询问,每组询问给出\(l,r\),求有多少个子序列\(t\)使得:\(l\leqslantt_1<t_2<\cdots<t_k\leqslantr\)。\(a_{t_i}|a_{t_{i+1}}(1\leqslanti<k)\)......
  • 机械学习—零基础学习日志(高数19——函数极限理解深化)
    零基础为了学人工智能,真的开始复习高数本次学习笔记,主要讲解函数极限的计算问题。极限四则运算规则这里有几个需要注意的地方。函数极限的四则运算,需要知道极限存在才能大胆放心的使用。而且使用超实数的概念会更好帮助我们理解,极限的运算。以下图来说。大量的同学,会直接......
  • GEE案例:Landsat 5、7、8影像构建1985-2023年rsei生态遥感指数详细代码
    之前关于RSEI的博客 GoogleEarthEngine(GEEÿ......
  • 1990-2022年 上市公司-战略差异度(原始数据、计算代码、参考文献和最终计算结果)
    上市公司战略差异度是衡量企业在战略制定和实施过程中所展现的独特性和创新性的指标。它体现了公司对市场环境、行业趋势及自身能力的独特见解和战略布局。通过分析上市公司的战略差异度,可以深入理解企业的市场竞争策略、行业定位和发展方向。战略差异度的重要性市场竞争力:战......
  • CF1883E+CF1995C-对数+贪心
    CF1883E+CF1995C对数+贪心CF1883ELookBack大致题意给你一个整数数组$a_1,a_2,…,a_n$。你需要用最少的运算次数使数组不递减。在一次操作中,您需要执行以下操作:选择一个索引\(1\leqi\leqn\)、设置$a_i=a_i⋅2$.数组\(b_1,b_2,…,b_n\)在所有$1\leqi\l......
  • VS2019: LNK2019 无法解析的外部符号 __imp__invalid_parameter
    VS2019开发一个项目,报错:如下,errorLNK2001:unresolvedexternalsymbol__imp___CrtDbgReport errorLNK2001:unresolvedexternalsymbol__imp___invalid_parametererrorLNK2001:unresolvedexternalsymbol__imp___CrtDbgReportW errorLNK2001:unresolvedexterna......
  • wps 最新 2019 专业版 下载安装教程,解锁全部功能,免费领取
    文章目录前言软件介绍软件下载安装步骤激活步骤小福利(安卓APP)软件介绍软件下载安装步骤前言本篇文章主要针对WPS的安装下载进行详细讲解,软件已激活,可放心使用;并且可以进行账号登录,进行云文档存储、编辑、分享,所有vip功能均可使用;没有限制;有任何问题可以在评论区讨论......
  • 利用 Oracle 19c 新特性 from service 修复备库 GAP
    转发自:https://mp.weixin.qq.com/s/Jz8lEQ6QAnjoTeErbX0q_g前 言相信我们DBA在OracleDataGuard环境中遇到过因主库归档空间有限,归档日志又没有备份,空间满的时候直接删除了归档,导致丢失归档日志,而备库还没有及时接收到这个归档,导致备库出现了GAP现象。因为日志的中断,备......
  • springboot 19种参数接收方式
    1.@PathVariable用来处理URL路径中的参数。@GetMapping("/user/{id}")publicStringgetUser(@PathVariableStringid){return"UserID:"+id;}2.@RequestParam用于处理URL中的查询参数。@GetMapping("/user")publicStringgetUser(@RequestP......