• 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

    测试负载均衡

  • 相关阅读:
    Failed to read auto-increment value from storage engine错误的处理方法
    今天测试了一下 sqlalchemy 性能
    PIL The _imaging C module is not installed
    Windows下C语言调用dll动态链接库
    Linux下动态库和静态库的生成和使用
    安卓中WebKit的使用
    php使用curl扩展发送get和post请求示例
    注意笔记
    Linux下Apache2.2和PHP5的安装配置
    PHP文件上传学习
  • 原文地址:https://www.cnblogs.com/sweetchildomine/p/8845945.html
Copyright © 2020-2023  润新知