profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Thursday, October 3, 2019

Getting started with operators using operator-sdk

Operators? What are they? Operators is a better way to do kubernetis native deployment of applications. You can read all about it here. I am using RHEL desktop so this is for rhel/centos linux.
There are few ways that you can do it. The hard way is be an expert in golang and write from scratch.
Easiest way for now is use the operator-sdk (by coreOS > redhat > IBM) and there is another way by the kubernetes itself.
Let us stop talking and do it.
1. Install, configure go for operator development
2. Install operator-sdk

Install configure Go

Here I am getting the golang 1.12. You can get the link for the version that you desire from here.

1
2
3
4
5
6
# download go
wget https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz
# extract go binaries from archive
tar -xzf go1.12.7.linux-amd64.tar.gz
# place go binaries appropriately
mv go /usr/local

We have to setup
- GOROOT - location of go binaries
- GOPATH - Go project location
edit your ~./bashrc and add the following

1
2
3
4
5
6
# go binaries location
export GOROOT=/usr/local/go
# go project location (your's varies)
export GOPATH=$HOME/Projects/Proj1
# add to system's path
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Now save and do

1
source ~./bashrc 
to load the new system variables.
Now a command from your terminal 'go version' command will reveal the version of go.

Please also do

1
2
3
go get github.com/golang/dep
cd $GOPATH/src/github.com/golang/dep
go install ./...
to install the dependency manager.

Install Operator-sdk


1
2
3
4
5
6
7
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 v0.4.0
make dep
make install

and you are good to go.
Sometimes you might notice that you will find 'dep' package was not found at your $PATH. Just do this.
1
2
3
4
mkdir -p $GOPATH/bin
cd $GOPATH/bin
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep
If your $GOPATH changes then you might have to do this again.

Now we get into the operator-sdk directory and start with our project/operator.
1. create new project
2. add api
3. add controller

1
2
3
4
5
6
7
8
# make sure you are the in the right directory
cd $GOPATH/src/github.com/operator-framework/operator-sdk
# new operator project
operator-sdk new podset-operator
# add api to it
operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=MyOperator
# add controller
operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=MyOperator

Now that the api and controller are in place. Now check out the pkg/apis/app/v1alpha1/myoperator_types.go here is where you define the spec. To begin with just add this
type PodSetSpec struct {
  Replicas int32 `json:"replicas"`
}type PodSetStatus struct {
  PodNames []string `json:"podNames"`
}
 Whenever we make change to the structure, we have to generate the k8s code by doing
operator-sdk generate k8s
Now we want to build the image and push it to a registry. Currently here I am going to use a public repo, you might want to use your own.

operator-sdk build quay.io/ambig/myoperator
docker push quay.io/ambig/myoperator

Now update our yaml files to use this image.

sed -i 's|REPLACE_IMAGE|quay.io/ambig/myoperator|g' deploy/operator.yaml

Let us deploy it now


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Setup Service Account
$ oc create -f deploy/service_account.yaml
# Setup RBAC
$ oc create -f deploy/role.yaml
$ oc create -f deploy/role_binding.yaml
# Setup the CRD
$ oc create -f deploy/crds/app_v1alpha1_myoperator_crd.yaml
# Deploy the myoperator-operator
$ oc create -f deploy/operator.yaml
# Deploy the custom resource
oc create -f deploy/crds/app_v1alpha1_myoperator_cr.yaml

No comments:

Post a Comment