通过helm2构建kubernetes应用(一)

helm部署

  1. 安装helm客户端
# 下载二进制包
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.14.3-linux-amd64.tar.gz
# 减压文件
tar xvf helm-v2.14.3-linux-amd64.tar.gz
# 拷贝文件到执行目录
cp linux-amd64/helm /usr/bin/
  1. helm server安装
# 初始化安装helm server
helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.3 \
--stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
# -i 指定了使用的镜像
# --stable-repo-url 指定镜像地址
  1. 配置RBAC(helm server需要集群管理的权限)
# 创建SA账户"tiller"
kubectl create serviceaccount --namespace kube-system tiller
# 创建集群角色规则,即:将cluster-admin角色赋予tiller账户
kubectl create clusterrolebinding tiller-cluster-rule \
--clusterrole=cluster-admin --serviceaccount=kube-system:tiller
# 为tiller-deploy(helm server)挂载tiller账户
kubectl patch deploy --namespace kube-system tiller-deploy \
-p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
  1. 验证安装
wangw@DESKTOP:~$ helm version
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
  1. 更新repo
wangw@DESKTOP:~$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "gistack" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.

基本用法

# 搜索应用(以mysql为例)
wangw@DESKTOP:~$ helm search mysql
NAME                                    CHART VERSION   APP VERSION     DESCRIPTION
gistack/mysql                           0.1.1           5.7             Fast, reliable, scalable, and easy to use open-source rel...
stable/mysql                            1.4.0           5.7.27          Fast, reliable, scalable, and easy to use open-source rel...
stable/mysqldump                        2.6.0           2.4.1           A Helm chart to help backup MySQL databases using mysqldump
stable/prometheus-mysql-exporter        0.5.1           v0.11.0         A Helm chart for prometheus mysql exporter with cloudsqlp...
stable/percona                          1.2.0           5.7.17          free, fully compatible, enhanced, open source drop-in rep...
stable/percona-xtradb-cluster           1.0.2           5.7.19          free, fully compatible, enhanced, open source drop-in rep...
stable/phpmyadmin                       4.0.0           4.9.1           phpMyAdmin is an mysql administration frontend
stable/gcloud-sqlproxy                  0.6.1           1.11            DEPRECATED Google Cloud SQL Proxy
stable/mariadb                          6.12.0          10.3.18         Fast, reliable, scalable, and easy to use open-source rel...
# 部署应用
wangw@DESKTOP:~$ helm install stable/mysql --name mysql --namespace default
NAME:   mysql
LAST DEPLOYED: Wed Oct 23 13:14:44 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME        DATA  AGE
mysql-test  1     0s

==> v1/Deployment
NAME   READY  UP-TO-DATE  AVAILABLE  AGE
mysql  0/1    1           0          0s

==> v1/PersistentVolumeClaim
NAME   STATUS  VOLUME                                    CAPACITY  ACCESS MODES  STORAGECLASS  AGE
mysql  Bound   pvc-998f967c-a667-416d-aba1-2a3c7bf943ec  8Gi       RWO           nfs-devel     0s

==> v1/Pod(related)
NAME                    READY  STATUS   RESTARTS  AGE
mysql-57f6d6fbbb-lrpf8  0/1    Pending  0         0s

==> v1/Secret
NAME   TYPE    DATA  AGE
mysql  Opaque  2     0s

==> v1/Service
NAME   TYPE       CLUSTER-IP    EXTERNAL-IP  PORT(S)   AGE
mysql  ClusterIP  10.43.91.169  <none>       3306/TCP  0s
# 查看已部署的应用
wangw@DESKTOP:~$ helm ls
NAME    REVISION        UPDATED                         STATUS          CHART           APP VERSION     NAMESPACE
mysql   1               Wed Oct 23 13:14:44 2019        DEPLOYED        mysql-1.4.0     5.7.27          default
# 删除应用
wangw@DESKTOP:~$ helm delete mysql
release "mysql" deleted
# 删除应用记录
wangw@DESKTOP:~$ helm ls --all
NAME    REVISION        UPDATED                         STATUS  CHART           APP VERSION     NAMESPACE
mysql   1               Wed Oct 23 13:14:44 2019        DELETED mysql-1.4.0     5.7.27          default
wangw@DESKTOP:~$ helm delete --purge mysql
release "mysql" deleted

