Helm là công cụ cho phép triển khai, quản lý các ứng dụng trong Kubernetes dễ dàng hơn. Helm sử dụng các chart (là tập hợp các file yaml (manifest)) để triển khai ứng dụng. Helm cung cấp các tính năng:
- Tìm kiếm và sử dụng các ứng dụng được cấu hình từ trước trên helm (helm-chart), ưu điểm là có thể sử dụng luôn mà chỉ cần thay đổi rất ít thông số, nhưng nhược điểm là kiến trúc được định nghĩa từ trước, thay đổi cần thêm thời gian tìm hiểu
- Tự mình cũng có thể định nghĩa một chart cho ứng dụng muốn, giúp cho các lần cài đặt sau đơn giản hơn nhiều.
- Sửa đổi các file manifest đơn giản bởi Helm hỗ trợ file cấu hình tập trung (values.yaml) và cho phép người dùng định nghĩa thêm (predefined-values và Partials)
Key definitions:
- Helm is a chart manager.
- Charts are packages of pre-configured Kubernetes resources.
- Release is a collection of Kubernetes resources deployed to the cluster using Helm.
Helm is used to:
- Make configurable releases
- Upgrade, delete, inspect releases made using Helm
Helm is made of two components:
- helm client. Used to create, fetch, search and validate charts and to instruct tiller.
- tiller server. Runs inside the Kubernetes cluster and manages the releases.
Chọn phiên bản Helm v2.9.1. Dùng luôn bản binary:
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.9.1-linux-amd64.tar.gz
tar xzf helm-v2.9.1-linux-amd64.tar.gz
cd linux-amd64
cp helm /usr/local/bin/
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
helm init --service-account tiller --upgradeMột Chart là tập hợp các file nằm trong cùng thư mục. Tên của thư mục cũng là tên của Chart.
$ mkdir ./hello-world
$ cd ./hello-worldChart cần 1 file định nghĩa là Chart.yaml, và có tối thiểu 2 trường là: name, version
$ cat <<'EOF' > ./Chart.yaml
name: hello-world
version: 1.0.0
EOFTrong thư mục Chart cần có thêm thư mục template, đây là nơi lưu trữ các file định nghĩa resources cho releases. Chart dùng các file này để sinh ra các K8s manifests.
$ mkdir ./templates
$ cat <<'EOF' > ./templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: gcr.io/google-samples/node-hello:1.0
ports:
- containerPort: 8080
protocol: TCP
EOF
$ cat <<'EOF' > ./templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: hello-world
EOFCài release mới tạo ở trên
$ helm install .
NAME: cautious-shrimp
LAST DEPLOYED: Thu Jan 5 11:32:04 2017
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world 10.0.0.175 <nodes> 8080:31419/TCP 0s
==> extensions/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-world 1 1 1 0 0sNOTE: Tuy nhiên, nếu cài release mà phụ thuộc các Chart khác thì phải install từ folder bên ngoài helm install hello-world/
Check xem pod và service đã lên chưa:
kubectl get po,svcXóa release:
helm delete <release_name>
#Xóa hẳn release
helm delete --purge <release_name># liệt kê các releases đã cài:
helm ls
# các releases bị xóa
helm ls --delete
NAME REVISION UPDATED STATUS CHART
cautious-shrimp 1 Thu Jan 5 11:32:04 2017 DELETED hello-world-1.0.0Rollback: Chạy lại release nếu chưa xóa hết (xem bằng câu lệnh bên trên)
helm rollback RELEASE_NAME REVISION_NUMBERHelm cung cấp khả năng cấu hình cho release thay vì phải sửa từng file manifests.
Ví dụ: Các khai báo trong file values.yaml (./values.yaml) sẽ được sử dụng để tham chiếu cho các manifest trong folder ./templates bằng .Values:
$ cat <<'EOF' > ./values.yaml
image:
repository: gcr.io/google-samples/node-hello
tag: '1.0'
EOF
$ cat <<'EOF' > ./templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: 8080
protocol: TCP
EOFNếu dùng repo stable của helm thì có thể dùng file values.yaml của mình để ghi đè lên, hoặc có thể dùng --set. Thứ tự ưu tiên --set > local values.yaml> default values.yaml
Ta có thể chỉ in ra các manifests mà không thực hiện chúng
helm install /hello-world --dry-run --debugUsing predefined values
Bên cạnh values.yaml, ta còn có thể dùng https://github.com/helm/helm/blob/master/docs/charts.md#predefined-values
Partials
Ta có thể tự định nghĩa các object để truyền vào như predefined values, file _helpers.tpl (./templates/_helpers.tpl) là nơi để định nghĩa các Partials:
$ cat <<'EOF' > ./templates/_helpers.tpl
{{- define "hello-world.release_labels" }}
app: {{ printf "%s-%s" .Release.Name .Chart.Name | trunc 63 }}
version: {{ .Chart.Version }}
release: {{ .Release.Name }}
{{- end }}
{{- define "hello-world.full_name" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 -}}
{{- end -}}
EOFSử dụng:
$ cat <<'EOF' > ./templates/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: {{ template "hello-world.full_name" . }}
labels:
{{- include "hello-world.release_labels" . | indent 4 }}
spec:
replicas: 1
template:
metadata:
labels:
{{- include "hello-world.release_labels" . | indent 8 }}
spec:
containers:
- name: hello-world
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: 8080
protocol: TCP
EOF
$ cat <<'EOF' > ./templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ template "hello-world.full_name" . }}
labels:
{{- include "hello-world.release_labels" . | indent 4 }}
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
app: {{ template "hello-world.full_name" . }}
EOF