jenkins 集成k8s项目实战
1.在gitee创建项目
注册账号,在gitee上创建一个名为"baimeibaimei-linux"的项目。
登录地址:
https://gitee.com/
2.将医疗器械源代码推送到gitee
2.1 安装git工具
yum -y install git
2.2 Git 全局设置
git config --global user.name ""
git config --global user.email ""
2.3 初始化代码仓库
mkdir baimei-linux && cd baimei-linux
git init
2.4 下载项目开发源代码
unzip baimei-yiliao.zip
rm -f baimei-yiliao.zip
2.5 编写Dockerfile及编译脚本
cat Dockerfile
FROM harbor.baimei.com/baimei-web/nginx:1.25.1-alpine
LABEL school=baimei \
class=linux \
office=www.baimei.com
EXPOSE 80
ADD . /usr/share/nginx/html
测试 自己 Dockerfile 是否正常运行:
打包镜像
cat build.sh
#!/bin/bash
RELEASE=$1
docker build -t harbor.baimei.com/baimei-jenkins/yiliao:v${RELEASE:-1} .
docker push harbor.baimei.com/baimei-jenkins/yiliao:v${RELEASE:-1}
2.6 将文件提交到本地仓库
git add .
git commmit -m 'baimei linux first commit'
2.7 提交代码到远程仓库
git remote add origin
git push -u origin "master"
输入, 131611, 158a
2.8 访问gitee验证代码,你访问自己节点就行
https://gitee.com/wang-huamao/yiliao
Jenkins集成K8S项目之Jenkins参数化构建docker镜像并推送到harbor仓库
1.安装docker环境
jenkins 主机需要安装 docker环境
root@jenkins166:~# wget http://192.168.11.253/Kubernetes/day11-/softwares/baimei-docker-binary-install-20.10.tar.gz
root@jenkins166:~#
root@jenkins166:~# tar xf baimei-docker-binary-install-20.10.tar.gz
root@jenkins166:~#
root@jenkins166:~# ./install-docker.sh install
2.添加docker的hosts解析
echo 10.0.0.250 harbor.baimei.com >> /etc/hosts
3.添加harbor的证书文件到Jenkins节点。
[root@harbor250 client]# scp -r /etc/docker/certs.d/ 10.0.0.166:/etc/docker/
4.在habor服务创建Jenkins普通用户
用户名: jenkins
密 码: Linux@2023
5.Jenkins登录harbor私有仓库
docker login -u jenkins -p Linux@2023 harbor.baimei.com
这一步只要成功了,基本下边的就没问题了。
6.Jenkins构建gitee项目
Jenkins集成K8S项目之业务迁移K8S:
1.Jenkins安装kubectl工具
1.1 配置kubectl软件源
root@jenkins166:~# cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
EOF
1.2 安装apt-key认证信息
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
1.3 更新软件源
apt-get update
1.4 验证是否支持软件源,查看支持的kubectl版本
apt-cache madison kubectl
1.5 开始安装指定的kubectl版本
apt-get -y install kubectl=1.23.17-00
1.6 验证kubectl的版本号
该步骤只能获取到Client Version信息,而无法获取到Server Version,属于正常现象。继续进行下一步即可解决。
kubectl version
master231节点将管理员的kubeconfig文件移动到Jenkins节点。
需要获取一些配置信息
[root@master231 ~]# scp /root/.kube/config 10.0.0.166:/root/.kube/
1.7 再次验证kubectl的版本号
kubectl version
2.创建harbor的登录凭据信息
2.1 响应式创建
先进行响应式创建, 然后再 根据 模板来修改
[root@master231 01-yiliao]# kubectl create secret docker-registry harbor-jenkins --docker-username=jenkins --docker-password=Linux@2023 --docker-email=jenkins@baimei.com --docker-server=harbor.baimei.com
kubectl get secrets harbor-jenkins -o yaml
2.2 声明式创建
[root@master231 01-yiliao]# cat 01-secrets-yiliao.yaml
apiVersion: v1
data:
.dockerconfigjson: eyJhdXRocyI6eyJoYXJib3Iub2xkYm95ZWR1LmNvbSI6eyJ1c2VybmFtZSI6ImplbmtpbnMiLCJwYXNzd29yZCI6IkxpbnV4QDIwMjMiLCJlbWFpbCI6ImplbmtpbnNAb2xkYm95ZWR1LmNvbSIsImF1dGgiOiJhbVZ1YTJsdWN6cE1hVzUxZUVBeU1ESXoifX19
kind: Secret
metadata:
name: harbor-jenkins
namespace: default
type: kubernetes.io/dockerconfigjson
3.创建deployment资源
cat 02-deploy-yiliao.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-yiliao
spec:
replicas: 3
selector:
matchExpressions:
- key: apps
values:
- "yiliao"
operator: In
template:
metadata:
labels:
apps: yiliao
school: baimei
class: linu86
spec:
imagePullSecrets:
- name: harbor-jenkins
containers:
- name: yiliao
image: harbor.baimei.com/baimei-jenkins/yiliao:v10
4.创建service资源
cat 03-svc-yiliao.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-yiliao
spec:
type: NodePort
selector:
apps: yiliao
ports:
- port: 80
targetPort: 80
nodePort: 30080
5.修改Jenkins的WebUI,配置如下:
./build.sh $version
kubectl set image deploy deploy-yiliao yiliao=harbor.baimei.com/baimei-jenkins/yiliao:v$version
dockerfile
build.sh
#!/bin/bash
RELEASE=$1
docker build -t harbor.baimei.com/baimei-jenkins/yiliao:v${RELEASE:-1} .
docker push harbor.baimei.com/baimei-jenkins/yiliao:v${RELEASE:-1}
Dockfile
FROM harbor.baimei.com/baimei-web/nginx:1.25.1-alpine
LABEL school=baimei \
class=linux86 \
office=www.baimei.com
EXPOSE 80
ADD . /usr/share/nginx/html
主要配置这3个地方。
Jenkins项目完善需求:
1.gitlab基于webhook自动拉取gitlab代码;
2.配置Jenkins邮件告警,无论成功或失败,均要发送邮件给运维或者项目开发负责人,相关业务负责人;
3.每次构建的镜像tag可以为每次代码提交的commit id;