当前位置:网站首页>Deployment of microservices based on kubernetes platform
Deployment of microservices based on kubernetes platform
2022-06-30 22:22:00 【Xiao Bai】
Catalog
Jenkins+Kubernetes+Docker Complete the continuous integration of micro services
Jenkins authentication k8s voucher
Manually upload dependency to parent project NFS Of Maven Shared warehouse Directory
Jenkins+Kubernetes+Docker Complete the continuous integration of micro services
establish NFS share directory
Let all Jenkins-Slave Build points to NFS Of Maven Shared warehouse Directory
[[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)
# Refresh
[[email protected] /opt/nfs]# exportfs -rv
exporting 192.168.37.0/24:/opt/nfs/maven
exporting 192.168.37.0/24:/opt/nfs/jenkinssee k8s colony
[[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 Command execution permission problem
[[email protected] /var/run]# chmod 777 docker.sockCreate a new pipeline project

Write build Pipeline


To configure harbor
To configure harbor voucher

Pipelined scripts
def git_url = "http://192.168.37.103:85/devops_group/tensquare_back.git"
def git_auth = "72a48f14-72c7-444f-a471-2d482e85d808"
// The name of the build
def tag = "latest"
//Harbor Private address
def harbor_url = "192.168.37.106:85"
//Harbor Project name of
def harbor_project_name = "tensquare"
//Harbor Proof of
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") {
// First step
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],
userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
// The second step
stage('make public sub project'){
// Compile and install public works
sh "mvn -f tensquare_common clean install"
}
// The third step
stage('make image'){
// Convert the selected item information into an array
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
// Take out the name and port of each item
def currentProject = selectedProjects[i];
// Project name
def currentProjectName = currentProject.split('@')[0]
// Project startup port
def currentProjectPort = currentProject.split('@')[1]
// Define the image name
def imageName = "${currentProjectName}:${tag}"
// compile , Building a local image
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
// Label the image
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
// Sign in Harbor, And upload the image
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
// Sign in
sh "docker login -u ${username} -p ${password} ${harbor_url}"
// Upload the image
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
// Delete local image
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
}
}
}
}
Eureka The service was built successfully !!!

here harbor The warehouse has been pushed up

Deployment of microservices
install Kubernetes Continuous Deploy plug-in unit
Jenkins authentication k8s voucher

add to k8s voucher

Click ok , obtain id Number
K8S Create access Harbor The private server needs the key permission certificate to pull the image , Set interaction free
[[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 
stay tensquare_eureka_server Create in the root directory of the microservice project deploy.yml The configuration file

---
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 # Reference... Through variables K8S visit Harbor What is needed for private servers to pull images secret Resource name
containers:
- name: eureka
image: $IMAGE_NAME
ports:
- containerPort: 10086
env:
- name: MY_POD_NAME #MY_POD_NAME Environment variables will be created from each Pod Of metadata.name Field to get the variable value , Such as eureka-0、eureka-1
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: EUREKA_SERVER # This environment variable will be changed by... In the project code application.yml File reference
value: "http://eureka-0.eureka:10086/eureka/,http://eureka-1.eureka:10086/eureka/"
- name: EUREKA_INSTANCE_HOSTNAME # This environment variable changes the... In the project code application.yml File reference
value: ${MY_POD_NAME}.eureka
podManagementPolicy: "Parallel"modify tensquare_eureka_server Micro service project application.yml file
tensquare_eureka_server\src\main\resources \application.yml
---
server:
port: ${PORT:10086}
spring:
application:
name: eureka
eureka:
server:
# Renewal time , That is, the interval between scan failure Services ( Default is 60*1000ms)
eviction-interval-timer-in-ms: 5000
enable-self-preservation: false
use-read-only-response-cache: false
client:
#eureka client How often do I get the service registration information Default 30s
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVER:http://127.0.0.1:${server.port}/eureka/} # quote deploy.yml Environment variables in configuration EUREKA_SERVER
instance:
# Heartbeat interval , That is, after sending a heartbeat , How long will it take to launch the next ( Default is 30s)
lease-renewal-interval-in-seconds: 5
# After receiving a heartbeat , The time between waiting for the next heartbeat , Greater than the heartbeat interval , I.e. service renewal expiration date ( Default is 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}}Upload code
To write pipeline Script
def git_url = "http://192.168.37.103:85/devops_group/tensquare_back.git"
def git_auth = "72a48f14-72c7-444f-a471-2d482e85d808"
// The name of the build
def tag = "latest"
//Harbor Private address
def harbor_url = "192.168.37.106:85"
//Harbor Project name of
def harbor_project_name = "tensquare"
//Harbor Proof of
def harbor_auth = "ea2f47a0-3f01-4149-a095-987675db7162"
// Gets the name of the currently selected item
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") {
// First step
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [],
userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
// The second step
stage('make public sub project'){
// Compile and install public works
sh "mvn -f tensquare_common clean install"
}
// The third step
stage('make image'){
// Convert the selected item information into an array
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
// Take out the name and port of each item
def currentProject = selectedProjects[i];
// Project name
def currentProjectName = currentProject.split('@')[0]
// Project startup port
def currentProjectPort = currentProject.split('@')[1]
// Define the image name
def imageName = "${currentProjectName}:${tag}"
// compile , Building a local image
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
// Label the image
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
// Sign in Harbor, And upload the image
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
// Sign in
sh "docker login -u ${username} -p ${password} ${harbor_url}"
// Upload the image
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
// Delete local image
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
}
}
stage(' Deploy the application to K8S') {
for(int i=0; i<selectedProjectNames.length; i++) {
def projectInfo=selectedProjectNames[i]
// Current project name
def currentProjectName="${projectInfo}".split("@")[0]
// Current project port
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}"
}
}
}
}structure
see pod,svc resources

