k8s中helm包管理工具使用

Helm

Kubernetes 包管理工具

Helm 可以帮助我们管理 Kubernetes 应用程序 - Helm Charts 可以定义、安装和升级复杂的 Kubernetes 应用程序,Charts 包很容易创建、版本管理、分享和分布

 

安装

到GitHub仓库下载:https://github.com/helm/helm/releases

把下载好的包helm-v3.10.3-linux-amd64.tar.gz,上传master节点/usr/bin目录下,改名 helm

[root@k8s-master1 ~]# ll /usr/bin/helm 
-rwxr-xr-x 1 root root 45125632 Dec 15 10:06 /usr/bin/helm

查询版本,看到下面的版本信息证明已经成功了

[root@k8s-master1 ~]# helm version
version.BuildInfo{Version:"v3.10.3", GitCommit:"835b7334cfe2e5e27870ab3ed4135f136eecc704", GitTreeState:"clean", GoVersion:"go1.18.9"}

 

示例

安装包,如mysql

添加仓库,官方仓库下载太慢:helm repo add 自定义名  仓库url

#添加仓库
[root@k8s-master1 ~]# helm repo add bitnami https://charts.bitnami.com/bitnami

#查询添加的仓库
[root@k8s-master1 ~]# helm repo list
NAME    URL                                                   
cxm     http://mirror.azure.cn/kubernetes/charts              
cyn     https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami 

 

 搜索mysql包

[root@k8s-master1 ~]# helm search repo mysql
NAME                            CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/mysql                   9.4.5           8.0.31          MySQL is a fast, reliable, scalable, and easy t...
cxm/mysql                       1.6.9           5.7.30          DEPRECATED - Fast, reliable, scalable, and easy...
cxm/mysqldump                   2.6.2           2.4.1           DEPRECATED! - A Helm chart to help backup MySQL...
cxm/prometheus-mysql-exporter   0.7.1           v0.11.0         DEPRECATED A Helm chart for prometheus mysql ex...
cyn/mysql                       0.3.5                           Fast, reliable, scalable, and easy to use open-...
bitnami/phpmyadmin              10.3.8          5.2.0           phpMyAdmin is a free software tool written in P...
cxm/percona                     1.2.3           5.7.26          DEPRECATED - free, fully compatible, enhanced, ...
cxm/percona-xtradb-cluster      1.0.8           5.7.19          DEPRECATED - free, fully compatible, enhanced, ...
cxm/phpmyadmin                  4.3.5           5.0.1           DEPRECATED phpMyAdmin is an mysql administratio...
cyn/percona                     0.3.0                           free, fully compatible, enhanced, open source d...
cyn/percona-xtradb-cluster      0.0.2           5.7.19          free, fully compatible, enhanced, open source d...
bitnami/mariadb                 11.4.2          10.6.11         MariaDB is an open source, community-developed ...
bitnami/mariadb-galera          7.4.10          10.6.11         MariaDB Galera is a multi-primary database clus...
cxm/gcloud-sqlproxy             0.6.1           1.11            DEPRECATED Google Cloud SQL Proxy                 
cxm/mariadb                     7.3.14          10.3.22         DEPRECATED Fast, reliable, scalable, and easy t...
cyn/gcloud-sqlproxy             0.2.3                           Google Cloud SQL Proxy                            
cyn/mariadb                     2.1.6           10.1.31         Fast, reliable, scalable, and easy to use open-...

 

 选择一个包安装

[root@k8s-master1 ~]# helm install mysql cxm/mysql  -n my-ns-helm

 

 安装

[root@k8s-master1 ~]# helm install mysql cxm/mysql -n my-ns-helm
WARNING: This chart is deprecated
NAME: mysql
LAST DEPLOYED: Thu Jan  5 17:18:35 2023
NAMESPACE: my-ns-helm
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql.my-ns-helm.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace my-ns-helm 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 -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/mysql 3306

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

