807-812 Helm 实战 8.7-8.12

发布时间 2023-10-29 12:52:16作者: zhan0

8.10 自定义Chart实站

8.10.1 创建chart包

# helm create k8syyds
Creating k8syyds

8.10.2 自定义template模板文件

删除默认模板文件

# cd k8syyds/
[root@master-1-230 k8syyds]# ll
总用量 8
drwxr-xr-x 2 root root    6 10月 28 23:14 charts
-rw-r--r-- 1 root root 1143 10月 28 23:14 Chart.yaml
drwxr-xr-x 3 root root  162 10月 28 23:14 templates
-rw-r--r-- 1 root root 1874 10月 28 23:14 values.yaml
[root@master-1-230 k8syyds]# rm -rf templates/*

生成一个deployment 模板

# kubectl create deployment aminglinux --image=nginx:1.23.2 -o yaml --dry-run > templates/deployment.yaml
W1028 23:15:53.336724   57914 helpers.go:692] --dry-run is deprecated and can be replaced with --dry-run=client.

修改deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ .Values.appname }}                  #将values.yaml中的appname对应的变量值渲染在这里
  name: k8syyds
spec:
  replicas: {{ .Values.replicas }}              #将values.yaml中的replicas对应的变量值渲染在这里
  selector:
    matchLabels:
      app: {{ .Values.appname }}                #标签可以和资源名称一样,因此也可以直接调用appname变量
  template:
    metadata:
      labels:
        app: {{ .Values.appname }}              #标签可以和资源名称一样,因此也可以直接调用appname变量
    spec:
      containers:
      - image: {{ .Values.image }}:{{ .Values.imageTag }}               #将values.yaml中的image、imageTag对应的变量值渲染在这里,表示镜像的版本号
        name: {{ .Values.appname }}                     #容器的名称也和资源的名称保持一致即可
        command: [ "/bin/sh","-c","/data/init.sh" ]
        ports:
        - name: web
          containerPort: 80
          protocol: TCP
        volumeMounts:
        - name: code
          mountPath: /data/code/k8syyds
        - name: config
          mountPath: /data/nginx/conf/conf.d/
      volumes:  
        - name: config
          configMap:
            name: {{ .Values.appname }}-cm                              #confimap的名字也可以使用程序名称的变量加上-cm
        - name : code
          persistentVolumeClaim:
            claimName: {{ .Values.appname }}-pvc                #pvc的名字也可以使用程序名称的变量加上-pv
            readOnly: false    

编辑svc模板:vim templates/service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: {{ .Values.appname }}                  #service要管理deployment的pod资源,因此这里的标签要和pod资源的标签对应上,直接调用appname这个变量
  name: {{ .Values.appname }}-svc               #service资源的名称,也可以直接调用appname这个变量,后面加一个-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: {{ .Values.appname }}                  #标签选择器还是调用appname这个变量
  type: NodePort

编辑configmap模板:vim templates/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.appname }}-cm                        #引入appname变量加上-cm作为cm资源的名称
data:
  k8syyds.com.conf: |
    server {
      listen 80;
      server_name k8syyds.com;
      location / {
        root /data/code/k8syyds;
        index index.html;
      }
    }

编辑pv/pvc模板:vim templates/pv-pvc.yaml

apiVersion: v1
kind:  PersistentVolume
metadata:
  name: {{ .Values.appname }}-pv                        #引入appname变量加上-pv作为pv资源的名称
  labels:
    pv: {{ .Values.appname }}-pv                        #标签也可以使用和pv名称一样的名字
spec:
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: {{ .Values.nfsPath }}                         #这里会引入nfsPath变量的值
    server: {{ .Values.nfsServer }}                     #这里会引入nfsServer变量的值
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ .Values.appname }}-pvc                       #引入appname变量加上-pvc作为pvc资源的名称
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  selector:
    matchLabels:
      pv: {{ .Values.appname }}-pv                      #指定pv的标签

定义value:vim values.yaml

appname: k8syyds
replicas: 2
image: registry.cn-shenzhen.aliyuncs.com/ikubernetesi/helm-custom-chart:v0  ##这是一个测试的镜像
imageTag: v0
nfsPath: /data/nfs/k8syyds  ##这个目录需要提前创建好
nfsServer: 192.168.1.230

8.10.3 安装chat

# helm install k8syyds-release .
NAME: k8syyds-release
LAST DEPLOYED: Sat Oct 28 23:57:13 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

8.10.4 查看svc

