zoukankan      html  css  js  c++  java
  • 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:5000/ms-basic-info-api
    docker tag push 192.168.91.137:5000/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:5000/ms-zuul
    docker push 192.168.91.137:5000/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

    测试负载均衡

  • 相关阅读:
    简单的购物车
    分页显示
    登录验证码的实现
    简单遗传算法代码
    jQ
    2.servlet的会话机制session
    1.servlet的会话机制cookie
    基本数据类型和引用数据类型的区别
    struts2-第一章-基础用法2
    struts2第一章-基本用法
  • 原文地址:https://www.cnblogs.com/sweetchildomine/p/8845945.html
Copyright © 2011-2022 走看看