当前位置:网站首页>基于kubernetes平台微服务的部署
基于kubernetes平台微服务的部署
2022-06-30 22:09:00 【小柏ぁ】
目录
Jenkins+Kubernetes+Docker完成微服务持续集成
Jenkins+Kubernetes+Docker完成微服务持续集成
创建NFS共享目录
让所有Jenkins-Slave构建指向NFS的Maven的共享仓库目录
[[email protected] /opt/nfs]# mkdir maven
[[email protected] /opt/nfs]# chmod 777 maven/
[[email protected] /opt/nfs]# vim /etc/exports
/opt/nfs/jenkins 192.168.37.0/24(rw,sync,no_root_squash)
/opt/nfs/maven 192.168.37.0/24(rw,sync,no_root_squash)
#刷新
[[email protected] /opt/nfs]# exportfs -rv
exporting 192.168.37.0/24:/opt/nfs/maven
exporting 192.168.37.0/24:/opt/nfs/jenkins查看k8s集群
[[email protected] ~]# showmount -e 192.168.37.106
Export list for 192.168.37.106:
/opt/nfs/maven 192.168.37.0/24
/opt/nfs/jenkins 192.168.37.0/24Docker命令执行权限问题
[[email protected] /var/run]# chmod 777 docker.sock新建一个流水线项目

编写构建Pipeline


配置harbor
配置harbor凭证

流水线脚本
def git_url = "http://192.168.37.103:85/devops_group/tensquare_back.git"
def git_auth = "72a48f14-72c7-444f-a471-2d482e85d808"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "192.168.37.106:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "ea2f47a0-3f01-4149-a095-987675db7162"
podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "192.168.37.106:85/library/jenkins-slave-maven:latest"
),
containerTemplate(
name: 'docker',
image: "docker:stable",
ttyEnabled: true,
command: 'cat'
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
nfsVolume(mountPath: '/usr/local/apache-maven/repo', serverAddress: '192.168.37.106' , serverPath: '/opt/nfs/maven'),
],
)
{
node("jenkins-slave") {
// 第一步
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],
userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
// 第二步
stage('make public sub project'){
//编译并安装公共工程
sh "mvn -f tensquare_common clean install"
}
// 第三步
stage('make image'){
//把选择的项目信息转为数组
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
//取出每个项目的名称和端口
def currentProject = selectedProjects[i];
//项目名称
def currentProjectName = currentProject.split('@')[0]
//项目启动端口
def currentProjectPort = currentProject.split('@')[1]
//定义镜像名称
def imageName = "${currentProjectName}:${tag}"
//编译,构建本地镜像
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
//给镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
//登录Harbor,并上传镜像
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
//登录
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//上传镜像
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
//删除本地镜像
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
}
}
}
}
Eureka服务构建成功!!!

此时harbor仓库已经推送上去了

微服务的部署
安装 Kubernetes Continuous Deploy插件
Jenkins认证k8s凭证

添加k8s凭证

点击确定,获取id号
K8S 创建访问Harbor私服拉取镜像所需要密钥权限凭证,设置免交互
[[email protected] ~/.kube]# kubectl create secret docker-registry registry-auth-secret --docker-username=jack --docker-password=Abc12345 [email protected] --docker-server=192.168.37.106:85 
在tensquare_eureka_server微服务项目根目录下创建 deploy.yml配置文件

