Skip to content

Commit 0cfa561

Browse files
author
Jeff McCormick
committed
add scaling command and update version numbers
1 parent c90b7fd commit 0cfa561

File tree

13 files changed

+404
-22
lines changed

13 files changed

+404
-22
lines changed

README.asciidoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= PostgreSQL Operator
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44
== Overview
55

@@ -16,6 +16,7 @@ Once the objects are detected, the PostgreSQL Operator enables users to perform
1616
* Create Cluster
1717
* Destroy Cluster
1818
* Backup Cluster
19+
* Scale a Cluster
1920
* Restore Cluster
2021
* Upgrade Cluster
2122
* View PVC
@@ -41,6 +42,7 @@ Some examples of using the command line interface:
4142
pgo show cluster all
4243
pgo show cluster db1 db2 db3
4344
pgo show cluster mycluster
45+
pgo show cluster mycluster --show-secrets=true
4446
----
4547

4648
.Create Cluster
@@ -49,6 +51,12 @@ pgo show cluster mycluster
4951
pgo create cluster mycluster
5052
----
5153

54+
.Scale Cluster
55+
[source,bash]
56+
----
57+
pgo scale mycluster --replica-count=2
58+
----
59+
5260
.Delete a Cluster
5361
[source,bash]
5462
----

client/cmd/scale.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2017 Crunchy Data Solutions, Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
log "github.com/Sirupsen/logrus"
21+
"github.com/crunchydata/postgres-operator/operator/util"
22+
"github.com/crunchydata/postgres-operator/tpr"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var ReplicaCount int
27+
28+
var scaleCmd = &cobra.Command{
29+
Use: "scale",
30+
Short: "Scale a Cluster",
31+
Long: `scale allows you to adjust a Cluster's replica configuration
32+
For example:
33+
34+
pgo scale mycluster --replica-count=1
35+
.`,
36+
Run: func(cmd *cobra.Command, args []string) {
37+
log.Debug("scale called")
38+
if ReplicaCount < 0 {
39+
fmt.Println(`--replica-count command line flag is required`)
40+
} else if len(args) == 0 {
41+
fmt.Println(`You must specify the clusters to scale.`)
42+
} else {
43+
scaleCluster(args)
44+
}
45+
},
46+
}
47+
48+
func init() {
49+
RootCmd.AddCommand(scaleCmd)
50+
51+
scaleCmd.Flags().IntVarP(&ReplicaCount, "replica-count", "r", -1, "The replica count to apply to the clusters")
52+
53+
}
54+
55+
func scaleCluster(args []string) {
56+
//get a list of all clusters
57+
clusterList := tpr.PgClusterList{}
58+
err := Tprclient.Get().
59+
Resource("pgclusters").
60+
Namespace(Namespace).
61+
Do().Into(&clusterList)
62+
if err != nil {
63+
log.Error("error getting list of clusters" + err.Error())
64+
return
65+
}
66+
67+
if len(clusterList.Items) == 0 {
68+
fmt.Println("no clusters found")
69+
return
70+
}
71+
72+
itemFound := false
73+
74+
for _, arg := range args {
75+
log.Debugf(" %s ReplicaCount is %d\n", arg, ReplicaCount)
76+
for _, cluster := range clusterList.Items {
77+
if arg == "all" || cluster.Spec.Name == arg {
78+
itemFound = true
79+
fmt.Printf("scaling %s to %d\n", arg, ReplicaCount)
80+
err = util.ScaleDeployment(Clientset, arg+"-replica", Namespace, ReplicaCount)
81+
if err != nil {
82+
log.Error(err.Error())
83+
}
84+
}
85+
}
86+
if !itemFound {
87+
fmt.Println(arg + " was not found")
88+
}
89+
itemFound = false
90+
91+
}
92+
}

docs/build.asciidoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= PostgreSQL Operator Build and Setup
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44
== Overview
55

@@ -63,7 +63,7 @@ export GOBIN=$GOPATH/bin
6363
export PATH=$PATH:$GOBIN
6464
export COROOT=$GOPATH/src/github.com/crunchydata/postgres-operator
6565
export CO_BASEOS=centos7
66-
export CO_VERSION=1.1.0
66+
export CO_VERSION=1.2.0
6767
export CO_IMAGE_TAG=$CO_BASEOS-$CO_VERSION
6868
....
6969

