Repository navigation
Expand file tree
/
Copy pathmanage-postgres.sh
More file actions
executable file
Β·151 lines (132 loc) Β· 4.34 KB
/
Copy pathmanage-postgres.sh
File metadata and controls
executable file
Β·151 lines (132 loc) Β· 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# PostgreSQL Operator Management Script
set -e
NAMESPACE="postgres-operator-deployment"
function usage() {
echo "Usage: $0 [command] [options]"
echo ""
echo "Commands:"
echo " list - List all PostgreSQL clusters"
echo " status <cluster> - Show cluster status"
echo " connect <cluster> - Get connection info for cluster"
echo " users <cluster> - List users and their credentials"
echo " logs <cluster> - Show cluster logs"
echo " scale <cluster> <n> - Scale cluster to n instances"
echo " backup <cluster> - Trigger manual backup"
echo ""
echo "Examples:"
echo " $0 list"
echo " $0 status dev-postgres"
echo " $0 connect dev-postgres"
echo " $0 scale prod-postgres 5"
}
function list_clusters() {
echo "π PostgreSQL Clusters:"
kubectl get postgresql -n $NAMESPACE -o custom-columns="NAME:.metadata.name,TEAM:.spec.teamId,INSTANCES:.spec.numberOfInstances,VERSION:.spec.postgresql.version,STATUS:.status.PostgreSQLStatus" 2>/dev/null || echo "No clusters found"
}
function cluster_status() {
local cluster=$1
if [[ -z "$cluster" ]]; then
echo "β Cluster name required"
exit 1
fi
echo "π Status for cluster: $cluster"
kubectl get postgresql $cluster -n $NAMESPACE -o yaml
echo ""
echo "π¦ Pods:"
kubectl get pods -n $NAMESPACE -l cluster-name=$cluster
echo ""
echo "πΎ PVCs:"
kubectl get pvc -n $NAMESPACE -l cluster-name=$cluster
}
function connect_info() {
local cluster=$1
if [[ -z "$cluster" ]]; then
echo "β Cluster name required"
exit 1
fi
echo "π Connection information for cluster: $cluster"
echo ""
echo "π Service endpoints:"
kubectl get svc -n $NAMESPACE -l cluster-name=$cluster
echo ""
echo "π User credentials (secrets):"
kubectl get secrets -n $NAMESPACE -l cluster-name=$cluster | grep -E "(username|password)"
echo ""
echo "π‘ To get a specific user's password:"
echo "kubectl get secret <cluster>.<username> -n $NAMESPACE -o jsonpath='{.data.password}' | base64 -d"
echo ""
echo "π Connection string example:"
echo "psql -h $cluster.$NAMESPACE.svc.cluster.local -U <username> -d <database>"
}
function list_users() {
local cluster=$1
if [[ -z "$cluster" ]]; then
echo "β Cluster name required"
exit 1
fi
echo "π₯ Users for cluster: $cluster"
kubectl get secrets -n $NAMESPACE -l cluster-name=$cluster | grep -v postgres-exporter | grep -v "^NAME"
echo ""
echo "π‘ To get credentials:"
echo "kubectl get secret $cluster.<username> -n $NAMESPACE -o jsonpath='{.data.username}' | base64 -d"
echo "kubectl get secret $cluster.<username> -n $NAMESPACE -o jsonpath='{.data.password}' | base64 -d"
}
function show_logs() {
local cluster=$1
if [[ -z "$cluster" ]]; then
echo "β Cluster name required"
exit 1
fi
echo "π Logs for cluster: $cluster"
kubectl logs -n $NAMESPACE -l cluster-name=$cluster -c postgres --tail=50
}
function scale_cluster() {
local cluster=$1
local instances=$2
if [[ -z "$cluster" || -z "$instances" ]]; then
echo "β Cluster name and instance count required"
exit 1
fi
echo "βοΈ Scaling cluster $cluster to $instances instances..."
kubectl patch postgresql $cluster -n $NAMESPACE --type='merge' -p="{\"spec\":{\"numberOfInstances\":$instances}}"
echo "β
Scaling initiated. Check status with: $0 status $cluster"
}
function trigger_backup() {
local cluster=$1
if [[ -z "$cluster" ]]; then
echo "β Cluster name required"
exit 1
fi
echo "πΎ Triggering backup for cluster: $cluster"
kubectl annotate postgresql $cluster -n $NAMESPACE zalando.org/manual-backup="$(date +%Y%m%d-%H%M%S)"
echo "β
Backup initiated. Check logs for progress."
}
# Main script logic
case "$1" in
list)
list_clusters
;;
status)
cluster_status "$2"
;;
connect)
connect_info "$2"
;;
users)
list_users "$2"
;;
logs)
show_logs "$2"
;;
scale)
scale_cluster "$2" "$3"
;;
backup)
trigger_backup "$2"
;;
*)
usage
exit 1
;;
esac