# kubectl  get svc -o wide
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)              AGE   SELECTOR
k8syyds-svc              NodePort    10.104.98.207   <none>        80:31216/TCP         10h   app=k8syyds
kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP              31d   <none>
redis-cluster            ClusterIP   10.102.241.2    <none>        6379/TCP             12h   app.kubernetes.io/instance=redis-cluster,app.kubernetes.io/name=redis-cluster
redis-cluster-headless   ClusterIP   None            <none>        6379/TCP,16379/TCP   12h   app.kubernetes.io/instance=redis-cluster,app.kubernetes.io/name=redis-cluster

8.10.5 在NFS服务器创建测试页

echo "hello kubernetes" > /data/nfs/k8syyds/index.html

8.10.6 浏览器访问验证

http://192.168.1.231:31216/


8.11使用Helm安装harbor

8.11.1 下载harbor的chart包

Harbor的chartmuseum可以让Helm直接将chart包推送到harbor,现在安装2.6.2版本

# helm search repo harbor -l
 # helm search repo harbor -l
NAME          	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/harbor	19.0.5       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	19.0.4       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	19.0.3       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	19.0.2       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	19.0.1       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	19.0.0       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	18.0.3       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	18.0.2       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	18.0.1       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	18.0.0       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	17.1.3       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	17.1.2       	2.9.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	17.1.1       	2.8.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	17.1.0       	2.8.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	17.0.0       	2.8.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.7.4       	2.8.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.7.3       	2.8.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.7.2       	2.8.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.7.1       	2.8.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.7.0       	2.8.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.8       	2.8.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.7       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.6       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.5       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.4       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.3       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.2       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.6.1       	2.8.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.5.3       	2.8.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.5.2       	2.8.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.5.1       	2.8.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.10      	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.9       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.8       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.7       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.6       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.5       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.4       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.3       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.2       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.1       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.4.0       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.7       	2.7.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.6       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.5       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.4       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.3       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.1       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.3.0       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.2.0       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.1.4       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.1.3       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.1.2       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.1.1       	2.7.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.1.0       	2.6.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.0.4       	2.6.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.0.3       	2.6.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.0.2       	2.6.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.0.1       	2.6.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	16.0.0       	2.6.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.3.0       	2.6.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.5       	2.6.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.4       	2.6.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.3       	2.6.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.2       	2.6.0      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.1       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.2.0       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.1.0       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.0.5       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.0.4       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.0.3       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.0.2       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	15.0.0       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	14.0.5       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	14.0.4       	2.5.3      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	14.0.3       	2.5.2      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	14.0.2       	2.5.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	14.0.1       	2.5.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	13.2.7       	2.5.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	13.2.6       	2.5.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	13.2.4       	2.5.1      	Harbor is an open source trusted cloud-native r...
bitnami/harbor	13.2.2       	2.5.1      	Harbor is an open source trusted cloud-native r...
# helm pull bitnami/harbor  --version 16.1.0 --untar
[root@master-1-230 8.11]# ll
总用量 0
drwxr-xr-x 6 root root 152 10月 29 11:01 harbor

 8.11.2 修改默认values.yaml

cd harbor
vi values.yaml  #更改
storageClass: "nfs-client"  ##这个是提前搭建好的nfs的storageclass

将所有"core.harbor.domain"替换为harbor.ikubernetes.cloud
sed -i 's/core.harbor.domain/harbor.ikubernetes.cloud/g' values.yaml

8.11.3 安装

# helm install myharbor --version 16.1.0 .
NAME: myharbor
LAST DEPLOYED: Sun Oct 29 11:11:31 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: harbor
CHART VERSION: 16.1.0
APP VERSION: 2.6.2

** Please be patient while the chart is being deployed **

