k8s 部署 Eureka及使用
配置参数参考:https://www.cnblogs.com/fangfuhai/p/7070325.htmlhttps://www.cnblogs.com/liukaifeng/p/10052594.html一、制作docker镜像文件所在测试k8s集群 master1/home/sunwenbo/test/docker/eureka目录树eureka/├── application.yaml├
配置参数参考:微服务架构:Eureka参数配置项详解 - 风中程序猿 - 博客园
Spring Cloud Eureka(四):Eureka 配置参数说明 - IT码客 - 博客园
一、制作docker镜像
文件所在测试k8s集群 master1
/home/sunwenbo/test/docker/eureka
目录树
eureka/
├── application.yaml
├── Dockerfile
└── entrypoint.sh
application.yaml
eureka:
instance:
#eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒
leaseRenewalIntervalInSeconds: 10
#Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
leaseExpirationDurationInSeconds: 30
metadata-map:
VERSION: 1.0.0
#与此实例相关联的主机名,是其他实例可以用来进行请求的准确名称
hostname: ${MY_POD_NAME}.register-server
#以IP地址注册到服务中心,相互注册使用IP地址,默认false
prefer-ip-address: false
client:
#客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: ${EUREKA_CLIENT_FETCH_REGISTRY:true}
#指示此实例是否应将其信息注册到eureka服务器以供其他服务发现,默认为false
register-with-eureka: ${EUREKA_CLIENT_REGISTER_WITH_EUREKA:true}
#可用区域映射到与eureka服务器通信的完全限定URL列表。每个值可以是单个URL或逗号分隔的备用
serviceUrl:
defaultZone: ${EUREKA_DEFAULT_ZONE:http://register-server-0.register-server:8000/eureka/,http://register-server-1.register-server:8000/eureka/,http://register-server-2.register-server:8000/eureka/}
#从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒
registryFetchIntervalSeconds: 5
#指示eureka客户端是否禁用增量提取
disable-delta: true
server:
#过期实例应该启动并运行的时间间隔,单位为毫秒
evictionIntervalTimerInMs: 4000
#启用自我保护机制,默认为true
enable-self-preservation: ${EUREKA_SERVER_ENABLE_SELF_PRESERVATION:true}
Dockerfile
FROM harbor.int.xxx-inc.com/eureka/eureka-1.0.0
RUN mkdir /config
COPY application.yaml /config
ADD entrypoint.sh /usr/bin/entrypoint.sh
EXPOSE 8000
CMD ["entrypoint.sh"]
entrypoint.sh
#!/bin/bash
java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -jar /eureka.jar --spring.config.location=/config/application.yaml
docker build -t harbor.int.xxx-inc.com/eureka/eureka:latest .
docker push harbor.int.xxx-inc.com/eureka/eureka:latest
二、k8s 部署StatefulSet 有状态 Eureka 配置无头服务
/home/sunwenbo/test/kubernetes
eureka/
├── eureka-statefulSet.yaml
├── eureka-svc.yaml
└── ingress-eureka.yaml
eureka-statefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: register-server
labels:
service: register-server
spec:
replicas: 3
serviceName: register-server
selector:
matchLabels:
service: register-server
template:
metadata:
labels:
service: register-server
annotations:
service: register-server
spec:
containers:
- name: register-server
image: harbor.int.xxx-inc.com/eureka/eureka
imagePullPolicy: Always
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- name: http
containerPort: 8000
protocol: TCP
resources:
requests:
memory: "100Mi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "2"
livenessProbe:
tcpSocket:
port: 8000
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health
port: 8001
scheme: HTTP
failureThreshold: 3
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
eureka-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: register-server
labels:
service: register-server
spec:
clusterIP: None
type: ClusterIP
ports:
- port: 8000
targetPort: http
protocol: TCP
name: http
selector:
service: register-server
ingress-eureka.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-eureka
namespace: default
annotations:
kubernetes.io/ingress.class: ingress-nginx1
spec:
rules:
- host: test.eureka.xxx-inc.com
http:
paths:
- path: /
backend:
serviceName: register-server
servicePort: 8000
部署命令
cd /home/sunwenbo/test/kubernetes/eureka
kubectl apply -f ./
三、使用Python 模拟服务注册
eureka-demo.py
# coding:utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.web import RequestHandler
import py_eureka_client.eureka_client as eureka_client
from tornado.options import define, options
define("port", default=32001, help="run on the given port", type=int)
class IndexHandler(RequestHandler):
def get(self):
username = self.get_argument('username', 'Hello')
self.write(username + ', Administrator User!')
#restfulAPI功能实现在这里
class TestHandler(RequestHandler):
def get(self):
self.write("hello world welcome to you")
def eurekaclient():
tornado.options.parse_command_line()
# 注册eureka服务
eureka_client.init(eureka_server="http://test.eureka.xxx-inc.com/eureka",
app_name="python-test02",
instance_port=32001)
#提供外部调用的接口
app = tornado.web.Application(handlers=[(r"/", IndexHandler),
(r"/test", TestHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
print("eureka exec")
if __name__ == "__main__":
eurekaclient()
运行完上述python程序后,Eureka web页面会显示如下注册信息
该日志为为euerka-2的日志,可以看到每10秒 euerka会调用euerka-client从eureka服务获得所有实例注册表信息,并更新自己的实例注册表信息。
当停掉一个注册服务时,euerka会很敏捷的发现该服务的状态,并修改该服务的状态为DOWN,后台日志也会提示该服务的状态已经为DOWN如下图
因为euerka会每10秒定时的更新注册表信息,之后会将消息通知到其他的euerka server,随后同步信息
更多推荐
所有评论(0)