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

測試負載均衡

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM