gitlab+docker+harbor+k8s+jenkins部署简单应用
gitlab+docker+harbor+k8s+jenkins部署简单应用
·
部署简单的K8s 应用
环境准备!!!!
k8安装
gitlab安装
harbor安装
jenkins安装
要部署 Kubernetes 应用的流程如下:
-
编写代码
-
测试
-
编写 Dockerfile
-
构建打包 Docker 镜像
-
推送 Docker 镜像到仓库
-
编写 Kubernetes YAML 文件
-
更改 YAML 文件中 Docker 镜像 TAG
-
利用 kubectl 工具部署应用
权限问题解决:
- 前面jenkins是用yum安装的,默认启动用户为jenkins,会有很多权限问题。建议更改启动用户,我这里直接修改了相关权限
usermod -a -G docker jenkins
chmod a+rw /var/run/docker.sock
chmod 777 /k8s/k8s.yaml
visudo
jenkins ALL=(ALL) NOPASSWD: ALL
- Dockerfile(需要上传到项目中再clone)
[root@k8s-master cicd]# vim Dockerfile
[root@k8s-master cicd]# cat Dockerfile
FROM alpine:3.10
MAINTAINER cicd "31817XXXXX@qq.ocm"
WORKDIR /root/cicd
- 部署的yaml文件(/tmp/k8s.yaml)
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:<BUILD_TAG>
imagePullPolicy: IfNotPresent
name: jenkins-demo
env:
- name: branch
value: <BRANCH_NAME>
[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>/${build_tag}/g' k8s.yaml
sed -i 's/<BRANCH_NAME>/${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'''
}
}
}
}
更多推荐
所有评论(0)