#查询
[root@k8s-master1 pv]# helm list -n my-ns-helm
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
mysql   my-ns-helm      1               2023-01-05 17:18:35.766225203 +0800 CST deployed        mysql-1.6.9     5.7.30 

 到这一步,查询pod会发现mysql并没有running

#查询状态为Pending
[root@k8s-master1 ~]# kubectl get pod -n my-ns-helm 
NAME                     READY   STATUS    RESTARTS   AGE
mysql-7b9cf5df76-rrtpj   0/1     Pending   0          2m50s

#查询pod信息,可以看到提示pvc没有绑定
#需要手动创建一个pv,绑定pvc
[root@k8s-master1 ~]# kubectl describe pod -n my-ns-helm mysql-7b9cf5df76-rrtpj 
.....
Events:
  Type     Reason            Age                 From               Message
  ----     ------            ----                ----               -------
  Warning  FailedScheduling  6s (x5 over 4m22s)  default-scheduler  0/3 nodes are available: 3 pod has unbound immediate PersistentVolumeClaims.

创建pv,绑定pvc

#查询目前pv,pvc
[root@k8s-master1 mysql]# kubectl get pv,pvc -n my-ns-helm 
NAME                          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                STORAGECLASS   REASON   AGE
persistentvolume/jenkins-pv   4Gi        RWX            Retain           Bound       my-ns-devops/jenkins-pvc                                     77d
persistentvolume/nginx-pv1    3Gi        RWX            Retain           Available                                                                112d
persistentvolume/nginx-pv2    5Gi        RWX            Retain           Released    my-ns-xmgc/nginxpvc1                                         112d
persistentvolume/nginx-pv3    8Gi        RWX            Retain           Released    my-ns-xmgc/nginxpvc2                                         112d
persistentvolume/redis-pv1    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-3   redis                   106d
persistentvolume/redis-pv2    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-5   redis                   106d
persistentvolume/redis-pv3    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-2   redis                   106d
persistentvolume/redis-pv4    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-0   redis                   106d
persistentvolume/redis-pv5    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-1   redis                   106d
persistentvolume/redis-pv6    2Gi        RWX            Retain           Released    my-ns-redis/redis-data-redis-app-4   redis                   106d

NAME                          STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/mysql   Pending                                                     8m50s

#pv yaml文件
[root@k8s-master1 pv]# cat mysql-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    app: mysql
  namespace: my-ns-helm
spec:
  capacity:
    storage: 8Gi 
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path:  "/data/k8s/mysql"
    server: 192.168.198.144
    readOnly: false
#创建pv
[root@k8s-master1 pv]# kubectl apply -f mysql-pv.yaml 
persistentvolume/mysql-pv created

 创建pv后再次查询pod,状态running,mysql安装成功

[root@k8s-master1 pv]# kubectl get pod -n my-ns-helm 
NAME                     READY   STATUS    RESTARTS   AGE
mysql-7b9cf5df76-rrtpj   1/1     Running   0          11m

登录mysql

#查询root密码
[root@k8s-master1 pv]# kubectl get secrets -n my-ns-helm mysql -o yaml | grep mysql-root-password | awk '{print $2}' | base64 -d 
8alWd1Ewh8

#登录mysql
[root@k8s-master1 pv]# kubectl exec -it -n my-ns-helm mysql-7b9cf5df76-rrtpj /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, remove-lost-found (init)
root@mysql-7b9cf5df76-rrtpj:/# 
root@mysql-7b9cf5df76-rrtpj:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 

卸载

[root@k8s-master1 ~]# helm uninstall mysql -n my-ns-helm
release "mysql" uninstalled

[root@k8s-master1 ~]# helm list -n my-ns-helm
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION

[root@k8s-master1 ~]# kubectl get deployments.apps -n my-ns-helm 
No resources found in my-ns-helm namespace.

 

posted @ 2023-01-04 08:59  IT运维成长笔记  阅读(257)  评论(0编辑  收藏  举报