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的负载均衡机制,下一篇文 ...
随机推荐
- android: 使用本地广播
前面我们发送和接收的广播全部都是属于系统全局广播,即发出的广播可以被其他任何 的任何应用程序接收到,并且我们也可以接收来自于其他任何应用程序的广播.这样就很容 易会引起安全性的问题,比如说我们发送的一 ...
- Android 编程下 WebView 加载一个网页如何得到网页的 Cookie 值
http://www.cnblogs.com/sunzn/archive/2013/04/03/2998113.html mWebView.setWebViewClient(new MyWebView ...
- [Mockito] Spring Unit Testing with Mockito
It is recommened to write unit testing with Mockito in Spring framework, because it is much faster w ...
- Spark LDA实战
选取了10个文档,其中4个来自于一篇论文,3篇来自于一篇新闻,3篇来自于另一篇新闻. 首先在pom文件中加入mysql-connector-java: <dependency> <g ...
- Duplicate复制数据库并创建物理StandBy(pfile版本)
1设定环境如下: Primary数据库 IP 172.17.22.16 SID orcl Standby数据库 IP 172.17.22.17 SID orcl 设置提示,以区分操作的位置 prima ...
- ISO镜像安装Ubuntu 13.04 64位,启动菜单制作
1.将光盘镜像中的vmlinuz.efi.initrd.lz,和镜像本身(ubuntu....iso) 三个文件复制到U盘根目录下.如果下面的方法没成功启动,你可能要把U盘格式化为USB-HDD FA ...
- sqlserver修改主键为自增
使用PowerDesigner创建一张表, 拷贝建表语句发现ID不是自增的, 以下是修改语句: ALTER TABLE USER_JOB_EXE_REC DROP COLUMN id; , ); 注: ...
- Cannot attach the file as database
Cannot attach the file as database这个异常是在EF的code frist里经常出现的,解决方法很简单,只要重新启动一下V11实例即可. CMD> sqlloca ...
- H5的Video事件,控制方法,及监听
1.标签基本属性 src :视频的属性 poster:视频封面,没有播放时显示的图片preload:预加载autoplay:自动播放loop:循环播放controls:浏览器自带的控制条width:视 ...
- 处理用千牛导出淘宝数据,供Logstash到Elasticsearch使用。(NodeJS)
var rf=require("fs"); // 加载编码转换模块 //npm install iconv-lite var iconv = require('iconv-lite ...