Edwa'Blog

如有错误,欢迎交流指正

导航

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

1.rs控制器

1.1 作用

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

1.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]# 

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

1.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 ~]# 

1.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]# 

2. deploy控制器

2.1作用

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

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

2.1 案例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 案例2-基于matchExpressions匹配

[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]# 

2.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]# 

2.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]# 

2.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   <none>           <none>
deloy-xiuxian-update-b8c754-97bh5   1/1     Running   0          35s   10.100.1.134   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-dgmzp   1/1     Running   0          35s   10.100.1.135   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-kpx6p   1/1     Running   0          35s   10.100.2.89    worker233   <none>           <none>
deloy-xiuxian-update-b8c754-wbc9q   1/1     Running   0          35s   10.100.2.88    worker233   <none>           <none>
[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    <none>         worker232   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-plszj   0/1     ContainerCreating   0          1s    <none>         worker233   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-tz78m   0/1     ContainerCreating   0          1s    <none>         worker232   <none>           <none>
deloy-xiuxian-update-b8c754-4s474       1/1     Running             0          54s   10.100.2.87    worker233   <none>           <none>
deloy-xiuxian-update-b8c754-97bh5       1/1     Running             0          54s   10.100.1.134   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-dgmzp       1/1     Running             0          54s   10.100.1.135   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-kpx6p       0/1     Terminating         0          54s   10.100.2.89    worker233   <none>           <none>
deloy-xiuxian-update-b8c754-wbc9q       1/1     Running             0          54s   10.100.2.88    worker233   <none>           <none>
[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    <none>         worker232   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-8tq8s   0/1     ContainerCreating   0          1s    <none>         worker233   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-m4kd2   0/1     ContainerCreating   0          3s    <none>         worker232   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-plszj   1/1     Running             0          3s    10.100.2.90    worker233   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-tz78m   1/1     Running             0          3s    10.100.1.136   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-4s474       1/1     Running             0          56s   10.100.2.87    worker233   <none>           <none>
deloy-xiuxian-update-b8c754-dgmzp       1/1     Running             0          56s   10.100.1.135   worker232   <none>           <none>
deloy-xiuxian-update-b8c754-wbc9q       1/1     Terminating         0          56s   10.100.2.88    worker233   <none>           <none>
[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   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-8tq8s   1/1     Running   0          106s   10.100.2.91    worker233   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-m4kd2   1/1     Running   0          108s   10.100.1.137   worker232   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-plszj   1/1     Running   0          108s   10.100.2.90    worker233   <none>           <none>
deloy-xiuxian-update-5f77fddbd7-tz78m   1/1     Running   0          108s   10.100.1.136   worker232   <none>           <none>
[root@master231 ~]# 

3. deploy实现响应式更新和回滚:

3.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]# 

3.2 查看应用发布的历史版本

[root@master231 deploy]# kubectl rollout history deployment deloy-xiuxian-update-strategy 
deployment.apps/deloy-xiuxian-update-strategy 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>

[root@master231 deploy]# 

3.3 查看当前应用的部署状态

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

3.4 暂停更新

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

3.5 恢复更新

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

3.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         <none>
4         <none>
5         <none>

[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         <none>
5         <none>
6         <none>

[root@master231 deploy]# 

3.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]# 

4.Ubuntu系统部署Jenkins环境

参考链接:
https://www.oracle.com/java/technologies/downloads/#java17
https://pkg.jenkins.io/debian-stable/
https://mirrors.jenkins-ci.org/debian-stable/
https://mirrors.jenkins-ci.org/

4.0 检查Jenkins节点是否能够联网及时区修改

[root@jenkins211 ~]# ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
'/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai'
[root@jenkins211 ~]#

4.1 部署Jenkins的秘钥

···bash
[root@jenkins211 ~]# curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee
/usr/share/keyrings/jenkins-keyring.asc > /dev/null

## 4.2 添加Jenkins的存储库
```bash
[root@jenkins211 ~]# echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
    https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
    /etc/apt/sources.list.d/jenkins.list > /dev/null

4.3 安装JRE环境

[root@jenkins211 ~]# apt-get update
[root@jenkins211 ~]# apt-get -y install fontconfig 

# 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.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

4.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

4.6 访问Jenkins的WebUI

10.0.0.211:8080

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

4.7 选择插件来安装

见视频。

4.8 跳过插件安装

见视频。

4.9 继续使用admin账号

见视频。

4.10 配置访问Jenkins的URL

见视频。

11 开始使用Jenkins

见视频。

5.Jenkins的基础配置

5.1 Jenkins的管理员密码和时区设置

5.1.1 点击配置按钮

见视频。

5.1.2 修改密码和时区

见视频。

5.1.3 使用新密码重新登录

见视频。

5.2 Jenkins的软件源配置

5.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 

5.2.2 修改搜索引擎地址

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

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

修改地址:

https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

6. 安装Jenkins常用插件

6.1 安装Jenkins中文插件,需重启

6.2 安装git插件

6.3 安装的webhook插件

6.4 安装pipeline插件

6.5 安装ansible插件

6.6 安装email插件

6.7 安装DingTalk插件

6.8 安装微信插件

6.9.安装git参数化构建

Git Parameter

6.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

7.推送代码到gitee

7.1 新建代码仓库

略,见视频。

7.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]# 

7.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]# 

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

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

7.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 <name>
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 <name>
Initialized empty Git repository in /root/oldboyedu-linux92/.git/
[root@worker233 oldboyedu-linux92]# 

7.6 推送代码到本地仓库

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

7.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]# 

7.8 推送文件到远程gitee仓库

[root@worker233 oldboyedu-linux92]# git push -u origin "master"
Username for 'https://gitee.com': jasonyin2020  # 写你自己的账号,我的账号是"jasonyin2020",你写你自己的账号。
Password for 'https://jasonyin2020@gitee.com':   # 写你自己的密码
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
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
[root@worker233 oldboyedu-linux92]# 

7.9 查看远程仓库代码的信息

https://gitee.com/jasonyin2020/oldboyedu-linux92

8.Jenkins和gitee的连通性测试

8.1 新建Jenkins的自由风格项目

略,见视频。

8.2 设置gitee仓库信息

略,见视频。

8.3 添加测试命令

···bash
pwd
ls -l ./

## 8.4 立即构建
略,见视频。
## 8.5 验证是否构建成功
略,见视频。
# 9.Jenkins从gitee拉取代码并推送镜像
## 9.1 创建harbor仓库
略,见视频。
## 9.2 修改Jenkins的项目配置的脚本内容
···bash
docker image build -t harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v1 .
docker push harbor.oldboyedu.com/oldboyedu-jenkins/yiliao:v1

9.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

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

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

9.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 ~]# 
···
## 9.6 登录harbor仓库
···bash
[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 ~]# 

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

略,见视频。

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

略。

10. Jenkins一键更新K8S应用

10.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   <none>        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]# 
···
##  10.2 访问测试 
···bash
http://10.0.0.233:30080/

10.3 Jenkins更新K8S应用

10.3.1 下载kubect的详细步骤

https://www.cnblogs.com/yinzhengjie/p/17981419#31-%E4%B8%8B%E8%BD%BDk8s%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AF%E4%BB%B6%E7%89%88

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

10.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

10.3.3 拷贝master节点的证书

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

10.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

10.3.5 测试验证

略。

11. Jenkins自定义文本参数进行版本构建

11.1 添加文本参数变量

略,见视频。

11.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

12. Jenkins自定义选项参数进行版本构建

12.1 添加选项参数变量

略,见视频。

12.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

13.Jenkins基于git参数化构建

13.1 添加git参数变量

注意选择分支,参数类型是"修订",可以基于"标签",前提是你推过标签。

13.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

14. Jenkins实现回滚

略。

今日内容回顾:
	- rs和rc的区别	*
	
	- deploy控制器 	****
		- 1.底层调用的是rs;
		- 2.支持声明式更新;
		- 3.默认有滚动更新策略;
		
	- Jenkins和K8S集成实现CI/CD	*****
		- Jenkins的环境部署 
		- 插件导入 
		- 推送代码到gitee
		- Jenkins拉取代码编译推送到habor仓库
		- Jenkins更新K8S应用
		- Jenkins回滚 
		
	
	- 镜像拉取策略:**
		Always
		IfNotPresent
		Never

posted on 2024-08-03 19:09  Edwa  阅读(79)  评论(0)    收藏  举报