下面為大家介紹我司生產(chǎn)環(huán)境使用了3年的基于K8S的dev ops 配置實(shí)現(xiàn) K8s + SpringCloud實(shí)現(xiàn)零宕機(jī)發(fā)版,優(yōu)雅重啟:健康檢查+滾動(dòng)更新+優(yōu)雅停機(jī)+彈性伸縮+Prometheus監(jiān)控+配置分離(鏡像復(fù)用)
匯總配置
業(yè)務(wù)層面
項(xiàng)目依賴 pom.xml
使用 spring-boot-starter-actuator 鏡像 存活、就緒檢查
使用 prometheus 進(jìn)行應(yīng)用監(jiān)控
<!-- 引入Spring boot的監(jiān)控機(jī)制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
定義訪問端口、路徑及權(quán)限 application.yaml
spring:
application:
name: project-sample
profiles:
active: @profileActive@
lifecycle:
timeout-per-shutdown-phase: 30s # 停機(jī)過程超時(shí)時(shí)長(zhǎng)設(shè)置30s,超過30s,直接停機(jī)
server:
port: 8080
shutdown: graceful # 默認(rèn)為IMMEDIATE,表示立即關(guān)機(jī);GRACEFUL表示優(yōu)雅關(guān)機(jī)
management:
server:
port: 50000 # 啟用獨(dú)立運(yùn)維端口
metrics:
tags:
application: ${spring.application.name}
endpoint: # 開啟shutdown和health、serviceregistry(用于nacos下線)端點(diǎn)
shutdown:
enabled: true
serviceregistry:
enabled: true
health:
probes:
enabled: true
endpoints:
web:
exposure:
base-path: /actuator # 指定上下文路徑,啟用相應(yīng)端點(diǎn)
include: health,shutdown,metrics,prometheus
運(yùn)維層面
- 打包屬于我們自己的dockerfile基礎(chǔ)鏡像
FROM openjdk:8-jdk-alpine
LABEL Author andanyang
## 時(shí)區(qū)環(huán)境
ENV TZ=Asia/Shanghai
ENV JAVA_OPT
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /root
## 使用 skywalking 不用可忽略
COPY skywalking-agent /root/agent
## 解決EasyExcel 缺少字體問題
RUN apk --update add curl bash ttf-dejavu && \
rm -rf /var/cache/apk/*
RUN apk --update --no-cache add tini
## 使用 tini 啟動(dòng)應(yīng)用 讓容器里也可以使用jps等命令
ENTRYPOINT ["tini"]
docker build -t common/jdk8 .
-
打包各個(gè)服務(wù)的鏡像
FROM common/jdk8 ## 不使用 skywalking 可移除 CMD /usr/bin/java -Djava.security.egd=file:/dev/./urandom -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/log/ -javaagent:/root/agent/skywalking-agent.jar ${JAVA_OPT} -jar ${app_name}.jar COPY ${app_name}-impl/target/${app_name}-impl.jar ${app_name}.jar
k8s部署模版deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {APP_NAME}
labels:
app: {APP_NAME}
spec:
selector:
matchLabels:
app: {APP_NAME}
replicas: {REPLICAS} # Pod副本數(shù)
strategy:
type: RollingUpdate # 滾動(dòng)更新策略
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
name: {APP_NAME}
labels:
app: {APP_NAME}
annotations:
timestamp: {TIMESTAMP}
prometheus.io/port: "50000" # 不能動(dòng)態(tài)賦值
prometheus.io/path: /actuator/prometheus
prometheus.io/scrape: "true" # 基于pod的服務(wù)發(fā)現(xiàn)
spec:
affinity: # 設(shè)置調(diào)度策略,采取多主機(jī)/多可用區(qū)部署
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- {APP_NAME}
topologyKey: "kubernetes.io/hostname" # 多可用區(qū)為"topology.kubernetes.io/zone"
terminationGracePeriodSeconds: 30 # 優(yōu)雅終止寬限期
containers:
- name: {APP_NAME}
image: {IMAGE_URL}
imagePullPolicy: Always
ports:
- containerPort: {APP_PORT}
- name: management-port
containerPort: 50000 # 應(yīng)用管理端口
readinessProbe: # 就緒探針
httpGet:
path: /actuator/health/readiness
port: management-port
initialDelaySeconds: 30 # 延遲加載時(shí)間
periodSeconds: 10 # 重試時(shí)間間隔
timeoutSeconds: 1 # 超時(shí)時(shí)間設(shè)置
successThreshold: 1 # 健康閾值
failureThreshold: 9 # 不健康閾值
livenessProbe: # 存活探針
httpGet:
path: /actuator/health/liveness
port: management-port
initialDelaySeconds: 30 # 延遲加載時(shí)間
periodSeconds: 10 # 重試時(shí)間間隔
timeoutSeconds: 1 # 超時(shí)時(shí)間設(shè)置
successThreshold: 1 # 健康閾值
failureThreshold: 6 # 不健康閾值
resources: # 容器資源管理
limits: # 資源限制(監(jiān)控使用情況)
cpu: 0.5
memory: 1Gi
requests: # 最小可用資源(靈活調(diào)度)
cpu: 0.1
memory: 200Mi
lifecycle:
preStop:
exec:
command: # 結(jié)束回調(diào)鉤子,在實(shí)戰(zhàn)中SpringClould 中防止注冊(cè)中心沒有及時(shí)下掉服務(wù),最好需要手動(dòng)下線服務(wù)。再安全下線
- /bin/sh
- "-c"
- >-
curl -X "POST" "http://localhost:50000/actuator/service-registry?status=DOWN" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8";sleep
10s;curl -X "POST" "http://localhost:50000/actuator/shutdown" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8";
env:
- name: TZ
value: Asia/Shanghai
- name: JAVA_OPT # 配置JAVA啟動(dòng)參數(shù)
value: "-Xmx4g -Xms4g"
---
kind: HorizontalPodAutoscaler # 彈性伸縮控制器
apiVersion: autoscaling/v2beta2
metadata:
name: {APP_NAME}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {APP_NAME}
minReplicas: {REPLICAS} # 縮放范圍
maxReplicas: 6
metrics:
- type: Resource
resource:
name: cpu # 指定資源指標(biāo)
target:
type: Utilization
averageUtilization: 50
友情導(dǎo)讀
k8s學(xué)習(xí)系列
Prometheus 實(shí)戰(zhàn)系列
Prometheus Operator實(shí)戰(zhàn)—— Prometheus、Alertmanager、Grafana 監(jiān)控Springboot服務(wù)
基于K8S構(gòu)建企業(yè)級(jí)Jenkins CI/CD平臺(tái)實(shí)戰(zhàn)(一) 之 環(huán)境搭建
基于K8S構(gòu)建企業(yè)級(jí)Jenkins CI/CD平臺(tái)實(shí)戰(zhàn)(二) 之 kubernetes-plugin 插件使用文章來源:http://www.zghlxwxcb.cn/news/detail-463416.html
Jenkins優(yōu)化Kubernetes部署流水線文章來源地址http://www.zghlxwxcb.cn/news/detail-463416.html
到了這里,關(guān)于企業(yè)級(jí)實(shí)戰(zhàn) Spring Boot + K8S 中的滾動(dòng)發(fā)布、優(yōu)雅停機(jī)、彈性伸縮、應(yīng)用監(jiān)控、配置分離的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!