k8s部署eureka集群

eureka 不阐述介绍,这里直接开始在kubernetes下部署eureka集群

1、配置文件的增加

eureka 只一个有状态的服务,部署有状态服务我们可以使用StatefulSet

1.1、增加dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat Dockerfile
FROM docker.io/xxlaila/centos7.6-jdk1.8:latest
MAINTAINER xxlaila "cq_xxlaila@163.com"
# Install dependent plugin

ADD target/kxl-eureka.jar /opt/webapps/kxl-eureka.jar
ADD application.yaml /opt/webapps/application.yaml

WORKDIR /opt/webapps

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev", "kxl-eureka.jar"]

1.2、修改配置文件

在做eureka集群的时候,application.yaml的配置文件很重要,配置文件做不好,将会直接影响到eureka的启动,还有集群的模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ cat application.yaml
spring:
application:
name: kxl-eureka
server:
port: 8761
eureka:
instance:
# prefer-ip-address: true
hostname: ${EUREKA_HOST_NAME:peer1} #服务主机名
appname: ${spring.application.name} #服务名称,默认为 unknow 这里直接取 spring.application.name 了
# server 从最后一次收到心跳到移除废弃服务的超时时间(秒)
lease-expiration-duration-in-seconds: 90
# client 给 server 发送心跳的间隔时间(秒),比 lease-expiration-duration-in-seconds 小
lease-renewal-interval-in-seconds: 30

