helm自定义模板
首先我们创建一个文件夹helm-test:
mkdir ./helm-test
cd ./helm-test
然后在该文件夹下创建一个自描述文件Chart.yaml,这个文件必须有name和version定义:
cat << ‘EOF’ > ./Chart.yaml
name: hello-world
version: 1.0.0
EOF
创建模板文件,用于生成kubernetes资源清单(manifests)–该模板文件名必须为templates,且该目录下的所有yaml文件都会被执行:
创建deployment.yaml文件,vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: hub.mwqtest.com/library/mynginx:latest
ports:
- containerPort: 80
protocol: TCP
创建service.yaml文件,vi service.yaml
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: hello-world
执行部署安装只需要执行helm install .即可(在helm-test目录下执行该命令)
**注意:**如果此时执行如下错误:
Error: no available release name found
应该是tiller没有正确的角色权限:可执行以下命令解决该问题:
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
创建成功会输出创建过程:
[root@k8s-master01 test]# helm install .
NAME: pilfering-toad
LAST DEPLOYED: Fri Nov 27 06:03:31 2020
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 0/1 1 0 6m59s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
hello-world-7c849d5bf6-h885n 0/1 ContainerCreating 0 6m59s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world NodePort 10.107.173.157 <none> 80:30235/TCP 6m59s
可以在此基础上联系helm的命令,执行helm help可查看相关命令。
相关配置放在配置文件中
对于镜像等后期需要修改的内容,可以单独建立一个value.yaml文件,然后在deployment.yaml文件中采用某种形式引用它,以便于后期修改。