(1)创建完一个chart:mychart

[root@k8s-master helm]# helm create mychart
Creating mychart

[root@k8s-master mychart]# ls
charts  Chart.yaml  templates  values.yaml

mychart/     #- chart 包目录名
├── charts  #依赖其他包的charts文件
├── Chart.yaml # 该chart的描述文件,包括ico地址,版本信息等
├── templates  # #存放k8s模板文件目录
│   ├── deployment.yaml # 创建k8s资源的yaml 模板
│   ├── _helpers.tpl # 下划线开头的文件,可以被其他模板引用
│   ├── hpa.yaml # 弹性扩缩容,配置服务资源CPU 内存
│   ├── ingress.yaml # ingress 配合service域名访问的配置
│   ├── NOTES.txt # 说明文件,helm install之后展示给用户看的内容
│   ├── serviceaccount.yaml # 服务账号配置
│   ├── service.yaml # kubernetes Serivce yaml 模板
│   └── tests # 测试模块
│       └── test-connection.yaml
└── values.yaml # 给模板文件使用的变量
 

(2)新增变量,修改变量文件values.yaml

[root@k8s-master mychart]# vim values.yaml 

在对底部添加
#自定义变量
replicas: 2
image: nginx
tag: 1.16
label: nginx
port: 80

(3)修改pod的yaml文件,在template目录下

指定参数格式: {{  }}

{{ .Release.Name }}  pod应用名
{{ .Values.变量名称 }}   变量名称是根据mychart/values.yaml文件新增的参数
[root@k8s-master templates]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: {{ .Release.Name }}-nginx       #pod的名字
spec:
  replicas: {{ .Values.replicas }}      #pod的副本数
  selector:
    matchLabels:
      app: {{ .Values.label }}          #pod标签选择器
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: {{ .Values.label }}       #设置此模板的标签
    spec:
      containers:
      - image: {{ .Values.image }}
        name: nginx
        resources: {}
status: {}
[root@k8s-master templates]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Release.Name }}-svc
  name: {{ .Release.Name }}-svc
spec:
  ports:
  - port: {{ .Values.port }}
    protocol: TCP
    targetPort: 80
  selector:
    app: {{ .Values.label }}
  type: NodePort
status:
  loadBalancer: {}

(4)创建自定义参数pod

[root@k8s-master helm]# helm install web7 mychart
NAME: web7
LAST DEPLOYED: Fri Nov 13 20:09:20 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

(5)验证

[root@k8s-master helm]# kubectl get svc
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        51d
ui-weave-scope   NodePort    10.102.105.28   <none>        80:32448/TCP   16d
web              NodePort    10.98.105.180   <none>        80:31375/TCP   25h
web7-svc         NodePort    10.111.115.9    <none>        80:31687/TCP   103m               与预期一致,端口号80
[root@k8s-master helm]# kubectl get pod
NAME                                            READY   STATUS        RESTARTS   AGE
weave-scope-agent-ui-655pp                      1/1     Running       3          16d
weave-scope-agent-ui-bzttt                      1/1     Running       2          16d
weave-scope-agent-ui-hkxfz                      1/1     Running       1          16d
weave-scope-cluster-agent-ui-5d4d6c97bf-5xpqf   1/1     Terminating   0          9d
weave-scope-cluster-agent-ui-5d4d6c97bf-f528k   1/1     Running       1          8d
weave-scope-frontend-ui-757c6668c5-vqpdj        1/1     Running       1          8d
weave-scope-frontend-ui-757c6668c5-zhlzp        1/1     Terminating   0          9d
web-d86c95cc9-vzmnd                             1/1     Running       0          25h
web7-nginx-86c57db685-f86b8                     1/1     Running       0          105m
web7-nginx-86c57db685-hd2b7                     1/1     Running       0          105m          #与预期一致,副本数2

浏览器访问http://ip:31687

完成

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