Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡
因为服务需要可缩容,所以不能使用host部署.
涉及两个应用,zuul,basic-info-api
验证,在k8s任意一个node 从zuul 访问 basic-info-api
创建一个SpringBoot应用: basic-info-api
用于检查健康状态的Controller
/**
* User: laizhenwei
* Date: 2018-04-12 Time: 16:10
*/
@RestController
@RequestMapping(path = "/")
public class Healthz { @Value("${spring.application.name}")
private String serviceId; @Autowired
private LoadBalancerClient loadBalancer; @GetMapping(path = "/healthz",produces = MediaType.TEXT_PLAIN_VALUE)
public String healthz(){
return "ok";
} @GetMapping(path = "/getHost",produces = MediaType.TEXT_PLAIN_VALUE)
public String getHost(){
ServiceInstance instance = loadBalancer.choose(serviceId);
return instance.getHost();
}
}
Application.java
@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication
public class BasicInfoApiApplication {
public static void main(String[] args) {
SpringApplication.run(BasicInfoApiApplication.class, args);
}
}
application.yaml
spring:
application:
name: BASIC-INFO-API
profiles:
active: dev
application-test.yaml
eureka:
instance:
appname: ${spring.application.name}
prefer-ip-address: true
#续约更新时间间隔
lease-renewal-interval-in-seconds: 10
#续约到期时间
lease-expiration-duration-in-seconds: 30
client:
serviceUrl:
defaultZone: http://192.168.91.141:8000/eureka/,http://192.168.91.142:8001/eureka/,http://192.168.91.143:8002/eureka/
logging:
config: classpath:logback-test.xml
打包上传到私有仓库
docker build -t ms-basic-info-api .
tag ms-basic-info-api 192.168.91.137:/ms-basic-info-api
docker tag push 192.168.91.137:/ms-basic-info-api
Deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: basic-info-api
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
app: basic-info-api
template:
metadata:
labels:
app: basic-info-api
spec:
terminationGracePeriodSeconds: 60
# hostNetwork: true
containers:
- name: basic-info-api
image: 192.168.91.137:5000/ms-basic-info-api
command: ["java"]
args: ["-jar", "/usr/local/basic-info-api.jar","--spring.profiles.active=test","--server.port=8300"]
ports:
- name: http
containerPort: 8300
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8300
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8300
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
--- apiVersion: v1
kind: Service
metadata:
name: basic-info-api
namespace: ms
labels:
app: basic-info-api
spec:
selector:
app: basic-info-api
clusterIP: 172.21.1.2
ports:
- name: http
port: 8300
protocol: TCP
master 执行
kubectl create -f basic-info-api.yaml
查看Eureka页面