client:
serviceUrl:
defaultZone: ${EUREKA_URL_LIST:http://peer1:8761/eureka/} # 指定服务中心 eureka server的地址
# client 是否从eureka上拉取注册信息, server模式可关掉
fetch-registry: ${BOOL_FETCH:true} # 是否拉取 eureka server 的注册信息。 默认为true
# client 是否注册到eureka上, server模式可关掉
register-with-eureka: ${BOOL_REGISTER:true} # 是否把服务中心本身当做eureka client 注册。默认为true
# client 间隔多久去拉去服务信息(秒)
registry-fetch-interval-seconds: 30
server:
# 自我保护机制,应对网络闪断情况,大面积丢失过多的client,不删除服务
enable-self-preservation: ${SELF_PRESERVATION:true} # 是否开启自我保护。 默认为 true.
# 每分钟心跳数 实际/期望,如果小于阈值(threshold),则触发自我保护机制
renewal-percent-threshold: 0.85
# 扫描失效服务的间隔时间(毫秒)
eviction-interval-timer-in-ms: 60000

application:
name: ${EUREKA_APPLICATION_NAME:eureka-server}

1.3、创建 eureka docker镜像

1
2
$ docker build -t xxlaila/kxl-eureka:v1 .
$ docker push xxlaila/kxl-eureka:v1

2、在k8s创建eureka集群

2.1、创建eureka集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
$ cat kxl-eureka.yaml
---
apiVersion: v1
kind: Service
metadata:
name: eureka
namespace: kube-dev
labels:
app: eureka
spec:
ports:
- port: 8761
name: eureka
clusterIP: None
selector:
app: eureka
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: eureka
namespace: kube-dev
spec:
serviceName: "eureka"
replicas: 3
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
containers:
- name: eureka
image: docker.io/xxlaila/kxl-eureka:v1
ports:
- containerPort: 8761
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
# Due to camelcase issues with "defaultZone" and "preferIpAddress", _JAVA_OPTIONS is used here
- name: eureka_client_serviceUrl_defaultZone
#value: http://eureka-0.eureka.<namespace>.svc.cluster.local:8761/eureka/,http://eureka-1.eureka.<namespace>.svc.cluster.local:8761/eureka/
value: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/,http://eureka-2.eureka:8761/eureka/
- name: EUREKA_CLIENT_REGISTERWITHEUREKA
value: "true"
- name: EUREKA_CLIENT_FETCHREGISTRY
value: "true"
# In the docker image, this is set to localhost. Otherwise, we could leave this empty.
# The hostnames must match with the the eureka serviceUrls, otherwise the replicas are reported as unavailable in the eureka dashboard
- name: EUREKA_INSTANCE_HOSTNAME
#value: "$(MY_POD_NAME).eureka.<namespace.svc.cluster.local"
value: "$(MY_POD_NAME).eureka"
#value: eureka
# For the other (stateless) services, this should probably be set to true, since their pods have no DNS-resolvable hostnames
#- name: EUREKA_INSTANCE_PREFERIPADDRESS
# value: "false"
# No need to start the pods in order. We just need the stable network identity
podManagementPolicy: "Parallel"

在做这个的时候其实遇到了很多坑,也是参考一些文章才完成的,参考文献

2.2、创建eureka Ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat eureka-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: eureka-ingress
namespace: kube-dev
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/secure-backends: "true"
spec:
rules:
- host: eureka.xxlaila.io
http:
paths:
- backend:
serviceName: eureka
servicePort: 8761

2.3、执行创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ kubectl apply -f .
$ kubectl get pods -n kube-dev
NAME READY STATUS RESTARTS AGE
eureka-0 1/1 Running 0 21m
eureka-1 1/1 Running 0 21m
eureka-2 1/1 Running 0 21m
$ kubectl get pods,svc,rs -n kube-dev
NAME READY STATUS RESTARTS AGE
pod/eureka-0 1/1 Running 0 21m
pod/eureka-1 1/1 Running 0 21m
pod/eureka-2 1/1 Running 0 21m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/eureka ClusterIP None <none> 8761/TCP 21m

2.4、访问验证

img
通过域名访问:
img

3、eureka 环境

    根据2.4小节可以看到,enviroonment为test,我们在dockerfile指定的为dev,所以这里就有点差池,但是查看了一下资料,这个要么就写多个application.yaml的配置文件,要么就打多个包,这样就比较麻烦,而且考虑到公司微服务的特殊性,既要满足于公司的微服务架构,有要考虑的模版的通用性,还需要考虑运维维护的便捷性。下面一起来看看基于公司的定制化来改变这个局限性。

3.1、公司的系统环境变量

1
2
3
4
5
6
7
8
CONFIG_API_SERVER=http://api.conf.xxlaila.io
RUN_CLUSTER=default
RUN_MODE=AUTO
RUN_ENV=demo
CONFIG_API_SERVER:公司配置中心api的地址,app拉取配置中心的配置
RUN_CLUSTER:默认集群,做灰度发布使用
RUN_MODE:
RUN_ENV:当前系统所运行的环境

3.2、修改eureka的配置文件

增加配置eureka:environment: ${RUN_ENV}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ cat application.yaml
spring:
application:
name: kxl-eureka
server:
port: 8761
eureka:
environment: ${RUN_ENV}
instance:
# prefer-ip-address: true
hostname: ${EUREKA_HOST_NAME:peer1}
appname: ${spring.application.name}
# server 从最后一次收到心跳到移除废弃服务的超时时间(秒)
lease-expiration-duration-in-seconds: 90
# client 给 server 发送心跳的间隔时间(秒),比 lease-expiration-duration-in-seconds 小
lease-renewal-interval-in-seconds: 30

client:
serviceUrl:
defaultZone: ${EUREKA_URL_LIST:http://peer1:8761/eureka/}
# client 是否从eureka上拉取注册信息, server模式可关掉
fetch-registry: ${BOOL_FETCH:true}
# client 是否注册到eureka上, server模式可关掉
register-with-eureka: ${BOOL_REGISTER:true}
# client 间隔多久去拉去服务信息(秒)
registry-fetch-interval-seconds: 30
server:
# 自我保护机制,应对网络闪断情况,大面积丢失过多的client,不删除服务
enable-self-preservation: ${SELF_PRESERVATION:true}
# 每分钟心跳数 实际/期望,如果小于阈值(threshold),则触发自我保护机制
renewal-percent-threshold: 0.85
# 扫描失效服务的间隔时间(毫秒)
eviction-interval-timer-in-ms: 60000

application:
name: ${EUREKA_APPLICATION_NAME:eureka-server}

3.3、修改eureka部署文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
$ cat kxl-eureka.yaml 
---
apiVersion: v1
kind: Service
metadata:
name: eureka
namespace: kube-dev
labels:
app: eureka
spec:
ports:
- port: 8761
name: eureka
clusterIP: None
selector:
app: eureka
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: eureka
namespace: kube-dev
spec:
serviceName: "eureka"
replicas: 3
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
containers:
- name: eureka
image: docker.io/xxlaila/kxl-eureka:v2
ports:
- containerPort: 8761
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
# Due to camelcase issues with "defaultZone" and "preferIpAddress", _JAVA_OPTIONS is used here
- name: eureka_client_serviceUrl_defaultZone
#value: http://eureka-0.eureka.<namespace>.svc.cluster.local:8761/eureka/,http://eureka-1.eureka.<namespace>.svc.cluster.local:8761/eureka/
value: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/,http://eureka-2.eureka:8761/eureka/
- name: EUREKA_CLIENT_REGISTERWITHEUREKA
value: "true"
- name: EUREKA_CLIENT_FETCHREGISTRY
value: "true"
# In the docker image, this is set to localhost. Otherwise, we could leave this empty.
# The hostnames must match with the the eureka serviceUrls, otherwise the replicas are reported as unavailable in the eureka dashboard
- name: EUREKA_INSTANCE_HOSTNAME
#value: "$(MY_POD_NAME).eureka.<namespace.svc.cluster.local"
value: "$(MY_POD_NAME).eureka"
- name: RUN_ENV
value: test
- name: CONFIG_API_SERVER
value: http://api.conf.xxlaila.io
- name: RUN_CLUSTER
value: default
- name: RUN_MODE
value: AUTO
#value: eureka
# For the other (stateless) services, this should probably be set to true, since their pods have no DNS-resolvable hostnames
#- name: EUREKA_INSTANCE_PREFERIPADDRESS
# value: "false"
# No need to start the pods in order. We just need the stable network identity
podManagementPolicy: "Parallel"

3.4、重建pod

pod重建以后我们经过访问可以看到

img
img

后续持续优化

坚持原创技术分享,您的支持将鼓励我继续创作!
0%