Kubernetes(K8S) helm chart

简介: Kubernetes(K8S) helm chart

感觉和放到一个 yaml 文件中,用 ---- 分隔,操作繁琐程度上,没有太大区别

创建自定义 Chart

# 创建自定义的 chart 名为 mychart
[root@k8smaster ~]# helm create mychart
Creating mychart
[root@k8smaster ~]# cd mychart/
[root@k8smaster mychart]# ls
charts  
Chart.yaml  # 当前chart 属性配置信息
templates   # 编写 yaml 文件放到这个目录,是 yaml 的集合
values.yaml # yaml文件可以使用的全局变量
[root@k8smaster mychart]#

创建 yaml 文件

在 templates 文件夹中创建两个 yaml 文件

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
  • service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  ports:   
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30390  # 可以指定暴露出去的端口,防止重启 Pod 后发生变化 
  selector:
    app: web1
  type: NodePort
status:
  loadBalancer: {}
# 切换到 templates 目录下
[root@k8smaster mychart]# cd templates/
# 创建一个web1应用--内容见上文
[root@k8smaster templates]# vi deployment.yaml
# 暴露端口--内容见上文
[root@k8smaster templates]# vi service.yaml
[root@k8smaster templates]#

安装

[root@k8smaster ~]# helm install web1 mychart/
NAME: web1
LAST DEPLOYED: Mon Nov 28 14:26:34 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=web1" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80
[root@k8smaster ~]# helm list
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
ui      default         1               2022-11-28 13:15:05.404335352 +0800 CST deployed        weave-scope-1.1.12      1.12.0     
web1    default         1               2022-11-28 14:26:34.854631893 +0800 CST deployed        mychart-0.1.0           1.16.0     
[root@k8smaster ~]# kubectl get pod,svc
NAME                                                READY   STATUS    RESTARTS   AGE
pod/nginx-f89759699-652r4                           1/1     Running   0          5d
pod/nginx-nfs-788564fbc8-g58xd                      1/1     Running   0          5d
pod/weave-scope-agent-ui-7qqd4                      1/1     Running   0          73m
pod/weave-scope-agent-ui-knqwk                      1/1     Running   0          73m
pod/weave-scope-agent-ui-r74cm                      1/1     Running   0          73m
pod/weave-scope-agent-ui-xnrht                      1/1     Running   0          73m
pod/weave-scope-cluster-agent-ui-7498b8d4f4-pmkzh   1/1     Running   0          73m
pod/weave-scope-frontend-ui-649c7dcd5d-jfg72        1/1     Running   0          73m
pod/web1-7f87dfbd56-w7p4d                           1/1     Running   0          91s
NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/javademo1        NodePort    10.106.43.46     <none>        8111:31452/TCP   40d
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          45d
service/nginx            NodePort    10.103.87.81     <none>        80:30339/TCP     45d
service/nginx-nfs        NodePort    10.99.84.9       <none>        80:30205/TCP     19d
service/ui-weave-scope   NodePort    10.101.4.212     <none>        80:30690/TCP     73m
service/web1             NodePort    10.100.238.141   <none>        80:30390/TCP     91s
[root@k8smaster ~]#

升级

[root@k8smaster ~]# helm upgrade web1 mychart/
Release "web1" has been upgraded. Happy Helming!
NAME: web1
LAST DEPLOYED: Mon Nov 28 14:29:39 2022
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=web1" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80
[root@k8smaster ~]#

yaml 高效复用

通过传递参数,动态渲染模板 yaml 内容,动态传入参数生成

在 chart values.yaml 文件,定义 yaml 文件全局变量

在 values.yaml 定义变量和值

在具体 yaml文件中,获取定义变量的值

  • image、tag、label、port、replicas
定义变量
[root@k8smaster ~]# cd mychart/
[root@k8smaster mychart]# vi values.yaml
image: nginx
replicas: 1
tag: 1.16
label: nginx
port: 80

使用变量

方便统一定义

{{ .Values.变量名称}}

{{ .Release.Name}} -> 获取版本名称

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name}}-deploy
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: {}
  • service.yaml
apiVersion: v1
kind: Service
metadata: 
  name: {{ .Release.Name}}-deploy
spec:
  ports:
  - port: {{ .Values.port}}
    protocol: TCP
    targetPort: 80    
  selector:
    app: {{ .Values.label}}
  type: NodePort
status:
  loadBalancer: {}
