kubernetes helm
一、helm介绍
二、helm 3 安装使用
# 下载,安装 wget https://get.helm.sh/helm-v3.4.2-linux-amd64.tar.gz tar -zxvf helm-v3.4.2-linux-amd64.tar.gz mv linux-amd64/helm /usr/bin/helm 添加存储库 helm repo add stable http://mirror.azure.cn/kubernetes/charts helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 更新产库地址 helm repo update 查看配置的存储库 helm repo list helm search repo stable 删除存储库: helm repo remove aliyun helm 基本使用 主要介绍三个命令: chart install chart upgrade chart rollback 使用 chart 部署一个应用 # helm search repo weave [root@master-node helm]# helm search repo weave NAME CHART VERSION APP VERSION DESCRIPTION stable/weave-cloud 0.3.9 1.4.0 DEPRECATED - Weave Cloud is a add-on to Kuberne... stable/weave-scope 1.1.12 1.12.0 DEPRECATED - A Helm chart for the Weave Scope c... [root@master-node helm]# #查看 chrt 信息 # helm show chart stable/mysql [root@master-node helm]# helm show chart stable/mysql apiVersion: v1 appVersion: 5.7.30 deprecated: true description: DEPRECATED - Fast, reliable, scalable, and easy to use open-source relational database system. home: https://www.mysql.com/ icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png keywords: - mysql - database - sql name: mysql sources: - https://github.com/kubernetes/charts - https://github.com/docker-library/mysql version: 1.6.9 #安装包 # helm install ui stable/weave-scope #查看发布状态 # helm list [root@master-node helm]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2022-02-21 17:18:53.936160584 +0800 CST deployed weave-scope-1.1.12 1.12.0 #helm status 安装之后名称 [root@master-node helm]# helm status ui NAME: ui LAST DEPLOYED: Mon Feb 21 17:18:53 2022 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: You should now be able to access the Scope frontend in your web browser, by using kubectl port-forward: kubectl -n default port-forward $(kubectl -n default get endpoints \ ui-weave-scope -o jsonpath='{.subsets[0].addresses[0].targetRef.name}') 8080:4040 then browsing to http://localhost:8080/. For more details on using Weave Scope, see the Weave Scope documentation: https://www.weave.works/docs/scope/latest/introducing/
#删除应用
[root@master-node home]# helm delete ui
release "ui" uninstalled
#kubernetes修改service的yaml文件 #查看所有的服务 kubectl get service 或 kubectl get svc #修改服务文件: kubectl edit svc [service名字]
三、构建一个helm Chart
#构建一个 Helm Chart [root@master-node helm]# helm create mychart Creating mychart #查看目录结构 [root@master-node helm]# tree mychart/ mychart/ ├── charts # 依赖信息 ├── Chart.yaml # chart版本和配置信息 ├── templates # K8s 资源模板信息, 结合 values.yaml 可生成K8s对象的manifest文件 │ ├── deployment.yaml │ ├── _helpers.tpl # 下划线开头,作为子模板,可被其他模板文件引用,Helm不会交给K8s处理 │ ├── ingress.yaml │ ├── NOTES.txt # helm 提示信息 │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml └── values.yaml # chart默认配置 Chart.yaml:用于描述这个 Chart 的基本信息,包括名字、描述信息以及版本等。 values.yaml :用于存储 templates 目录中模板文件中用到变量的值。 Templates: 目录里面存放所有 yaml 模板文件。 charts:目录里存放这个 chart 依赖的所有子 chart。 NOTES.txt :用于介绍 Chart 帮助信息, helm install 部署后展示给用户。例如:如何使用这个 Chart、列出缺省的设置等。 _helpers.tpl:放置模板助手的地方,可以在整个 chart 中重复使用 #创建deploymentyaml文件 # --dry-run参数 :试着运行,不真正运行。 [root@master-node templates]# kubectl create deployment web1 --image=nginx --dry-run -o yaml > deployment.yml W0222 10:55:52.334693 20222 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client. [root@master-node templates]# ll total 4 -rw-r--r-- 1 root root 380 Feb 22 10:55 deployment.yml #对外发布,暴露nginx端口号,这里没加--dry-run参数,所以输出yaml文件的时候也同时执行了, [root@master-node templates]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web1 -o yaml > service.yaml [root@master-node templates]# ll total 8 -rw-r--r-- 1 root root 380 Feb 22 10:55 deployment.yml -rw-r--r-- 1 root root 1059 Feb 22 11:04 service.yaml [root@master-node templates]# #创建 Chart 后,接下来就是将其部署: #helm install web mychart/ [root@master-node helm]# helm install web mychart/ NAME: web LAST DEPLOYED: Tue Feb 22 11:08:59 2022 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None #应用升级 helm upgrade chart名称
四、实现yaml高效复用
vi values.yaml # Default values for mychart. # This is a YAML-formatted file. # Declare variables to be passed into your templates. replicas: 1 image: nginx tag: 1.16 label: nginx port: 80 image: repository: nginx pullPolicy: IfNotPresent imagePullSecrets: [] nameOverride: "" fullnameOverride: "" serviceAccount: # Specifies whether a service account should be created create: true # The name of the service account to use. # If not set and create is true, a name is generated using the fullname template name: podSecurityContext: {} # fsGroup: 2000 securityContext: {} # capabilities: # drop: # - ALL # readOnlyRootFilesystem: true # runAsNonRoot: true # runAsUser: 1000
[root@master-node mychart]# vi templates/deployment.yml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web1 name: {{ .Release.Name }}-configmap spec: replicas: 1 selector: matchLabels: app: {{.values.label}} strategy: {} template: metadata: creationTimestamp: null labels: app: {{.values.label}} spec: containers: - image: {{.values.image}} name: nginx resources: {} status: {}
[root@master-node mychart]# vi templates/service.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: "2022-02-22T03:04:45Z" labels: app: {{ .Release.Name }}-svc managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:labels: .: {} f:app: {} f:spec: f:externalTrafficPolicy: {} f:ports: .: {} k:{"port":80,"protocol":"TCP"}: .: {} f:port: {} f:protocol: {} f:targetPort: {} f:selector: .: {} f:app: {} f:sessionAffinity: {} f:type: {} manager: kubectl-expose operation: Update time: "2022-02-22T03:04:45Z" name: web1 namespace: default resourceVersion: "7352599" selfLink: /api/v1/namespaces/default/services/web1 uid: 81ebe4da-8e6e-4679-af48-0f4bf95cb6ae spec: clusterIP: 10.96.6.182 externalTrafficPolicy: Cluster ports: - nodePort: 31259 port: {{ .values.port }} protocol: TCP targetPort: 80 selector: app: {{ .values.label }} sessionAffinity: None type: NodePort status: loadBalancer: {}