作者:小宋提刑官_783 | 来源:互联网 | 2023-06-04 14:57
文章目录一、基于kubernetes平台微服务的部署二、进入Jenkins中三、修改idea1、修改eureka2、修改zuul3、修改gathering4、修改admin四、网页
文章目录
- 一、基于kubernetes平台微服务的部署
- 二、进入Jenkins中
- 三、修改idea
- 1、修改eureka
- 2、修改zuul
- 3、修改gathering
- 4、修改admin
- 四、网页结果及本地结果
- 五、使用postman访问数据库
一、基于kubernetes平台微服务的部署
cd /root
cd .kube/
cat config //获取K8S的密钥,复制密钥//在k8s所有主机上操作
docker login -u tom -p Abcd1234 192.168.10.131:85kubectl create secret docker-registry registry-auth-secret --docker-server=192.168.10.131:85 --docker-username=admin --docker-password=Harbor12345 -- docker-email=admin@qq.com
kubectl get secrets
二、进入Jenkins中
选择kubernetes configuretion [kubeconfig]
获取ID信息
更改脚本
def git_address = "http://192.168.10.129:82/root/tensquare_back.git"
def git_auth = "e5437e82-74e0-44b3-8cf9-7c3b0e3be36d"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "192.168.10.131:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "685f52af-1e0c-45e7-b73a-3387533245ae"
//K8S的凭证
def k8s_auth="06f40a0e-5f44-467c-a18e-50c976366f1d"
//k8s_harbor凭证
def secret_name="registry-auth-secret"podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [ containerTemplate(name: 'jnlp',image: "192.168.10.131: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.10.135' , serverPath: '/opt/nfs/maven'),],
)
{
node("jenkins-slave"){// 第一步stage('pull code'){checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])}// 第二步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}
}
三、修改idea
1、修改eureka
---
apiVersion: v1
kind: Service
metadata:name: eurekalabels:app: eureka
spec:type: NodePortports:- port: 10086name: eurekatargetPort: 10086selector:app: eureka
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: eureka
spec:serviceName: "eureka"replicas: 2selector:matchLabels:app: eurekatemplate:metadata:labels:app: eurekaspec:imagePullSecrets:- name: $SECRET_NAMEcontainers:- name: eurekaimage: $IMAGE_NAMEports:- containerPort: 10086env:- name: MY_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: EUREKA_SERVERvalue: "http://eureka-0.eureka:10086/eureka/,http://eureka- 1.eureka:10086/eureka/"- name: EUREKA_INSTANCE_HOSTNAMEvalue: ${MY_POD_NAME}.eurekapodManagementPolicy: "Parallel"
修改application.yml配置文件
server:port: ${PORT:10086}
spring:application:name: eurekaeureka:server:# 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)eviction-interval-timer-in-ms: 5000enable-self-preservation: falseuse-read-only-response-cache: falseclient:# eureka client间隔多久去拉取服务注册信息 默认30sregistry-fetch-interval-seconds: 5serviceUrl:defaultZone: ${EUREKA_SERVER:http://127.0.0.1:${server.port}/eureka/}instance:# 心跳间隔时间,即发送一次心跳之后,多久在发起下一次(缺省为30s)lease-renewal-interval-in-seconds: 5# 在收到一次心跳之后,等待下一次心跳的空档时间,大于心跳间隔即可,即服务续约到期时间(缺省为90s)lease-expiration-duration-in-seconds: 10instance-id: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}:${server.port}@${random.l ong(1000000,9999999)}hostname: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}
kubectl get pods
kubectl get service
2、修改zuul
新增deploy.yml
把tensquare_parent文件放入/opt/nfs/maven/com/tensquare中
---
apiVersion: v1
kind: Service
metadata:name: zuullabels:app: zuul
spec:type: NodePortports:- port: 10020name: zuultargetPort: 10020selector:app: zuul
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: zuul
spec:serviceName: "zuul"replicas: 2selector:matchLabels:app: zuultemplate:metadata:labels:app: zuulspec:imagePullSecrets:- name: $SECRET_NAMEcontainers:- name: zuulimage: $IMAGE_NAMEports:- containerPort: 10020podManagementPolicy: "Parallel"
修改application.yml
server:port: 10020 # 端口# 基本服务信息
spring:application:name: tensquare-zuul # 服务ID# Eureka配置
eureka:client:service-url: defaultZone: http://eureka-0.eureka:10086/eureka,http://eureka-1.eureka:10086/eureka # Eureka访问地址instance:prefer-ip-address: true# 修改ribbon的超时时间
ribbon:ConnectTimeout: 1500 # 连接超时时间,默认500msReadTimeout: 3000 # 请求超时时间,默认1000ms# 修改hystrix的熔断超时时间
hystrix:command:default:execution:isolation:thread:timeoutInMillisecond: 2000 # 熔断超时时长,默认1000ms# 网关路由配置
zuul:routes:admin:path: /admin/**serviceId: tensquare-admin-servicegathering:path: /gathering/**serviceId: tensquare-gathering# jwt参数
jwt:config:key: itcastttl: 1800000
3、修改gathering
添加deploy.yml
---
apiVersion: v1
kind: Service
metadata:name: gatheringlabels:app: gathering
spec:type: NodePortports:- port: 9002name: gatheringtargetPort: 9002selector:app: gathering
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: gathering
spec:serviceName: "gathering"replicas: 2selector:matchLabels:app: gatheringtemplate:metadata:labels:app: gatheringspec:imagePullSecrets:- name: $SECRET_NAMEcontainers:- name: gatheringimage: $IMAGE_NAMEports:- containerPort: 9002podManagementPolicy: "Parallel"
修改application.yml
server: port: 9002
spring: application: name: tensquare-gathering #指定服务名datasource: driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.10.130:3306/tensquare_gathering?characterEncoding=UTF8username: rootpassword: abc123jpa: database: mysqlshow-sql: true
#Eureka客户端配置
eureka:client:service-url:defaultZone: http://eureka-0.eureka:10086/eureka,http://eureka-1.eureka:10086/eurekainstance:lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期prefer-ip-address: true
4、修改admin
添加deploy.yml
---
apiVersion: v1
kind: Service
metadata:name: adminlabels:app: admin
spec:type: NodePortports:- port: 9001name: admintargetPort: 9001selector:app: admin
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: admin
spec:serviceName: "admin"replicas: 2selector:matchLabels:app: admintemplate:metadata:labels:app: adminspec:imagePullSecrets:- name: $SECRET_NAMEcontainers:- name: adminimage: $IMAGE_NAMEports:- containerPort: 9001podManagementPolicy: "Parallel"
修改application.yml
server: port: 9001
spring: application: name: tensquare-admin-service #指定服务名datasource: driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.10.130:3306/tensquare_user?characterEncoding=UTF8username: rootpassword: abc123jpa: database: mysqlshow-sql: true#Eureka配置
eureka:client:service-url:defaultZone: http://eureka-0.eureka:10086/eureka,http://eureka-1.eureka:10086/eurekainstance:lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期prefer-ip-address: true# jwt参数
jwt:config:key: itcastttl: 1800000
四、网页结果及本地结果
五、使用postman访问数据库
首先确认idea中的application.yml文件中mysql的访问地址是否正确
第一步://注意30679是K8S上的端口
使用post:http://192.168.10.136:30679/admin/admin/login
第二步:点击Body
第三步:点击Raw----->JSON
第四步:"loginname":"admin","password":"123456"
第五步:点击sending
第六步:获取token---保存
第一步://注意30679保持不变
第二步:使用get:http://192.168.10.136:30679/gathering/gathering
第三步:点击:Headers
第四步:粘贴进token
第五步:sending