关于kubernetes:聊聊如何停止某个pod的流量

3次阅读

共计 2904 个字符,预计需要花费 8 分钟才能阅读完成。

本文次要钻研一下如何进行某个 pod 的流量

配置

# Copyright Istio Authors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

##################################################################################################
# Ratings service
##################################################################################################
apiVersion: v1
kind: Service
metadata:
  name: ratings
  labels:
    app: ratings
    service: ratings
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: ratings
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ratings-v1
  labels:
    app: ratings
    version: v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: ratings
      version: v1
  template:
    metadata:
      labels:
        app: ratings
        version: v1
    spec:
      containers:
      - name: ratings
        image: jvm-tools-demo:v2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /actuator/health/liveness
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        securityContext:
          runAsUser: 1000
        resources:
          # keep request = limit to keep this container in guaranteed class
          requests:
            cpu: 50m
            memory: 128Mi          
---

这里咱们配置了 livenessProbe 及 readinessProbe

readinessProbe

org/springframework/boot/actuate/availability/ReadinessStateHealthIndicator.java

public class ReadinessStateHealthIndicator extends AvailabilityStateHealthIndicator {public ReadinessStateHealthIndicator(ApplicationAvailability availability) {super(availability, ReadinessState.class, (statusMappings) -> {statusMappings.add(ReadinessState.ACCEPTING_TRAFFIC, Status.UP);
            statusMappings.add(ReadinessState.REFUSING_TRAFFIC, Status.OUT_OF_SERVICE);
        });
    }

    @Override
    protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {return applicationAvailability.getReadinessState();
    }

}

springboot 的 ReadinessStateHealthIndicator 提供了 /actuator/health/readiness 用于检测是否能够接管流量

变更

AvailabilityChangeEvent.publish(applicationContext, ReadinessState.REFUSING_TRAFFIC);
        return ReadinessState.REFUSING_TRAFFIC;

AvailabilityChangeEvent 提供了 publish 办法,能够将 ReadinessState 变更为 REFUSING_TRAFFIC

pods

kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
ratings-v1-54bf49c9bc-d88jb   1/1     Running   0          4m34s
ratings-v1-54bf49c9bc-dfjhh   1/1     Running   0          4m34s
ratings-v1-54bf49c9bc-flvcq   0/1     Running   0          4m34s

变更之后能够发现,k8s 的 get pods 显示其中一个 pod 的 ready 为 0,这里有个延时,取决于 periodSeconds 参数值,默认为 10(s)

小结

通过配置 pod 的 liveness 和 readiness,并在运行时变更 springboot 的 ReadinessState 变更为 REFUSING_TRAFFIC,能够将该 pod 从流量中移除,同时整个服务的正本个数不会像变更 label 那样多进去 pod。

doc

  • Configure Liveness, Readiness and Startup Probes
正文完
 0