Kubernetes生产实践系列之二十七:Kubernetes Operator之构建memcached operator并进行部署

一、前言

在文章《Kubernetes生产实践系列之二十六:Kubernetes Operator之Operator SDK开发环境构建》中,对于Operator的基本原理和开发方法进行了介绍。其实在之前的文章《Kubernetes生产实践系列之六:使用etcd-operator在Kubernetes部署 HA etcd集群》和文章《Kubernetes生产实践系列之十一:Kubernetes Mysql Operator方案对比》中,已经在生产环境中使用了Operator进行有状态的数据存储服务etcd和Mysql的交付:

 

其实业界现在已经将Operator作为应用在Kubernetes进行标准化、自动化、规模化部署和交付事实标准,网站https://operatorhub.io/收集了大量的不同技术和业务方向的Operator,而网站https://github.com/operator-framework/awesome-operators也列举了不少靠谱可用的Operator:

本文将根据例子构建一个memcached operator并进行部署:

 转载自https://blog.youkuaiyun.com/cloudvtech

二、构建Operator

2.1 下载代码

cd /root/gowork/src/

git clone https://github.com/operator-framework/operator-sdk-samples.git
cd /root/gowork/src/operator-sdk-samples/memcached-operator

2.2 修改部分代码逻辑

Operator binary code

/root/gowork/src/operator-sdk-samples/memcached-operator/cmd/manager/main.go 

Controller code

/root/gowork/src/memcached-operator/vendor/github.com/operator-framework/operator-sdk-samples/memcached-operator/pkg/controller/memcached/memcached_controller.go 

2.3 构建

export GOROOT=/usr/lib/golang
export GOPATH=/root/gowork
export GO111MODULE=off

go get -u github.com/golang/dep/cmd/dep
/root/gowork/bin/dep ensure -v


export IMAGE=172.2.2.11:5000/memcached-operator:v0.3.1
operator-sdk build $IMAGE --verbose --image-build-args --no-cache

docker push $IMAGE

 转载自https://blog.youkuaiyun.com/cloudvtech

三、部署Operator

3.1 部署

sed "s@REPLACE_IMAGE@$IMAGE@g" -i deploy/operator.yaml

kubectl create ns $NAMESPACE
kubectl create -f deploy/crds/cache_v1alpha1_memcached_crd.yaml -n $NAMESPACE
kubectl create -f deploy/service_account.yaml -n $NAMESPACE
kubectl create -f deploy/role.yaml -n $NAMESPACE
kubectl create -f deploy/role_binding.yaml -n $NAMESPACE
kubectl create -f deploy/operator.yaml -n $NAMESPACE

 cache_v1alpha1_memcached_crd.yaml

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: memcacheds.cache.example.com
spec:
  group: cache.example.com
  names:
    kind: Memcached
    listKind: MemcachedList
    plural: memcacheds
    singular: memcached
  scope: Namespaced
  subresources:
    status: {}
  validation:
    openAPIV3Schema:
      description: Memcached is the Schema for the memcacheds API
      properties:
        apiVersion:
          description: 'APIVersion defines the versioned schema of this representation
            of an object. Servers should convert recognized schemas to the latest
            internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
          type: string
        kind:
          description: 'Kind is a string value representing the REST resource this
            object represents. Servers may infer this from the endpoint the client
            submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
          type: string
        metadata:
          type: object
        spec:
          description: MemcachedSpec defines the desired state of Memcached
          properties:
            size:
              description: Size is the size of the memcached deployment
              format: int32
              type: integer
          required:
          - size
          type: object
        status:
          description: MemcachedStatus defines the observed state of Memcached
          properties:
            nodes:
              description: Nodes are the names of the memcached pods
              items:
                type: string
              type: array
              x-kubernetes-list-type: set
          required:
          - nodes
          type: object
      type: object
  version: v1alpha1
  versions:
  - name: v1alpha1
    served: true
    storage: true

3.2 查看部署结果

kubectl get pod -n $NAMESPACE
NAME                                  READY   STATUS    RESTARTS   AGE
memcached-operator-57f77b4bd8-dcp7v   1/1     Running   0          8s

 转载自https://blog.youkuaiyun.com/cloudvtech

 

四、部署memcached集群

4.1 部署

kubectl apply -f cache_v1alpha1_memcached_cr.yaml -n $NAMESPACE

cache_v1alpha1_memcached_cr.yaml

apiVersion: cache.example.com/v1alpha1
kind: Memcached
metadata:
  name: example-memcached
spec:
  # Add fields here
  size: 3

 

4.2 Operator的日志

4.3 修改集群部署参数增加replica:3->7

 

转载自https://blog.youkuaiyun.com/cloudvtech

 

 

 

一、为什么学习kubernetes众所周知,随着容器的快速发展,容器管理工具kubernetes也应运而生,目前不仅百度、京东、阿里、google等大公司在使用kubernetes,一些中小企业也开始把业务迁移到kubernetes,那么作为运维、开发、测试或者架构师来说,必须要掌握这项技术,才能提现我们的工作价值,才能在行业具备保持较高的技术水平,kubernetes作为成熟的容器编排工具,具有容器集群的自动化部署、自动化伸缩和故障自恢复的能力,让容器的部署和管理变得更加容易,能够给企业和提供一个智能化的容器云管理平台,为企业快速上云提供一个安全可靠的解决方案,此课程主要介绍kubernetes1.14/kubernetes1.15版本高可用集群的安装部署和使用,通过我多年工作经验总结,带你深入体验企业实战案例,让您轻松快速的掌握k8s,接下来让我们一起出发吧。 二、课程亮点 三、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值