Browser access

Deploy other microservices
Change... In the configuration file eureka The cluster address
tensquare_zuul\src\main\resources\application.yml
# Eureka Register configuration information
eureka:
client:
service-url:
defaultZone: http://eureka-0.eureka:10086/eureka/,http://eureka-1.eureka:10086/eureka/ #Eureka Access address
instance:
prefer-ip-address: truecreate profile 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 # Reference... Through variables K8S visit Harbor What is needed for private servers to pull images secret Resource name
containers:
- name: zuul
image: $IMAGE_NAME
ports:
- containerPort: 10020
podManagementPolicy: "Parallel"Manually upload dependency to parent project NFS Of Maven Shared warehouse Directory

Begin to build

Other project names and ports can be modified
边栏推荐
- 电脑版微信文件存储在哪个文件夹可以找到
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- 去中心化交易所系统开发技术原理丨数字货币去中心化交易所系统开发(说明案例)
- Technical principle of decentralized exchange system development - digital currency decentralized exchange system development (illustrative case)
- Why does the computer speed slow down after vscode is used for a long time?
- Uniapp third party network request
- Is Wu Enda's machine learning suitable for entry?
- Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
- Anfulai embedded weekly report no. 271: June 20, 2022 to June 26, 2022
- Notes [introduction to JUC package and future]
猜你喜欢

"More Ford, more China" saw through the clouds, and the orders of Changan Ford's flagship products exceeded 10000

JVM Part 21 of interview with big companies Q & A

【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)

Graduation project

【MySQL入门】第一话 · 初入“数据库”大陆

项目管理到底管的是什么?

Web APIs comprehensive case -tab column switching - dark horse programmer

2022中国国潮发展新动向

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

零样本和少样本学习
随机推荐
B_ QuRT_ User_ Guide(31)
项目管理到底管的是什么?
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
[introduction to MySQL] the first conversation · first time in the "database" Mainland
100 important knowledge points that SQL must master: creating and manipulating tables
B_ QuRT_ User_ Guide(33)
On several key issues of digital transformation
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
Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
"Team training competition" Shandong multi university training 3
部门新来了个阿里25K出来的,让我见识到了什么是天花板
When unittest automatically tests multiple use cases, the logging module prints repeatedly to solve the problem
Web APIs comprehensive case -tab column switching - dark horse programmer
Uniapp third party network request
Niubi | the tools I have treasured for many years have made me free to fish with pay
Mysql:sql overview and database system introduction | dark horse programmer
多线程经典案例
基于kubernetes平台微服务的部署
Technical principle of decentralized exchange system development - digital currency decentralized exchange system development (illustrative case)
《安富莱嵌入式周报》第270期:2022.06.13--2022.06.19