kubernetes 部署 Eureka 集群,解决unavailable-replicas,available-replicas条件

时间:2018-04-14 12:38:57   收藏:0   阅读:8742

环境

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.0</modelVersion>

    <groupId>org.lzw.ms</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka</name>
    <description>eureka for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M9</spring-cloud.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </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页面

技术分享图片

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!