Skip to main content

One post tagged with "Rancher"

View All Tags

Rancher滚动无感知切换镜像

· 2 min read
chenweibo

springboot 引入依赖

   <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

修改配置暴露端点

server:
port: 7878
shutdown: graceful

liteflow:
rule-source: config/**/*.el.xml
# Spring
spring:
mvc:
async:
request-timeout: 30000
application:
# 应用名称
name: xxxx
main:
allow-bean-definition-overriding: true
profiles:
# 环境配置
active: @spring.profiles.active@
lifecycle:
# 优雅下线超时时间
timeout-per-shutdown-phase: 5m
# 暴露 shutdown 接口
management:
endpoint:
health:
probes:
enabled: true
# 启用 shutdown 端点
shutdown:
enabled: true

创建一个端点来从 nacos 主动下线并且停止服务


import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "deregister")
@Log4j2
public class NacosServiceDeregisterEndpoint {

@Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties;

@Autowired
private NacosRegistration nacosRegistration;

@Autowired
private NacosServiceRegistry nacosServiceRegistry;

/**
* 从 nacos 中主动下线,用于 k8s 滚动更新时,提前下线分流流量
*
*/
@ReadOperation
public String endpoint() {
String serviceName = nacosDiscoveryProperties.getService();
String groupName = nacosDiscoveryProperties.getGroup();
String clusterName = nacosDiscoveryProperties.getClusterName();
String ip = nacosDiscoveryProperties.getIp();
int port = nacosDiscoveryProperties.getPort();

log.info("deregister from nacos, serviceName:{}, groupName:{}, clusterName:{}, ip:{}, port:{}", serviceName, groupName, clusterName, ip, port);
log.error("nacos下线成功提示");
// 设置服务下线
nacosServiceRegistry.setStatus(nacosRegistration, "DOWN");

return "success";
}
}

rancher 配置

//设置优雅停机字段
terminationGracePeriodSeconds: 600

//设置PreStop
sh -c 'curl http://127.0.0.1:7878/actuator/deregister;sleep 30;curl -X POST http://127.0.0.1:7878/actuator/shutdown;'

//设置健康检查等探针


spec:
containers:
- image: xxx.xxx.com/xxx/xxx-xx-xxx:3.3.0
imagePullPolicy: Always
lifecycle:
preStop:
exec:
command:
- sh
- '-c'
- >-
curl http://127.0.0.1:7878/actuator/deregister;sleep 30;curl -X POST
http://127.0.0.1:7878/actuator/shutdown;
livenessProbe:
failureThreshold: 10
httpGet:
path: /actuator/health/liveness
port: 7878
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
name: zdcloud-barcode-web
ports:
- containerPort: 7878
name: 7878tcp2
protocol: TCP
readinessProbe:
failureThreshold: 10
httpGet:
path: /actuator/health/readiness
port: 7878
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
startupProbe:
failureThreshold: 30
httpGet:
path: /actuator/health/liveness
port: 7878
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
__active: true
resources: {}