operator-sdk 实践指南

  1. 环境
  • Ubuntu 20.04
  • go 1.16.2
  • k3s 1.20.4
  • operator 1.15.0

1.下载安装

参考
https://sdk.operatorframework.io/docs/installation/

export ARCH=$(case $(uname -m) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(uname -m) ;; esac)
export OS=$(uname | awk '{print tolower($0)}')
export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/v1.15.0
curl -LO ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH}
chmod +x operator-sdk_${OS}_${ARCH} && sudo mv operator-sdk_${OS}_${ARCH} /usr/local/bin/operator-sdk

查看安装后的版本

operator-sdk version
operator-sdk version: "v1.15.0", commit: "f6326e832a8a5e5453d0ad25e86714a0de2c0fc8", kubernetes version: "1.21", go version: "go1.16.10", GOOS: "linux", GOARCH: "amd64"

快速开始

在$GOPATH目录下面创建一个新目录并初始化

mkdir memcached-operator
cd memcached-operator
operator-sdk init --domain example.com --repo github.com/example/memcached-operator
init.png
  1. 创建 API
operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
image.png
  1. 编译并推镜像
make docker-build  IMG="example.com/memcached-operator:v0.0.1"
image.png

国内直接使用原生的下载,会导致下载文件失败
解决办法是修改dockerfile设置GOPROXY下面ENV开头的两行, 和busybox部分是修改的,添加的位置如下:
Dockerfile

# Build the manager binary
FROM golang:1.16 as builder

WORKDIR /workspace
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -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:nonroot
FROM busybox:1.35.0

WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]

运行效果图


image.png

推镜像前保证已经登录,根据自己得到实际环境修改

make docker-push IMG="example.com/memcached-operator:v0.0.1"

直接部署

make deploy IMG="example.com/memcached-operator:v0.0.1"
kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml

卸载

make undeploy

你可能感兴趣的:(operator-sdk 实践指南)