kubernetes有狀態(tài)部署:statefulset

問(wèn)題背景

在k8s上部署eureka的時(shí)候,3個(gè)節(jié)點(diǎn)的注冊(cè)列表各不一樣,完全沒(méi)起到注冊(cè)中心集群的效果,相當(dāng)于3個(gè)節(jié)點(diǎn)各自為戰(zhàn)。了解相關(guān)信息后(查了Stack Overflow),需要在springboot的服務(wù)中配置pod的hostname和域名,但k8s本身又是無(wú)狀態(tài)部署,IP會(huì)一直發(fā)生改動(dòng)。

關(guān)鍵字

解決方案流程,暫時(shí)沒(méi)有時(shí)間寫(xiě)的比較清楚,下面列出關(guān)鍵詞和主體思路,后續(xù)還有需要的話(huà)直接從以下關(guān)鍵詞入手可以解決問(wèn)題

  1. kubernetes statefulset
  2. k8s配置文件中的yaml配置環(huán)境變量,直接在springboot的配置文件中用${}使用即可

配置文件一覽

springboot配置文件

spring:
  application:
    name: register-center
server:
  port: 33001

eureka:
  environment: prod
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 60000
  instance:
    appname: ${spring.application.name}
    prefer-ip-address: false
    hostname: ${MY_POD_NAME}.push-register-center
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90
  client:
    registry-fetch-interval-seconds: 2
    register-with-eureka: true
    fetch-registry: true
    service-url:
#      defaultZone: @eureka.url@
      defaultZone: http://push-register-center-0.push-register-center:33001/eureka/,http://push-register-center-1.push-register-center:33001/eureka/

k8s配置文件

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: push-register-center
  namespace: rec
  labels:
    service: push-register-center
spec:
  replicas: 2
  serviceName: push-register-center
  selector:
    matchLabels:
      service: push-register-center
  template:
    metadata:
      labels:
        service: push-register-center

    spec:
      containers:
        - name: push-register-center
          image: hub.meiyou.com/ai-center/push-register-center:myversion
          env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          resources:
            limits:
              cpu: 8
              memory: 8Gi
            requests:
              cpu: 4
              memory: 4Gi
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 33001
          livenessProbe:
            httpGet:
              path: /actuator/health
              port: 33001
            initialDelaySeconds: 20
            periodSeconds: 5
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: 33001
            initialDelaySeconds: 20
            periodSeconds: 5
            timeoutSeconds: 5
      nodeSelector:
        cpu-alg2: "true"
---

apiVersion: v1
kind: Service
metadata:
  name: push-register-center
  namespace: rec
spec:
  type: NodePort
  ports:
    - port: 33001
      protocol: TCP
      nodePort: 33051
  selector:
    service: push-register-center

最終結(jié)果

發(fā)布變得非常平滑,3個(gè)節(jié)點(diǎn)的注冊(cè)列表變得一致,springbootadmin再也沒(méi)有一直叫掉節(jié)點(diǎn)的告警了,舒服了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容