环境
k8s master: 1个
k8s node: 3个
三个eureka 指定node启动,并且使用network=host
完整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"> <modelVersion>4.0.0modelVersion> <groupId>org.lzw.msgroupId> <artifactId>eurekaartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>jarpackaging> <name>eurekaname> <description>eureka for Spring Bootdescription> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.1.RELEASEversion> <relativePath/> parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <spring-cloud.version>Finchley.M9spring-cloud.version> <skipTests>trueskipTests> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> <exclusions> <exclusion> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-tomcatartifactId> exclusion> exclusions> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-jettyartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-dependenciesartifactId> <version>${spring-cloud.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> <repositories> <repository> <id>spring-milestonesid> <name>Spring Milestonesname> <url>https://repo.spring.io/milestoneurl> <snapshots> <enabled>falseenabled> snapshots> repository> repositories> project>
用于k8s 心跳健康检查
/** * User: laizhenwei * Date: 2018-04-12 Time: 16:09 */ @RestController @RequestMapping(path = "/") public class Healthz { @GetMapping(path = "/healthz",produces = MediaType.TEXT_PLAIN_VALUE) public String healthz(){ return "ok"; } }
启动类
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
application.yml
spring:
application:
name: EUREKA
application-test.yml available-replicas条件为
1.eureka.instance.appname 必须等于 spring.application.name 并且不可缺省,所以直接占位符 appname: ${spring.application.name}
2.prefer-ip-address: 必须为false 或者缺省
3.fetch-registry 必须非false 或者缺省
eureka:
instance:
appname: ${spring.application.name}
# prefer-ip-address: true
lease-expiration-duration-in-seconds: 90
server:
enable-self-preservation: true
#5秒清理一次
eviction-interval-timer-in-ms: 5000
client:
register-with-eureka: true
# fetch-registry: true
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/,http://${eureka-rs1.hostname}:${eureka-rs1.port}/eureka/,http://${eureka-rs2.hostname}:${eureka-rs2.port}/eureka/
logging:
config: classpath:logback-test.xml
打包成镜像
docker build -t ms-eureka .
上传到私有仓库
docker tag ms-eureka 192.168.91.137:5000/ms-eureka docker push 192.168.91.137:5000/ms-eureka
编写 Deployment.yaml,因为使用host 部署,虽然部署在不同的k8s节点,但是因为k8s设计如果节点挂了可以在其他节点中拉起来保持副本集的数量,随便漂移,所以即使是使用host 并且指定k8s节点,其他节点部署也不能ip相同
这里只列出eureka-0, 剩下两个,自行修改参数
eureka-0
nodeName: k8s-node-0 这里指定只再 0号节点启动 eureka-0
livenessProbe,readinessProbe为心跳检查,会请求到上面写的 Healthz Controller
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: eureka-0
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
app: eureka-0
template:
metadata:
labels:
app: eureka-0
spec:
nodeName: k8s-node-0
terminationGracePeriodSeconds: 60
hostNetwork: true
containers:
- name: eureka
image: 192.168.91.137:5000/ms-eureka
command: ["java"]
args: ["-jar", "/usr/local/eureka.jar","--spring.profiles.active=test","--server.port=8000","--spring.application.name=eureka","--eureka.instance.appname=eureka","--eureka.instance.hostname=k8s-node-0","--eureka-rs1.hostname=k8s-node-1","--eureka-rs1.port=8001","--eureka-rs2.hostname=k8s-node-2","--eureka-rs2.port=8002"]
ports:
- name: http
containerPort: 8000
hostPort: 8000
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8000
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8000
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
name: eureka-0
namespace: ms
labels:
app: eureka-0
spec:
ports:
- port: 8000
name: eureka-0
targetPort: 8000
selector:
app: eureka-0
启动3个 eureka
kubectl create -f eureka-0.yaml
kubectl create -f eureka-1.yaml
kubectl create -f eureka-2.yaml
查看部署情况
打开eureka页面