CICD 七(安装 ArgoCD,创建 Helm 模板)

安装 ArgoCD

在 192.168.0.217 上安装
参考 :https://www.cnblogs.com/klvchen/p/13362637.html
简单的可以使用 Nodeport 的方式开放端口

# 修改为 NodePort
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

# 获取 admin 的密码
kubectl get pods -n argocd -l app.kubernetes.io/name=argocd-server -o name | cut -d'/' -f 2

# 安装 ArgoCD 命令行工具
参考:
https://argoproj.github.io/argo-cd/getting_started/
https://argoproj.github.io/argo-cd/cli_installation/

# 记得登录
argocd login IP:NodePort


配置 Repositories 和 Projects

repo 地址填入: https://codeup.aliyun.com/5fd97f761acb1ae7cc188588/CICD.git
填入用户名和密码,点击测试可能会报错,但是不影响真正使用。



注意:红色框框都是需要填写的


创建 Helm 模板

在 192.168.0.217 上操作
创建 Helm 模板的作用主要用于制定一些通用配置,例如:CPU,内存的限制,暴露的端口,滚动更新策略等,方便我们的程序复用该模板配置。

mkdir /data/charts/ -p && cd /data/charts/

helm create yp-flask

cd yp-flask

# 删除 charts  文件夹
rm -rf charts 

# 删除 templates 文件夹下的某些配置
cd templates
rm -f  hpa.yaml NOTES.txt  serviceaccount.yaml

# 修改 deployment.yaml 
vi deployment.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "yp-flask.fullname" . }}
  labels:
    {{- include "yp-flask.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "yp-flask.selectorLabels" . | nindent 6 }}
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "yp-flask.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

# 修改 _helpers.tpl 
vi _helpers.tpl 

{{/*
Expand the name of the chart.
*/}}
{{- define "yp-flask.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "yp-flask.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s"  $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "yp-flask.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "yp-flask.labels" -}}
helm.sh/chart: {{ include "yp-flask.chart" . }}
{{ include "yp-flask.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "yp-flask.selectorLabels" -}}
app.kubernetes.io/name: {{ include "yp-flask.name" . }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "yp-flask.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "yp-flask.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

# 修改 ingress.yaml 
vi ingress.yaml

{{- if .Values.ingress.enabled -}}
{{- $fullName := include "yp-flask.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    {{- include "yp-flask.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ .path }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: {{ $svcPort }}
          {{- end }}
    {{- end }}
  {{- end }}

# 修改 service.yaml 
vi service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ include "yp-flask.fullname" . }}
  labels:
    {{- include "yp-flask.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "yp-flask.selectorLabels" . | nindent 4 }}


# 测试打包
cd /data/charts/

helm lint yp-flask
helm template yp-flask

helm package yp-flask

# 上传到 chartmuseum
curl --data-binary "@yp-flask-0.1.0.tgz" http://192.168.0.200:82/api/charts
helm repo update 
helm search repo chartmuseum

使用上面创建的 helm 模板 yp-flask 来创建一个新的应用

cd /data/charts/
helm create my-flask/
cd my-flask/

# 只保留 Chart.yaml,values.yaml 这两个文件
rm -rf templates charts

# 修改 Chart.yaml
vi Chart.yaml 

apiVersion: v2
appVersion: 1.16.0
dependencies:
- name: yp-flask
  repository: http://192.168.0.200:82
  version: 0.1.0
description: A Helm chart for Kubernetes
name: my-flask
type: application
version: 0.1.0

# 修改  values.yaml
vi values.yaml  

yp-flask:
  nameOverride: klvchen
  replicaCount: 2
  image:
    repository: ikubernetes/myapp
    tag: v1

# 更新依赖
helm dependency list
helm dependency update

# 测试打包
cd /data/charts/
helm lint ./my-flask/

helm template ./my-flask/

curl --data-binary "@my-flask-0.1.0.tgz" http://192.168.0.200:82/api/charts


# 下载 
curl -O http://192.168.0.200:82/charts/my-flask-0.1.0.tgz

# 删除一个chart版本
curl  -s -X DELETE  http://192.168.0.200:82/api/charts/my-flask/0.1.0

# 查看所有 chart
curl http://192.168.0.200:82/index.yaml
posted @ 2020-12-19 15:39  klvchen  阅读(522)  评论(0)    收藏  举报