@@ -80,16 +80,16 @@ cd postgres-operator
8080
At this point if you want to avoid building the images and binary
8181
from source, you can pull down the Docker images as follows:
8282
....
83-
docker pull crunchydata/lspvc:centos7-1.1.0
84-
docker pull crunchydata/postgres-operator:centos7-1.1.0
83+
docker pull crunchydata/lspvc:centos7-1.2.0
84+
docker pull crunchydata/postgres-operator:centos7-1.2.0
8585
....
8686

8787
Then to get the *pgo* client, go to the Releases page and download the tar ball, uncompress
8888
it into your $HOME directory:
8989
....
9090
cd $HOME
91-
wget https://github.com/CrunchyData/postgres-operator/releases/download/v1.1.0/postgres-operator.1.1.0.tar.gz
92-
tar xvzf ./postgres-operator.1.1.0.tar.gz
91+
wget https://github.com/CrunchyData/postgres-operator/releases/download/v1.2.0/postgres-operator.1.2.0.tar.gz
92+
tar xvzf ./postgres-operator.1.2.0.tar.gz
9393
....
9494

9595
Lastly, add the *pgo* client into your PATH.

docs/config.asciidoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= PostgreSQL Operator Configuration
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44

55
This document describes the configuration options
@@ -28,7 +28,7 @@ CLUSTER:
2828
PVC_NAME: crunchy-pvc
2929
PVC_SIZE: 100M
3030
PVC_ACCESS_MODE: ReadWriteMany
31-
CCP_IMAGE_TAG: centos7-9.5-1.4.0
31+
CCP_IMAGE_TAG: centos7-9.5-1.4.1
3232
PORT: 5432
3333
PG_MASTER_USER: master
3434
PG_MASTER_PASSWORD: password
@@ -41,8 +41,8 @@ CLUSTER:
4141
STRATEGY: 1
4242
REPLICAS: 2
4343
PGO:
44-
LSPVC_TEMPLATE: /home/jeffmc/.pgo.lspvc-template.json
45-
CO_IMAGE_TAG: centos7-1.1.0
44+
LSPVC_TEMPLATE: /home/youruserid/.pgo.lspvc-template.json
45+
CO_IMAGE_TAG: centos7-1.2.0
4646
DEBUG: false
4747
....
4848

@@ -132,7 +132,7 @@ The operator will create new PVCs using this naming convention:
132132
*dbname-pvc* where *dbname* is the database name you have specified. For
133133
example, if you run:
134134
....
135-
pgo create database example1
135+
pgo create cluster example1
136136
....
137137

138138
It will result in a PVC being created named *example1-pvc*.

docs/design.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= postgres Operator Design
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44
image::crunchy_logo.png?raw=true[]
55

docs/strategies.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= PostgreSQL Operator Deployment Strategies
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44

55
This document describes the various deployment strategies
@@ -12,7 +12,7 @@ by the postgres operator.
1212
== Strategies
1313

1414
To support different types of deployments, the operator supports
15-
multiple strategy implementations. For Release 1.0, there is
15+
multiple strategy implementations. Currently there is
1616
only a default *cluster* strategy.
1717

1818
In the future, more deployment strategies will be supported

docs/user-guide.asciidoc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= postgres Operator User Guide
2-
v1.1.0, {docdate}
2+
v1.2.0, {docdate}
33

44
This document is meant for users and demonstrates
55
the basic interface of the *pgo* command line interface.
@@ -101,18 +101,22 @@ When you create a Cluster, you will see in the output a variety of Kubernetes ob
101101
Since Postgres is a single-master database by design, the master
102102
Deployment is set to a replica count of 1, it can not scale beyond 1.
103103

104-
The replica Deployment is set to an initial value of 2, you will
105-
see there are 2 replica databases running. Those replica databases
104+
The replica Deployment is set to an initial value of 0, you will
105+
see there are 0 replica databases running. Those replica databases
106106
are in read-only mode, but you can scale up the number of replicas
107-
beyond 2 if you need higher read scaling.
107+
beyond 0 if you need higher read scaling. To set the number of
108+
replicas issue the following command:
109+
....
110+
pgo scale mycluster --replica-count=1
111+
....
108112

