Kubernetes(K8S) helm chart

[复制链接]
发表于 2022-12-6 07:12:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
感觉和放到一个 yaml 文件中,用 ---- 分隔,操作繁琐程度上,没有太大区别
创建自定义 Chart
  1. # 创建自定义的 chart 名为 mychart
  2. [root@k8smaster ~]# helm create mychart
  3. Creating mychart
  4. [root@k8smaster ~]# cd mychart/
  5. [root@k8smaster mychart]# ls
  6. charts  
  7. Chart.yaml  # 当前chart 属性配置信息
  8. templates   # 编写 yaml 文件放到这个目录,是 yaml 的集合
  9. values.yaml # yaml文件可以使用的全局变量
  10. [root@k8smaster mychart]#
复制代码
创建 yaml 文件

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

  • deployment.yaml
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     app: web1
  7.   name: web1
  8. spec:
  9.   replicas: 1
  10.   selector:
  11.     matchLabels:
  12.       app: web1
  13.   strategy: {}
  14.   template:
  15.     metadata:
  16.       creationTimestamp: null
  17.       labels:
  18.         app: web1
  19.     spec:
  20.       containers:
  21.       - image: nginx
  22.         name: nginx
  23.         resources: {}
  24. status: {}
复制代码

  • service.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   creationTimestamp: null
  5.   labels:
  6.     app: web1
  7.   name: web1
  8. spec:
  9.   ports:   
  10.   - port: 80
  11.     protocol: TCP
  12.     targetPort: 80
  13.     nodePort: 30390  # 可以指定暴露出去的端口,防止重启 Pod 后发生变化
  14.   selector:
  15.     app: web1
  16.   type: NodePort
  17. status:
  18.   loadBalancer: {}
复制代码
  1. # 切换到 templates 目录下
  2. [root@k8smaster mychart]# cd templates/
  3. # 创建一个web1应用--内容见上文
  4. [root@k8smaster templates]# vi deployment.yaml
  5. # 暴露端口--内容见上文
  6. [root@k8smaster templates]# vi service.yaml
  7. [root@k8smaster templates]#
复制代码
安装
  1. [root@k8smaster ~]# helm install web1 mychart/
  2. NAME: web1
  3. LAST DEPLOYED: Mon Nov 28 14:26:34 2022
  4. NAMESPACE: default
  5. STATUS: deployed
  6. REVISION: 1
  7. NOTES:
  8. 1. Get the application URL by running these commands:
  9.   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}")
  10.   echo "Visit http://127.0.0.1:8080 to use your application"
  11.   kubectl --namespace default port-forward $POD_NAME 8080:80
  12. [root@k8smaster ~]# helm list
  13. NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
  14. ui      default         1               2022-11-28 13:15:05.404335352 +0800 CST deployed        weave-scope-1.1.12      1.12.0     
  15. web1    default         1               2022-11-28 14:26:34.854631893 +0800 CST deployed        mychart-0.1.0           1.16.0     
  16. [root@k8smaster ~]# kubectl get pod,svc
  17. NAME                                                READY   STATUS    RESTARTS   AGE
  18. pod/nginx-f89759699-652r4                           1/1     Running   0          5d
  19. pod/nginx-nfs-788564fbc8-g58xd                      1/1     Running   0          5d
  20. pod/weave-scope-agent-ui-7qqd4                      1/1     Running   0          73m
  21. pod/weave-scope-agent-ui-knqwk                      1/1     Running   0          73m
  22. pod/weave-scope-agent-ui-r74cm                      1/1     Running   0          73m
  23. pod/weave-scope-agent-ui-xnrht                      1/1     Running   0          73m
  24. pod/weave-scope-cluster-agent-ui-7498b8d4f4-pmkzh   1/1     Running   0          73m
  25. pod/weave-scope-frontend-ui-649c7dcd5d-jfg72        1/1     Running   0          73m
  26. pod/web1-7f87dfbd56-w7p4d                           1/1     Running   0          91s
  27. NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
  28. service/javademo1        NodePort    10.106.43.46     <none>        8111:31452/TCP   40d
  29. service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          45d
  30. service/nginx            NodePort    10.103.87.81     <none>        80:30339/TCP     45d
  31. service/nginx-nfs        NodePort    10.99.84.9       <none>        80:30205/TCP     19d
  32. service/ui-weave-scope   NodePort    10.101.4.212     <none>        80:30690/TCP     73m
  33. service/web1             NodePort    10.100.238.141   <none>        80:30390/TCP     91s
  34. [root@k8smaster ~]#
复制代码

升级
  1. [root@k8smaster ~]# helm upgrade web1 mychart/
  2. Release "web1" has been upgraded. Happy Helming!
  3. NAME: web1
  4. LAST DEPLOYED: Mon Nov 28 14:29:39 2022
  5. NAMESPACE: default
  6. STATUS: deployed
  7. REVISION: 2
  8. NOTES:
  9. 1. Get the application URL by running these commands:
  10.   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}")
  11.   echo "Visit http://127.0.0.1:8080 to use your application"
  12.   kubectl --namespace default port-forward $POD_NAME 8080:80
  13. [root@k8smaster ~]#
