Dockerfile 文件
# openjdk 8u222
FROM openjdk:8u222-jre
MAINTAINER 组织名称
LABEL description=XXXX平台
# 设置环境常量
ENV TZ=Asia/Shanghai \
# 工作目录名
WORKPATH=/部署项目名称
# 工作目录
WORKDIR /usr/local/workspace/$WORKPATH
# 拷贝Jar
ADD ./*.jar ./app.jar
# 切换为上海时区
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo $TZ > /etc/timezone
# 启动镜像
ENTRYPOINT ["java", "-Dfile.encoding=utf-8", "-Djava.security.egd=file:/dev/./urandom", "-jar", "app.jar"]
CMD ["--spring.profiles.active=dev"]
# 暴露端口
EXPOSE 8080
K8S部署文件
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: {DEPLOY_PROJECT}-deploy
namespace: 命名空间前缀-{DEPLOY_ENV}
spec:
replicas: 1
template:
metadata:
labels:
app: {DEPLOY_PROJECT}-deploy
spec:
# HOST 地址
hostAliases:
volumes:
- name: {DEPLOY_PROJECT}-volume
hostPath:
path: /data/projects
imagePullSecrets:
- name: aliyun-secret
containers:
- name: {DEPLOY_PROJECT}-deploy
image: {REPOSITORY}
imagePullPolicy: Always # 每次都拉去镜像
args: [{ARGS}]
resources:
requests:
cpu: 1
memory: 2048Mi
limits:
cpu: 2
memory: 3072Mi
volumeMounts:
- name: {DEPLOY_PROJECT}-volume
mountPath: /usr/local/workspace/{DEPLOY_PROJECT}/logs
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: {DEPLOY_PROJECT}-service
namespace: 命名空间前缀-{DEPLOY_ENV}
labels:
app: {DEPLOY_PROJECT}-service
spec:
selector:
app: {DEPLOY_PROJECT}-deploy
ports:
- port: 8080
targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {DEPLOY_PROJECT}-ingress
namespace: 命名空间前缀-{DEPLOY_ENV}
annotations:
nginx.ingress.kubernetes.io/Access-Control-Allow-Origin: '*'
nginx.ingress.kubernetes.io/cors-allow-headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,R-Authorization,X-Token,Pagination
nginx.ingress.kubernetes.io/cors-allow-methods: 'PUT, GET, POST, OPTIONS'
nginx.ingress.kubernetes.io/cors-allow-origin: '*'
nginx.ingress.kubernetes.io/enable-cors: 'true'
nginx.ingress.kubernetes.io/service-weight: ''
spec:
rules:
- host: {DEPLOY_ENV}.xxxx.com
http:
paths:
- path: /
backend:
serviceName: {DEPLOY_PROJECT}-service
servicePort: 8080
新建K8S命名空间+Docker仓库Token
## 创建命名空间
```shell
kubectl create namespace 命名空间前缀-dev
创建 Docker认证Token
kubectl --namespace 命名空间前缀-dev \
create secret docker-registry aliyun-secret \
--docker-server=registry.cn-beijing.aliyuncs.com \
--docker-username=Docker仓库账号 \
--docker-password=Docker仓库密码
Ansible Host 文件
[开发环境]
10.0.0.111 ansible_connection=ssh ansible_ssh_user=用户名 ansible_ssh_pass=用户密码 host1=host1:10.0.0.20 host2=host2:10.0.0.21 host3=host3:10.0.0.22 host4=host4:10.0.0.23
Jenkins Pipeline 代码
pipeline {
agent {
node {
label 'haimaxy-jnlp'
}
}
environment {
JAVA_HOME = '/data/projects/jenkins-home/opt/env/jdk/jdk8u292-b10'
MAVEN_HOME = '/data/projects/jenkins-home/opt/env/maven/apache-maven-3.6.3'
DOCKER_REPOSITORY_HOST = 'registry.cn-beijing.aliyuncs.com'
DOCKER_REPOSITORY_USERNAME = 'Docker仓库账号'
DOCKER_REPOSITORY_PASSWORD = 'Docker仓库密码'
ORG_NAME = '组织名称'
ANSIBLE_HOSTS_PATH = 'AnsibleHost文件路径'
DEPLOY_PROJECT = '部署项目名称'
DEPLOY_ENV = 'dev'
SERVER = '开发环境'
}
parameters {
gitParameter(name: 'GIT_TAG',
type: 'PT_BRANCH',
branchFilter: 'origin/(.*)',
defaultValue: 'master',
selectedValue: 'DEFAULT',
sortMode: 'DESCENDING_SMART',
description: '请选择代码标签')
string(name: 'ARGS', defaultValue: '', description: '参数配置 格式为 "","",""...')
}
stages {
stage('git checkout') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params.GIT_TAG}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[url: '项目Git地址', credentialsId: '1658a146-5944-4702-9fb8-f36ee3008222',]]
])
}
}
stage('code build') {
steps {
sh '''
ls
${MAVEN_HOME}/bin/mvn clean package -Dmaven.test.skip=true
'''
}
}
stage('docker build and push') {
steps {
sh '''
CURR_IMAGE=${ORG_NAME}/${DEPLOY_PROJECT}:${GIT_TAG}
REPOSITORY=${DOCKER_REPOSITORY_HOST}/${CURR_IMAGE}
# 打包镜像 并 发布到云仓库
cd docker-build/
docker build -t ${CURR_IMAGE} .
docker tag ${CURR_IMAGE} ${REPOSITORY}
docker login --username=${DOCKER_REPOSITORY_USERNAME} --password=${DOCKER_REPOSITORY_PASSWORD} ${DOCKER_REPOSITORY_HOST}
docker push ${REPOSITORY}
'''
}
}
stage('remote docker deploy') {
steps {
sh '''
CURR_IMAGE=${ORG_NAME}/${DEPLOY_PROJECT}:${GIT_TAG}
REPOSITORY=${DOCKER_REPOSITORY_HOST}/${CURR_IMAGE}
# 拼接K8S参数
K8S_ARGS='"--spring.profiles.active='${DEPLOY_ENV}'"'
if [ -n "$ARGS" ]; then
K8S_ARGS="$K8S_ARGS, ${ARGS}"
fi
cd docker-build
# 制作HOST地址文件
cat >> hosts.yaml << EOF
- ip: 10.0.0.20
hostnames:
- "host1"
- ip: 10.0.0.21
hostnames:
- "host2"
- ip: 10.0.0.22
hostnames:
- "host3"
- ip: 10.0.0.23
hostnames:
- "host4"
EOF
# 修改配置
sed -i "s#{DEPLOY_PROJECT}#${DEPLOY_PROJECT}#g" k8s-deploy.yaml
sed -i "s#{DEPLOY_ENV}#${DEPLOY_ENV}#g" k8s-deploy.yaml
sed -i "s#{ARGS}#${K8S_ARGS}#g" k8s-deploy.yaml
sed -i "s#{REPOSITORY}#${REPOSITORY}#g" k8s-deploy.yaml
sed -i "s#{CONTAINER_PORT}#${PORT}#g" k8s-deploy.yaml
# 替换HOST
sed "/hostAliases/r hosts.yaml" k8s-deploy.yaml > k8s-deploy-file.yaml
# 制作部署配置文件
cat >> ansible-deploy.yaml << EOF
- hosts: ${SERVER}
tasks:
- name: 拷贝 YAML
tags: copy_yaml
copy: src="k8s-deploy-file.yaml" dest="~/k8s-deploy/k8s-${DEPLOY_PROJECT}-deploy.yaml"
- name: 启动镜像
tags: back_code
shell: |
cd ~/k8s-deploy
kubectl delete -f k8s-${DEPLOY_PROJECT}-deploy.yaml
kubectl create -f k8s-${DEPLOY_PROJECT}-deploy.yaml
EOF
# 执行远程命令
ansible-playbook ansible-deploy.yaml -i ${ANSIBLE_HOSTS_PATH}
'''
}
}
}
}