helm基本知识(下)

上一篇只是简单的安装测试了一下helm的使用,这里补充一下关于helm的其他相关命令

先查看本地导入的helm仓库

[root@master1 ~]# helm repo ls 
NAME  	URL                                                   
aliyun	https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

 搜索仓库里面的应用chart,这里会把应用的版本号和属于哪个仓库等信息显示出来

[root@master1 ~]# helm search repo mysql
NAME                         	CHART VERSION	APP VERSION	DESCRIPTION                                       
aliyun/mysql                 	0.3.5        	           	Fast, reliable, scalable, and easy to use open-...
aliyun/percona               	0.3.0        	           	free, fully compatible, enhanced, open source d...
aliyun/percona-xtradb-cluster	0.0.2        	5.7.19     	free, fully compatible, enhanced, open source d...
aliyun/gcloud-sqlproxy       	0.2.3        	           	Google Cloud SQL Proxy                            
aliyun/mariadb               	2.1.6        	10.1.31    	Fast, reliable, scalable, and easy to use open-...
[root@master1 ~]# 

通过inspect命令可以查看到详细的应用信息

[root@master1 ~]# helm inspect chart aliyun/mysql
apiVersion: v1
description: Fast, reliable, scalable, and easy to use open-source relational database
  system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
maintainers:
- email: viglesias@google.com
  name: Vic Iglesias
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 0.3.5

通过参数values可以看到改应用可以支持哪些配置选项,这些选项我们都可以自定义的,下面会有实验案例

[root@master1 go_code]# helm inspect values  aliyun/mysql
## mysql image version
## ref: https://hub.docker.com/r/library/mysql/tags/
##
image: "mysql"
imageTag: "5.7.14"

## Specify password for root user
##
## Default: random 10 character string
# mysqlRootPassword: testing

## Create a database user
##
# mysqlUser:
# mysqlPassword:

## Allow unauthenticated access, uncomment to enable
##
# mysqlAllowEmptyPassword: true

## Create a database
##
# mysqlDatabase:

## Specify an imagePullPolicy (Required)
## It's recommended to change this to 'Always' if the image tag is 'latest'
## ref: http://kubernetes.io/docs/user-guide/images/#updating-images
##
imagePullPolicy: IfNotPresent

livenessProbe:
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  successThreshold: 1
  failureThreshold: 3

readinessProbe:
  initialDelaySeconds: 5
  periodSeconds: 10
  timeoutSeconds: 1
  successThreshold: 1
  failureThreshold: 3

## Persist data to a persistent volume
persistence:
  enabled: true
  ## database data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # storageClass: "-"
  accessMode: ReadWriteOnce
  size: 8Gi

## Configure resource requests and limits
## ref: http://kubernetes.io/docs/user-guide/compute-resources/
##
resources:
  requests:
    memory: 256Mi
    cpu: 100m

# Custom mysql configuration files used to override default mysql settings
configurationFiles:
#  mysql.cnf: |-
#    [mysqld]
#    skip-name-resolve


## Configure the service
## ref: http://kubernetes.io/docs/user-guide/services/
service:
  ## Specify a service type
  ## ref: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services---service-types
  type: ClusterIP
  port: 3306
  # nodePort: 32000

那么如何去自定义这些配置信息呢?

我们可以通过建立一个yaml文件,把我们想要的覆盖的配置信息,写到我们的yaml文件中,在install安装的时候指定这个yaml文件,测试用例如下:

这里我们先安装一个应用

[root@master1 k8s]# helm install mysql  aliyun/mysql 

查看部署的应用

[root@master1 go_code]# helm ls
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql	default  	1       	2021-10-31 17:41:53.194961182 +0800 CST	deployed	mysql-0.3.5	           

也可以通过inspect查看详细信息。

通过kubectl命令查看一下应用的部署情况

 发现pod没有起来,这里默认开启了PV绑定进行数据持久化,因为我们没有合适的pv去跟我们的pvc进行绑定,导致pod一直处于pending状态,我们先不处理它

[root@master1 go_code]# helm inspect values  aliyun/mysql |grep persistence -A 5
persistence:
  enabled: true
  ## database data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is

 这里我们把这个存储改成false,并修改其他的配置信息,具体的自定义yaml文件如下

[root@master1 mychart]# cat config.yaml 
mysqlUser: leechm
mysqlDatabase: testdb
persistence:
  enabled: false
service:
  type: NodePort
[root@master1 mychart]# 

由于我们之前安装了这个应用,所以我们也可以动态的更改,当然可以删除这个应用,再通过-f参数重新install部署,这里我们通过upgrade升级的方式直接动态的修改

