採用Operator-sdk輕鬆將helm chart轉爲Operator

去年就接觸Operator,從Oracle發佈的WebLogic Operator到mySQL Operator,構建的源碼一大堆,但感受一直缺乏合適的開發框架可以避免複雜性快速生成,node

隨着技術的日益成熟,目前基於helm Operator輕鬆解決快速安裝的問題,值得嘗試一下。linux

下圖是Operator框架的成熟度模型,基於不一樣的階段,採用不一樣的技術知足全生命週期管理的需求nginx

本文主要是以tomcat爲例來進行快速的生成一個tomcat Operator.git

1.環境準備

  • 安裝go

下載安裝,參考 https://golang.org/doc/install?download=go1.11.5.linux-amd64.tar.gz,再也不詳訴。github

  • 安裝編譯環境
yum -y install gcc automake autoconf libtool make
  • 安裝Operator CLI
mkdir -p $GOPATH/src/github.com/operator-framework
cd $GOPATH/src/github.com/operator-framework
git clone https://github.com/operator-framework/operator-sdk
cd operator-sdk
git checkout master
make install

 

2.構建Tomcat Operator

  • 新建項目
cd /usr/local/go/src/github.com/operator-framework/operator-sdk
operator-sdk new tomcat-operator --cluster-scoped --api-version=example.com/v1alpha1 --kind=Tomcat --type=helm

這裏創建的是cluster-scope,意思是全集羣均可以用,缺省是當前的命名空間,看一下有什麼東西golang

[root@master operator-sdk]# tree tomcat-operator
tomcat-operator
├── build
│   └── Dockerfile
├── deploy
│   ├── crds
│   │   ├── example_v1alpha1_tomcat_crd.yaml
│   │   └── example_v1alpha1_tomcat_cr.yaml
│   ├── operator.yaml
│   ├── role_binding.yaml
│   ├── role.yaml
│   └── service_account.yaml
├── helm-charts
│   └── tomcat
│       ├── charts
│       ├── Chart.yaml
│       ├── templates
│       │   ├── deployment.yaml
│       │   ├── _helpers.tpl
│       │   ├── ingress.yaml
│       │   ├── NOTES.txt
│       │   ├── service.yaml
│       │   └── tests
│       │       └── test-connection.yaml
│       └── values.yaml
└── watches.yaml

 

  • 客戶化Operator的邏輯

主要是針對templates下面的deployment.yaml進行修改,由於缺省是以nginx爲模板來作的,因此端口都是80,須要修改爲8080docker

主要的修改就是deployment.yaml和values.yaml, 固然若是須要部署多個服務,同時多個服務由必定的依賴關係能夠在helm中進行實現,鏡像及版本的修改在values.yaml裏面,我貼一段json

[root@master tomcat]# cat values.yaml 
# Default values for tomcat.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: registry.example.com/tomcat
  tag: 8-slim
  pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

service:
  type: ClusterIP
  port: 8080

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  paths: []

我就修改了image和端口部分。api

 example_v1alpha1_tomcat_cr.yaml這個文件主要用於部署tomcat類型的實例的yaml文件,tomcat

能夠修改deploy/crds/example_v1alpha1_tomcat_cr.yaml,部署多個實例

[root@master crds]# cat example_v1alpha1_tomcat_cr.yaml
apiVersion: example.com/v1alpha1
kind: Tomcat
metadata:
  name: example-tomcat
spec:
  # Default values copied from <project_dir>/helm-charts/tomcat/values.yaml
  
  # Default values for tomcat.
  # This is a YAML-formatted file.
  # Declare variables to be passed into your templates.
  
  replicaCount: 2
  
  image:
    repository: registry.example.com/tomcat
    tag: 8-slim
    pullPolicy: IfNotPresent
  
  nameOverride: ""
  fullnameOverride: ""

 打開能夠看到,基本就是指定了Kind爲Tomcat,下面具體的值均可以從values.yaml中拷貝,而且能夠覆蓋values.yaml的值。

 

3.部署Operator到集羣

  • 部署CRD
oc create -f deploy/crds/example_v1alpha1_nginx_crd.yaml

Openshift須要知道客戶化的資源定義,這個定義就經過這個腳本,指定了watch.

  • 生成Operator的鏡像

首先部署的時候是基於build目錄下的Dockerfile,由於訪問不到,因此作了個跳轉,把Dockerfile的quay.io/operator-framework/helm-operator:v0.5.0路徑修改掉

[root@master build]# cat Dockerfile 
FROM docker.io/ericnie2017/helm-operator:latest

COPY helm-charts/ ${HOME}/helm-charts/
COPY watches.yaml ${HOME}/watches.yaml

而後運行

operator-sdk build registry.example.com/example/tomcat-operator:v0.0.1
[root@master tomcat-operator]# operator-sdk build registry.example.com/example/tomcat-operator:v0.0.1
INFO[0000] Building Docker image registry.example.com/example/tomcat-operator:v0.0.1 
Sending build context to Docker daemon 111.1 kB
Step 1/3 : FROM docker.io/ericnie2017/helm-operator:latest
 ---> f0d56774da3e
Step 2/3 : COPY helm-charts/ ${HOME}/helm-charts/
 ---> 9f77f7fba44d
