环境准备!!!!
k8安装
gitlab安装
harbor安装
jenkins安装
要部署 Kubernetes 应用的流程如下:
编写代码
测试
编写 Dockerfile
构建打包 Docker 镜像
推送 Docker 镜像到仓库
编写 Kubernetes YAML 文件
更改 YAML 文件中 Docker 镜像 TAG
利用 kubectl 工具部署应用
权限问题解决:
usermod -a -G docker jenkins
chmod a+rw /var/run/docker.sock
chmod 777 /k8s/k8s.yaml
visudo
jenkins ALL=(ALL) NOPASSWD: ALL
[root@k8s-master cicd]# vim Dockerfile
[root@k8s-master cicd]# cat Dockerfile
FROM alpine:3.10
MAINTAINER cicd "31817XXXXX@qq.ocm"
WORKDIR /root/cicd
apiVersion: app/v1
kind: Deployment
metadata:
name: jenkins-demo
spec:
selector:
matchLabels:
app: jenkins-demo
template:
metadata:
labels:
app: jenkins-demo
spec:
containers:
- image: cnych/jenkins-demo:>
imagePullPolicy: IfNotPresent
name: jenkins-demo
env:
- name: branch
value: >
[root@k8s-master k8s]# kubectl create namespace jenkins-demo
namespace/jenkins-demo created
pipeline {
agent any
stages {
stage('clone') {
steps {
echo "1.Clone Stage"
withCredentials([usernamePassword(credentialsId: 'gitlab', passwordVariable: 'gitlabPassword', usernameVariable: 'gitlabUser')]) {
git branch: 'main', url: "http://${gitlabUser}:${gitlabPassword}@192.168.47.102/cicd/cicd.git"
}
git branch: 'main', url: 'http://root:12345678@192.168.47.102/cicd/cicd.git'
script {
build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
}
}
stage('test') {
steps {
echo "2.Test Stage"
}
}
stage('Build') {
steps {
echo "3.Build Docker Image Stage"
sh "docker build -t 192.168.47.101:80/library/jenkins-demo:${build_tag} ."
}
}
stage('Push') {
steps {
echo "4.Push Docker Image Stage"
//sh "docker login 192.168.47.101:80 -u admin -p 123456"
//sh "docker push cnych/jenkins-demo:${build_tag}"
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
sh "docker login 192.168.47.101:80 -u ${harborUser} -p ${harborPassword}"
sh "docker push 192.168.47.101:80/library/jenkins-demo:${build_tag}"
}
}
}
stage('Deploy') {
input {
message 'Choose a deploy environment'
ok 'submit'
parameters {
choice choices: ['Dev','QA','main'], name:'userInput'
}
}
steps {
echo "5. Deploy Stage"
echo "This is a deploy step to ${userInput}"
sh '''cd /k8s
sed -i 's//${build_tag}/g' k8s.yaml
sed -i 's//${userInput}/g' k8s.yaml'''
script {
if (userInput == "Dev") {
// deploy dev stuff
} else if (userInput == "QA"){
// deploy qa stuff
} else {
// deploy prod stuff
}
}
sh '''sudo kubectl apply -f /k8s/k8s.yaml'''
}
}
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hLUTYYud-1666084178398)(云原生学习笔记.assets/image-20221008184321973.png)]](https://1000bd.com/contentImg/2024/05/23/0a1cb494758c4311.png)