Helm-从0手动创建charts

Helm-从0手动创建charts

创建 chart 目录结构:

mkdir my-nginx
cd my-nginx

创建 Chart.yaml :

cat > Chart.yaml << EOF
apiVersion: v2
appVersion: v1.0
description: A Helm chart for Kubernetes
name: nginx-app
version: 0.1.0
EOF

创建 templates 目录:

[root@master my-nginx]# mkdir templates

创建 deployment.yaml:

[root@master my-nginx]# kubectl create deploy my-nginx --image nginx --dry-run=client -o yaml > templates/deployment.yaml
[root@master my-nginx]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: my-nginx
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@master my-nginx]# tree ~/my-nginx/
/root/my-nginx/
├── Chart.yaml
└── templates
    └── deployment.yaml

以上完成后就可以使用 helm 部署,部署名为 my-nginx 的应用:

[root@master my-nginx]# helm install my-nginx .
NAME: my-nginx
LAST DEPLOYED: Tue Dec  6 17:03:59 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

查看创建的应用:

[root@master my-nginx]# helm ls
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART               APP VERSION
my-nginx        default         1               2022-12-06 17:03:59.142846484 +0800 CST deployed        nginx-app-0.1.0     v1.0

查看创建的实际资源:

[root@master my-nginx]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-6b74b79f57-lccqb   1/1     Running   0          78s

继续创建 service.yaml:

[root@master01 my-nginx]# kubectl expose deploy my-nginx --port 80 --dry-run=client -o yaml > templates/service.yaml
[root@master my-nginx]# cat templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: my-nginx
    app.kubernetes.io/managed-by: Helm
  name: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: my-nginx
status:
  loadBalancer: {}

创建 values.yaml:

cat > values.yaml << EOF
replicaCount: 2
service:
  type: NodePort
  nodePort: 30080
EOF

修改 deployment.yaml,注入变量,改为模板形式:

[root@master my-nginx]# cat templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: my-nginx
  name: my-nginx
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: my-nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

修改 service.yaml,注入变量,改为模板形式:

[root@master my-nginx]# cat templates/service.yaml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: my-nginx
  name: my-nginx
spec:
  type: {{ .Values.service.type }}
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: {{ .Values.service.nodePort }}
  selector:
    app: my-nginx
status:
  loadBalancer: {}

此时的目录结构:

[root@master my-nginx]# tree
.
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   └── service.yaml
└── values.yaml

修改后可升级 helm 版本,也可以删除原 helm 重新部署:

[root@master my-nginx]# helm upgrade my-nginx .
Release "my-nginx" has been upgraded. Happy Helming!
NAME: my-nginx
LAST DEPLOYED: Tue Dec  6 17:12:34 2022
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

查看升级后的信息,REVISION 版本变为 2。

[root@master my-nginx]# helm ls
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART               APP VERSION
my-nginx        default         2               2022-12-06 17:12:34.223199278 +0800 CST deployed        nginx-app-0.1.0     v1.0

查看创建的资源,副本扩容为 2,新创建了 NodePort 类型的 service:

[root@master my-nginx]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-6b74b79f57-h7gtb   1/1     Running   0          18s
my-nginx-6b74b79f57-lccqb   1/1     Running   0          26m

[root@master my-nginx]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        134m
my-nginx     NodePort    10.102.132.43   <none>        80:30080/TCP   17m

访问应用:

# curl http://ip:nodeport
[root@master my-nginx]# curl http://192.168.226.201:30080
[root@master my-nginx]# curl http://192.168.226.202:30080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 卸载
[root@master ~]# helm uninstall my-nginx
release "my-nginx" uninstalled
# 当您编辑chart时,可以通过运行helm lint来验证它的格式是否正确
[root@master my-nginx]# helm lint
==> Linting .
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed
# 当需要打包chart以进行分发时,可以运行helm package命令
[root@master ~]# helm package my-nginx
Successfully packaged chart and saved it to: /root/nginx-app-0.1.0.tgz
# 现在可以通过helm install命令轻松安装该chart
[root@master ~]# helm install my-nginx ./nginx-app-0.1.0.tgz
NAME: my-nginx
LAST DEPLOYED: Wed Dec  7 13:29:05 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

你可能感兴趣的:(kubernetes,kubernetes)