【K8S】helm chart多环境部署最佳实践-示例

时间:2022-02-11 21:48:34

Chart.yaml

apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: nginx
version: 0.1.0

values.yaml

# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 2

image:
  repository: nginx
  tag: 1.15-alpine

envs:
  PARAM_JVMTOOL: "123456"
  PARAM_SPRING: "654321"

resources:
  requests:
    cpu: "0.1"
    memory: "256M"

values-dev.yaml

# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

envs:
  PARAM_AAA: "123456_aaa_dev"
  PARAM_BBB: "123456_bbb_dev"
  PARAM_CCC: "123456_ccc_dev"
  PARAM_DDD: "123456_ddd_dev"

resources:
  limits:
    cpu: "0.5"
    memory: "512M"

values-test.yaml

# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

envs:
  PARAM_AAA: "123456_aaa_test"
  PARAM_BBB: "123456_bbb_test"
  PARAM_CCC: "123456_ccc_test"
  PARAM_DDD: "123456_ddd_test"

resources:
  limits:
    cpu: "0.5"
    memory: "512M"

values-stage.yaml

# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

envs:
  PARAM_AAA: "123456_aaa_stage"
  PARAM_BBB: "123456_bbb_stage"
  PARAM_CCC: "123456_ccc_stage"
  PARAM_DDD: "123456_ddd_stage"

resources:
  limits:
    cpu: "0.5"
    memory: "1024M"

values-prod.yaml

# Default values for nginx.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

envs:
  PARAM_AAA: "123456_aaa_prod"
  PARAM_BBB: "123456_bbb_prod"
  PARAM_CCC: "123456_ccc_prod"
  PARAM_DDD: "123456_ddd_prod"

resources:
  limits:
    cpu: "0.5"
    memory: "1024M"

templates/deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: Always
        name: nginx
        command: ["/bin/sh", "-c", "echo $HOSTNAME > /usr/share/nginx/html/index.html && exec nginx -g 'daemon off;'"]
        ports: 
        - containerPort: 80
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        env:
        {{- range $key, $value := .Values.envs }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}

templates/service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: nginx
  sessionAffinity: None
  type: ClusterIP
  clusterIP: None

---
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    # 30000-32767
    # nodePort: 30080
  selector:
    app: hello-world
  #sessionAffinity: None
  type: NodePort

deploy.sh

#!/bin/bash

envType=${1:-dev}
helm install --name=nginx -f values.yaml -f values-dev.yaml --set image.repository=docker-repo.xxx.com/k2/nginx --set image.tag=1.12.2 --set envs.PARAM_JVMTOOL=xxxxxxxx --set envs.PARAM_AAA=yyyyyyyyy --dry-run --debug .

debug.txt

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: docker-repo.xxx.com/k2/nginx:1.12.2
        imagePullPolicy: Always
        name: nginx
        command: ["/bin/sh", "-c", "echo $HOSTNAME > /usr/share/nginx/html/index.html && exec nginx -g 'daemon off;'"]
        ports: 
        - containerPort: 80
        resources:
          limits:
            cpu: "0.5"
            memory: 512M
          requests:
            cpu: "0.1"
            memory: 256M
          
        env:
        - name: PARAM_AAA
          value: "yyyyyyyyy"
        - name: PARAM_BBB
          value: "123456_bbb_dev"
        - name: PARAM_CCC
          value: "123456_ccc_dev"
        - name: PARAM_DDD
          value: "123456_ddd_dev"
        - name: PARAM_JVMTOOL
          value: "xxxxxxxx"
        - name: PARAM_SPRING
          value: "654321"