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

Tuesday, August 6, 2019

Terraform or Cloud Native Templates ?

What is terraform?
tldr. Terraform is a yaml templating engine which does infrastructure as code. You can have an entire or part of your IT infrastructure as some yaml code and deploy it to your private or public cloud.

What is Cloud Native Template?
Azure Resource Manager (ARM), AWS Cloud Formation (CF), Google Cloud Deployment Manager (CDM) are native templating engines just like terraform but mostly in json format.

Psql steps:
  1. provision an sql database
  2. Store secrets generated to some vault (ex:- credentials for the database)
  3. apply a custom configuration
  4. create few users and roles
  5. create few tables
  6. run pre defined psql commands against it
  7. connect it to some application
Endpoints:
  1. AWS
  2. Azure
  3. GCP
Now let us say you are asked to develop 1 terraform template for each of the above endpoints (cloud providers) and also the Cloud Native Templates too.

Why Terraform?
86% Common Code and thus
  • 86% Faster design and discovery
  • 86% Faster development cycle
  • 86% Faster testing
  • 86% Faster deployment
  • 86% Faster UAT (User Acceptance Test)
  • 86% greater mobility between cloud vendors
  • Greater community support
  • Greater availability of code
  • Can write custom features, resources or providers if you are know go very well.
Why not terraform?
  • There are some features of cloud vendors which are not yet exposed in terraform by the vendor

Why Cloud native templates?
  • You are not planning to move your work load to another cloud or you don't mind vendor lock in.
  • You are looking for some features of the cloud providers which aren't yet available on terraform.
Why not Cloud Native Templates?
  • 86% slower design and discovery
  • 86% slower development cycle
  • 86% slower testing
  • 86% slower deployment
  • 86% slower  UAT (User Acceptance Test)
  • 86% lower mobility between cloud vendors
  • Lower community support
  • Lower availability of code
  • Can't write custom features, resources or providers if you are know go very well.