序
本文主要研究一下如何停止某个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。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。