复制代码
yaml 高效复用

通过传递参数,动态渲染模板 yaml 内容,动态传入参数生成
在 chart values.yaml 文件,定义 yaml 文件全局变量
在 values.yaml 定义变量和值
在具体 yaml文件中,获取定义变量的值

  • image、tag、label、port、replicas
定义变量
  1. [root@k8smaster ~]# cd mychart/
  2. [root@k8smaster mychart]# vi values.yaml
  3. image: nginx
  4. replicas: 1
  5. tag: 1.16
  6. label: nginx
  7. port: 80
复制代码

使用变量

方便统一定义
{{ .Values.变量名称}}
{{ .Release.Name}} -> 获取版本名称

  • deployment.yaml
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: {{ .Release.Name}}-deploy
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: {{ .Values.label}}
  10.   strategy: {}
  11.   template:
  12.     metadata:
  13.       creationTimestamp: null
  14.       labels:
  15.         app: {{ .Values.label}}
  16.     spec:
  17.       containers:
  18.       - image: {{ .Values.image}}
  19.         name: nginx
  20.         resources: {}
  21. status: {}
复制代码

  • service.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4.   name: {{ .Release.Name}}-deploy
  5. spec:
  6.   ports:
  7.   - port: {{ .Values.port}}
  8.     protocol: TCP
  9.     targetPort: 80   
  10.   selector:
  11.     app: {{ .Values.label}}
  12.   type: NodePort
  13. status:
  14.   loadBalancer: {}
复制代码
  1. [root@k8smaster mychart]# vi templates/deployment.yaml
  2. [root@k8smaster mychart]# vi templates/deployment.yaml
  3. [root@k8smaster mychart]# cd
  4. [root@k8smaster ~]# helm install --dry-run web2 mychart/
  5. # Source: mychart/templates/service.yaml
  6. apiVersion: v1
  7. kind: Service
  8. metadata:
  9.   name: web2-deploy
  10. spec:
  11.   ports:
  12.   - port: 80
  13.     protocol: TCP
  14.     targetPort: 80
  15.   selector:
  16.     app: nginx
  17.   type: NodePort
  18. status:
  19.   loadBalancer: {}
  20. ---
  21. # Source: mychart/templates/deployment.yaml
  22. apiVersion: apps/v1
  23. kind: Deployment
  24. metadata:
  25.   name: web2-deploy
  26. spec:
  27.   replicas: 1
  28.   selector:
  29.     matchLabels:
  30.       app: nginx
  31.   strategy: {}
  32.   template:
  33.     metadata:
  34.       creationTimestamp: null
  35.       labels:
  36.         app: nginx
  37.     spec:
  38.       containers:
  39.       - image: nginx
  40.         name: nginx
  41.         resources: {}
  42. status: {}
  43. [root@k8smaster ~]# helm install web2 mychart/
  44. NAME: web2
  45. LAST DEPLOYED: Mon Nov 28 15:13:48 2022
  46. NAMESPACE: default
  47. STATUS: deployed
  48. REVISION: 1
  49. [root@k8smaster ~]# kubectl get pod,svc
  50. NAME                                                READY   STATUS    RESTARTS   AGE
  51. pod/nginx-f89759699-652r4                           1/1     Running   0          5d1h
  52. pod/nginx-nfs-788564fbc8-g58xd                      1/1     Running   0          5d1h
  53. pod/weave-scope-agent-ui-7qqd4                      1/1     Running   0          119m
  54. pod/weave-scope-agent-ui-knqwk                      1/1     Running   0          119m
  55. pod/weave-scope-agent-ui-r74cm                      1/1     Running   0          119m
  56. pod/weave-scope-agent-ui-xnrht                      1/1     Running   0          119m
  57. pod/weave-scope-cluster-agent-ui-7498b8d4f4-pmkzh   1/1     Running   0          119m
  58. pod/weave-scope-frontend-ui-649c7dcd5d-jfg72        1/1     Running   0          119m
  59. pod/web1-7f87dfbd56-w7p4d                           1/1     Running   0          47m
  60. pod/web2-deploy-f89759699-9z5n4                     1/1     Running   0          22s
  61. NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
  62. service/javademo1        NodePort    10.106.43.46     <none>        8111:31452/TCP   40d
  63. service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          45d
  64. service/nginx            NodePort    10.103.87.81     <none>        80:30339/TCP     45d
  65. service/nginx-nfs        NodePort    10.99.84.9       <none>        80:30205/TCP     19d
  66. service/ui-weave-scope   NodePort    10.101.4.212     <none>        80:30690/TCP     119m
  67. service/web1             NodePort    10.100.238.141   <none>        80:30390/TCP     47m
  68. service/web2-deploy      NodePort    10.111.219.124   <none>        80:30277/TCP     22s
  69. [root@k8smaster ~]# helm uninstall web2
  70. release "web2" uninstalled
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表