helm是kubernetes集群上的软件包管理工具,使用它可以方便地、快速地进行部署云应用,也可以方便地搜索stable系列的云应用,直接fetch然后install进来即可,省心省力就得靠它了~

安装helm

1
2
3
4
5
6
7
wget https://get.helm.sh/helm-v2.14.3-linux-amd64.tar.gz
tar zxvf helm-v2.14.3-linux-amd64.tar.gz
cd linux-amd64
cp helm tiller /usr/local/bin/
scp helm tiller k8s-node1:/usr/local/bin/
scp helm tiller k8s-node2:/usr/local/bin/
helm init --history-max 200

小试牛刀

1
2
3
$ helm repo update
$ helm install stable/mysql
Error: no available release name found

发现错误,修复之,参考链接:https://github.com/helm/helm/issues/3055

1
2
3
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'

0)下载配置

下载stable/mysql相关的配置

1
2
3
helm fetch stable/mysql
tar xvmf mysql-1.3.1.tgz # 添加 -m 参数以忽略时间Warning
cd mysql/

修改values.xml上关于storageClass的配置:

1
2
3
4
persistence:
  enabled: true
  storageClass: "local-storage-mysql"
  # ...

storageClass设定为了local-storage-mysql,稍后将创建这个存储。

1)创建Local StorageClass

新建文件local-storage-mysql.yaml

1
2
3
4
5
6
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage-mysql
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

2)创建PV

新建文件local-pv-mysql.yaml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv-mysql
spec:
  capacity:
    storage: 8Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage-mysql
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /app/local-storage/mysql
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - k8s-node1

通过nodeAffinity来选择节点,这里使用hostname为k8s-node1的节点,同时需要在这个节点上创建好/app/local-storage/mysql目录。

3)应用配置文件

1
2
kubectl apply -f local-storage-mysql.yaml
kubectl apply -f local-pv-mysql.yaml

4)通过helm启动mysql服务

1
2
3
4
helm install --name t1-mysql -f values.yaml stable/mysql
# 查看运行状态
kubectl get pods
kubectl describe pods t1-mysql-58874677c-9fmgj

5)验证一下

1
kubectl run --rm -it --restart=Never --image=mysql:5.7.14 mysql-client --command -- mysql --host $(kubectl get svc | awk '/t1-mysql/{print $3}') -uroot -p"$(kubectl get secret --namespace default t1-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)"

其中:

  • 获取IP:kubectl get svc | awk '/t1-mysql/{print $3}
  • 获取密码:kubectl get secret --namespace default t1-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode

Tip: 这个deployment会在交互式shell退出之后自动删除~