序
本文次要钻研一下如何进行某个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: v1kind: Servicemetadata: name: ratings labels: app: ratings service: ratingsspec: ports: - port: 8080 name: http selector: app: ratings---apiVersion: apps/v1kind: Deploymentmetadata: name: ratings-v1 labels: app: ratings version: v1spec: 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 podsNAME READY STATUS RESTARTS AGEratings-v1-54bf49c9bc-d88jb 1/1 Running 0 4m34sratings-v1-54bf49c9bc-dfjhh 1/1 Running 0 4m34sratings-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