久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

kubebuilder怎么安裝使用

185次閱讀
沒有評論

共計(jì) 8145 個(gè)字符,預(yù)計(jì)需要花費(fèi) 21 分鐘才能閱讀完成。

本篇內(nèi)容主要講解“kubebuilder 怎么安裝使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓丸趣 TV 小編來帶大家學(xué)習(xí)“kubebuilder 怎么安裝使用”吧!

kubebuilder

kubebuilder 能幫我們節(jié)省大量工作,讓開發(fā) CRD 和 adminsion webhook 變得異常簡單。

安裝

通過源碼安裝:

git clone https://github.com/kubernetes-sigs/kubebuilder
cd kubebuilder
make build
cp bin/kubebuilder $GOPATH/bin

或者下載二進(jìn)制:

os=$(go env GOOS)
arch=$(go env GOARCH)
# download kubebuilder and extract it to tmp
curl -sL https://go.kubebuilder.io/dl/2.0.0-beta.0/${os}/${arch} | tar -xz -C /tmp/
# move to a long-term location and put it on your path
# (you ll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
sudo mv /tmp/kubebuilder_2.0.0-beta.0_${os}_${arch} /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin

還需要裝下 kustomize 這可是個(gè)渲染 yaml 的神器,讓 helm 顫抖。

go install sigs.k8s.io/kustomize/v3/cmd/kustomize

使用

注意你得先有個(gè) kubernetes 集群,一步安裝走你

創(chuàng)建 CRD

kubebuilder init --domain sealyun.com --license apache2 --owner  fanux 
kubebuilder create api --group infra --version v1 --kind VirtulMachine

安裝 CRD 并啟動(dòng) controller

make install #  安裝 CRD
make run #  啟動(dòng) controller

然后我們就可以看到創(chuàng)建的 CRD 了

# kubectl get crd
NAME AGE
virtulmachines.infra.sealyun.com 52m

來創(chuàng)建一個(gè)虛擬機(jī):

# kubectl apply -f config/samples/
# kubectl get virtulmachines.infra.sealyun.com 
NAME AGE
virtulmachine-sample 49m

看一眼 yaml 文件:

# cat config/samples/infra_v1_virtulmachine.yaml 
apiVersion: infra.sealyun.com/v1
kind: VirtulMachine
metadata:
 name: virtulmachine-sample
spec:
 # Add fields here
 foo: bar

這里僅僅是把 yaml 存到 etcd 里了,我們 controller 監(jiān)聽到創(chuàng)建事件時(shí)啥事也沒干。

把 controller 部署到集群中

make docker-build docker-push IMG=fanux/infra-controller
make deploy

我是連的遠(yuǎn)端的 kubenetes, make docker-build 時(shí) test 過不去,沒有 etcd 的 bin 文件,所以先把 test 關(guān)了。

修改 Makefile:

# docker-build: test
docker-build:

Dockerfile 里的 gcr.io/distroless/static:latest 這個(gè)鏡像你也可能拉不下來,隨意改改就行,我改成了 golang:1.12.7

也有可能構(gòu)建時(shí)有些代碼拉不下來,啟用一下 go mod vendor 把依賴打包進(jìn)去

go mod vendor
如果你本地有些代碼拉不下來,可以用 proxy:

export GOPROXY=https://goproxy.io


再改下 Dockerfile, 注釋掉 download:

修改后:

# Build the manager binary
FROM golang:1.12.7 as builder
WORKDIR /go/src/github.com/fanux/sealvm
# Copy the Go Modules manifests
COPY . . 
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o manager main.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
# FROM gcr.io/distroless/static:latest
FROM golang:1.12.7
WORKDIR /
COPY --from=builder /go/src/github.com/fanux/sealvm/manager .
ENTRYPOINT [/manager]

make deploy 時(shí)報(bào)錯(cuò):Error: json: cannot unmarshal string into Go struct field Kustomization.patches of type types.Patch

把 config/default/kustomization.yaml 中的 patches: 改成 patchesStrategicMerge: 即可