---
apiVersion: v1
kind: Service
metadata:
name: eureka
labels:
app: eureka
spec:
type: NodePort
ports:
- port: 10086
name: eureka
targetPort: 10086
selector:
app: eureka
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka
spec:
serviceName: "eureka"
replicas: 2
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
imagePullSecrets:
- name: $SECRET_NAME #通过变量引用K8S访问Harbor私服拉取镜像所需要的secret资源名称
containers:
- name: eureka
image: $IMAGE_NAME
ports:
- containerPort: 10086
env:
- name: MY_POD_NAME #MY_POD_NAME环境变量会从每个被创建的Pod的metadata.name字段获取变量值,如eureka-0、eureka-1
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: EUREKA_SERVER #此环境变量会被项目代码中的application.yml文件引用
value: "http://eureka-0.eureka:10086/eureka/,http://eureka-1.eureka:10086/eureka/"
- name: EUREKA_INSTANCE_HOSTNAME #此环境变量会项目代码中的application.yml文件引用
value: ${MY_POD_NAME}.eureka
podManagementPolicy: "Parallel"修改tensquare_eureka_server微服务项目的 application.yml 文件
tensquare_eureka_server\src\main\resources \application.yml
---
server:
port: ${PORT:10086}
spring:
application:
name: eureka
eureka:
server:
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eviction-interval-timer-in-ms: 5000
enable-self-preservation: false
use-read-only-response-cache: false
client:
#eureka client间隔多久去拉取服务注册信息 默认30s
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://127.0.0.1:${server.port}/eureka/} #引用deploy.yml配置中的环境变量EUREKA_SERVER
instance:
#心跳间隔时间,即发送一次心跳之后,多久在发起下一次(缺省为30s)
lease-renewal-interval-in-seconds: 5
#在收到一次心跳之后,等待下一次心跳的空档时间,大于心跳间隔即可,即服务续约到期时间(缺省为90s)
lease-expiration-duration-in-seconds: 10
instance-id: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}:${server.port}@${random.long(1000000,9999999)}
hostname: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}上传代码
编写pipeline脚本
def git_url = "http://192.168.37.103:85/devops_group/tensquare_back.git"
def git_auth = "72a48f14-72c7-444f-a471-2d482e85d808"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "192.168.37.106:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "ea2f47a0-3f01-4149-a095-987675db7162"
//获取当前选择项目名称
def selectedProjectNames="${project_name}".split(",")
def secret_name = "registry-auth-secret"
def k8s_auth = "2e95a29c-79f1-41dd-be38-3f9691b25807"
podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "192.168.37.106:85/library/jenkins-slave-maven:latest"
),
containerTemplate(
name: 'docker',
image: "docker:stable",
ttyEnabled: true,
command: 'cat'
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
nfsVolume(mountPath: '/usr/local/apache-maven/repo', serverAddress: '192.168.37.106' , serverPath: '/opt/nfs/maven'),
],
)
{
node("jenkins-slave") {
// 第一步
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],
userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
// 第二步
stage('make public sub project'){
//编译并安装公共工程
sh "mvn -f tensquare_common clean install"
}
// 第三步
stage('make image'){
//把选择的项目信息转为数组
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
//取出每个项目的名称和端口
def currentProject = selectedProjects[i];
//项目名称
def currentProjectName = currentProject.split('@')[0]
//项目启动端口
def currentProjectPort = currentProject.split('@')[1]
//定义镜像名称
def imageName = "${currentProjectName}:${tag}"
//编译,构建本地镜像
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
//给镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
//登录Harbor,并上传镜像
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
//登录
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//上传镜像
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
//删除本地镜像
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
}
}
stage('部署应用到K8S') {
for(int i=0; i<selectedProjectNames.length; i++) {
def projectInfo=selectedProjectNames[i]
//当前的项目名称
def currentProjectName="${projectInfo}".split("@")[0]
//当前的项目端口
def currentProjectPort="${projectInfo}".split("@")[1]
def imageName = "${currentProjectName}:${tag}"
def harbor_imageName = "${harbor_url}/${harbor_project_name}/${imageName}"
sh """
sed -i 's#\$IMAGE_NAME#${harbor_imageName}#' ${currentProjectName}/deploy.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' ${currentProjectName}/deploy.yml
"""
kubernetesDeploy configs: "${currentProjectName}/deploy.yml", kubeconfigId: "${k8s_auth}"
}
}
}
}构建
查看pod,svc资源

浏览器访问

部署其他微服务
更改配置文件中的eureka集群地址
tensquare_zuul\src\main\resources\application.yml
# Eureka注册配置信息
eureka:
client:
service-url:
defaultZone: http://eureka-0.eureka:10086/eureka/,http://eureka-1.eureka:10086/eureka/ #Eureka访问地址
instance:
prefer-ip-address: true创建配置文件deploy.yml
---
apiVersion: v1
kind: Service
metadata:
name: zuul
labels:
app: zuul
spec:
type: NodePort
ports:
- port: 10020
name: zuul
targetPort: 10020
selector:
app: zuul
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: zuul
spec:
serviceName: "zuul"
replicas: 2
selector:
matchLabels:
app: zuul
template:
metadata:
labels:
app: zuul
spec:
imagePullSecrets:
- name: $SECRET_NAME #通过变量引用K8S访问Harbor私服拉取镜像所需要的secret资源名称
containers:
- name: zuul
image: $IMAGE_NAME
ports:
- containerPort: 10020
podManagementPolicy: "Parallel"手动上传父工程依赖到NFS的Maven共享仓库目录中

开始构建

其他项目名字和端口进行修改即可
边栏推荐
- 牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
- Develop technology - get time 10 minutes ago
- Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
- Uniapp rich text editor
- Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
- B_ QuRT_ User_ Guide(33)
- 去中心化交易所系统开发技术原理丨数字货币去中心化交易所系统开发(说明案例)
- 谈谈数字化转型的几个关键问题
- Best wishes for Lao Wu's party
- [backtracking] full arrangement leetcode46
猜你喜欢

How to upload binary pictures in uniapp

Ten of the most heart piercing tests / programmer jokes, read the vast crowd, how to find?

盘点华为云GaussDB(for Redis)六大秒级能力

Label Contrastive Coding based Graph Neural Network for Graph Classification

Uniapp life cycle / route jump

Starting from pg15 xid64 ticket skipping again

Stinky tofu made by Grandma

A new one from Ali 25K came to the Department, which showed me what the ceiling is

msf之ms17-010永恒之蓝漏洞

Graduation project
随机推荐
交易所系统开发如何开发?数字货币交易所系统开发成熟技术案例
Why does the computer speed slow down after vscode is used for a long time?
Introduce an online platform for multi omics integration and network visual analysis
Windbg调试工具介绍
[BSP video tutorial] BSP video tutorial issue 19: AES encryption practice of single chip bootloader, including all open source codes of upper and lower computers (June 26, 2022)
About, Qianxin detects code vulnerabilities and XSS series solves them
[backtracking] full arrangement II leetcode47
2022中国国潮发展新动向
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Summary of errors reported when using YML file to migrate CONDA environment
Stinky tofu made by Grandma
What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3
How to develop the exchange system? Mature technology case of digital currency exchange system development
[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises
Femas:云原生多运行时微服务框架
Niubi | the tools I have treasured for many years have made me free to fish with pay
微服務鏈路風險分析
Nansen复盘加密巨头自救:如何阻止百亿多米诺倾塌
艾芬医生事件解析
HDFS集中式缓存管理(Centralized Cache Management)