创建 zuul 服务
健康检查Controller
/**
* 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";
}
}
Application.java
@EnableZuulProxy
@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
application.yaml
spring:
application:
name: API-GATEWAY
profiles:
active: dev
application-test.yaml
eureka:
instance:
appname: ${spring.application.name}
prefer-ip-address: true
#续约更新时间间隔
lease-renewal-interval-in-seconds: 10
#续约到期时间
lease-expiration-duration-in-seconds: 30
client:
serviceUrl:
defaultZone: http://192.168.91.141:8000/eureka/,http://192.168.91.142:8001/eureka/,http://192.168.91.143:8002/eureka/ logging:
config: classpath:logback-test.xml
打包镜像,上传到私有仓库
docker build -t ms-zuul .
docker tag ms-zuul 192.168.91.137:/ms-zuul
docker push 192.168.91.137:/ms-zuul
Deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: zuul
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
app: zuul
template:
metadata:
labels:
app: zuul
spec:
terminationGracePeriodSeconds: 60
containers:
- name: zuul
image: 192.168.91.137:5000/ms-zuul
command: ["java"]
args: ["-jar", "/usr/local/zuul.jar","--spring.profiles.active=test","--server.port=8200"]
ports:
- name: http
containerPort: 8200
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8200
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 8200
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
--- apiVersion: v1
kind: Service
metadata:
name: zuul
namespace: ms
labels:
app: zuul
spec:
selector:
app: zuul
ports:
- name: http
port: 8200
protocol: TCP
master 执行
kubectl create -f zuul.yaml
查看Eureka

进入k8s任意一个节点 根据zuul访问basic-info-api
curl http://172.20.20.4:8200/basic-info-api/healthz

测试伸缩 与 负载均衡


因刚才重启了一个节点,zuul地址改变了
curl http://172.20.20.6:8200/basic-info-api/getHost
测试负载均衡

Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡的更多相关文章
- SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)
FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...
- 微服务Kong(十)——负载均衡参考
KONG为请求多个后端服务提供了多种负载均衡方案:一种是简单的基于DNS,另一种是更加动态的环形均衡器,他在不需要DNS服务器的情况下也允许服务注册. 一.基于DNS的负载均衡 当使用基于DNS的负载 ...
- 三十一、【WCF路由中间件】WCFHosting服务主机的路由器与负载均衡和实现思路
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://pan.baid ...
- SpringCloud升级之路2020.0.x版-32. 改进负载均衡算法
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们梳理了实现 Feign 断路器以及线程隔离的思路,这一节,我们先不看如何源 ...
- 基于Dubbo框架构建分布式服务(集群容错&负载均衡)
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...
- HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例
一.HAProxy的ACL的功能 ACL(Access Control List)访问控制列表,HAProxy中的ACL的匹配条件和控制条件有许多种,功能很强大,可以通过源地址.源端口.目标地址.目标 ...
- 微服务通信之feign集成负载均衡
前言 书接上文,feign接口是如何注册到容器想必已然清楚,现在我们着重关心一个问题,feign调用服务的时候是如何抉择的?上一篇主要是从读源码的角度入手,后续将会逐步从软件构架方面进行剖析. 一.R ...
- springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
创建5个项目: 1.服务注册中心 2.服务提供者1 3.服务提供者2(与服务提供者1的代码实现一样,这是是为了模拟负载均衡) 4.ribbon客户端项目 5.feign客户端项目 如图: 一.注册中心 ...
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
上一篇文章单独介绍了Ribbon框架的使用,及其如何实现客户端对服务访问的负载均衡,但只是单独从Ribbon框架实现,没有涉及spring cloud.本文着力介绍Ribbon的负载均衡机制,下一篇文 ...
随机推荐
- ERROR: While executing gem … (Gem::RemoteFetcher::FetchError)
原文地址:https://www.zfanw.com/blog/error-while-executing-gem-gem-remote-fetch-error.html 我对命令行下安装 gem 包 ...
- AB Test 是什么
关于AB Test是什么 一种灰度发布方式. ps:什么是灰度发布 每个灰度对象都是0%(白色)到100%(黑色)的中间值,灰度发布是指在黑白之间,能够平滑过度的一种发布方式. 实现方式 让一部分用户 ...
- windows命令行下杀死进程的方法
xp和win7下有两个好东东tasklist和tskill.tasklist能列出所有的进程,和相应的信息.tskill能查杀进程,语法很简单:tskill程序名!或者是tskill 进程id 例如: ...
- Linux下 $(cd `dirname $0`;pwd)
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".".这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目 ...
- Socket编程的UDP与TCP,应用在哪些地方
随着网络技术飞速发展,网速已不再是传输的瓶颈,UDP协议以其简单.传输快的优势,在越来越多场景下取代了TCP,如网页浏览.流媒体.实时游戏.物联网. 1,网速的提升给UDP稳定性提供可靠网络保障 CD ...
- zeromq学习笔记1——centos下安装 zeromq-4.1.2
1.前言 MQ(message queue)是消息队列的简称,可在多个线程.内核和主机盒之间弹性伸缩.ZMQ的明确目标是“成为标准网络协议栈的一部分,之后进入Linux内核”.现在还未看到它们的成功. ...
- npm install node-sass失败
Cannot download "https://github.com/sass/node-sass/releases/download/v3.8.0/win32-x64-46_bindin ...
- 【Zookeeper】源码分析之Leader选举(二)之FastLeaderElection
一.前言 前面学习了Leader选举的总体框架,接着来学习Zookeeper中默认的选举策略,FastLeaderElection. 二.FastLeaderElection源码分析 2.1 类的继承 ...
- SpringCloud服务如何在Eureka安全优雅的下线
如果直接KILL SpringCloud的服务,因为Eureka采用心跳的机制来上下线服务,会导致服务消费者调用此已经kill的服务提供者然后出错,处理这种情况有2中方案. 如需平滑的发布服务请参考: ...
- supervisor //todo
#安装easy-installyum install python-setuptools #安装 supervisoreasy_install supervisor #创建主配置文件echo_supe ...