kustomize build config/default 這個(gè)命令就渲染出了 controller 的 yaml 文件,可以體驗(yàn)下

看 你的 controller 已經(jīng)跑起來了:

kubectl get deploy -n sealvm-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
sealvm-controller-manager 1 1 1 0 3m
kubectl get svc -n sealvm-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sealvm-controller-manager-metrics-service ClusterIP 10.98.71.199  none  8443/TCP 4m

開發(fā)增加對象數(shù)據(jù)參數(shù)

看下 config/samples 下面的 yaml 文件:

apiVersion: infra.sealyun.com/v1
kind: VirtulMachine
metadata:
 name: virtulmachine-sample
spec:
 # Add fields here
 foo: bar

這里參數(shù)里有 foo:bar,那我們來加個(gè)虛擬 CPU,內(nèi)存信息:

直接 api/v1/virtulmachine_types.go 即可

// VirtulMachineSpec defines the desired state of VirtulMachine
//  在這里加信息
type VirtulMachineSpec struct {
 // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
 // Important: Run  make  to regenerate code after modifying this file
 CPU string `json: cpu ` //  這是我增加的
 Memory string `json: memory `
// VirtulMachineStatus defines the observed state of VirtulMachine
//  在這里加狀態(tài)信息,比如虛擬機(jī)是啟動(dòng)狀態(tài),停止?fàn)顟B(tài)啥的
type VirtulMachineStatus struct {
 // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
 // Important: Run  make  to regenerate code after modifying this file
}

然后 make 一下:

make   make install   make run

這時(shí)再去渲染一下 controller 的 yaml 就會(huì)發(fā)現(xiàn) CRD 中已經(jīng)帶上 CPU 和內(nèi)存信息了:

kustomize build config/default

properties:
 cpu:
 description:  INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
 Important: Run  make  to regenerate code after modifying this file 
 type: string
 memory:
 type: string

修改一下 yaml:

apiVersion: infra.sealyun.com/v1
kind: VirtulMachine
metadata:
 name: virtulmachine-sample
spec:
 cpu:  1 
 memory:  2G
# kubectl apply -f config/samples 
virtulmachine.infra.sealyun.com  virtulmachine-sample  configured
# kubectl get virtulmachines.infra.sealyun.com virtulmachine-sample -o yaml 
apiVersion: infra.sealyun.com/v1
kind: VirtulMachine
metadata:
 annotations:
 kubectl.kubernetes.io/last-applied-configuration: |
 {apiVersion : infra.sealyun.com/v1 , kind : VirtulMachine , metadata :{ annotations :{}, name : virtulmachine-sample , namespace : default }, spec :{cpu : 1 , memory : 2G}}
 creationTimestamp: 2019-07-26T08:47:34Z
 generation: 2
 name: virtulmachine-sample
 namespace: default
 resourceVersion:  14811698 
 selfLink: /apis/infra.sealyun.com/v1/namespaces/default/virtulmachines/virtulmachine-sample
 uid: 030e2b9a-af82-11e9-b63e-5254bc16e436
spec: #  新的 CRD 已生效
 cpu:  1 
 memory: 2G

Status 同理,就不再贅述了,比如我把 status 里加一個(gè) Create, 表示 controller 要去創(chuàng)建虛擬機(jī)了 (主要一些控制層面的邏輯),創(chuàng)建完了把狀態(tài)改成 Running

Reconcile 唯一需要實(shí)現(xiàn)的接口

controller 把輪訓(xùn)與事件監(jiān)聽都封裝在這一個(gè)接口里了. 你不需要關(guān)心怎么事件監(jiān)聽的.

獲取虛擬機(jī)信息

func (r *VirtulMachineReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {ctx = context.Background()
 _ = r.Log.WithValues(virtulmachine , req.NamespacedName)
 vm :=  v1.VirtulMachine{}
 if err := r.Get(ctx, req.NamespacedName, vm); err != nil { #  獲取 VM 信息
 log.Error(err,  unable to fetch vm)
 } else { fmt.Println(vm.Spec.CPU, vm.Spec.Memory) #  打印 CPU 內(nèi)存信息
 return ctrl.Result{}, nil}

make make install make run 這個(gè)時(shí)候去創(chuàng)建一個(gè)虛擬機(jī) kubectl apply -f config/samples, 日志里就會(huì)輸出 CPU 內(nèi)存了. List 接口同理,我就不贅述了

r.List(ctx,  vms, client.InNamespace(req.Namespace), client.MatchingField(vmkey, req.Name))

更新狀態(tài)

在 status 結(jié)構(gòu)體中加入狀態(tài)字段:

type VirtulMachineStatus struct {Status string `json: status `}

controller 里去更新狀態(tài):

vm.Status.Status =  Running 
if err := r.Status().Update(ctx, vm); err != nil {log.Error(err,  unable to update vm status)
}

如果出現(xiàn):the server could not find the requested resource 這個(gè)錯(cuò)誤,那么在 CRD 結(jié)構(gòu)體上需要加個(gè)注釋 // +kubebuilder:subresource:status:

// +kubebuilder:subresource:status
// +kubebuilder:object:root=true
type VirtulMachine struct {
 metav1.TypeMeta `json: ,inline `
 metav1.ObjectMeta `json: metadata,omitempty `
 Spec VirtulMachineSpec `json: spec,omitempty `
 Status VirtulMachineStatus `json: status,omitempty `
}

這樣就好了

編譯啟動(dòng)后再去 apply 發(fā)現(xiàn)狀態(tài)已經(jīng)變成 running:

# kubectl get virtulmachines.infra.sealyun.com virtulmachine-sample -o yaml
status:
 status: Running

刪除

time.Sleep(time.Second * 10)
if err := r.Delete(ctx, vm); err != nil {log.Error(err,  unable to delete vm  ,  vm , vm)
}

10s 之后我們將 GET 不到

其它接口

Reconcile 結(jié)構(gòu)體聚合了 Client 接口,所以 client 的所有方法都是可以直接調(diào)用,大部分是對 CRD object 的相關(guān)操作

type Client interface {
 Reader
 Writer
 StatusClient
}
// Reader knows how to read and list Kubernetes objects.
type Reader interface {
 // Get retrieves an obj for the given object key from the Kubernetes Cluster.
 // obj must be a struct pointer so that obj can be updated with the response
 // returned by the Server.
 Get(ctx context.Context, key ObjectKey, obj runtime.Object) error
 // List retrieves list of objects for a given namespace and list options. On a
 // successful call, Items field in the list will be populated with the
 // result returned from the server.
 List(ctx context.Context, list runtime.Object, opts ...ListOptionFunc) error
// Writer knows how to create, delete, and update Kubernetes objects.
type Writer interface {
 // Create saves the object obj in the Kubernetes cluster.
 Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error
 // Delete deletes the given obj from Kubernetes cluster.
 Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOptionFunc) error
 // Update updates the given obj in the Kubernetes cluster. obj must be a
 // struct pointer so that obj can be updated with the content returned by the Server.
 Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error
 // Patch patches the given obj in the Kubernetes cluster. obj must be a
 // struct pointer so that obj can be updated with the content returned by the Server.
 Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error
// StatusClient knows how to create a client which can update status subresource
// for kubernetes objects.
type StatusClient interface {Status() StatusWriter
}

到此,相信大家對“kubebuilder 怎么安裝使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是丸趣 TV 網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-04發(fā)表,共計(jì)8145字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 册亨县| 巩留县| 乐清市| 温州市| 阳信县| 玉树县| 绥江县| 东阿县| 常山县| 大名县| 黑水县| 大连市| 酒泉市| 湖州市| 林芝县| 农安县| 涡阳县| 通海县| 哈巴河县| 明溪县| 吉首市| 谢通门县| 临猗县| 利津县| 潍坊市| 武宁县| 梁山县| 临清市| 延吉市| 枞阳县| 勐海县| 临桂县| 金华市| 孟连| 鹤庆县| 慈溪市| 淳化县| 辉南县| 苍溪县| 呼伦贝尔市| 拜泉县|