k8s1.14-helm基本使用

仓库

Helm 的 Repo 仓库和 Docker Registry 比较类似,Chart 库可以用来存储和共享打包 Chart 的位置,我们在安装了 Helm 后,默认的仓库地址是 google 的一个地址,这对于我们不能科学上网的同学就比较苦恼了,没办法访问到官方提供的 Chart 仓库,可以用helm repo list来查看当前的仓库配置:

1
2
3
4
helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com/
local http://127.0.0.1:8879/charts

替换阿里的仓库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@master101 sy]# helm repo remove stable
"stable" has been removed from your repositories



[root@master101 sy]# helm repo list
NAME URL
local http://127.0.0.1:8879/charts


[root@master101 sy]# helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"stable" has been added to your repositories
[root@master101 sy]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.
[root@master101 sy]# helm repo list
NAME URL
local http://127.0.0.1:8879/charts
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

仓库添加完成后,可以使用 update 命令进行仓库更新。当然如果要我们自己来创建一个 web 服务器来服务 Helm Chart 的话,只需要实现下面几个功能点就可以提供服务了:

  • 将索引和Chart置于服务器目录中
  • 确保索引文件index.yaml可以在没有认证要求的情况下访问
  • 确保 yaml 文件的正确内容类型(text/yaml 或 text/x-yaml)

查找 chart

Helm 将 Charts 包安装到 Kubernetes 集群中,一个安装实例就是一个新的 Release,要找到新的 Chart,我们可以通过搜索命令完成。

直接运行helm search命令可以查看有哪些 Charts 是可用的:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@master101 sy]# helm search
NAME CHART VERSION APP VERSION DESCRIPTION
stable/acs-engine-autoscaler 2.1.3 2.1.1 Scales worker nodes within agent pools
stable/aerospike 0.1.7 v3.14.1.2 A Helm chart for Aerospike in Kubernetes
stable/anchore-engine 0.1.3 0.1.6 Anchore container analysis and policy evaluation engine s...
stable/artifactory 7.0.3 5.8.4 Universal Repository Manager supporting all major packagi...
stable/artifactory-ha 0.1.0 5.8.4 Universal Repository Manager supporting all major packagi...
stable/aws-cluster-autoscaler 0.3.2 Scales worker nodes within autoscaling groups.
stable/bitcoind 0.1.0 0.15.1 Bitcoin is an innovative payment network and a new kind o...
stable/buildkite 0.2.1 3 Agent for Buildkite
stable/centrifugo 2.0.0 1.7.3 Centrifugo is a real-time messaging server.
stable/cert-manager 0.2.2 0.2.3 A Helm chart for cert-manager
stable/chaoskube 0.6.2 0.6.1 Chaoskube periodically kills random pods in your Kubernet...

搜索mysql:

1
2
3
4
5
6
7
[root@master101 sy]# helm search mysql
NAME CHART VERSION APP VERSION DESCRIPTION
stable/mysql 0.3.5 Fast, reliable, scalable, and easy to use open-source rel...
stable/percona 0.3.0 free, fully compatible, enhanced, open source drop-in rep...
stable/percona-xtradb-cluster 0.0.2 5.7.19 free, fully compatible, enhanced, open source drop-in rep...
stable/gcloud-sqlproxy 0.2.3 Google Cloud SQL Proxy
stable/mariadb 2.1.6 10.1.31 Fast, reliable, scalable, and easy to use open-source rel...

inspect 命令来查看一个 chart 的详细信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@master101 sy]# helm inspect  stable/mysql
description: Fast, reliable, scalable, and easy to use open-source relational database
system.
engine: gotpl
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

---
## 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:

安装 chart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
helm install stable/mysql
NAME: mewing-squid
LAST DEPLOYED: Tue Sep 4 23:31:23 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mewing-squid-mysql Pending 1s

==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mewing-squid-mysql ClusterIP 10.108.197.48 <none> 3306/TCP 1s

==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
mewing-squid-mysql 1 0 0 0 1s

==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
mewing-squid-mysql-69f587bdf9-z7glv 0/1 Pending 0 0s

==> v1/Secret
NAME TYPE DATA AGE
mewing-squid-mysql Opaque 2 1s

==> v1/ConfigMap
NAME DATA AGE
mewing-squid-mysql-test 1 1s


NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mewing-squid-mysql.default.svc.cluster.local

To get your root password run:

MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mewing-squid-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 mewing-squid-mysql -p

To connect to your database directly from outside the K8s cluster:
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306

# Execute the following command to route the connection:
kubectl port-forward svc/mewing-squid-mysql 3306

mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}

--name参数指定 名字:

1
helm install stable/mysql --name mydb

自定义 chart

要查看 chart 上可配置的选项,使用helm inspect values命令即可,比如我们这里查看上面的 mysql 的配置选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[root@master101 sy]#  helm inspect values stable/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 格式的文件中来覆盖上面的任何配置,在安装的时候直接使用该配置文件即可:(config.yaml)

1
2
3
4
mysqlUser: syUser
mysqlDatabase: syDB
service:
type: NodePort

指定该 yaml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
helm install -f config.yaml stable/mysql --name mydb
NAME: mydb
LAST DEPLOYED: Wed Sep 5 00:09:44 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Secret
NAME TYPE DATA AGE
mydb-mysql Opaque 2 1s

==> v1/ConfigMap
NAME DATA AGE
mydb-mysql-test 1 1s

==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mydb-mysql Pending 1s

==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mydb-mysql NodePort 10.96.150.198 <none> 3306:32604/TCP 0s

==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
mydb-mysql 1 1 1 0 0s

==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
mydb-mysql-dfc999888-hbw5d 0/1 Pending 0 0s
...

pod状况

1
2
3
kubectl get pods
NAME READY STATUS RESTARTS AGE
mydb-mysql-dfc999888-hbw5d 0/1 Pending 0 44m

看到是pending状态,describe查下:

1
2
3
4
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 2m (x37 over 12m) default-scheduler pod has unbound PersistentVolumeClaims (repeated 2 times)

可以通过 storageclass 或者手动创建一个合适的 PV 对象来解决这个问题。

也可以更新helm,来直接禁用掉数据持久化,可以在上面的 config.yaml 文件中设置:

1
2
persistence:
enabled: false

另外一种方法就是在安装过程中使用--set来覆盖对应的 value 值,比如禁用数据持久化,我们这里可以这样来覆盖:

1
helm install stable/mysql --set persistence.enabled=false --name mydb

升级

将数据持久化禁用掉来对上面的 mydb 进行升级:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@master101 sy]# echo config.yaml
mysqlUser: haimaxyUser
mysqlDatabase: haimaxyDB
service:
type: NodePort
persistence:
enabled: false


[root@master101 sy]# helm upgrade -f config.yaml mydb stable/mysql
helm upgrade -f config.yaml mydb stable/mysql
Release "mydb" has been upgraded. Happy Helming!
LAST DEPLOYED: Wed Sep 5 00:38:33 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
...

可以使用 helm history 命令查看 release 的历史版本:

1
2
3
4
helm history mydb
REVISION UPDATED STATUS CHART DESCRIPTION
1 Wed June 17 00:09:44 2018 SUPERSEDED mysql-0.10.1 Install complete
2 Wed June 17 00:38:33 2018 DEPLOYED mysql-0.10.1 Upgrade complete
Donate