3

我正在尝试使用Helm v2执行Kubernetes 滚动更新;但是,我做不到。

当我helm upgrade慢速 Tomcat 映像执行 a 时,原始 pod 被破坏。

我想弄清楚如何通过使用新实例增量更新 Pods 实例并耗尽旧实例来实现零停机时间。

为了演示,我创建了一个示例慢速 Tomcat Docker 映像和一个Helm 图表

安装:

helm install https://github.com/h-q/slowtom/raw/master/docs/slowtom.tgz --name slowtom \
     -f https://github.com/h-q/slowtom/raw/master/docs/slowtom/environments/initial.yaml

您可以通过运行来跟踪日志kubectl logs -f slowtom-sf-0,一旦准备好您就可以访问应用程序http://localhost:30901

升级:

(这就是我需要帮助的地方)

helm upgrade slowtom https://github.com/h-q/slowtom/raw/master/docs/slowtom.tgz \
     -f https://github.com/h-q/slowtom/raw/master/docs/slowtom/environments/upgrade.yaml

除了标签版本号之外,它与部署文件upgrade.yaml相同。initial.yaml

在这里,原来的 pod 被破坏了,新的 pod 开始了。同时,用户无法访问应用程序http://localhost:30901

删除:

helm del slowtom --purge

参考

本地头盔图

下载舵图:

curl -LO https://github.com/h-q/slowtom/raw/master/docs/slowtom.tgz
tar vxfz ./slowtom.tgz

从本地 helm-chart 安装:

helm install --debug ./slowtom --name slowtom -f ./slowtom/environments/initial.yaml

从本地 helm-chart 升级:

helm upgrade --debug slowtom ./slowtom -f ./slowtom/environments/upgrade.yaml

码头工人形象

Dockerfile

FROM tomcat:8.5-jdk8-corretto

RUN mkdir /usr/local/tomcat/webapps/ROOT && \
    echo '<html><head><title>Slow Tomcat</title></head><body><h1>Slow Tomcat Now Ready</h1></body></html>' >> /usr/local/tomcat/webapps/ROOT/index.html

RUN echo '#!/usr/bin/env bash' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo 'x=2' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo 'secs=$(($x * 60))' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo 'while [ $secs -gt 0 ]; do' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo '   >&2 echo -e "Blast off in $secs\033[0K\r"' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo '   sleep 1' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo '   : $((secs--))' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo 'done' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo '>&2 echo "slow cataline done. will now start real catalina"' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    echo 'exec catalina.sh run' >> /usr/local/tomcat/bin/slowcatalina.sh && \
    chmod +x /usr/local/tomcat/bin/slowcatalina.sh 

ENTRYPOINT ["/usr/local/tomcat/bin/slowcatalina.sh"]

Helm 图表内容

slowtom/Chart.yaml

apiVersion: v1
description: slow-tomcat Helm chart for Kubernetes
name: slowtom
version: 1.1.2  # whatever

slowtom/values.yaml

# Do not use this file, but ones from environmments folder

slowtom/environments/initial.yaml

# Storefront
slowtom_sf:
  name: "slowtom-sf"
  hasHealthcheck: "true"
  isResilient: "false"
  replicaCount: 2
  aspect_values:
    - name: y_aspect
      value: "storefront"

image:
  repository: hqasem/slow-tomcat
  pullPolicy: IfNotPresent
  tag: 1
env:
  - name: y_env
    value: whatever

slowtom/environments/upgrade.yaml

# Storefront
slowtom_sf:
  name: "slowtom-sf"
  hasHealthcheck: "true"
  isResilient: "false"
  replicaCount: 2
  aspect_values:
    - name: y_aspect
      value: "storefront"

image:
  repository: hqasem/slow-tomcat
  pullPolicy: IfNotPresent
  tag: 2
env:
  - name: y_env
    value: whatever

slowtom/templates/deployment.yaml

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Values.slowtom_sf.name }} 
  labels:
    chart: "{{ .Chart.Name | trunc 63 }}"
    chartVersion: "{{ .Chart.Version | trunc 63 }}" 
    visualize: "true" 
    app: {{ .Values.slowtom_sf.name }} 
spec:
  replicas: {{ .Values.slowtom_sf.replicaCount }} 
  selector: 
    matchLabels:
      app: {{ .Values.slowtom_sf.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.slowtom_sf.name }}
        visualize: "true" 
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      containers: 
        - name: {{ .Values.slowtom_sf.name }} 
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/usr/local/tomcat/bin/slowcatalina.sh"]
          args: ["whatever"]
          env:
{{ toYaml .Values.env | indent 12 }}
{{ toYaml .Values.slowtom_sf.aspect_values | indent 12 }}
          resources: 
{{ toYaml .Values.resources | indent 12 }}
---

slowtom/templates/service.yaml

kind: Service
apiVersion: v1
metadata:
  name: {{.Values.slowtom_sf.name}} 
  labels:
    chart: "{{ .Chart.Name | trunc 63 }}"
    chartVersion: "{{ .Chart.Version | trunc 63 }}"
    app: {{.Values.slowtom_sf.name}}
    visualize: "true" 
    hasHealthcheck: "{{ .Values.slowtom_sf.hasHealthcheck }}" 
    isResilient: "{{ .Values.slowtom_sf.isResilient }}"
spec:
  type: NodePort
  selector:
    app: {{.Values.slowtom_sf.name}}
  sessionAffinity: ClientIP
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      name: http
      nodePort: 30901
---
4

2 回答 2

2

与 不同DeploymentStatefulSet在滚动更新期间销毁旧 pod 之前不会启动新 pod。相反,期望是您有多个 pod,它们将被一个一个替换。由于您只配置了 1 个副本,因此必须先将其销毁。将您的副本数增加到 2 个或更多,或者切换到Deployment模板。

https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#rolling-update

于 2020-10-01T15:49:42.753 回答
0

我通过将准备就绪或启动探针添加到我的deployment.yaml

slowtom/templates/deployment.yaml

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Values.slowtom_sf.name }} 
  labels:
    chart: "{{ .Chart.Name | trunc 63 }}"
    chartVersion: "{{ .Chart.Version | trunc 63 }}" 
    visualize: "true" 
    app: {{ .Values.slowtom_sf.name }} 
spec:
  replicas: {{ .Values.slowtom_sf.replicaCount }} 
  selector: 
    matchLabels:
      app: {{ .Values.slowtom_sf.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.slowtom_sf.name }}
        visualize: "true" 
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      containers: 
        - name: {{ .Values.slowtom_sf.name }} 
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/usr/local/tomcat/bin/slowcatalina.sh"]
          args: ["whatever"]
          env:
{{ toYaml .Values.env | indent 12 }}
{{ toYaml .Values.slowtom_sf.aspect_values | indent 12 }}
          resources: 
{{ toYaml .Values.resources | indent 12 }}
          readinessProbe:
            httpGet:
              path: /
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 1
            failureThreshold: 3
---
于 2021-03-17T08:49:11.050 回答