码迷,mamicode.com
首页 > 其他好文 > 详细

k8s之helm入门

时间:2020-09-11 16:11:17      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:map   update   应用更新   upgrade   was   har   chmod   自定义   extension   

作者:李毓

什么是helm

我们在实际操作k8s的时候,会发现要部署一个应用必须先编写deployment,daemonset,service的资源文件,非常麻烦。项目多的时候,非常难于管理,这个时候helm就出现了,他的作用可以看作类似于centos的yum工具,或者ubuntu的apt。都是包管理器。他的作用就是对应用程序做封装,进行版本管理和依赖检查,然后对应用程序进行分发。helm主要由两部分组成,一个是elm客户端、一个是Tiller服务端。
helm有三个主要概念:
1.chart:定义应用所需要的资源。
2.Repository:在仓库中下载chart
3.release:k8s中的chart实例,每个实例都是一个版本。

安装helm

现在我们来安装helm
系统约定

  • kubernetes1.18.6
  • 可访问镜像仓库
  • helm安装在k8s集群的机器中

我们先去官网下载下来
https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz

[root@adm-master ~]# tar -zxvf helm-v3.2.4-linux-amd64.tar.gz
linux-amd64/
linux-amd64/helm
linux-amd64/README.md
linux-amd64/LICENSE
[root@adm-master ~]# cp linux-amd64/helm /usr/local/bin/
[root@adm-master ~]# chmod +x /usr/local/bin/helm
[root@adm-master ~]# he
head helm helm2 help hexdump
[root@adm-master ~]# helm version
version.BuildInfo{Version:"v3.2.4", GitCommit:"0ad800ef43d3b826f31a5ad8dfbb4fe05d143688", GitTreeState:"clean", GoVersion:"go1.13.12"}

由于helm3比helm2方便很多,不再需要安装tiller,所以这样就算安装好了
那么helm怎么用呢?

创建一个chart

helm create testnginx

查看里面的结构

技术图片

为了方便测试,改一下暴露方式。

技术图片

安装这个chart

[root@adm-master ~]# helm install ./testnginx --generate-name
NAME: testnginx-1598707352
LAST DEPLOYED: Sat Aug 29 21:22:32 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services testnginx-1598707352)
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT

查看是否安装成功

技术图片

我们访问一下

技术图片

至此,我们用helm安装一个应用就成功了。

当然这个是我们自己自定义的一个应用,现在网上有很多成熟的应用,我们也可以直接拿来用,下面简单说一说用法。

添加仓库

helm repo add stable http://aliacs-k8s-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/app/charts/ 
helm repo add incubator http://aliacs-k8s-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/app/charts-incubator/

查看仓库

helm repo list

查找应用

[root@adm-master ~]# helm search repo nginx
NAME                        CHART VERSION   APP VERSION DESCRIPTION                                       
incubator/ack-ingress-nginx 1.34.2          0.30.0      An Nginx Ingress Controller that uses ConfigMap

拉取应用

helm pull incubator/ack-ingress-nginx

修改配置文件

[root@adm-master ~]# cd ack-ingress-nginx/
[root@adm-master ack-ingress-nginx]# ls
Chart.yaml  ci  README.md  templates  values.yaml
[root@adm-master ack-ingress-nginx]# vim values.yaml 
[root@adm-master ack-ingress-nginx]# 

启用宿主机网络

技术图片

安装

[root@adm-master ~]# helm install ./ack-ingress-nginx --generate-name
NAME: ack-ingress-nginx-1598707957
LAST DEPLOYED: Sat Aug 29 21:32:37 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The nginx-ingress controller has been installed.
It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status by running ‘kubectl --namespace default get services -o wide -w ack-ingress-nginx-1598707957-controller‘

An example Ingress that makes use of the controller:

  apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                serviceName: exampleService
                servicePort: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls

创建好了

技术图片

除了上面的一些命令,下面再介绍一些常用的几个命令。

查看安装的应用

[root@adm-master ~]# helm list
NAME                            NAMESPACE   REVISION    UPDATED                                 STATUS      CHART                       APP VERSION
ack-ingress-nginx-1598707957    default     1           2020-08-29 21:32:37.485600339 +0800 CST deployed    ack-ingress-nginx-1.34.2    0.30.0     
ack-ingress-nginx-1598748240    default     1           2020-08-30 08:44:00.160214613 +0800 CST deployed    ack-ingress-nginx-1.34.2    0.30.0     
nginx-1598703649                default     1           2020-08-29 20:20:49.161478619 +0800 CST deployed    nginx-0.1.0                 1.16.0     
test-1598706678                 default     1           2020-08-29 21:11:18.658604777 +0800 CST deployed    test-0.1.0                  1.16.0     
test1-1598706774                default     1           2020-08-29 21:12:54.102423232 +0800 CST deployed    test1-0.1.0                 1.16.0     
testnginx-1598707352            default     1           2020-08-29 21:22:32.666499735 +0800 CST deployed    testnginx-0.1.0             1.16.0     

查看应用状态

[root@adm-master ~]# helm status testnginx-1598707352
NAME: testnginx-1598707352
LAST DEPLOYED: Sat Aug 29 21:22:32 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services testnginx-1598707352)
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT

应用更新升级

[root@adm-master ~]# helm upgrade ack-ingress-nginx-1598748240 ./ack-ingress-nginx
Release "ack-ingress-nginx-1598748240" has been upgraded. Happy Helming!
NAME: ack-ingress-nginx-1598748240
LAST DEPLOYED: Sun Aug 30 09:15:31 2020
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
The nginx-ingress controller has been installed.
Get the application URL by running these commands:
  export HTTP_NODE_PORT=32080
  export HTTPS_NODE_PORT=32443
  export NODE_IP=$(kubectl --namespace default get nodes -o jsonpath="{.items[0].status.addresses[1].address}")

  echo "Visit http://$NODE_IP:$HTTP_NODE_PORT to access your application via HTTP."
  echo "Visit https://$NODE_IP:$HTTPS_NODE_PORT to access your application via HTTPS."

An example Ingress that makes use of the controller:

  apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                serviceName: exampleService
                servicePort: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls

应用回滚

[root@adm-master ~]# helm hist ack-ingress-nginx-1598748240
REVISION    UPDATED                     STATUS      CHART                       APP VERSION DESCRIPTION     
1           Sun Aug 30 08:44:00 2020    superseded  ack-ingress-nginx-1.34.2    0.30.0      Install complete
2           Sun Aug 30 09:15:31 2020    deployed    ack-ingress-nginx-1.34.2    0.30.0      Upgrade complete

[root@adm-master ~]# helm rollback ack-ingress-nginx-1598748240 1
Rollback was a success! Happy Helming!

应用删除

[root@adm-master ~]# helm delete ack-ingress-nginx-1598707957
release "ack-ingress-nginx-1598707957" uninstalled

k8s之helm入门

标签:map   update   应用更新   upgrade   was   har   chmod   自定义   extension   

原文地址:https://blog.51cto.com/14783669/2525786

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!