109-
There are 2 connections available to the postgres cluster, one is
113+
There are 2 service connections available to the postgres cluster, one is
110114
to the master database which allows read-write SQL processing, and
111115
the other is to the set of read-only replica databases. The replica
112-
service performs round-robin load balancing to the 2 replica databases.
116+
service performs round-robin load balancing to the replica databases.
113117

114118
You can connect to the master database and verify that it is replicating
115-
to the 2 replica databases as follows:
119+
to the replica databases as follows:
116120
....
117121
psql -h 10.107.180.159 -U postgres postgres -c 'table pg_stat_replication'
118122
....

examples/golang/addreplica.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import "fmt"
4+
import "flag"
5+
import "encoding/json"
6+
import log "github.com/Sirupsen/logrus"
7+
8+
//import "k8s.io/client-go/pkg/api/v1"
9+
10+
//import "github.com/crunchydata/postgres-operator/operator/util"
11+
import "k8s.io/client-go/tools/clientcmd"
12+
import "k8s.io/client-go/kubernetes"
13+
14+
//import v1beta1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
15+
import api "k8s.io/client-go/pkg/api"
16+
17+
type ThingSpec struct {
18+
Op string `json:"op"`
19+
Path string `json:"path"`
20+
Value string `json:"value"`
21+
}
22+
23+
func main() {
24+
25+
fmt.Println("secrets...")
26+
kubeconfig := flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file")
27+
28+
flag.Parse()
29+
// uses the current context in kubeconfig
30+
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
31+
if err != nil {
32+
panic(err.Error())
33+
}
34+
// creates the clientset
35+
clientset, err := kubernetes.NewForConfig(config)
36+
if err != nil {
37+
panic(err.Error())
38+
}
39+
40+
deploymentName := "friday-replica"
41+
42+
things := make([]ThingSpec, 1)
43+
things[0].Op = "replace"
44+
things[0].Path = "/spec/replicas"
45+
things[0].Value = "1"
46+
47+
var patchBytes []byte
48+
patchBytes, err = json.Marshal(things)
49+
if err != nil {
50+
log.Error("error in converting patch " + err.Error())
51+
}
52+
log.Debug(string(patchBytes))
53+
54+
_, err = clientset.Deployments("default").Patch(deploymentName, api.JSONPatchType, patchBytes)
55+
if err != nil {
56+
log.Error("error creating master Deployment " + err.Error())
57+
panic(err.Error())
58+
}
59+
log.Info("patch succeeded")
60+
61+
}

examples/golang/createsecret.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
23+
"k8s.io/client-go/kubernetes"
24+
//"k8s.io/client-go/pkg/api"
25+
//"k8s.io/client-go/pkg/api/errors"
26+
27+
//"k8s.io/client-go/pkg/runtime"
28+
//"k8s.io/client-go/pkg/runtime/serializer"
29+
30+
//"k8s.io/client-go/pkg/api/unversioned"
31+
"k8s.io/client-go/pkg/api/v1"
32+
//"k8s.io/client-go/rest"
33+
"k8s.io/client-go/tools/clientcmd"
34+
)
35+
36+
var (
37+
kubeconfig = flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file")
38+
)
39+
40+
func main() {
41+
flag.Parse()
42+
// uses the current context in kubeconfig
43+
var namespace = "default"
44+
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
45+
if err != nil {
46+
panic(err.Error())
47+
}
48+
// creates the clientset
49+
clientset, err := kubernetes.NewForConfig(config)
50+
if err != nil {
51+
panic(err.Error())
52+
}
53+
54+
secret := v1.Secret{}
55+
secret.Name = "pgroot-secret"
56+
//secret.ObjectMeta.Name = "pgroot-secret"
57+
secret.Data = make(map[string][]byte)
58+
secret.Data["username"] = []byte("testuser")
59+
secret.Data["password"] = []byte("mypassword")
60+
61+
_, err = clientset.Secrets(namespace).Create(&secret)
62+
if err != nil {
63+
fmt.Println(err.Error())
64+
} else {
65+
fmt.Printf("careated secret")
66+
}
67+
68+
}

0 commit comments

Comments
 (0)