k8s中级篇-Helm安装与入门

前言

相信对于包管理工具,大家肯定是不陌生的,如:java有maven/gradle、python有pip、nodejs有npm/yarn等,而k8s也有其包管理工具-helm。使用helm我们可以更为方便地将应用发布到k8s集群。

环境

主机名ip角色
mldong01192.168.0.245master
mldong02192.168.0.54node01
mldong03192.168.0.22node02

三台主机为华为软开云的ECS,CentOS Linux release 7.6.1810 (Core)

安装Helm

这里安装的是目前最新版本helm3.4.2,Linux amd64

https://github.com/helm/helm/releases

image-20201227120741065

注:安装前要先把kubectl安装好,其实使用rke安装的k8s集群,默认已经帮安装helm了。因为我是在另一个非集群节点上的使用的,所以要自行安装相关工具。

# 下载二进制包
wget https://get.helm.sh/helm-v3.4.2-linux-amd64.tar.gz
# 解压
 tar -zxvf helm-v3.4.2-linux-amd64.tar.gz
# 复制到需要的目录
 mv linux-amd64/helm /usr/local/bin/helm
# 验证是否安装成功
 helm -h

Helm常用命令

添加常用的chart源

[root@mldong ~]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
[root@mldong ~]# helm repo add aliyuncs https://apphub.aliyuncs.com
"aliyuncs" has been added to your repositories

查看chart列表

[root@mldong ~]# helm repo list
NAME            URL                               
bitnami         https://charts.bitnami.com/bitnami
aliyuncs        https://apphub.aliyuncs.com       

搜索本地chart

[root@mldong01 ~]# helm search repo nginx
NAME                                    CHART VERSION   APP VERSION             DESCRIPTION                                       
aliyuncs/nginx                          5.1.5           1.16.1                  Chart for the nginx server                        
aliyuncs/nginx-ingress                  1.30.3          0.28.0                  An nginx Ingress controller that uses ConfigMap...
aliyuncs/nginx-ingress-controller       5.3.4           0.29.0                  Chart for the nginx Ingress controller            
aliyuncs/nginx-lego                     0.3.1                                   Chart for nginx-ingress-controller and kube-lego  
aliyuncs/nginx-php                      1.0.0           nginx-1.10.3_php-7.0    Chart for the nginx php server                    
bitnami/nginx                           8.2.3           1.19.6                  Chart for the nginx server                        
bitnami/nginx-ingress-controller        7.0.6           0.42.0                  Chart for the nginx Ingress controller            
bitnami/kong                            3.1.0           2.2.1                   Kong is a scalable, open source API layer (aka ...

下载chart包到本地

# 创建目录
mkdir -p /java_projects/k8s/helm-charts
# 进入目录
cd /java_projects/k8s/helm-charts
# 下载chart包
[root@mldong helm-charts]# helm pull aliyuncs/nginx --untar
[root@mldong helm-charts]# tree -L 2 nginx/
nginx/
├── Chart.yaml
├── ci
│   └── values-with-ingress-metrics-and-serverblock.yaml
├── README.md
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── server-block-configmap.yaml
│   ├── servicemonitor.yaml
│   ├── svc.yaml
│   └── tls-secrets.yaml
├── values.schema.json
└── values.yaml

2 directories, 13 files

没有tree工具的可以先安装

yum install tree 

Helm使用技巧

以上述的nginx为例,简单分享一下使用技巧

根据默认配置发布服务

# 发布
helm install nginx aliyuncs/nginx
# 查看运行情况
kubectl get svc -w nginx
# 删除
helm delete nginx

指定命名空间发布服务

# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test
# 查看运行情况
kubectl get svc -n nginx-test -w nginx
# 删除
helm delete nginx -n nginx-test
# 删除命名空间
kubectl delete ns nginx-test

查看chart源码的方式

可以使用上述pull的方式下载到本地,这里不再做介绍。

# 相看chart基本信息
helm show chart aliyuncs/nginx
# 主要是关注sources,一般都会有github地址,然后去浏览器打开,比如现在这个

image-20201230103918406

当然,这个并不是很友好,没有直接的chart地址,需要自己去找

image-20201230104131337

image-20201230104201082

然后下面是模板

image-20201230104255702

然后下面是配置说明

image-20201230104321634

查看配置文件的方式

上面那个其实已经包含了查看配置的方式,这里再提供一个使用命令查看的

helm show values aliyuncs/nginx

命令行中修改配置发布

因为我已经知道部分配置了,这里就启用一下ingress

# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test --set ingress.enabled=true --set ingress.hostname=a.mldong.com
# 查看运行情况
kubectl get svc -n nginx-test -w nginx
# 查看ingress
kubectl get ingress -n nginx-test
# 查看ingress配置文件
kubectl get ingress -n nginx-test -o yaml
# 删除
helm delete nginx -n nginx-test
# 删除命名空间
kubectl delete ns nginx-test

源码上对应的逻辑

image-20201230105001773

使用外部配置文件发布

这个是建议使用的方式,因为–set所支持的数据类型有限

# 先创建配置文件
cat <<EOF >  nginx-values.yaml
ingress: 
  enabled: true
  hostname: a.mldong.com
EOF
# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test -f nginx-values.yaml
# 查看运行情况
kubectl get svc -n nginx-test -w nginx
# 查看ingress
kubectl get ingress -n nginx-test
# 查看ingress配置文件
kubectl get ingress -n nginx-test -o yaml
# 删除
helm delete nginx -n nginx-test
# 删除命名空间
kubectl delete ns nginx-test

配置修改

# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test
# 查看运行情况
kubectl get svc -n nginx-test -w nginx
# 查看ingress
kubectl get ingress -n nginx-test
# 更新配置发布
helm upgrade nginx aliyuncs/nginx -n nginx-test --set ingress.enabled=true --set ingress.hostname=a.mldong.com
# 再次查看ingress
kubectl get ingress -n nginx-test
# 删除
helm delete nginx -n nginx-test
# 删除命名空间
kubectl delete ns nginx-test

启用debug模式运行

# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test --debug
# 查看运行情况
kubectl get svc -n nginx-test -w nginx
# 查看ingress
kubectl get ingress -n nginx-test
# 查看ingress配置文件
kubectl get ingress -n nginx-test -o yaml
# 删除
helm delete nginx -n nginx-test
# 删除命名空间
kubectl delete ns nginx-test

–dry-run参数使用

该参数运行并不会真正的运行,一般会使用该参数与–debug参数来进行调试用的

# 创建命名空间-如不存在
kubectl create ns nginx-test
# 发布
helm install nginx aliyuncs/nginx -n nginx-test --dry-run

小结

本文只是讲了一些Helm最最常用的小案例,想要熟练使用,还是得多练习。建议多看看chart的源码模板,这些模板大多设计得非常优雅,考虑得比较全面。现在想来,我初级篇的持续集成模板,真有点弱爆了。不过对于入门来说,太多配置反而不适合。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值