1. Get the Harbor URL:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace default -w myharbor'
    export SERVICE_IP=$(kubectl get svc --namespace default myharbor --template "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
    echo "Harbor URL: http://$SERVICE_IP/"

2. Login with the following credentials to see your Harbor application

  echo Username: "admin"
  echo Password: $(kubectl get secret --namespace default myharbor-core-envvars -o jsonpath="{.data.HARBOR_ADMIN_PASSWORD}" | base64 -d)

 8.11.4 查看端口

# kubectl get svc |grep harbor |grep LoadBalancer
myharbor                  LoadBalancer   10.109.168.141   <pending>     80:30336/TCP,443:31788/TCP,4443:30645/TCP   33s

 8.11.5 查看密码

# kubectl get secret --namespace default myharbor-core-envvars -o jsonpath="{.data.HARBOR_ADMIN_PASSWORD}" | base64 -d
a75r2IFRas

 8.11.6 浏览器登录

https://192.168.1.231:31788/

 

 

 

8.12将Chart推送到私有仓库harbor

8.12.1 安装helm-push 插件

从gitee 镜像仓库 https://gitee.com/ikubernetesi/helm-push安装插件,官网地址:https://github.com/chartmuseum/helm-push

helm plugin install helm plugin install https://gitee.com/ikubernetesi/helm-push

手动安装插件

mkdir /root/.local/share/helm/plugins/helm-push
cd /root/.local/share/helm/plugins/helm-push
wget https://github.com/chartmuseum/helm-push/releases/download/v0.10.4/helm-push_0.10.4_linux_amd64.tar.gz
tar -zxvf helm-push_0.10.4_linux_amd64.tar.gz

检查插件10.109.168.141

# helm plugin list
NAME   	VERSION	DESCRIPTION                      
cm-push	0.10.4 	Push chart package to ChartMuseum

 8.12.2 添加harbor地址

在harbor浏览器后台,添加新的项目 chart_repo

 helm 添加新仓库(harbor.ikubernetes.cloud 域名解析到10.109.168.141)

# helm repo add myharbor https://harbor.ikubernetes.cloud/chartrepo/chart_repo --username=admin --password=a75r2IFRas
Error: looks like "https://harbor.ikubernetes.cloud/chartrepo/chart_repo" is not a valid chart repository or cannot be reached: Get "https://harbor.ikubernetes.cloud/chartrepo/chart_repo/index.yaml": tls: failed to verify certificate: x509: certificate signed by unknown authority

# echo -n | openssl s_client -showcerts -connect harbor.ikubernetes.cloud:443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >> /etc/ssl/certs/ca-bundle.trust.crt
[root@master-1-230 8.12]# 
[root@master-1-230 8.12]# helm repo add myharbor https://harbor.ikubernetes.cloud/chartrepo/chart_repo --username=admin --password=a75r2IFRas
"myharbor" has been added to your repositories

8.12.3 推送自定义chart

# ll
总用量 0
drwxr-xr-x 4 root root 93 10月 28 23:54 k8syyds
[root@master-1-230 8.10]# helm cm-push k8syyds/ myharbor
Pushing k8syyds-0.1.0.tgz to myharbor...
Done.

8.13.4 查看

# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "myharbor" chart repository
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈
[root@master-1-230 8.10]# helm search repo k8syyds
NAME            	CHART VERSION	APP VERSION	DESCRIPTION                
myharbor/k8syyds	0.1.0        	1.16.0     	A Helm chart for Kubernetes

8.13.5 更新自定义chart

# cat Chart.yaml values.yaml
 # cat Chart.yaml 
apiVersion: v2
name: k8syyds
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.5

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.5"
[root@master-1-230 k8syyds]# cat values.yaml 
# Default values for k8syyds.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.


appname: k8syyds
replicas: 1
image: aminglinux/helm-custom-chart  ##这是一个测试的镜像
imageTag: v0
nfsPath: /data/nfs/k8syyds  ##这个目录需要提前创建好
nfsServer: 192.168.1.230

replicaCount: 1


serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""

podAnnotations: {}

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80

nodeSelector: {}

tolerations: []

affinity: {}

 

8.13.6 升级本地版本

[root@master-1-230 k8syyds]# helm  upgrade k8syyds-release .
Release "k8syyds-release" has been upgraded. Happy Helming!
NAME: k8syyds-release
LAST DEPLOYED: Sun Oct 29 12:36:33 2023
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

8.13.7 推送到私有仓库

[root@master-1-230 k8syyds]# cd ..
[root@master-1-230 8.10]# helm cm-push k8syyds/ myharbor
Pushing k8syyds-0.1.5.tgz to myharbor...
Done.

8.13.8 利用远程仓库安装release

#更新本地仓库
# cd k8syyds/
[root@master-1-230 k8syyds]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "myharbor" chart repository
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

#删除之前的release
cd k8syyds/
[root@master-1-230 k8syyds]# helm uninstall k8syyds-release
release "k8syyds-release" uninstalled

#安装远程仓库
# helm install k8syyds-2 myharbor/k8syyds
NAME: k8syyds-2
LAST DEPLOYED: Sun Oct 29 12:44:32 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None