[root@k8smaster mychart]# vi templates/deployment.yaml 
[root@k8smaster mychart]# vi templates/deployment.yaml 
[root@k8smaster mychart]# cd
[root@k8smaster ~]# helm install --dry-run web2 mychart/
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata: 
  name: web2-deploy
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
status:
  loadBalancer: {}
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web2-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@k8smaster ~]# helm install web2 mychart/
NAME: web2
LAST DEPLOYED: Mon Nov 28 15:13:48 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
[root@k8smaster ~]# kubectl get pod,svc 
NAME                                                READY   STATUS    RESTARTS   AGE
pod/nginx-f89759699-652r4                           1/1     Running   0          5d1h
pod/nginx-nfs-788564fbc8-g58xd                      1/1     Running   0          5d1h
pod/weave-scope-agent-ui-7qqd4                      1/1     Running   0          119m
pod/weave-scope-agent-ui-knqwk                      1/1     Running   0          119m
pod/weave-scope-agent-ui-r74cm                      1/1     Running   0          119m
pod/weave-scope-agent-ui-xnrht                      1/1     Running   0          119m
pod/weave-scope-cluster-agent-ui-7498b8d4f4-pmkzh   1/1     Running   0          119m
pod/weave-scope-frontend-ui-649c7dcd5d-jfg72        1/1     Running   0          119m
pod/web1-7f87dfbd56-w7p4d                           1/1     Running   0          47m
pod/web2-deploy-f89759699-9z5n4                     1/1     Running   0          22s
NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/javademo1        NodePort    10.106.43.46     <none>        8111:31452/TCP   40d
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          45d
service/nginx            NodePort    10.103.87.81     <none>        80:30339/TCP     45d
service/nginx-nfs        NodePort    10.99.84.9       <none>        80:30205/TCP     19d
service/ui-weave-scope   NodePort    10.101.4.212     <none>        80:30690/TCP     119m
service/web1             NodePort    10.100.238.141   <none>        80:30390/TCP     47m
service/web2-deploy      NodePort    10.111.219.124   <none>        80:30277/TCP     22s
[root@k8smaster ~]# helm uninstall web2
release "web2" uninstalled
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
打赏
0
1
1
0
54
分享
相关文章
容器数据保护:基于容器服务 Kubernetes 版(ACK)备份中心实现K8s存储卷一键备份与恢复
阿里云ACK备份中心提供一站式容器化业务灾备及迁移方案,减少数据丢失风险,确保业务稳定运行。
K8s集群实战:使用kubeadm和kuboard部署Kubernetes集群
总之,使用kubeadm和kuboard部署K8s集群就像回归童年一样,简单又有趣。不要忘记,技术是为人服务的,用K8s集群操控云端资源,我们不过是想在复杂的世界找寻简单。尽管部署过程可能遇到困难,但朝着简化复杂的目标,我们就能找到意义和乐趣。希望你也能利用这些工具,找到你的乐趣,满足你的需求。
410 33
ACK Gateway with AI Extension:面向Kubernetes大模型推理的智能路由实践
本文介绍了如何利用阿里云容器服务ACK推出的ACK Gateway with AI Extension组件,在Kubernetes环境中为大语言模型(LLM)推理服务提供智能路由和负载均衡能力。文章以部署和优化QwQ-32B模型为例,详细展示了从环境准备到性能测试的完整实践过程。
基于阿里云容器服务Kubernetes版(ACK)的微服务架构设计与实践
本文介绍了如何基于阿里云容器服务Kubernetes版(ACK)设计和实现微服务架构。首先概述了微服务架构的优势与挑战,如模块化、可扩展性及技术多样性。接着详细描述了ACK的核心功能,包括集群管理、应用管理、网络与安全、监控与日志等。在设计基于ACK的微服务架构时,需考虑服务拆分、通信、发现与负载均衡、配置管理、监控与日志以及CI/CD等方面。通过一个电商应用案例,展示了用户服务、商品服务、订单服务和支付服务的具体部署步骤。最后总结了ACK为微服务架构提供的强大支持,帮助应对各种挑战,构建高效可靠的云原生应用。
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
飞轮科技推出了 Doris 的 Kubernetes Operator 开源项目(简称:Doris Operator),并捐赠给 Apache 基金会。该工具集成了原生 Kubernetes 资源的复杂管理能力,并融合了 Doris 组件间的分布式协同、用户集群形态的按需定制等经验,为用户提供了一个更简洁、高效、易用的容器化部署方案。
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
容器服务Kubernetes场景下可观测体系生产级最佳实践
阿里云容器服务团队在2024年继续蝉联Gartner亚洲唯一全球领导者象限,其可观测体系是运维的核心能力之一。该体系涵盖重保运维、大规模集群稳定性、业务异常诊断等场景,特别是在AI和GPU场景下提供了全面的观测解决方案。通过Tracing、Metric和Log等技术,阿里云增强了对容器网络、存储及多集群架构的监控能力,帮助客户实现高效运维和成本优化。未来,结合AI助手,将进一步提升问题定位和解决效率,缩短MTTR,助力构建智能运维体系。
Kubernetes(k8s)和Docker Compose本质区别
理解它们的区别和各自的优势,有助于选择合适的工具来满足特定的项目需求。
706 19
二进制安装Kubernetes(k8s)v1.32.0
本指南提供了一个详细的步骤,用于在Linux系统上通过二进制文件安装Kubernetes(k8s)v1.32.0,支持IPv4+IPv6双栈。具体步骤包括环境准备、系统配置、组件安装和配置等。
2396 11
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
本文源自2024云栖大会苏雅诗的演讲,探讨了K8s集群业务为何需要灾备及其重要性。文中强调了集群与业务高可用配置对稳定性的重要性,并指出人为误操作等风险,建议实施周期性和特定情况下的灾备措施。针对容器化业务,提出了灾备的新特性与需求,包括工作负载为核心、云资源信息的备份,以及有状态应用的数据保护。介绍了ACK推出的备份中心解决方案,支持命名空间、标签、资源类型等维度的备份,并具备存储卷数据保护功能,能够满足GitOps流程企业的特定需求。此外,还详细描述了备份中心的使用流程、控制台展示、灾备难点及解决方案等内容,展示了备份中心如何有效应对K8s集群资源和存储卷数据的灾备挑战。
深入理解Kubernetes——容器编排的王者之道
深入理解Kubernetes——容器编排的王者之道
125 1

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问