postgres-crd-v2

command
v0.0.0-...-de64a1c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2020 License: Apache-2.0 Imports: 32 Imported by: 0

README

==================
Postgres Operator
==================

This is a Kubernetes Operator for Postgres.

The goal of this Operator is to support various life-cycle actions 
for a Postgres instance, such as:

- Create user and password when Postgres instance is created
- Create database at the time of Postgres instance creation
- Initialize the database with some data
- Create database on an already created Postgres instance
- Modify user password on an existing Postgres instance
- etc.

This Operator is implemented as Kubernetes Custom Resource Definition (CRD). 
It reduces out-of-band automation that you have to implement for provisioning
a Postgres instance on Kubernetes and managing its various life-cycle actions.


How does it work?
=================

A new 'kind' named 'Postgres' is defined (see artifacts/examples/crd.yaml).

The Operator registers Postgres CRD at start up (in main.go).

The Custom Resource Controller (controller.go) listens for the creation of resources
of the 'Postgres' kind (e.g.: artifacts/examples/initializeclient.yaml).
In the spec of a Postgres resource you can define 

- Databases that you want created using the 'database' attribute
- Users with their passwords that you want created using the 'users' attribute
- The 'initcommands' attribute should be used to specify any table creation and
  data insert commands. See artifacts/examples/initializeclient.yaml for example.

The controller handles Postgres resource creation event by creating a 
Kubernetes Deployment with the Postgres image specified in the CRD definition.
It exposes this Deployment using a Kubernetes Service.
Currently the created Service is of type NodePort as it makes it easy to test
the controller on Minikube. In real deployments this can be changed to LoadBalancer
type of Service. It is also possible to use an Ingress resource to expose the
Service at some path instead of at an IP address.

The Deployment should be changed to a Stateful Set in real deployments.


How to test?
============

Pre-requisite step:
-------------------
1) Install Go's dep dependency management tool:
   https://github.com/golang/dep

2) Install Postgres client:

- brew install postgresql

- sudo apt-get install postgresql-client


Conceptual Steps:
------------------

One time steps:

- Run the Postgres Operator

Steps that will be run multiple times for multiple customers:

- Create Postgres custom resources


Actual steps:
--------------
0) If using Minikube, enable using local docker images:
 
   - eval $(minikube docker-env)

1) Clone this repository and put it inside 'src' directory of your GOPATH
   at following location:

   $GOPATH/src/github.com/cloud-ark/kubeplus

2) Install dependencies:

   - cd $GOPATH/src/github.com/cloud-ark/kubeplus/postgres-crd-v2

   - dep ensure

3) In one shell window run the Postgres Operator

   - cd $GOPATH/src/github.com/cloud-ark/kubeplus/postgres-crd-v2

   - Follow either one of the following 2 options:

     A] Deploy the controller as a Deployment in the cluster using
        controller Docker image built locally
     
         - ./build-local-deploy-artifacts.sh
     
         - cd artifacts/deployment

	 - kubectl apply -f default-sa.yaml

         - kubectl create -f deployment-minikube.yaml

     B] Deploy the controller with Helm chart (here the controller
        Docker image is pulled from Docker hub)

        - helm install ./postgres-crd-v2-chart

4) In another shell window check that Postgres CRD has been registered.

   - kubectl get customresourcedefinition

   If it is not registered you can manually register it as follows:

   - cd $GOPATH/src/github.com/cloud-ark/kubeplus/postgres-crd-v2

   - kubectl create -f artifacts/examples/crd.yaml


5) In the second window create Postgres custom resource

   - kubectl apply -f artifacts/examples/initializeclient.yaml

   - kubectl describe postgres client25

   - Verify (see below)

6) Test Life-cycle actions (execute and verify)

   - kubectl apply -f artifacts/examples/add-user.yaml

   - kubectl apply -f artifacts/examples/delete-user.yaml 

   - kubectl apply -f artifacts/examples/modify-password.yaml

   - kubectl apply -f artifacts/examples/add-db.yaml

   - kubectl apply -f artifacts/examples/delete-db.yaml

7) Clean up

   - kubectl get deployments

   - kubectl delete deployments ...

   - helm list

   - helm delete ...

   - ./deletecrds.sh ...

   
Verify:
--------
1) kubectl get crd

2) kubectl get postgres client25

3) kubectl describe postgres client25

4) psql -h <IP of the Host> -p <port> -U <username> -d <db-name>
   - When prompted for password, enter <password>
   - IP: Minikube IP (find using 'minikube ip' command)
   - port: Port of the exposed Service
   - username: Name of the user from artifacts/examples/initializeclient.yaml
   - db-name: Name of the database from setupCommands artifacts/examples/initializeclient.yaml
   - password: Value of password from setupCommands artifacts/examples/initializeclient.yaml


Suggestions/Issues:
====================

Suggestions to improve this CRD are welcome. Please submit a Pull request, or
give your suggestions here:

https://github.com/cloud-ark/kubeplus/issues

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
client-gen
client-gen makes the individual typed clients using gengo.
client-gen makes the individual typed clients using gengo.
client-gen/generators
Package generators has the generators for the client-gen utility.
Package generators has the generators for the client-gen utility.
conversion-gen
conversion-gen is a tool for auto-generating Conversion functions.
conversion-gen is a tool for auto-generating Conversion functions.
deepcopy-gen
deepcopy-gen is a tool for auto-generating DeepCopy functions.
deepcopy-gen is a tool for auto-generating DeepCopy functions.
defaulter-gen
defaulter-gen is a tool for auto-generating Defaulter functions.
defaulter-gen is a tool for auto-generating Defaulter functions.
go-to-protobuf
go-to-protobuf generates a Protobuf IDL from a Go struct, respecting any existing IDL tags on the Go struct.
go-to-protobuf generates a Protobuf IDL from a Go struct, respecting any existing IDL tags on the Go struct.
go-to-protobuf/protobuf
go-to-protobuf generates a Protobuf IDL from a Go struct, respecting any existing IDL tags on the Go struct.
go-to-protobuf generates a Protobuf IDL from a Go struct, respecting any existing IDL tags on the Go struct.
go-to-protobuf/protoc-gen-gogo
Package main defines the protoc-gen-gogo binary we use to generate our proto go files, as well as takes dependencies on the correct gogo/protobuf packages for godeps.
Package main defines the protoc-gen-gogo binary we use to generate our proto go files, as well as takes dependencies on the correct gogo/protobuf packages for godeps.
import-boss
import-boss enforces import restrictions in a given repository.
import-boss enforces import restrictions in a given repository.
openapi-gen
This package generates openAPI definition file to be used in open API spec generation on API servers.
This package generates openAPI definition file to be used in open API spec generation on API servers.
set-gen
set-gen is an example usage of gengo.
set-gen is an example usage of gengo.
pkg
apis/postgrescontroller/v1
Package v1 is the v1 version of the API.
Package v1 is the v1 version of the API.
client/clientset/versioned
This package has the automatically generated clientset.
This package has the automatically generated clientset.
client/clientset/versioned/fake
This package has the automatically generated fake clientset.
This package has the automatically generated fake clientset.
client/clientset/versioned/scheme
This package contains the scheme of the automatically generated clientset.
This package contains the scheme of the automatically generated clientset.
client/clientset/versioned/typed/postgrescontroller/v1
This package has the automatically generated typed clients.
This package has the automatically generated typed clients.
client/clientset/versioned/typed/postgrescontroller/v1/fake
Package fake has the automatically generated clients.
Package fake has the automatically generated clients.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL