Helm is a package management tool for Kubernetes that manages chart – pre-configured package resources, similar to Ubuntu’s APT and CentOS’s YUM.
1 Helm
1.1 installation
# # configuration go environment wget https://golang.org/dl/go1.15.5.linux-amd64.tar.gz tar ZXVF go1.15.5. Linux - amd64. Tar. Gz CD go mv bin/go / usr/bin / # # installation helm git clone https://github.com/helm/helm.git CD helm make mv bin/helm/usr/binCopy the code
1.2 Helm creates Chart
base) [root@node46 zhangjx]# helm create mycharts
(base) [root@node46 zhangjx]# cd mycharts
(base) [root@node46 mycharts]# ls
charts Chart.yaml templates values.yaml
Copy the code
1.2.1 Charts. Yaml, this should be the configuration file
(base) [root@node46 mycharts]# cat Chart.yaml | grep -v '#'
apiVersion: v2
name: mycharts
description: A Helm chart for Kubernetes
type: application
version: 0.1.1
appVersion: 1.16.0
Copy the code
1.2.2 values.yaml, corresponding to different CONFIGURATIONS of K8S resources
(base) [root@node46 mycharts]# cat values.yaml | grep -v "#"
replicaCount: 1
image:
repository: nginx
pullPolicy: IfNotPresent
tag: latest
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
create: true
annotations: {}
name: ""
podAnnotations: {}
podSecurityContext: {}
securityContext: {}
service:
type: ClusterIP
port: 80
ingress:
enabled: false
annotations: {}
hosts:
- host: chart-example.local
paths: []
tls: []
resources: {}
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}
Copy the code
1.2.3 Templates yamL files for K8S-related resources (take Deployment as an example
(base) [root@node46 mycharts]# ls templates/
deployment.yaml _helpers.tpl hpa.yaml ingress.yaml NOTES.txt serviceaccount.yaml service.yaml tests
(base) [root@node46 mycharts]# cd templates/
(base) [root@node46 templates]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "mycharts.fullname" . }}
labels:
{{- include "mycharts.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "mycharts.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "mycharts.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "mycharts.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Copy the code
mycharts
The configuration information in chart. yaml is displayedValues
Corresponding to the configuration information under VALUES
1.3 installation chart
(base) [root@node46 mycharts]# kubectl create ns nginx namespace/nginx created (base) [root@node46 mycharts]# helm lint [INFO] chart.yaml: icon is recommended 1 chart(s) linted, 0 chart(s) failed (base) [root@node46 mycharts]# helm install helm-nginx . NAME: helm-nginx LAST DEPLOYED: Tue Nov 17 11:16:53 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands: export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mycharts,app.kubernetes.io/instance=helm-nginx" -o jsonpath="{.items[0].metadata.name}") export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o Jsonpath ="{.spec.containers[0].ports[0].containerPort}") echo "Visit http://127.0.0.1:8080 to use your application" kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT (base) [root@node46 mycharts]# kubectl get all -n nginx NAME READY STATUS RESTARTS AGE pod/helm-nginx-mycharts-c94774b66-rbp8p 1/1 Running 0 18s NAME TYPE CLUSTER-IP External-ip PORT(S) AGE Service/Helm-nginx-MyCharts ClusterIP 10.1.209.186 < None > 80/TCP 18s NAME READY up-to-date AVAILABLE AGE deployment.apps/helm-nginx-mycharts 1/1 1 1 18s NAME DESIRED CURRENT READY AGE replicaset.apps/helm-nginx-mycharts-c94774b66 1 1 1 18s (base) [root@node46 mycharts]#Copy the code
1.4 View and pack
(base) [root@node46 zhangjx]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION helm-nginx default 1 2020-11-17 11:16:53.983298944 +0800 CST Deployed MyCharts -0.1.1 1.16.0 ## Deployed MyCharts [root@node46 Zhangjx]# helm Package ./mycharts/ Successfully packaged chart and saved it to: [root@node46 zhangjx]# helm uninstall helm-nginx release "helm-nginx" Uninstalled ## Install (base) [root@node46 zhangjx]# helm install helm-nginx./mycharts-0.1.1. TGZ NAME: helm-nginx LAST DEPLOYED: Tue Nov 17 13:37:31 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands: export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mycharts,app.kubernetes.io/instance=helm-nginx" -o jsonpath="{.items[0].metadata.name}") export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o Jsonpath ="{.spec.containers[0].ports[0].containerPort}") echo "Visit http://127.0.0.1:8080 to use your application" kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT (base) [root@node46 zhangjx]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION Helm - Nginx Default 1 2020-11-17 13:37:31.328734126 +0800 CST Deployed MyCharts -0.1.1 1.16.0 (base) [root@node46 zhangjx]# kubectl create NS nginx-1 namespace/nginx-1 created ## Set namespace to nginx-1 (base) [root@node46 zhangjx]# helm install helm-nginx-1 --set namespace=nginx-1./ mycharts-0.1.tgz NAME: helm-nginx-1 LAST DEPLOYED: Tue Nov 17 13:40:43 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands: export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mycharts,app.kubernetes.io/instance=helm-nginx-1" -o jsonpath="{.items[0].metadata.name}") export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o Jsonpath ="{.spec.containers[0].ports[0].containerPort}") echo "Visit http://127.0.0.1:8080 to use your application" kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT (base) [root@node46 zhangjx]# kubectl get pod -n nginx-1 NAME READY STATUS RESTARTS AGE helm-nginx-1-mycharts-67748f87b7-skfjs 1/1 Running 0 10sCopy the code
How to manage packaged application files? Tiller (Helm Init) was used to manage the packaged files before Helm 3.0, but the Helm init command was cancelled after 3.0. Chartmuseum manages the Helm packaged applications to facilitate the persistence of Kubernetes application information
1.5 Common Helm Commands
helm create
: Create a new chart locally;helm dependency
: Manage chart dependencies;helm instal
L: Install chart;helm lint
: Check whether chart configuration is incorrect;helm list
: lists all releases;helm package
: Package local chart;helm repo
: List, add, update and delete chart warehouse;helm rollback
Roll back release to historical version;helm pull
: Pull remote chart to local;helm search
: Use keywords to search chart;helm uninstall
: uninstall release;helm upgrade
: Upgrade release;helm show
: View the configuration of Charts.
2 Chartmuseum
2.1 installation
curl -LO https://s3.amazonaws.com/chartmuseum/release/latest/bin/linux/amd64/chartmuseum
chmod +x chartmuseum
cp chartmuseum /usr/local/bin
Copy the code
2.2 Configuring and Starting Chartmuseum
2.2.1 Service File Configuration
# cat /etc/systemd/system/chartmuseum.service
[Unit]
Description=chartmuseum
Requires=network-online.target
After=network-online.target
[Service]
EnvironmentFile=/etc/chartmuseum/chartmuseum.config
User=root
Restart=allways
ExecStart=/usr/local/bin/chartmuseum $ARGS
ExecStop=/usr/local/bin/chartmuseum step-down
[Install]
WantedBy=multi-user.target
Copy the code
2.2.2 the/etc/chartmuseum chartmuseum. Config configuration
# mkdir -p /etc/chartmuseum/
# cat /etc/chartmuseum/chartmuseum.config
ARGS=\
--port=9090 \
--storage="local" \
--storage-local-rootdir="/var/lib/chartmuseum/chartstorage" \
--log-json \
--basic-auth-user=admin \
--basic-auth-pass=admin
Copy the code
- –port: chartMuseum service listening port
- –storage: local: local storage is used
- –storage-local-rootdir: indicates the path of the local storage point, which is the storage path of helm Push Chart
- –log-json: Logs are displayed in JSON format
- –basic-auth-user: user name (use the basic authentication mode, user name + password, use the certificate mode refer to me)
- –basic-auth-pass: password (chartmuseum) –username XXX –password (chartmuseum
2.2.3 Starting the Service
systemctl start chartmuseum
systemctl status chaetmuseum
Copy the code
3 Chartmuseum works with the Helm
3.1 increase the repo
Helm Repo add Chartmuseum http://192.168.5.46:9090 --username admin --password admin (base) [root@node46 bak]# Helm repo List NAME URL ChartMuseum http://192.168.5.46:9090Copy the code
3.2 the use of
3.2.1 upload
# curl -u admin: admin - data - binary "@ my" http://192.168.4.32:9090/api/chartsCopy the code
3.2.2 view
GET /index.yaml
Get all charts of ChartMuseum
# curl http://192.168.5.46:9090/index.yaml - u admin: admin apiVersion: v1 entries: mycharts: - apiVersion: Description: A Helm chart for Kubernetes digest: d0363f6588e36345f05656cbae5a6e4639c1e1bd606ad8761a46600d1e11bd07 name: mycharts type: application urls: - Charts/mychars-0.1.1. TGZ version: 0.1.1 Generated: "2020-11-17T14:05:19+08:00" serverInfo: {}Copy the code
3.2.3 download
- use
curl
(base) [root @ node46 bak] # curl - http://192.168.5.46:9090/charts/mycharts-0.1.1.tgz - O u admin: Received Total admin % % % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3600 0 3600 0 0 612k 0 --:--:-- - : -- : -- -- - : - 703 k (base) [root @ node46 bak] # ls mycharts - while. TGZCopy the code
helm pull
(base) [root@node46 Zhangjx]# helm repo list NAME URL ChartMuseum http://192.168.5.46:9090 (base) [root@node46 bak]# Helm pull --username admin -- Password admin --repo http://192.168.5.46:9090 myCharts -- Version 0.1.6 (base) [root@node46 Bak] # ls mycharts - 0.1.6. TGZCopy the code
3.2.4 Listing All Charts (JSON Format)
(base) [root @ node46 zhangjx] # http://192.168.5.46:9090/api/charts curl - s - u admin: admin | jq {" mycharts ": [{" name" : Mycharts ", "version": "0.1.6", "description": "A Helm chart for Kubernetes", "apiVersion": "v2", "appVersion": "1.16.0", "type" : "application", "urls" : [" charts/mycharts - 0.1.6..tgz "], "created" : "The 2020-11-17 T14: o. 068906 + 08:00", and "digest" : "c0081239734e0592cdf5b98da8ff25a12ed5ce847de29ea52ddce7c0fe70a34f" }, { "name": "mycharts", "version": "0.1.1", "description": "A Helm chart for Kubernetes", "apiVersion": "v2", "appVersion": "1.16.0", "type": "Application ", "urls": [" Charts/mycharts-0.1.tgz"], "created": "2020-11-17T14:04:28.005906+08:00", "Digest ": "d0363f6588e36345f05656cbae5a6e4639c1e1bd606ad8761a46600d1e11bd07" } ] }Copy the code
3.2.5 Deleting a Charts
(base) [root @ node46 zhangjx] # curl -x DELETE http://192.168.5.46:9090/api/charts/mycharts/0.1.1 -u admin: admin {"deleted":true}Copy the code
3.2.6 installation charts
# helm install helm-nginx-2 --username admin --password admin --repo http://192.168.5.46:9090 mycharts --version 0.1.6
Copy the code
4 summarizes
Using Helm to manage Kubernetes application is to manage the yamL file that creates Kubernetes resources, package the resources needed by the same application into the same directory, and manage the variables that often change with Helm, so that it is easy to deploy and migrate.