1.创建eureka服务
1.1创建父级项目,父级项目pom.xml文件中的打包类型为pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.digua</groupId>
<artifactId>cloud</artifactId>
<version>release</version>
<packaging>pom</packaging>
<modules>
<module>eureka</module>
<module>order</module>
<module>monitor</module>
<module>common</module>
<module>zuul</module>
</modules>
<properties>
<java.version>1.8</java.version>
<mybatis.plus.version>3.4.2</mybatis.plus.version>
<mysql.connect.java.version>8.0.27</mysql.connect.java.version>
<druid.version>1.2.8</druid.version>
<spring-boot-dependencies.version>2.3.7.RELEASE</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>Hoxton.SR12</spring-cloud-dependencies.version>
</properties>
</project>
1.2创建eureka服务
1.1.2配置pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.digua</groupId>
<artifactId>cloud</artifactId>
<version>release</version>
</parent>
<groupId>com</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>eureka</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.1.3创建eureka的application.yml文件
server:
port: 8001
spring:
application:
name: eureka01
eureka:
client:
service-url:
defaultZone: ${EUREKA_URL:http://127.0.0.1:8001/eureka/}
1.2构建项目推送到docke仓库
此步骤省略可参考以下连接
1.2.1-docker安装配置
1.2.2-Dockerfile 文件编写
1.2.3 -jenkins构建项目
1.2.4-Harbor仓库搭建
2.创建k8s对应的yaml文件
2.1 由于eureka是集群形式存在,可以有一个也可以有N个,对于eureka的相互注册要保持有序,所以使用k8s中的StatefulSets来进行
2.1.1- StatefulSet是为了解决有状态服务的问题(对应Deployments和ReplicaSets是为无状态服务而设计),配合Headless Service(无头服务) 进行服务的扩缩容
##格式会程这种样式
statefulSet的名称-0(从0开始每增加一个pod就递增).Headless (无头服务名称).命名空间.svc.cluster.local
statefulSetName-{0...N-1}.serviceName.namespace.svc.cluster.local
2.2 创建 Headless Service 无头服务
apiVersion: v1
kind: Service
metadata:
name: eureka-hl ##服务名称
namespace: test ##命名空间
spec:
clusterIP: None ##无头服务
ports:
- name: eureka
port: 8001
protocol: TCP
targetPort: 8001
selector:
workload.user.cattle.io/workloadselector: apps.statefulset-test-eureka
sessionAffinity: None
type: ClusterIP
2.3 创建 StatefulSet 有状态服务
apiVersion: apps/v1
kind: StatefulSet ##资源类型
metadata:
labels:
workload.user.cattle.io/workloadselector: apps.statefulset-test-eureka
name: eureka ##服务名称
namespace: test ##命名空间
spec:
podManagementPolicy: OrderedReady
replicas: 2 ##副本数目
revisionHistoryLimit: 10 ##历史最大保留数目
selector:
matchLabels:
workload.user.cattle.io/workloadselector: apps.statefulset-test-eureka
serviceName: eureka-hl ## Headless Service 无头服务名称
template:
metadata:
labels:
workload.user.cattle.io/workloadselector: apps.statefulset-test-eureka
spec:
containers: ##容器信息
- env:
- name: EUREKA_URL
- ##相同的服务内可以简写,如下
value: http://eureka-0.eureka-hl:8001/eureka/,http://eureka-1.eureka-hl:8001/eureka/
image: 192.168.34.8:85/hhd/eureka:1 ##镜像
imagePullPolicy: Always ##拉取策略
name: eureka
restartPolicy: Always ##重启策略
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate
3.部署查看eureka服务
3.1使用命令部署两个服务
kubectl create -f 文件名称.yaml
3.2.查看服务
kubectl get sts eureka -n test
3.3 通过浏览器查看eureka的dashboard页面
3.3.1 首先暴露eureka的NodePort端口
apiVersion: v1
kind: Service
metadata:
name: eureka-np
namespace: test
spec:
clusterIP: 10.108.244.16
externalTrafficPolicy: Cluster
ports:
- name: eureka
nodePort: 30001
port: 8001
protocol: TCP
targetPort: 8001
selector:
workload.user.cattle.io/workloadselector: apps.statefulset-test-eureka
sessionAffinity: None
type: NodePort
4.k8s中部署微服务的其他模块
4.1创建服务对应的pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud</artifactId>
<groupId>com.digua</groupId>
<version>release</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>order</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<finalName>order</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.2 配置application.yml文件
server:
port: 8011
spring:
application:
name: order-server01
eureka:
client:
service-url:
defaultZone: ${EUREKA_URL:http://127.0.0.1:8001/eureka/}
4.3 构建发布harbor过程忽略,当前发布的是一个无状态服务,所以用到Deployment无状态服务
请注意,因为是同一个命名空间下,可以直接通过Headless的服务名称访问pod
上边2.2部分创建的Headless,通过 eureka-hl 既可以轮询访问下边的pod
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
workload.user.cattle.io/workloadselector: apps.deployment-test-order
name: order
namespace: test
spec:
progressDeadlineSeconds: 600
replicas: 0
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: apps.deployment-test-order
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
workload.user.cattle.io/workloadselector: apps.deployment-test-order
spec:
containers:
- env:
- name: EUREKA_URL
##可以通过Headless 的服务名称直接访问
value: http://eureka-hl:8001/eureka/
image: 192.168.34.8:85/hhd/order:1
imagePullPolicy: Always
name: order
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
4.4 发布项目,并在eureka的dashboard页面查看文章来源:https://www.toymoban.com/news/detail-431426.html
kubectl apply -f 文件名称.yaml
文章来源地址https://www.toymoban.com/news/detail-431426.html
到了这里,关于k8s发布eureka集群,创建微服务项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!