Helm学习之3values变量使用

values.yaml使用

资源文件不一样的地方都可以使用变量传入,如name labels image prot targetport等等,这些值都可以定义成变量,写入values.yaml。如下

[root@master mychart]# cat values.yaml
replicas: 1
image: nginx
tag: 1.15
serviceport: 80
targetport: 80
label: nginx

再templates的yaml文件引用values.yaml定义的变量

引用表达式: ** {{.Values.变量名}}

取当前版本的名称,动态的 {{.Release.Name}}

内置对象

Release.Name          			release 名字 
Release.Namespace 				release 命名空间 
Release.Service 				release 服务的名称 
Release.Revision 			 	release 修订版本号,从 1 开始累加
书写刚才的资源文件,引入变量
[root@master ~]# cat mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{.Values.label}}
  name: {{.Release.Name}}
spec:
  replicas: {{.Values.replicas}}
  selector:
    matchLabels:
      app: {{.Values.label}}
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: {{.Values.label}}
    spec:
      containers:
      - image: {{.Values.image}}:{{.Values.tag}}
        name: nginx
        resources: {}
status: {}
[root@master ~]# cat mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: {{.Values.label}}
  name: {{.Release.Name}}-svc
spec:
  ports:
  - port: {{.Values.serviceport}}
    protocol: TCP
    targetPort: 80
  selector:
    app: {{.Values.label}}
  type: NodePort
status:
  loadBalancer: {}
调试变量的引用,及安装
[root@master ~]# helm install --dry-run test2 mychart     //一定要测试变量是否传递
NAME: test2
LAST DEPLOYED: Mon May  2 04:17:33 2022
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: test2-svc
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:
  creationTimestamp: null
  labels:
    app: nginx
  name: test2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15
        name: nginx
        resources: {}
status: {}

[root@master ~]# helm install  test2 mychart
NAME: test2
LAST DEPLOYED: Mon May  2 04:18:55 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
验证安装结果
[root@master ~]# kubectl get svc
NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             
test2-svc                 NodePort    10.100.37.70     <none>        80:32618/TCP  
[root@master ~]# kubectl get deployments.apps
NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
test2                                1/1     1            1           60s
[root@master ~]# kubectl get pod
NAME                                                  READY   STATUS    RESTARTS   AGE
test2-7d79cb6c68-cdp8g                                1/1     Running   0          88s

安装前自定义 chart 配置选项

自定义 chart 配置选项,安装过程中有两种方法可以传递配置数据:

--values(或-f):指定带有覆盖的 YAML 文件。这可以多次指定,最右边的文件优先
--set:在命令行上指定替代。如果两者都用,--set优先级高
helm upgrade test2 --set replicas=5  mychart
name:value        --set  name=value
a:b
c:d               --set a=b,c=d
outer:
  inner:value     --set outer.inner=value
name:
  - a
  - b
  - c             --set name={a,b,c}   
servers:
  - prot: 80      --set  servers[0].port=80
servers:
  - prot: 80 
    host: example --set  servers[0].port=80,servers[0].host=example
name: "value1,value2"  --set   name=value1\,value2
nodeSelector:
  kubernetes.io/role: master   --set noeSelector."kubernetes\.io/role"=master

升级、回滚和删除

helm upgrade --set imageTag=1.17 web nginx 		#更改发布的配置
helm upgrade -f values.yaml web nginx 		#更改发布的配置
helm rollback test 1 		#将应用回滚到第一个版本
helm uninstall test  #卸载发行版
helm get all --revision 1 test  #查看历史版本配置信息 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值