创建自定义应用

# 创建一个app目录用于存放helm生成的文件
wangw@DESKTOP:~$ mkdir app && cd app
# 创建应用模板(默认为nginx应用),命名为myapp1
wangw@DESKTOP:~/app$ helm create myapp1
Creating myapp1
# 生成一个myapp1的文件夹,文件结构如下
wangw@DESKTOP:~/app$ tree myapp1/
myapp1/
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 8 files
# Chart.yaml,应用说明,包括版本、作者、注释等
# charts文件夹,存放应用依赖的其它应用打包文件
# templates文件夹,存放应用编排模板文件
# values.yaml,模板中引用的值,主要修改此处实现定制
# 通过模板创建出的应用能满足大多数无状态应用的定制

应用上传到本地仓库

# 开启本地仓库
wangw@DESKTOP:~$ helm serve
Regenerating index. This may take a moment.
Now serving you on 127.0.0.1:8879
# 检查myapp1文件
wangw@DESKTOP:~/app$ helm lint myapp1/
==> Linting myapp1/
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, no failures
# 打包myapp1
wangw@DESKTOP:~/app$ helm package myapp1/
Successfully packaged chart and saved it to: /home/wangw/app/myapp1-0.1.0.tgz
# 生成了myapp1-0.1.0.tgz文件
wangw@DESKTOP:~/app$ ls
myapp1  myapp1-0.1.0.tgz
# 查看本地仓库,已经存在了myapp1
wangw@DESKTOP:~/app$ curl localhost:8879

<html>
<head>
        <title>Helm Repository</title>
</head>
<h1>Helm Charts Repository</h1>
<ul>

  <li>myapp1<ul>
    <li><a href="http://127.0.0.1:8879/charts/myapp1-0.1.0.tgz">myapp1-0.1.0</a></li>
  </ul>
  </li>

</ul>
<body>
<p>Last Generated: 2019-10-23 13:35:32.0383579 &#43;0800 CST</p>
</body>
</html>

添加本地仓库

# 添加仓库
wangw@DESKTOP:~/app$ helm repo add localrepo http://localhost:8879
"localrepo" has been added to your repositories
# 查看已添加仓库列表
wangw@DESKTOP:~/app$ helm repo list
NAME            URL
stable          https://kubernetes-charts.storage.googleapis.com/
localrepo       http://localhost:8879

部署自定义应用

# 搜索myapp1
wangw@DESKTOP:~/app$ helm search myapp1
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
localrepo/myapp1        0.1.0           1.0             A Helm chart for Kubernetes
# 部署myapp1
wangw@DESKTOP:~/app$ helm install myapp1 --name myapp1 --namespace default
NAME:   myapp1
LAST DEPLOYED: Wed Oct 23 13:42:19 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME    READY  UP-TO-DATE  AVAILABLE  AGE
myapp1  0/1    0           0          0s

==> v1/Service
NAME    TYPE       CLUSTER-IP    EXTERNAL-IP  PORT(S)  AGE
myapp1  ClusterIP  10.43.209.50  <none>       80/TCP   0s


NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myapp1,app.kubernetes.io/instance=myapp1" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:80
# 查看myapp1运行情况
wangw@DESKTOP:~/app$ helm status myapp1
LAST DEPLOYED: Wed Oct 23 13:42:19 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Deployment
NAME    READY  UP-TO-DATE  AVAILABLE  AGE
myapp1  1/1    1           1          2m10s

==> v1/Pod(related)
NAME                     READY  STATUS   RESTARTS  AGE
myapp1-5945568b98-ctmmw  1/1    Running  0         2m10s

==> v1/Service
NAME    TYPE       CLUSTER-IP    EXTERNAL-IP  PORT(S)  AGE
myapp1  ClusterIP  10.43.209.50  <none>       80/TCP   2m10s

NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myapp1,app.kubernetes.io/instance=myapp1" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:80

应用访问

# 本地代理pod端口
wangw@DESKTOP:~/app$ kubectl port-forward myapp1-5945568b98-ctmmw 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
# 访问应用
wangw@DESKTOP:~$ curl  http://127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

应用删除

wangw@DESKTOP:~/app$ helm delete --purge myapp1
release "myapp1" deleted
posted @ 2020-10-19 19:16  longtds  阅读(171)  评论(0编辑  收藏  举报