Skip to content

rolling-update in k8s

example

update.sh

#!/bin/bash
# shellcheck disable=SC2164 disable=SC1090  disable=SC2154
SHELL_FOLDER=$(cd "$(dirname "$0")" && pwd)
cd "$SHELL_FOLDER"

source <(curl -sSL https://dev.kubectl.net/func/log.sh)
source <(curl -sSL https://dev.kubectl.net/func/version_utils.sh)

namespace="rolling-update"
deployment_name="rolling-update-example"
container_name="rolling-update-example"
image_name="rolling-update-example"

# 判断deployment(rolling-update-example)是否存在
if kubectl get deployment/$deployment_name -n $namespace >/dev/null 2>&1; then
  log_info "k8s" "deployment($deployment_name) exists"
else
  log_info "k8s" "deployment($deployment_name) does not exist, create it"
  kubectl apply -f deploy.yml
  log_info "k8s" "deployment($deployment_name) created,then exit"
  exit
fi

if [ -z "$git_version" ]; then
  log_error "git_version is empty"
  exit 1
fi

kubectl set image deployment/$deployment_name \
  -n $namespace \
  $container_name=$image_name:$git_version

deploy.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rolling-update-example
  namespace: rolling-update
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rolling-update-example
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # 更新过程中允许超出期望副本数的最大值,可以是数字或百分比
      maxUnavailable: 0  # 更新过程中允许不可用的最大副本数,可以是数字或百分比
  template:
    metadata:
      labels:
        app: rolling-update-example
    spec:
      containers:
        - image: "rolling-update-example:latest"
          name: rolling-update-example
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: JAVA_OPTIONS
              value: -Xms256M -Xmx256M
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
            - containerPort: 50000
          # lifecycle:
          #   preStop: # 结束回调钩子
          #     exec:
          #       command: [ "curl", "-XPOST", "127.0.0.1:50000/actuator/shutdown" ]
          livenessProbe:
            httpGet:
              path: /liveness
              port: 8080
            initialDelaySeconds: 30      # 延迟加载时间
            periodSeconds: 10            # 重试时间间隔
            timeoutSeconds: 5            # 超时时间设置
            successThreshold: 1          # 健康阈值
            failureThreshold: 6          # 不健康阈值
          readinessProbe:
            httpGet:
              path: /readiness
              port: 8080
            initialDelaySeconds: 30      # 延迟加载时间
            periodSeconds: 10            # 重试时间间隔
            timeoutSeconds: 5            # 超时时间设置
            successThreshold: 1          # 健康阈值
            failureThreshold: 6          # 不健康阈值
---
# service
apiVersion: v1
kind: Service
metadata:
  name: rolling-update-example-service
  namespace: rolling-update
spec:
  selector:
    app: rolling-update-example
  type: ClusterIP
  ports:
    - port: 8080
      targetPort: 8080