kubernetes有状态部署:statefulset

问题背景

在k8s上部署eureka的时候,3个节点的注册列表各不一样,完全没起到注册中心集群的效果,相当于3个节点各自为战。了解相关信息后(查了Stack Overflow),需要在springboot的服务中配置pod的hostname和域名,但k8s本身又是无状态部署,IP会一直发生改动。

关键字

解决方案流程,暂时没有时间写的比较清楚,下面列出关键词和主体思路,后续还有需要的话直接从以下关键词入手可以解决问题

  1. kubernetes statefulset
  2. k8s配置文件中的yaml配置环境变量,直接在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

最终结果

发布变得非常平滑,3个节点的注册列表变得一致,springbootadmin再也没有一直叫掉节点的告警了,舒服了

你可能感兴趣的:(kubernetes有状态部署:statefulset)