【jenkins】jenkins流水线+k8s+阿里云搭建自动部署
使用jenkins流水线,构建镜像推送到阿里云镜像仓库,再从镜像仓库拉取镜像部署到k8s集群
·
准备环境:
1、准备一台服务器A安装docker
2、服务器A上面安装好jenkins
3、准备一个k8s集群
一、jenkins将项目打包镜像上传到阿里云
1、创建阿里云私有仓库
2、Jenkins流水线配置
pipeline {
agent any
environment {
DOCKER_REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
DOCKER_REPOSITORY = 'doyo_k8s'
DOCKER_IMAGE = 'doyo_pic'
DOCKER_TAG = '0.0.1'
DOCKER_USERNAME = '你的用户名'
DOCKER_PASSWORD = '你的密码'
KUBECONFIG = credentials('kubernetes_id')
KUBE_NAMESPACE = 'doyo'
DEPLOYMENT_NAME = 'doyo_pic'
}
tools{
maven "maven3"
}
stages {
stage('拉取代码') {
steps {
git credentialsId: '4a502fac-8c7f-4235-93f9-92e1430d28bb', url: 'https://gitee.com/wang_du_yu_n/doyo_pic.git'
echo '拉取代码完成'
}
}
stage('开始构建') {
steps {
sh "mvn clean package"
echo '构建完成'
}
}
stage('创建镜像') {
steps {
sh "docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} ."
echo '镜像创建完成'
}
}
stage('推送镜像仓库') {
steps {
sh "docker login ${DOCKER_REGISTRY} -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
sh "docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
sh "docker push ${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
echo '镜像推送完成'
}
}
}
post {
always {
echo "完成"
}
failure {
echo "失败"
}
}
}
3、编写dockerfile
在路径/root/.jenkins/workspace/doyo_pic 下面创建dockerfile
cd /root/.jenkins/workspace/doyo_pic
vi dockerfile
FROM openjdk:11
EXPOSE 9003
# 设置工作目录
WORKDIR /root/.jenkins/workspace/doyo_pic
# 此处的.jar文件路径是相对于 dockerfile的路径
ENTRYPOINT ["java","-jar","target/*.jar"]
此时的目录结构:
4、开始构建
构建成功,去阿里云私有仓库中刷新,可以看见构建好的镜像
二、jenkins配置k8s从阿里云拉取镜像并部署
1、jenkins安装k8s插件
Kubernetes plugin
2、jenkins配置k8s
1、创建k8s凭据文件
检查/etc/kubernetes/admin.conf目录下有没有这个配置文件,有的话,生成kubeconfig.yaml配置文件,保存到本地。没有的话,需要找到自己k8s集群的admin.conf文件在哪里,然后生成配置文件保存下来。
sudo cat /etc/kubernetes/admin.conf > kubeconfig.yaml
注意修改endpoint为自己的集群ip:192.168.31.247
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1EWXdNakF6TURJd00xb1hEVE16TURVek1EQXpNREl3TTFvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTXFSCitDN2ZBdTd6TWsxekhFZ20vVkRwWlcwQTB4VGlDOHExTVJWaTBuczdpZlE2Z2dhRnRxVzAwLzNVV3hOS3NlNjkKOWFjamR4TENBOVcwSW1iM2NwRjROU2FzMmE2ZzFrRzNUTjZhRHl3NnFQcDBqZmZvK1ZRbGN5N1VUM3l0cG9HbgpiYTlQOG5JalQrM05hZjRpRFVBV2hZdkc3d2JEWHlhcWdUV2JMYjNsazFTd0hWWTNMb2w1c2ZRakM4eTk1eHJOClpScWtsWXhSNjhWOHJOdVhhcUFyK29BMFEyR0k0S1FsRGhhTXN6ZVF1TmRtaHd3Z1BRYkZxWGVrTHlzR251RXYKTkV6WlZtSWxKOG9HcWZ6T2lFWWdxeEdyZG1CUmovK3dSektpTTZ4Q0ZQTU1aWHgxbEFuMnVsYzl1eGNoeENmWQpsVmxkTzMwc1V0T3M4OHFtNjJzQ0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZOb2wzajNwZlhUaTdtM0JTTlhjYXJQcHBmL21NQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFESVI5V3k3cWplU3ZRLzNZQlV5NTFnenBNVE9paDlvdXJBSGtzcDR2TEJ1cDlSeko5dApPRkFRR21sRWdmQmxBRjgza292Y3JiWWp6eWxMUzRRcGhCcVR1QlFvaHpFbGNWa0M0c2hYWmFLUkVMdFpad2IxCld2eHBSMWI5dWx1RjlJdElleXlRZVdUQjUya01GUHhISEw0VWtkUXFoRFlXc3BzTVdqQWRBeklKSVR1UXRBM1gKcW5GU3lNM3pYRkc5Mm1xTkpuV1d2ZWF1REVpR3BLOEpEM21SdzZzNnJ1S3lmQjN5dW1vRE1yV0t6YlhlbEZNNwovUlVQOHgxdGFsd1ZGYk5QMFZZT0RiMFBUWURIWEpYeUN0azlENzN0OEg4OFROT2VkNHhQSkk5OUtsNmwrcXdQCmpKM0VzMnRHekltSWxTaTdXUktMYVBhZ01ueDgzNlhnOXlwSAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://192.168.31.247:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURFekNDQWZ1Z0F3SUJBZ0lJQ1diL0Z5V0NZNzB3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TXpBMk1ESXdNekF5T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GVG5QeDhFWVowY2VyaXl0R3F0bDRnSwpxdi9QazdjYXVpcDQ2aEpZcWJ0cERKbTNMSGIxMWxIL0FoTDhYcTE3L1ZwdmZadlBDUWZlOG14dUl1UGptZmJnClJoUlZBb0dCQUtKRUQzenFXVjJDbnh6bGlscEhFN084TkdJa1I2OFoyS0QvUVhuQ21HVDc4RU14QjNPemNDS1YKNTJEYmxJQ0RDWjlZNkhkVHhSOVhJZTJ0Q3Vjc0tzOWp0WVVuTHRwb1RDNVRMeVVRcXE1NFVSZTRmeUNXOUNVVQozbEdqbHdTZ20weUtMYmZGVTdRK29GL3orRWR1MElPRGJrdE40U3JIckE1MlZlNFJyQTk1Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
2、jekins创建k8s凭据
选择文件类型,选择生成的配置文件上传,点击创建即可。
3、新建k8s云节点
连接成功,表示jenkins配置k8s成功。
3、jenkins流水线配置
pipeline {
agent any
environment {
DOCKER_REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
DOCKER_REPOSITORY = 'doyo_k8s'
DOCKER_IMAGE = 'doyo_pic'
DOCKER_TAG = '0.0.1'
DOCKER_USERNAME = '17610481017'
DOCKER_PASSWORD = 'wz857741375+'
KUBECONFIG = credentials('kubernetes_id')
KUBE_NAMESPACE = 'doyo'
DEPLOYMENT_NAME = 'doyo_pic'
}
tools{
maven "maven3"
}
stages {
stage('拉取代码') {
steps {
git credentialsId: '4a502fac-8c7f-4235-93f9-92e1430d28bb', url: 'https://gitee.com/wang_du_yu_n/doyo_pic.git'
echo '拉取代码完成'
}
}
stage('打包') {
steps {
sh "mvn clean package"
echo '打包完成'
}
}
stage('构建镜像') {
steps {
sh "docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} ."
echo '镜像创建完成'
}
}
stage('推送镜像仓库') {
steps {
sh "docker login ${DOCKER_REGISTRY} -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
sh "docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
sh "docker push ${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
echo '镜像推送完成'
}
}
stage('拉取镜像') {
steps {
sh "docker login ${DOCKER_REGISTRY} -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}"
sh "docker pull ${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
echo '镜像拉取完成'
}
}
stage('部署') {
steps {
container('kubectl') {
withCredentials([file(credentialsId: 'kubernetes_id', variable: 'KUBECONFIG')]) {
sh "kubectl --kubeconfig=${KUBECONFIG} --namespace=${KUBE_NAMESPACE} create deployment ${DEPLOYMENT_NAME} --image=${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_IMAGE}:${DOCKER_TAG}"
}
}
echo '部署完成'
}
}
}
post {
always {
echo "完成"
}
failure {
echo "失败"
}
}
}
4、k8s pod配置(部署失败,解决方法待寻找)
apiVersion: apps/v1
kind: Deployment
metadata:
name: doyo-pic
labels:
app: doyo-pic
spec:
replicas: 1
selector:
matchLabels:
app: doyo-pic
template:
metadata:
labels:
app: doyo-pic
spec:
containers:
- name: doyo-pic
image: your-image-name:tag
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: your-service-name
spec:
selector:
app: your-app-label
ports:
- protocol: TCP
port: 80
targetPort: 8080
更多推荐
已为社区贡献1条内容
所有评论(0)