jenkins持续构建k8s服务
本文默认您已经完成以下步骤:
k8s集群安装(本司使用rancher 部署k8s)jenkins自动化部署工具安装harbor镜像仓库安装
一、准备工作
1.1 自动化部署流程
1.2 jenkins凭证配置
配置gitlab凭证 k8s平台配置harbor凭证(证书没有的话问harbor相关人员要)
1.3 configmap配置
具体配置如下:
keyvalue说明
config_serveridFREGATA-SERVER-CONFIG配置中心nameeureka_addresshttp://ycloud-eureserver-0.default-default-registrydns-com.ycloud:10010/eureka/注册中心地址eureka_client_use_DNStrue开启dnseureka_port10010注册中心端口kafka_bootstrap_serversycloud-kafka-0.ycloud-kafka-hs.ycloud.svc.cluster.local:30802kafka信息rabbitmq_addressycloud-rabbitmq.ycloudmq信息rabbitmq_passwordyclouddevmq密码rabbitmq_port5672mq端口rabbitmq_usernameyclouddevmq账号zipkin_serverhttp://zipkin-server.ycloud.svc.cluster.local:8880链路追踪地址
eureka_address配置地址参考:https://blog.csdn.net/han949417140/article/details/106242264
1.4 jenkins自动部署模板配置
git模板项目地址: https://github.com/HLDBanana/template-java.git
properties([
parameters([
choice(
choices
: ['dev'],
description
: '流水线类型',
name
: 'PP_TYPE'
),
text(defaultValue
: 'http://192.168.101.94:8081/Data-Middleground-Develop-Area/product-code/service/dataMiddle-multipleCheck.git',
description
: '必填,执行部署的项目源代码仓库地址',
name
: 'PJ_URL'
),
text(
defaultValue
: 'guohai',
description
: '必填,执行部署的目标源代码项目仓库的分支名称',
name
: 'PJ_BRANCH'
),
credentials(
defaultValue
: 'hld_git_cert',
description
: '必填,登录项目仓库使用的凭据ID,在持续交付中心凭据管理页面创建',
name
: 'PJ_KEY',
required
: true,
credentialType
: 'com.cloudbees.plugins.credentials.common.StandardCredentials'
),
choice(
choices
: ['tag:guohai-dev'],
description
: '必填,应用部署的K8S集群名称,格式要求【名称:tag】,tag要与对应集群的持续交付中心slave端tag一致',
name
: 'K8S_API_SERVER'
),
choice(
choices
: ['false', 'true'],
description
: '仅执行部署,选择true以后:不会进行编译打包,选项:NEED_SCA、NEED_JUNIT不生效,需在执行过程选择chart版本;false会执行从源码到部署',
name
: 'ONLY_DEPLOY'
),
choice(
choices
: ['false', 'true'],
description
: '是否需要服务调试。默认为false,表示当前服务不开启服务调试',
name
: 'NEED_SERVICE_DEBUG'
),
choice(
choices
: ['false', 'true'],
description
: '是否需要执行静态代码检查,true需要,false不需要',
name
: 'NEED_SCA'
),
choice(
choices
: ['false', 'true'],
description
: '后端项目是否需要执行Junit单元测试,true需要,false不需要',
name
: 'NEED_JUNIT'
),
choice(
choices
: ['false', 'true'],
description
: '是否需要执行镜像扫描',
name
: 'NEED_IMAGE_SCAN'
),
choice(choices
: ['false', 'true'], description
: '是否需要执行ZAP安全测试', name
: 'NEED_SAFE_TEST'),
choice(choices
: ['High', 'Medium', 'Low'], description
: '执行ZAP安全测试的等级', name
: 'ATK_LEVEL'),
choice(
choices
: ['', 'lugia:https://registry.npm.taobao.org/'],
description
: '前端项目自定义npm私服',
name
: 'NRM_LIB'
)
]),
pipelineTriggers([
[
$
class : 'GitLabPushTrigger',
branchFilterType
: 'All',
triggerOpenMergeRequestOnPush
: "never",
skipWorkInProgressMergeRequest
: true,
secretToken
: '44fcdbc7c1e0c77c607b7f63c67458de',
branchFilterType
: "NameBasedFilter",
includeBranchesSpec
: "master",
excludeBranchesSpec
: "",
]
])
])
def DOCKERGROUP
= "ex-dataservice"
def REPLICAS
= "1"
def NAMESPACE
= "bcloud"
def GROUP
= "com.yss"
def RELEASEDIR
= "dist"
def CONTAINER_JVM_FLAGS
= ""
def NEED_IPVS_LOADBALANCE
= "false"
def LOADBALANCE_VIP
= ""
def MYDOMAIN1
= "yss-datamiddle-multiplecheck.com"
def SECRETNAME1
= ""
def MYDOMAIN2
= "udf-yss-datamiddle-multiplecheck.com"
def SECRETNAME2
= ""
def CONTAINERPORT1
= "30180"
def CONTAINERPORT2
= "30181"
def NODE_LABEL
= "demo"
def CONFIG_CENTER_PROFILE
= ""
def TEST_SERVICE
= "yss-datamiddle-multiplecheck"
def EXTERNAL_STATIC_RESOURCE_PATH
= ""
def nodeTag
= ""
if (null
!= env
.K8S_API_SERVER
&& env
.K8S_API_SERVER
!= "") {
nodeTag
= env
.K8S_API_SERVER
.split(":")[1]
echo nodeTag
}
if (null
== env
.PP_TYPE
) {
node
{
stage('任务参数配置') {
echo
"首次执行只会导入任务默认参数,请修改参数后再执行任务"
}
}
return
}
def map
= [:]
map
.put('SPRING_PROFILES_ACTIVE', '')
map
.put('CONFIG_CENTER_PROFILE', CONFIG_CENTER_PROFILE
)
map
.put('DOCKERGROUP', DOCKERGROUP
)
map
.put('REPLICAS', REPLICAS
)
map
.put('NAMESPACE', NAMESPACE
)
map
.put('GROUP', GROUP
)
map
.put('NODE', nodeTag
)
map
.put('NEED_API_TEST', 'false')
map
.put('NEED_PM_TEST', 'false')
map
.put('NEED_SAFE_TEST', 'false')
map
.put('EXTERNALIP', '192.168.101.84')
map
.put('NEED_IPVS_LOADBALANCE', NEED_IPVS_LOADBALANCE
)
map
.put('LOADBALANCE_VIP', LOADBALANCE_VIP
)
map
.put('MYDOMAIN1', MYDOMAIN1
)
map
.put('SECRETNAME1', SECRETNAME1
)
map
.put('MYDOMAIN2', MYDOMAIN2
)
map
.put('SECRETNAME2', SECRETNAME2
)
map
.put('CONTAINERPORT1', CONTAINERPORT1
)
map
.put('CONTAINERPORT2', CONTAINERPORT2
)
map
.put('RELEASEDIR', RELEASEDIR
)
map
.put('PP_TYPE', "${PP_TYPE}")
map
.put('NODE_LABEL', NODE_LABEL
)
def APPNAME
= ""
map
.put('APPNAME', APPNAME
)
map
.put('TEST_SERVICE', TEST_SERVICE
)
map
.put('EXTERNAL_STATIC_RESOURCE_PATH', EXTERNAL_STATIC_RESOURCE_PATH
)
map
.put('CONTAINER_JVM_FLAGS', CONTAINER_JVM_FLAGS
)
map
.put('NEED_MAVENDEPOLY', false)
def cicdLib
= "yss-cicd-basic"
map
.put('CICDLIB', cicdLib
)
library cicdLib
BasicLib("${PP_TYPE}", map
)
二、 jenkins配置自动化部署
2.1 jenkins加载模板配置
新建任务–>流水线
流水线配置 配置完成之后保存、应用加载配置 出现如下图代表模板配置加载成功 查看已加载配置配置gitlab钩子函数 具体配置参考:gitlab钩子函数
至此 jenkins配置完成,点击立即构建之后会自动编译项目打jar包、生成镜像文件并上传harbor镜像仓库,之后会自动启动一个k8s pod。
三、k8s配置
configmap和harbor凭证都已经在上文创建完成,这里只需要修改一下pod对应的yaml 凭证配置
构建之后k8s集群会创建一个pod并启动咱们的服务 没有修改harbor凭证配置之前报错如下:调整harbor凭证配置
重新部署pod 查看pod内信息 配置前端nginx负载 配置模板:
user root
;
worker_processes
1;
events
{
worker_connections
1024;
}
http
{
include mime
.types
;
default_type application
/octet
-stream
;
sendfile on
;
upstream mysvr
{
# 后台pom
.xml中parent
.artifactId值
server yss
-datamiddle
-multiplecheck
:30180;
}
# HTTP server
server
{
listen
80; #http访问的端口
server_name localhost
; #设置访问域名,不要设置IP
#charset koi8
-r
;
#access_log logs
/host
.access
.log main
;
location
/ {
root
/usr
/local
/nginx
/http
;
try_files $uri $uri
/ /index
.html last
;
index index
.html index
.htm
;
}
location
/yapi
/api
{
proxy_redirect off
;
proxy_set_header Host $host
;
proxy_set_header X
-Real
-IP $remote_addr
;
proxy_set_header X
-Forwarded
-For $proxy_add_x_forwarded_for
;
#proxy_pass http
://192.168.101.84:30180/;
proxy_pass http
://mysvr
/;
}
error_page
500 502 503 504 /50x
.html
;
location
= /50x
.html
{
root html
;
}
}
# HTTPS server
server
{
listen
443 ssl
; #https访问的端口
server_name localhost
; #使用域名访问,不使用IP访问
ssl_certificate
/etc
/nginx
/ssl
/tls
.crt
; #自签名证书路径,证书名称可以修改,路径
/etc
/nginx
/ssl
/保持不变
ssl_certificate_key
/etc
/nginx
/ssl
/tls
.key
; #自签名证书路径,证书名称可以修改,路径
/etc
/nginx
/ssl
/保持不变
location
/ {
root
/usr
/local
/nginx
/http
;
try_files $uri $uri
/ /index
.html last
;
index index
.html index
.htm
;
}
location
/yapi
/api
{
proxy_redirect off
;
proxy_set_header Host $host
;
proxy_set_header X
-Real
-IP $remote_addr
;
proxy_set_header X
-Forwarded
-For $proxy_add_x_forwarded_for
;
#proxy_pass http
://192.168.101.84:30180;
proxy_pass http
://mysvr
/;
}
error_page
500 502 503 504 /50x
.html
;
location
= /50x
.html
{
root html
;
}
}
}
四、验证
访问接口
五、常见错误
5.1 dns异常
bootstrap
.yaml添加配置:
eureka
.client
.eureka
-server
-d
-n
-s
-name
: registrydns
.com
5.2 服务连不上注册中心
bootstrap
.yaml添加配置:
eureka
.client
. eureka
-server
-u
-r
-l
-context
: eureka
具体配置可以参考:微服务项目开发手册