当前位置:网站首页>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/jenkins
see 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/24
Docker Command execution permission problem
[[email protected] /var/run]# chmod 777 docker.sock
Create 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: true
create 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
边栏推荐
- msf之ms17-010永恒之蓝漏洞
- The Three Musketeers: One for All!
- Cas classique multithreadé
- JVM Part 21 of interview with big companies Q & A
- KVM IO性能测试数据
- 吴恩达的机器学习适合入门吗?
- How to use filters in jfinal to monitor Druid for SQL execution?
- I want to know who I need to know to open a stock account? In addition, is it safe to open a mobile account?
- AtCoder Beginner Contest 257
- Document layout analysis: a comprehensive survey 2019 paper learning summary
猜你喜欢
机器学习工作要求研究生吗?
How to change the win11 computer name? Win11 method of changing computer name
腾讯3年,功能测试进阶自动化测试,送给在手工测试中迷茫的你
[micro service ~nacos] configuration center of Nacos
Win11电脑名如何更改?Win11更改电脑名的方法
2022中国国潮发展新动向
[introduction to MySQL] the first conversation · first time in the "database" Mainland
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
Online education program user login and registration
分享十万级TPS的IM即时通讯综合消息系统的架构
随机推荐
What are database OLAP and OLTP? Same and different? Applicable scenarios
Neo4j load CSV configuration and use
Win11如何优化服务?Win11优化服务的方法
Online education program user login and registration
[introduction to MySQL] the first conversation · first time in the "database" Mainland
Stinky tofu made by Grandma
电脑版微信文件存储在哪个文件夹可以找到
KVM IO性能测试数据
How to change the win11 computer name? Win11 method of changing computer name
latex左侧大括号 latex中大括号多行公式
Apache服务器OpenSSL升级
AtCoder Beginner Contest 257
How to use data sets in machine learning?
机器学习工作要求研究生吗?
B_ QuRT_ User_ Guide(32)
Uniapp routing uni simple router
Pytorch quantitative perception training (qat) steps
"Trust machine" empowers development
实现多方数据安全共享,解决普惠金融信息不对称难题
Best wishes for Lao Wu's party