因为服务需要可缩容,所以不能使用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, 伸缩与负载均衡的更多相关文章

  1. SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)

    FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...

  2. 微服务Kong(十)——负载均衡参考

    KONG为请求多个后端服务提供了多种负载均衡方案:一种是简单的基于DNS,另一种是更加动态的环形均衡器,他在不需要DNS服务器的情况下也允许服务注册. 一.基于DNS的负载均衡 当使用基于DNS的负载 ...

  3. 三十一、【WCF路由中间件】WCFHosting服务主机的路由器与负载均衡和实现思路

    回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://pan.baid ...

  4. SpringCloud升级之路2020.0.x版-32. 改进负载均衡算法

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们梳理了实现 Feign 断路器以及线程隔离的思路,这一节,我们先不看如何源 ...

  5. 基于Dubbo框架构建分布式服务(集群容错&负载均衡)

    Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...

  6. HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例

    一.HAProxy的ACL的功能 ACL(Access Control List)访问控制列表,HAProxy中的ACL的匹配条件和控制条件有许多种,功能很强大,可以通过源地址.源端口.目标地址.目标 ...

  7. 微服务通信之feign集成负载均衡

    前言 书接上文,feign接口是如何注册到容器想必已然清楚,现在我们着重关心一个问题,feign调用服务的时候是如何抉择的?上一篇主要是从读源码的角度入手,后续将会逐步从软件构架方面进行剖析. 一.R ...

  8. springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用

    创建5个项目: 1.服务注册中心 2.服务提供者1 3.服务提供者2(与服务提供者1的代码实现一样,这是是为了模拟负载均衡) 4.ribbon客户端项目 5.feign客户端项目 如图: 一.注册中心 ...

  9. Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制

    上一篇文章单独介绍了Ribbon框架的使用,及其如何实现客户端对服务访问的负载均衡,但只是单独从Ribbon框架实现,没有涉及spring cloud.本文着力介绍Ribbon的负载均衡机制,下一篇文 ...

随机推荐

  1. block 相关清单

    对Objective-C中Block的追探  李博士

  2. MUI右滑关闭窗口用Webview的drag实现

    mui.init({swipeBack:true}); 必须要用很快的速度摩擦屏幕才能达到右滑关闭窗口的效果,且在右边一般都会失效. mui这个滑动,是纯前端的,这个效率在Android上确实一般. ...

  3. iOS:用Block写一个链式编程

    一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...

  4. 奇怪吸引子---NoseHoover

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  5. Spark机器学习(7):KMenas算法

    KMenas算法比较简单,不详细介绍了,直接上代码. import org.apache.log4j.{Level, Logger} import org.apache.spark.{SparkCon ...

  6. scala sortBy and sortWith

    sortBy: sortBy[B](f: (A) ⇒ B)(implicit ord: math.Ordering[B]): List[A] 按照应用函数f之后产生的元素进行排序 sorted: so ...

  7. spring-mybatis-data-common程序级分表操作实例

    spring-mybatis-data-common-2.0新增分表机制,在1.0基础上做了部分调整. 基于机架展示分库应用数据库分表实力创建 create table tb_example_1( i ...

  8. [Aaronyang] 写给自己的WPF4.5 笔记9[复杂数据处理三步曲,数据展示ListView泪奔2/3]

     我的文章一定要做到对读者负责,否则就是失败的文章  ---------   www.ayjs.net    aaronyang技术分享 作者留言:        小小的推荐,作者的肯定,读者的支持. ...

  9. 那天有个小孩教我WCF[一][2/3]

    接着上次的继续讲吧 我们开始吧 9.创建数据库 use master go --创建库 if exists(select * from sysdatabases where name='NewsDB' ...

  10. 【转载整理】mysql权限分配详解

    原文:https://www.cnblogs.com/Csir/p/7889953.html MySQL权限级别 1)全局性的管理权限,作用于整个MySQL实例级别 2)数据库级别的权限,作用于某个指 ...