当前位置:网站首页>基于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共享仓库目录中

开始构建

其他项目名字和端口进行修改即可
边栏推荐
- [450. delete nodes in binary search tree]
- win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法
- 《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26
- JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir
- 艾芬医生事件解析
- A new one from Ali 25K came to the Department, which showed me what the ceiling is
- Win11电脑名如何更改?Win11更改电脑名的方法
- Femas:云原生多运行时微服务框架
- 2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main() { ints := make
- MFC interface library bcgcontrolbar v33.0 - desktop alarm window, grid control upgrade, etc
猜你喜欢

Modify the name of the launched applet

机器学习中如何使用数据集?

Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online

Introduction to go web programming: a probe into the excellent test library gocovey

What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3

Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
![[introduction to MySQL] the first conversation · first time in the](/img/73/cc85eb469384c3df94479318293c6f.png)
[introduction to MySQL] the first conversation · first time in the "database" Mainland

吴恩达的机器学习适合入门吗?

Pytorch quantitative practice (2)

Web APIs comprehensive case -tab column switching - dark horse programmer
随机推荐
Anti leakage family photo in attack and defense drill
Golang application ━ installation, configuration and use of Hugo blog system
在启牛开的股票账户安全吗?如何申请低佣金的股票账户?
Why does the computer speed slow down after vscode is used for a long time?
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
100 important knowledge points that SQL must master: creating and manipulating tables
PostgreSQL存储结构浅析
How to develop the exchange system? Mature technology case of digital currency exchange system development
与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3
Modify the name of the launched applet
Summary of errors reported when using YML file to migrate CONDA environment
Windbg调试工具介绍
Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes
HDFS集中式缓存管理(Centralized Cache Management)
Develop technology - get time 10 minutes ago
Pytorch quantitative practice (1)
When unittest automatically tests multiple use cases, the logging module prints repeatedly to solve the problem
HDFS centralized cache management
Some memory problems summarized
实现多方数据安全共享,解决普惠金融信息不对称难题