当前位置: 首页 > 工具软件 > operator-sdk > 使用案例 >

使用operator-sdk创建自己的k8s operator

冯通
2023-12-01

前言

通过operator-sdk我们可以简单的创建自己k8s应用。
官方的文档其实已经很详细了。这篇文章主要记录了,在根据官方文档一步步做的过程当中完整的一个过程。相比官方的文档会更加详细一些。
如果仅仅只是想要使用通过官方的步骤已经创建好的image,我已经有创建好一个,可以通过以下命令获取。

# docker pull feli5/app-operator

前提条件

一台CentOS,物理机,虚拟机都可以。

安装docker

# yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
# yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce docker-ce-cli containerd.io
# systemctl start docker
# systemctl enable docker

如何安装docker

安装go和dep

安装operator-sdk前提是go已经安装好了,并且需要用到dep安装依赖,所以我们需要先安装go和dep。其中gopath是go的工作目录,可以是任意目录,在配置中做相应的修改就行了。

# curl -LO https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
# tar zxf go1.12.5.linux-amd64.tar.gz
# mv go /usr/local/
# mkdir ~/gopath
# vi /root/.bash_profile
GOPATH=/root/gopath
export GOPATH

GOROOT=/usr/local/go
export GOROOT

PATH=$PATH:$HOME/bin:$GOPATH/bin:$GOROOT/bin

# curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

如何安装dep

安装operator-sdk

# yum -y install git
# RELEASE_VERSION=v0.8.0
# curl -OJL https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
# chmod +x operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu && sudo cp operator-# sdk-${RELEASE_VERSION}-x86_64-linux-gnu /usr/local/bin/operator-sdk && rm operator-# sdk-${RELEASE_VERSION}-x86_64-linux-gnu
#
# 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 dep
# make install

安装operator-sdk的官方文档

创建app-operator

安装kubectl

# curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
# chmod +x ./kubectl
# mv ./kubectl /usr/local/bin/kubectl

如何安装kubectl

配置git

# git config --global user.email xxx@xxx.com
# git config --global user.name "xxx"

创建app-operator

# mkdir -p $GOPATH/src/github.com/example-inc/
# cd $GOPATH/src/github.com/example-inc/
# export GO111MODULE=on
# operator-sdk new app-operator
# cd app-operator
# operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService
# operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService
# go mod vendor
# operator-sdk build <your own docker registry>:latest
# docker push <your own docker registry>:latest
# sed -i 's|REPLACE_IMAGE|<your own docker registry>|g' deploy/operator.yaml

配置~/.kube/config,之后创建app-operator

# kubectl create -f deploy/service_account.yaml
# kubectl create -f deploy/role.yaml
# kubectl create -f deploy/role_binding.yaml
# kubectl create -f deploy/crds/app_v1alpha1_appservice_crd.yaml
# kubectl create -f deploy/operator.yaml
# kubectl create -f deploy/crds/app_v1alpha1_appservice_cr.yaml

如何创建app-operator
到此为止我都成功跑完了,但是发现app-operator无法启动起来。
经过调查发现原因是在容器中没有app-operator这个用户。这个问题记录在https://github.com/operator-framework/operator-sdk/issues/1494
临时的解决办法很简单,就是在Dockerfile中把最后一行去掉,然后重新编docker image并上传。之后重新更新一下app-operator,就可以看到app-operator工作啦。

# cat build/Dockerfile
FROM registry.access.redhat.com/ubi7/ubi-minimal:latest

ENV OPERATOR=/usr/local/bin/app-operator \
    USER_UID=1001 \
    USER_NAME=app-operator

COPY build/_output/bin/app-operator ${OPERATOR}

COPY build/bin /usr/local/bin
RUN  /usr/local/bin/user_setup

ENTRYPOINT ["/usr/local/bin/entrypoint"]

# operator-sdk build <your own docker registry>:latest
# docker push <your own docker registry>:latest
# kubectl delete -f deploy/operator.yaml
# kubectl create -f deploy/operator.yaml
# kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/app-operator-75d9c94849-47dxm   1/1     Running   0          6m54s
pod/example-appservice-pod          1/1     Running   0          5m59s

NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/app-operator   ClusterIP   10.108.153.204   <none>        8383/TCP   6m50s
service/kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP    65d

NAME                           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-operator   1         1         1            1           6m56s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/app-operator-75d9c94849   1         1         1       6m56s
 类似资料: