新建eureka-service工程

1. 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">
    <parent>
        <artifactId>test-eureka</artifactId>
        <groupId>com.test</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <docker.image.prefix>pasq</docker.image.prefix>
        <!--docker harbor地址-->
        <docker.repostory>harborIp:harborPort</docker.repostory>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 使用Maven插件直接将应用打包为一个Docker镜像 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <!-- 这里使用新版dockerfile-maven-plugin插件 -->
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <configuration>
                    <!-- Dockerfile目录指定 -->
                    <dockerfile>Dockerfile</dockerfile>
                    <repository>${docker.repostory}/${docker.image.prefix}/${project.artifactId}</repository>
                    <!-- 生成镜像tag 如不指定 默认为latest -->
                    <tag>1.0.0</tag>
                    <buildArgs>
                        <JAR_FILE>./target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
2. yml
server:
  port: ${PORT:9007}
eureka:
  instance:
    hostname: ${EUREKA_INSTANCE_HOSTNAME:${spring.application.name}}
    appname: ${spring.application.name}
  client:
    registerWithEureka: ${BOOL_REGISTER:true}
    fetchRegistry: ${BOOL_FETCH:true}
    serviceUrl:
      defaultZone: ${EUREKA_SERVER:http://${eureka.instance.hostname}:${server.port}/eureka/}
  server:
    enable-self-preservation: ${SELF_PRESERVATION:false}
spring:
  application:
    name: eureka-server-test
3. Dockerfile【与工程src目录同级】
# 基础镜像
FROM openjdk:8-jdk-alpine
# 作者(可选)
MAINTAINER duhyper
# 删除无用组件 优化容器体积(可选)
RUN rm -rf /var/lib/apt/lists/*
# 创建jar包存放目录
RUN mkdir -p /usr/local/duhyper
# 获取pom文件中build参数
ARG JAR_FILE
# copy当前工程jar包至容器内
COPY ${JAR_FILE} /usr/local/duhyper/app.jar
# 运行jar包
ENTRYPOINT ["java","-jar","/usr/local/duhyper/app.jar"]
4. 创建启动类
package com.test.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class AppMain {
    public static void main(String[] args) {
        SpringApplication.run(AppMain.class, args);
    }
}

创建成功后打包编译镜像并推送至docker harbor

创建K8S配置文件eureka-server.yml

# eureka-statefulset.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: eureka-service-test
  labels:
    app: eureka-service-test
spec:
  ports:
    - port: 9007                                    # service对外提供服务的端口
      targetPort: 9007 
      nodePort: 9007
      name: eureka-service-test
  # clusterIP: None
  type: NodePort
  selector:
    app: eureka-service-test
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: eureka-service-test
spec:
  serviceName: "eureka-service-test"
  replicas: 3
  selector:
    matchLabels:
      app: eureka-service-test
  template:
    metadata:
      labels:
        app: eureka-service-test
    spec:
      containers:
        - name: eureka-service-test
          image: harborIp:harborPort/duhyper/eureka-server:1.0.0
          ports:
            - containerPort: 9007
          resources:
            limits:
              # jvm会自动发现该限制
              memory: 512Mi
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: JAVA_OPTS
              value: -XX:+UnlockExperimentalVMOptions
                -XX:+UseCGroupMemoryLimitForHeap
                -XX:MaxRAMFraction=2
                -XX:CICompilerCount=8
                -XX:ActiveProcessorCount=8
                -XX:+UseG1GC
                -XX:+AggressiveOpts
                -XX:+UseFastAccessorMethods
                -XX:+UseStringDeduplication
                -XX:+UseCompressedOops
                -XX:+OptimizeStringConcat
            - name: EUREKA_SERVER
              value: "http://eureka-service-test-0.eureka-service-test:9007/eureka/,http://eureka-service-test-1.eureka-service-test:9007/eureka/,http://eureka-service-test-2.eureka-service-test:9007/eureka/"
            - name: EUREKA_INSTANCE_HOSTNAME
              value: ${MY_POD_NAME}.eureka
  podManagementPolicy: "Parallel"

运行kubectl create -f eureka-server.yml, 然后访问k8s任意节点ip:9007访问eureka如下:
eureka

eureka客户端配置

eureka:
  instance:
    // k8s部署服务提供方 --与本地环境通信时使用这个配置【配置为k8s任意节点ip】
    ip-address: 192.168.1.235
    // k8s部署服务提供方 --k8s集群内部通信时使用这个配置
    # instance-id: ${spring.cloud.client.ip-address}:${server.port}
    // 本地部署 --使用这个配置
    # instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
  client:
    registerWithEureka: ${BOOL_REGISTER:true}
    fetchRegistry: ${BOOL_FETCH:true}
    serviceUrl:
      // k8s集群部署 自定义namespace使用这个配置 下面namespace替换为自己定义的namespace
#      defaultZone: http://eureka-service-test-0.eureka-service-test.namespace:9007/eureka/,http://eureka-service-test-1.eureka-service-test.namespace:9007/eureka/,http://eureka-service-test-2.eureka-service-test.namespace:9007/eureka/,
      // k8s集群部署 未定义/默认namespace使用这个配置
#      defaultZone: http://eureka-service-test-0.eureka-service-test:9007/eureka/,http://eureka-service-test-1.eureka-service-test:9007/eureka/,http://eureka-service-test-2.eureka-service-test:9007/eureka/,
      // 本地部署使用这个配置 【这里输入k8s全部节点,逗号分割】
      defaultZone: http://192.168.1.237:9007/eureka/,
spring:
  application:
    name: eureka-data

server:
  port: 8762
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