helm和operator 含十分钟弄懂
原文链接:https://zhuanlan.zhihu.com/p/151306450HELM 是干什么的 ?在K8S CLUSTER里安装一个应用比较麻烦,需要定义POD YAML, Deployment YAML, SeviceAccount, Secret, Configmap等等很麻烦, HELM通过一个叫HELM CHART的东西把这些打包了,方便部署应用。HELM 是K8S的包管理工具
一 operator
十分钟弄懂:
十分钟弄懂 k8s Operator 应用的制作流程 - 知乎
CoreOS有状态应用管理的框架--Operator详解
CoreOS有状态应用管理的框架--Operator详解_程序源234的博客-CSDN博客
Operator基于Third Party Resources扩展了新的应用资源,并通过控制器来保证应用处于预期状态。比如etcd operator通过下面的三个步骤模拟了管理etcd集群的行为:
通过Kubernetes API观察集群的当前状态;
分析当前状态与期望状态的差别;
调用etcd集群管理API或Kubernetes API消除这些差别。
与其他工具的关系:
StatefulSets:StatefulSets为有状态服务提供了DNS、持久化存储等,而Operator可以自动处理服务失效、备份、重配置等复杂的场景。
Puppet:Puppet是一个静态配置工具,而Operator则可以实时、动态地保证应用处于预期状态
Helm:Helm是一个打包工具,可以将多个应用打包到一起部署,而Operator则可以认为是Helm的补充,用来动态保证这些应用的正常运行
扩展
名词解释:ThirdPartyResources:Kubernetes(k8s)中文文档 名词解释:ThirdPartyResources_Kubernetes中文社区
二 helm
原文链接:HELM 及 Operator - 知乎
HELM 是干什么的 ? helm官方手册:Helm | 模板函数列表
在K8S CLUSTER里安装一个应用比较麻烦,需要定义POD YAML, Deployment YAML, SeviceAccount, Secret, Configmap等等很麻烦, HELM通过一个叫HELM CHART的东西把这些打包了,方便部署应用。HELM 是K8S的包管理工具,类似 apt与ubuntu的关系。
Operator与HELM干的事情差不多,也是方便安装应用的,但是operator是通过扩展k8s的API来实现的,也就是说operator是为k8s增加了自定义的能力,但这个自定义的能力是原生k8s的,是一等公民,享受所有的k8s的API能力。 一个operator由CR (Customer Resource)及controller 组成,这个CR由CRD (YAML 文件)定义,controller负责watch CR的变化。来控制资源的实现。你把一个应用做成Operator形式可以由3种办法,一种是基于HELM CHART来做,因为HELM CHART已经定义了很多的YAML模板及配置参数,还有一种是基于ANSIBLE来做,与HELM类似,最灵活的是基于golang。这几种方式都需要operator-SDK.
以下我们看一下如何制作基于HELM的operator.
- 安装golang, 安装helm, 安装 operator-SDK,省去
ubuntu@client:~$ operator-sdk new bookstore-operator --api-version=velotio.com/v1alpha1 --kind=BookStore --type=helm --helm-chart=book-store --helm-chart-repo=https://akash-gautam.github.io/helmcharts/
INFO[0000] Creating new Helm operator 'bookstore-operator'.
INFO[0002] Created helm-charts/book-store
INFO[0002] Generating RBAC rules
WARN[0003] Skipping rule generation for manifest-2. Failed to determine resource scope for apps/v1beta1, Kind=Deployment.
WARN[0003] Skipping rule generation for manifest-3. Failed to determine resource scope for apps/v1beta1, Kind=Deployment.
WARN[0003] The RBAC rules generated in deploy/role.yaml are based on the chart's default manifest. Some rules may be missing for resources that are only enabled with custom values, and some existing rules may be overly broad. Double check the rules generated in deploy/role.yaml to ensure they meet the operator's permission requirements.
INFO[0003] Created build/Dockerfile
INFO[0003] Created deploy/service_account.yaml
INFO[0003] Created deploy/role.yaml
INFO[0003] Created deploy/role_binding.yaml
INFO[0003] Created deploy/operator.yaml
INFO[0003] Created deploy/crds/velotio.com_v1alpha1_bookstore_cr.yaml
INFO[0003] Generated CustomResourceDefinition manifests.
INFO[0003] Project creation complete.
ubuntu@client:~$
--api-version=http://velotio.com/v1alpha1 --kind=BookStore --type=helm --helm-chart=book-store --helm-chart-repo=https://akash-gautam.github.io/helmcharts
因为operator是k8s的第一类居民,所以由同样的API group/version,并且kind是你自己定义的,与kind=Pod是一个等级的。 helm-chart指定生成的类型。 chart直接从git 上拉。你可以看到git的chart由模板化的yaml文件及value 组成。 这个命令生成下列文件
ubuntu@client:~$ tree /home/ubuntu/bookstore-operator/
/home/ubuntu/bookstore-operator/
├── build
│ └── Dockerfile
├── deploy
│ ├── crds
│ │ ├── velotio.com_bookstores_crd.yaml
│ │ └── velotio.com_v1alpha1_bookstore_cr.yaml
│ ├── operator.yaml
│ ├── role.yaml
│ ├── role_binding.yaml
│ └── service_account.yaml
├── helm-charts
│ └── book-store
│ ├── Chart.yaml
│ ├── templates
│ │ ├── NOTES.txt
│ │ ├── _helpers.tpl
│ │ ├── bookapp-deployment.yaml
│ │ ├── bookapp-service.yaml
│ │ ├── mongodb-deployment.yaml
│ │ └── mongodb-service.yaml
│ └── values.yaml
└── watches.yaml
6 directories, 16 files
watch.yaml是operator的controller用的,监控crd的变化。deploy目录下的CRD是资源的定义,只不过是自定义的资源. 剩下的yaml是部署所需要的比如,role,rolebing, service account这些. helm-chart目录下的是应用本身的模板文件及value. build目录下的dockerfile供我们生成这个operator的docker images.
ubuntu@client:~$ cd bookstore-operator/
ubuntu@client:~/bookstore-operator$ sudo /home/ubuntu/go/bin/operator-sdk build akash125/bookstore-operator:v0.0.1
INFO[0000] Building OCI image interbeing/bookstore-operator:v0.0.1
Sending build context to Docker daemon 26.62kB
Step 1/3 : FROM quay.io/operator-framework/helm-operator:v0.18.0
---> 8b7196632d68
Step 2/3 : COPY watches.yaml ${HOME}/watches.yaml
---> c889cdaa6f14
Step 3/3 : COPY helm-charts/ ${HOME}/helm-charts/
---> cab6f1d8b406
Successfully built cab6f1d8b406
Successfully tagged akash125/bookstore-operator:v0.0.1
INFO[0001] Operator build complete.
ubuntu@client:~/bookstore-operator$ docker push interbeing/visitors-operator:v0.0.1
将image 上传到dockerhub ,然后deploy.yaml中的image名字换成刚生成的image
ubuntu@client:~/bookstore-operator$ cd deploy/
ubuntu@client:~/bookstore-operator/deploy$ ls
crds operator.yaml role.yaml role_binding.yaml service_account.yaml
ubuntu@client:~/bookstore-operator/deploy$ sed -i 's/REPLACE_IMAGE/interbeing\/bookstore-operator:v0.0.1/g' operator.yaml
ubuntu@client:~/bookstore-operator/deploy$ more operator.yaml
然后部署这个operator
ubuntu@client:~/bookstore-operator$ ls ./deploy/
crds operator.yaml role.yaml role_binding.yaml service_account.yaml
ubuntu@client:~/bookstore-operator$ kubectl apply -f ./deploy/service_account.yaml
ubuntu@client:~/bookstore-operator$ kubectl apply -f ./deploy/role.yaml
ubuntu@client:~/bookstore-operator$ kubectl apply -f ./deploy/role_binding.yaml
ubuntu@client:~/bookstore-operator/deploy/crds$ kubectl apply -f velotio.com_bookstores_crd.yaml
ubuntu@client:~/bookstore-operator/deploy/crds$ kubectl apply -f velotio.com_v1alpha1_bookstore_cr.yaml
ubuntu@client:~/bookstore-operator/deploy/crds$
ubuntu@client:~/bookstore-operator$ kubectl apply -f ./deploy/operator.yaml
部署完毕,可以看到operator 已经正常运行
ubuntu@client:~/calico$ k get pod
NAME READY STATUS RESTARTS AGE
bookstore-operator-6dccb74b87-79mw5 1/1 Running 0 10m
example-bookstore-book-store-79874c7f7d-b4bs2 1/1 Running 1 10m
example-bookstore-book-store-mongodb-7d6686cbbc-x7smx 1/1 Running 0 10m
ubuntu@client:~/calico$ k get deployment BookStore
Error from server (NotFound): deployments.apps "BookStore" not found
ubuntu@client:~/calico$ k get BookStore
NAME AGE
example-bookstore 39m
ubuntu@client:~/calico$
ubuntu@client:~/calico$ k get svc | grep 11m
bookstore-operator-metrics ClusterIP 10.7.252.36 <none> 8383/TCP,8686/TCP 11m
example-bookstore-book-store LoadBalancer 10.7.253.161 34.96.221.175 80:30019/TCP 11m
mongodb-service ClusterIP 10.7.254.16 <none> 27017/TCP 11m
ubuntu@client:~/calico$
测试, 给数据库增加二条纪录
ubuntu@client:~/kubia/bookstore-operator$ curl -X POST -d '{"name":"book1" ,"author":"a1"}' 34.96.221.175/books && echo
{"id":"5ef9637d0bf64000014c0416","name":"book1","author":"a1","price":0}
ubuntu@client:~/kubia/bookstore-operator$ curl -X POST -d '{"name":"book2" ,"author":"a2"}' 34.96.221.175/books && echo
{"id":"5ef963840bf64000014c0417","name":"book2","author":"a2","price":0}
ubuntu@client:~/kubia/bookstore-operator$ curl http://34.96.221.175/books
[{"id":"5ef9637d0bf64000014c0416","name":"book1","author":"a1","price":0},{"id":"5ef963840bf64000
更多推荐
所有评论(0)