1)编写资源清单并提交到gitee
1.编写资源清单
[root@worker233 oldboyedu-linux101-yiliao]# cat deploy-svc-yiliao.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-yiliao
spec:
replicas: 3
selector:
matchLabels:
apps: yiliao
template:
metadata:
labels:
apps: yiliao
spec:
containers:
- name: c1
image: harbor250.oldboyedu.com/oldboyedu-yiliao/demo:v0.1
---
apiVersion: v1
kind: Service
metadata:
name: svc-yiliao
spec:
type: LoadBalancer
selector:
apps: yiliao
ports:
- port: 80
[root@worker233 oldboyedu-linux101-yiliao]#
2.提交资源清单代码到gitee
[root@worker233 oldboyedu-linux101-yiliao]# git add deploy-svc-yiliao.yaml
[root@worker233 oldboyedu-linux101-yiliao]#
[root@worker233 oldboyedu-linux101-yiliao]# git commit -m 'add k8s yiliao manifests'
[master 048d3db] add k8s yiliao manifests
1 file changed, 32 insertions(+)
create mode 100644 deploy-svc-yiliao.yaml
[root@worker233 oldboyedu-linux101-yiliao]#
[root@worker233 oldboyedu-linux101-yiliao]# git push origin master
Username for 'https://gitee.com': yinzhengjie
Password for 'https://yinzhengjie@gitee.com':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 556 bytes | 556.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag d14f4da9
To https://gitee.com/yinzhengjie/oldboyedu-linux101-yiliao.git
cf2df7f..048d3db master -> master
[root@worker233 oldboyedu-linux101-yiliao]#
3.gitee查看远程仓库
2)jenkins部署医疗项目到K8S集群
1.jenkins安装kubectl客户端工具管理K8S集群
[root@jenkins211 ~]# wget http://192.168.21.253/Resources/Kubernetes/Project/DevOps/Jenkins/kubectl-1.23.17
[root@jenkins211 ~]# mv kubectl-1.23.17 /usr/local/bin/kubectl
[root@jenkins211 ~]# chmod +x /usr/local/bin/kubectl
2.准备认证文件
[root@jenkins211 ~]# mkdir -p .kube
[root@jenkins211 ~]# scp 10.0.0.231:/root/.kube/config .kube
root@10.0.0.231’s password: #输入密码
config 100% 5634 10.7MB/s 00:00
[root@jenkins211 ~]#
[root@jenkins211 ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.17", GitCommit:"953be8927218ec8067e1af2641e540238ffd7576", GitTreeState:"clean", BuildDate:"2023-02-22T13:34:27Z", GoVersion:"go1.19.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.17", GitCommit:"953be8927218ec8067e1af2641e540238ffd7576", GitTreeState:"clean", BuildDate:"2023-02-22T13:27:46Z", GoVersion:"go1.19.6", Compiler:"gc", Platform:"linux/amd64"}
[root@jenkins211 ~]#
[root@jenkins211 ~]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master231 Ready control-plane,master 16d v1.23.17 10.0.0.231 <none> Ubuntu 22.04.4 LTS 5.15.0-163-generic docker://20.10.24
worker232 Ready <none> 16d v1.23.17 10.0.0.232 <none> Ubuntu 22.04.4 LTS 5.15.0-163-generic docker://20.10.24
worker233 Ready <none> 6d22h v1.23.17 10.0.0.233 <none> Ubuntu 22.04.4 LTS 5.15.0-163-generic docker://20.10.24
[root@jenkins211 ~]#
3.修改jenkins的构建命令(此操作会在jk节点执行)
docker build -t harbor250.oldboyedu.com/oldboyedu-yiliao/demo:v0.1 .
docker login -u admin -p 1 harbor250.oldboyedu.com
docker push harbor250.oldboyedu.com/oldboyedu-yiliao/demo:v0.1
docker logout harbor250.oldboyedu.com
kubectl apply -f deploy-svc-yiliao.yaml
kubectl get deploy,svc,pods -o wide
3)参数化构建之选项参数实战
1.修改源代码 #gitee里也可以修改,修改后保存即可
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# vim code/index.html # 自行修改
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git commit -am 'update test v0.3'
[master 28aa42a] update test v0.3
1 file changed, 1 insertion(+), 1 deletion(-)
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git push origin master
Username for 'https://gitee.com': yinzhengjie
Password for 'https://yinzhengjie@gitee.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 466 bytes | 466.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 9445ecb2
To https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git
b79556d..28aa42a master -> master
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
2.添加jenkins变量
假设变量名称为'version'
3.修改编译的脚本内容引用变量
set +e # 表示就算后续的命令如果返回值不是0,那么脚本依然向下执行;其中'set -e'表示立刻停止(是严格模式)
docker build -t harbor250.oldboyedu.com/oldboyedu-yiliao/demo:$version .
docker login -u admin -p 1 harbor250.oldboyedu.com
docker push harbor250.oldboyedu.com/oldboyedu-yiliao/demo:$version
docker logout harbor250.oldboyedu.com
# deploy project
kubectl get deployments.apps deploy-yiliao 2>/dev/null
if [ $? -eq 0 ];then
#如果有上面的deploy-yiliao,就做更新;如果没有,就用默认的资源清单
kubectl set image deploy deploy-yiliao c1=harbor250.oldboyedu.com/oldboyedu-yiliao/demo:${version}
else
kubectl apply -f deploy-svc-yiliao.yaml
fi
kubectl get deploy,svc,po -o wide --show-labels
4.构建测试
5.访问测试。
4)git参数化构建实现发布和回滚
- 由于git的版本号不好记, 要在工作中做好留痕(发送到邮箱,钉钉等), 方便发布和回滚
1.添加git参数
先将之前的选项参数删除,再添加git参数。
2.再次修改代码
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# vim code/index.html
...
27 <body>
28 <h1 style='color: red;'>~</h1>
...
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git commit -am 'update test xixi'
[master bc46fd9] update test xixi
1 file changed, 1 insertion(+), 1 deletion(-)
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git push origin master
Username for 'https://gitee.com': yinzhengjie
Password for 'https://yinzhengjie@gitee.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 388 bytes | 388.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 3d4ae085
To https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git
28aa42a..bc46fd9 master -> master
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
3.查看提交的历史版本信息
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git log
commit bc46fd919969e6445b1ed3e58a619dd2e6d076db (HEAD -> master, origin/master)
Author: <8669059+yinzhengjie@user.noreply.gitee.com>
Date: Tue Dec 16 16:33:09 2025 +0800
update test xixi
commit 28aa42a4a10cdae108156fa3cf09400d37a9605c
Author: <8669059+yinzhengjie@user.noreply.gitee.com>
Date: Tue Dec 16 16:25:34 2025 +0800
update test v0.3
commit b79556d4cffdb0bed59fd29acf12245550510969
Author: <8669059+yinzhengjie@user.noreply.gitee.com>
Date: Tue Dec 16 15:29:07 2025 +0800
add manifests
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git reflog
bc46fd9 (HEAD -> master, origin/master) HEAD@{0}: commit: update test xixi
28aa42a HEAD@{1}: commit: update test v0.3
b79556d HEAD@{2}: commit (initial): add manifests
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
4.jenkins参数化构建
5.访问webUI
6.回滚版本(因为harbor已经有此镜像了,直接部署即可)
set +e # 表示就算后续的命令如果返回值不是0,那么脚本依然向下执行,其中'set -e'表示立刻停止。
kubectl get deployments.apps deploy-yiliao 2>/dev/null
if [ $? -eq 0 ];then
#如果有上面的deploy-yiliao,就做更新;如果没有,就用默认的资源清单
kubectl set image deploy deploy-yiliao c1=harbor250.oldboyedu.com/oldboyedu-yiliao/demo:${version}
else
kubectl apply -f deploy-svc-yiliao.yaml
fi
kubectl get deploy,svc,po -o wide --show-labels
5)优化代码结构并提交到gitee
1.修改目录结构
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# tree -L 2 -a
.
├── code # 源代码文件
│ ├── about.html
│ ├── album.html
│ ├── article_detail.html
│ ├── article.html
│ ├── comment.html
│ ├── contact.html
│ ├── css
│ ├── images
│ ├── index.html
│ ├── js
│ ├── product_detail.html
│ └── product.html
├── Dockerfile # 编写的Dockerfile文件
├── .dockerignore # 忽略不必要的文件
├── .git
│ ├── branches
│ ├── COMMIT_EDITMSG
│ ├── config
│ ├── description
│ ├── HEAD
│ ├── hooks
│ ├── index
│ ├── info
│ ├── logs
│ ├── objects
│ └── refs
└── manifests # K8S集群部署的资源清单
├── deployments.yaml
└── services.yaml
12 directories, 18 files
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
2.查看Dockerfile文件内容
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# cat Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
MAINTAINER Jason Yin
RUN rm -rf /usr/share/nginx/html/*
# 不推荐这样使用,只要修改任意文件,就可能无法充分利用缓存技术!
# COPY . /usr/share/nginx/html/
# 推荐将源代码放在一个特定目录中进行拷贝即可。
COPY code/ /usr/share/nginx/html/
EXPOSE 80
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
3.查看忽略的文件内容
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# cat .dockerignore
manifests
.git
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
4.查看资源清单
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# cat manifests/deployments.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-yiliao
spec:
replicas: 3
selector:
matchLabels:
apps: yiliao
template:
metadata:
labels:
apps: yiliao
spec:
containers:
- name: c1
image: harbor250.oldboyedu.com/oldboyedu-yiliao/demo:v0.1
ports:
- containerPort: 80
name: web
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# cat manifests/services.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-yiliao
spec:
type: LoadBalancer
selector:
apps: yiliao
ports:
- port: 80
targetPort: web
nodePort: 8080
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
5.删除远程仓库项目重新创建(因为之前的环境存在扩展性差的问题!!!)
将gitee的'oldboyedu-yiliao-linux100'删除后重新创建。
6.提交代码到本地
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# rm -rf .git/ # 移除之前的环境
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git init
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git add .
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git commit -m 'add manifests'
6.提交代码到gitee
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git remote add origin https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git remote -v
origin https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git (fetch)
origin https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git (push)
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]# git push origin master
Username for 'https://gitee.com': yinzhengjie
Password for 'https://yinzhengjie@gitee.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 585 bytes | 585.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag c24569ba
To https://gitee.com/yinzhengjie/oldboyedu-yiliao-linux100.git
37e2714..ec359fe master -> master
[root@harbor250.oldboyedu.com oldboyedu-yiliao-linux100]#
7.gitee查看远程仓库