Removing intermediate container efd44d601b0a
Step 3/3 : COPY watches.yaml ${HOME}/watches.yaml
 ---> 7469e31336af
Removing intermediate container 73189235ec15
Successfully built 7469e31336af
INFO[0001] Operator build complete.    

build語句會把咱們定製的Operator生成一個鏡像,運行完push到鏡像倉庫讓全集羣能夠訪問。

[root@master tomcat-operator]# docker push registry.example.com/example/tomcat-operator:v0.0.1 
The push refers to a repository [registry.example.com/example/tomcat-operator]
ae10451a67a5: Pushed 
bebcddc5922f: Pushed 
e256e39f5897: Pushed 
d724046711d4: Pushed 
903dc29d7cf3: Pushed 
e79522dce35e: Pushed 
v0.0.1: digest: sha256:a439041a9de91f0fee04f4cd15c554d8a03ec37a286760415b015cbdce7f4315 size: 1569
  • 創建相關的角色,權限和CRD的信息

執行幾個sed操做用於對生成模板的替換操做。

[root@master tomcat-operator]# sed -i 's|REPLACE_IMAGE|registry.example.com/example/tomcat-operator:v0.0.1|g' deploy/operator.yaml
[root@master tomcat-operator]# oc config view --minify -o jsonpath='{.contexts[0].context.namespace}'
default

[root@master tomcat-operator]# sed -i "s|REPLACE_NAMESPACE|default|g" deploy/role_binding.yaml

 

一切就緒,開始建立

oc create -f deploy/service_account.yaml
oc create -f deploy/role.yaml
oc create -f deploy/role_binding.yaml
oc create -f deploy/operator.yaml

由於openshift本身的權限要求比較嚴格,乾脆直接加成集羣管理員了。

[root@master tomcat-operator]# oc adm policy add-cluster-role-to-user cluster-admin system:serviceaccount:default:tomcat-operator 
cluster role "cluster-admin" added: "system:serviceaccount:default:tomcat-operator"

查看一下,已經建立起來了。這時這個Operator類型就做爲一個Pod在容器內運行。

[root@master crds]# oc get deployment
NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
tomcat-operator   1         1         1            1           30m

 

4.建立和刪除tomcat Operator實例

創建實例,先查看一下這個建立的yaml文件,Kind就是咱們指定的類型Tomcat,而下面的值就是從values.yaml而來,能夠覆蓋也能夠不用覆蓋。

[root@master crds]# cat example_v1alpha1_tomcat_cr.yaml
apiVersion: example.com/v1alpha1
kind: Tomcat
metadata:
  name: example-tomcat
spec:
  # Default values copied from <project_dir>/helm-charts/tomcat/values.yaml
  
  # Default values for tomcat.
  # This is a YAML-formatted file.
  # Declare variables to be passed into your templates.
  
  replicaCount: 2
  
  image:
    repository: registry.example.com/tomcat
    tag: 8-slim
    pullPolicy: IfNotPresent
  
  nameOverride: ""
  fullnameOverride: ""
  
  service:
    type: ClusterIP
    port: 8080
  
  ingress:
    enabled: false
    annotations: {}
      # kubernetes.io/ingress.class: nginx
      # kubernetes.io/tls-acme: "true"
    paths: []
    hosts:
      - chart-example.local
    tls: []
    #  - secretName: chart-example-tls
    #    hosts:
    #      - chart-example.local
  
  resources: {}
    # We usually recommend not to specify default resources and to leave this as a conscious
    # choice for the user. This also increases chances charts run on environments with little
    # resources, such as Minikube. If you do want to specify resources, uncomment the following
    # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
    # limits:
    #  cpu: 100m
    #  memory: 128Mi
    # requests:
    #  cpu: 100m
    #  memory: 128Mi
  
  nodeSelector: {}
  tolerations: []
  
  affinity: {}
  

 

 

 

[root@master crds]# oc create  -f example_v1alpha1_tomcat_cr.yaml
tomcat.example.com/example-tomcat created
[root@master crds]# oc get pods 
NAME                                                        READY     STATUS    RESTARTS   AGE
docker-registry-1-gl8jh                                     1/1       Running   6          18d
example-tomcat-1xvukmzvgn1tijep2w61xgm56-69457d7456-fm49d   0/1       Running   0          11s
example-tomcat-1xvukmzvgn1tijep2w61xgm56-69457d7456-twjhk   0/1       Running   0          11s
registry-console-1-6m4cq                                    1/1       Running   2          8d
router-3-7gx4b                                              1/1       Running   2          9d
tomcat-operator-75dc656956-hhnfd                            1/1       Running   0          27m

看到已經運行起來了,可是沒有ready,沒有ready的緣由是readness和liveness的端口在deployment.yaml裏面設置錯了,沒有修改爲8080.

進去查看Pod日誌,已經正常運行。

 

查看一下自定義對象,有一個example-tomcat,包含了2個pod

[root@master crds]# oc get Tomcat
NAME             AGE
example-tomcat   10s

 

刪除實例

[root@master crds]# oc delete -f example_v1alpha1_tomcat_cr.yaml
tomcat.example.com "example-tomcat" deleted

 

好把,這是一個簡單的Helm Operator上手的實驗,隨着Operator的成熟,在OpenShift 4.0版本中已經有不少組件化的部署都基於Operator來實現。

相關文章
相關標籤/搜索