[root@master1 mychart]# helm upgrade  mysql  aliyun/mysql -f config.yaml 
Release "mysql" has been upgraded. Happy Helming!
NAME: mysql
LAST DEPLOYED: Sun Oct 31 18:10:26 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql-mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=$(kubectl get nodes --namespace default -o jsonpath='{.items[0].status.addresses[0].address}')
    MYSQL_PORT=$(kubectl get svc --namespace default mysql-mysql -o jsonpath='{.spec.ports[0].nodePort}')

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
[root@master1 mychart]# 

验证一下,可以看到相关信息已经修改成功了

[root@master1 go_code]# helm  ls 
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql	default  	2       	2021-10-31 18:10:26.332236109 +0800 CST	deployed	mysql-0.3.5	           
[root@master1 go_code]# kubectl get svc 
NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes    ClusterIP   10.1.0.1     <none>        443/TCP          29m
mysql-mysql   NodePort    10.1.1.233   <none>        3306:31247/TCP   28m
[root@master1 go_code]# 
[root@master1 go_code]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
mysql-mysql-59fb4f4f4f-vxjjk   0/1     Running   0          2m12s
[root@master1 go_code]# 

上面是通过-f指定我们的yaml文件修改配置信息的,我也可以通过终端命令行的方式,即--set参数

[root@master1 mychart]# kubectl get svc 
NAME          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes    ClusterIP   10.1.0.1      <none>        443/TCP    63m
mysql-mysql   ClusterIP   10.1.61.201   <none>        3306/TCP   7s
[root@master1 mychart]# 
[root@master1 mychart]# helm upgrade mysql aliyun/mysql --set service.type=NodePort
Release "mysql" has been upgraded. Happy Helming!
NAME: mysql
LAST DEPLOYED: Sun Oct 31 18:44:20 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-mysql.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql-mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=$(kubectl get nodes --namespace default -o jsonpath='{.items[0].status.addresses[0].address}')
    MYSQL_PORT=$(kubectl get svc --namespace default mysql-mysql -o jsonpath='{.spec.ports[0].nodePort}')

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
[root@master1 mychart]# 
[root@master1 mychart]# kubectl get svc 
NAME          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
kubernetes    ClusterIP   10.1.0.1      <none>        443/TCP          63m
mysql-mysql   NodePort    10.1.61.201   <none>        3306:32057/TCP   17s
[root@master1 mychart]# 

当然也支持回退到之前的应用

先查看历史版本

[root@master1 go_code]# helm history mysql
REVISION	UPDATED                 	STATUS    	CHART      	APP VERSION	DESCRIPTION     
1       	Sun Oct 31 17:41:53 2021	superseded	mysql-0.3.5	           	Install complete
2       	Sun Oct 31 18:10:26 2021	deployed  	mysql-0.3.5	           	Upgrade complete

通过rollback命令回退

[root@master1 mychart]# helm rollback --atomic mysql 1
[root@master1 mychart]# helm ls 
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS	CHART      	APP VERSION
mysql	default  	3       	2021-10-31 18:16:08.747683445 +0800 CST	deployed	mysql-0.3.5	           
[root@master1 mychart]# 
[root@master1 mychart]# kubectl get svc
NAME          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes    ClusterIP   10.1.0.1      <none>        443/TCP    45m
mysql-mysql   ClusterIP   10.1.73.214   <none>        3306/TCP   46s
[root@master1 mychart]# 
[root@master1 mychart]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
mysql-mysql-6c589d547c-wfnvb   0/1     Pending   0          10m
[root@master1 mychart]# 

这里补充一个知识点,--set的指定的值会覆盖values和-f指定的yaml里面的值,关于自定义值的优先级的说明 : --set xxx > -f xxx.yaml > values.yaml

具体演示如下:

[root@master1 mychart]# helm create chart-test
Creating chart-test
[root@master1 mychart]# 
[root@master1 mychart]# 
[root@master1 mychart]# ls
chart-test
[root@master1 mychart]# rm -rf chart-test/templates/*
[root@master1 mychart]# 
[root@master1 mychart]# cat chart-test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "hello world"
  mydata: {{ .Values.mydata }}
[root@master1 mychart]# 
[root@master1 mychart]# cat chart-test/values.yaml 
# Default values for chart-test.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

mydata: leechm

replicaCount: 1
.......
[root@master1 mychart]# cat mydata.yaml 
mydata: f
[root@master1 mychart]# 

ok,上面的文件准备好之后,我们开始测试优先级的说明,注意看mydata的值

[root@master1 mychart]# helm install --dry-run mychart ./chart-test/ --set mydata=test

分析:mydata的值为--set指定的

[root@master1 mychart]# helm install --dry-run mychart ./chart-test/

分析:mydata的值为values.yaml里面指定的

[root@master1 mychart]# helm install --dry-run mychart ./chart-test/ --set mydata=test -f mydata.yaml

分析:mydata的值为--set指定的。。。


十月再见

十一月你好

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值