使用壓縮包安裝chart
我們使用helm package
打包的時候,預設會在當前位置生成一個tgz壓縮包,然後helm把它複製到到$HOME/.helm/repository
目錄下,現在還沒有講解helm私服搭建,使用的是helm自帶的一個本地伺服器,有時候這個伺服器不能正常工作,但是我們又想體驗功能,這時候可以使用本地chart壓縮包來安裝
我在當前目錄下有以下檔案
[centos@k8s-master helm]$ ls
get_helm.sh mychart mychart-0.1.0.tgz mychart-0.2.0.tgz
[centos@k8s-master helm]$ helm install --name tylertest mychart-0.1.0.tgz
NAME: tylertest
LAST DEPLOYED: Thu Apr 25 09:37:32 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tylertest-mychart 0/1 1 0 0s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
tylertest-mychart-545479dd4b-wbdm4 0/1 ContainerCreating 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tylertest-mychart ClusterIP 10.107.107.190 <none> 80/TCP 0s
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=tylertest" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
並且通過helm list可以看到它了
[centos@k8s-master helm]$ helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
kubernetes-dashboard 1 Thu Mar 14 09:06:25 2019 DEPLOYED kubernetes-dashboard-1.2.0 1.10.1 kube-system
tylertest 1 Thu Apr 25 09:37:32 2019 DEPLOYED mychart-0.1.0 1.0 default
當然也可以進入到$HOME/.helm/repository
來安裝它
安裝的時候指定不同的環境
我們在安裝一個chart的時候,由於環境不同,配置可能也會有差別,我們知道,chart的配置都存在values.yaml檔案裡,我們可以編寫多個儲存配置的yaml檔案,在安裝的時候通過-f
指定 不同環境的values檔案
[centos@k8s-master helm]$ helm install --name=tylertest -f mychart/values.yaml mychart
NAME: tylertest
LAST DEPLOYED: Thu Apr 25 09:44:00 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tylertest-mychart 0/1 0 0 0s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
tylertest-mychart-545479dd4b-m5wj5 0/1 Pending 0 0s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tylertest-mychart ClusterIP 10.108.206.51 <none> 80/TCP 0s
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=tylertest" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
注意,values.yaml是安裝時預設情況下使用的檔案,我們可以可以把它複製若干(名字可以是任意名稱,只需要在install的時候通過
-f
指定它的位置),根據不同環境進行修改.
這種方法同樣也可以解決另外一個問題,那就是很多公共倉庫的chart依賴gcr.io上的映象導致國內無法安裝成功,如果映象地址做成了引數,則我們可以修改,如果沒有做成引數,我們仍然有辦法,那就是先通過
helm fetch
把它拉到本地,然後解壓,然後修改其映象檔案重新打包,通過上面講到過的使用本地壓縮包安裝的方式來安裝.
讓 Helm 連線到指定其它Kubernetes叢集
Helm 預設使用和 kubectl 命令相同的配置訪問 Kubernetes 叢集,其配置預設在 ~/.kube/config
中。
部署時指定名稱空間
我們前面的示例都預設是在的名稱空間,實際生產環境中往往都不是按這種預設設定,而是安裝在指定的名稱空間中.如果想部署到指定的命令空間,執行helm install
命令時 可以加上 --namespace 引數,比如:
helm install local/mychart --name tylertest --namespace mynamespace
檢視某一部署應用的詳細資訊
其它跟kubectl get類似,helm也有get命令,可以通過它來檢視部署應用詳細資訊
[centos@k8s-master helm]$ helm get tylertest
REVISION: 1
RELEASED: Thu Apr 25 09:44:00 2019
CHART: mychart-0.2.0
USER-SUPPLIED VALUES:
affinity: {}
fullnameOverride: ""
image:
pullPolicy: IfNotPresent
repository: nginx
tag: stable
ingress:
annotations: {}
enabled: false
hosts:
- host: chart-example.local
paths: []
tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
port: 80
type: ClusterIP
tolerations: []
COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
pullPolicy: IfNotPresent
repository: nginx
tag: stable
ingress:
annotations: {}
enabled: false
hosts:
- host: chart-example.local
paths: []
tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
port: 80
type: ClusterIP
tolerations: []
HOOKS:
---
# tylertest-mychart-test-connection
apiVersion: v1
kind: Pod
metadata:
name: "tylertest-mychart-test-connection"
labels:
app.kubernetes.io/name: mychart
helm.sh/chart: mychart-0.2.0
app.kubernetes.io/instance: tylertest
app.kubernetes.io/managed-by: Tiller
annotations:
"helm.sh/hook": test-success
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['tylertest-mychart:80']
restartPolicy: Never
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: tylertest-mychart
labels:
app.kubernetes.io/name: mychart
helm.sh/chart: mychart-0.2.0
app.kubernetes.io/instance: tylertest
app.kubernetes.io/managed-by: Tiller
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: tylertest
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tylertest-mychart
labels:
app.kubernetes.io/name: mychart
helm.sh/chart: mychart-0.2.0
app.kubernetes.io/instance: tylertest
app.kubernetes.io/managed-by: Tiller
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: tylertest
template:
metadata:
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: tylertest
spec:
containers:
- name: mychart
image: "nginx:stable"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{}
預設情況下會顯示最新的版本的相關資訊,如果想要檢視指定釋出版本的資訊可加上 --revision 引數。
$ helm get --revision 1 tylertest
chart中定義依賴
可以在要打包的chart目錄中建立一個requirements.yaml檔案定義該chart的依賴
$ cat > ./mychart/requirements.yaml <<EOF
dependencies:
- name: mariadb
version: 0.6.0
repository: https://kubernetes-charts.storage.googleapis.com
EOF
通過helm命令更新和下載cahrt的依賴
helm dep update ./mychart
在次安裝執行chart時會把依賴中定義的chart執行起來