Skip to content

Commit 11d34b6

Browse files
committed
Replaces SSH with kubectl for communicating between
the pgBackRest dedicated repository host and the primary PG database. Specifically, a script has been created that translates any SSH commands issued by pgBackRest into the proper kubectl command. pgBackRest is then configured to call this script when SSH is required, instead of calling SSH directly. This allows kubectl to be utilized as a drop-in replacement for SSH, and therefore prevents the need to install and run SSH on any containers created by the PGO (for instance, SSH no longer needs to be enabled on the dedicated repository host nor the PG primary pod). However, it should be noted that SSH can still be enabled in any backrest container if necessary while the kubectl solution is fully verified and validated. Therefore, at this time SSH is still installed installed in any backrest containers. Additionally, kubectl and the associated yum repo are now installed in any backrest containers to support the use of kubectl instead of SSH.
1 parent 3cb2677 commit 11d34b6

9 files changed

+121
-14
lines changed

bin/pgo-backrest-repo/pgo-backrest-repo.sh

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
# limitations under the License.
1515

1616

17-
function trap_sigterm() {
17+
function trap_sigterm_tail() {
1818
echo "Signal trap triggered, beginning shutdown.."
19-
killall sshd
19+
killall tail
2020
}
2121

22-
trap 'trap_sigterm' SIGINT SIGTERM
22+
function trap_sigterm_sshd() {
23+
echo "Signal trap triggered, beginning shutdown.."
24+
killall sshd
25+
}
2326

24-
CONFIG=/sshd
2527
REPO=/backrestrepo
2628

2729
echo "PGBACKREST env vars are set to:"
2830
set | grep PGBACKREST
2931

30-
echo "CONFIG is.."
31-
ls $CONFIG
3232
echo "REPO is ..."
3333
ls $REPO
3434

@@ -37,14 +37,32 @@ if [ ! -d $PGBACKREST_REPO_PATH ]; then
3737
mkdir -p $PGBACKREST_REPO_PATH
3838
fi
3939

40-
mkdir ~/.ssh/
41-
cp $CONFIG/config ~/.ssh/
42-
#cp $CONFIG/authorized_keys ~/.ssh/
43-
cp $CONFIG/id_rsa /tmp
44-
chmod 400 /tmp/id_rsa ~/.ssh/config
40+
# save a copy of certain pod env vars for pgbackrest ssh cmd wrapper
41+
env | grep "^KUBERNETES" | sed "s/^/export /" >> "/tmp/pod_env.sh"
42+
env | grep "^CLUSTER_NAME" | sed "s/^/export /" >> "/tmp/pod_env.sh"
43+
44+
if [[ "${ENABLE_SSHD}" == "true" ]]
45+
then
46+
trap 'trap_sigterm_sshd' SIGINT SIGTERM
4547

46-
# start sshd which is used by pgbackrest for remote connections
47-
/usr/sbin/sshd -D -f $CONFIG/sshd_config &
48+
CONFIG=/sshd
49+
echo "SSHD CONFIG is.."
50+
ls $CONFIG
51+
52+
mkdir ~/.ssh/
53+
cp $CONFIG/config ~/.ssh/
54+
#cp $CONFIG/authorized_keys ~/.ssh/
55+
cp $CONFIG/id_rsa /tmp
56+
chmod 400 /tmp/id_rsa ~/.ssh/config
57+
58+
# start sshd which is used by pgbackrest for remote connections
59+
/usr/sbin/sshd -D -f $CONFIG/sshd_config &
60+
else
61+
trap 'trap_sigterm_tail' SIGINT SIGTERM
62+
63+
# start a random process to keep the container running
64+
tail -f /dev/null
65+
fi
4866

4967
wait
5068

centos7/Dockerfile.pgo-backrest-repo.centos7

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ ENV PGVERSION="11" PGDG_REPO="pgdg-redhat-repo-latest.noarch.rpm" PGDG_REPO_DISA
1111
# PGDG PostgreSQL Repository
1212
RUN rpm -Uvh https://download.postgresql.org/pub/repos/yum/${PGVERSION}/redhat/rhel-7-x86_64/${PGDG_REPO}
1313

14+
# Kubernetes repository
15+
RUN echo $'[kubernetes] \n\
16+
name=Kubernetes \n\
17+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 \n\
18+
enabled=1 \n\
19+
gpgcheck=1 \n\
20+
repo_gpgcheck=1 \n\
21+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' > /etc/yum.repos.d/kubernetes.repo
22+
1423
RUN yum -y install \
1524
--disablerepo="${PGDG_REPO_DISABLE}" \
1625
--enablerepo="${PGDG_REPO_ENABLE}" \
@@ -20,6 +29,7 @@ RUN yum -y install \
2029
pgbackrest-"${BACKREST_VERSION}" \
2130
procps-ng \
2231
psmisc \
32+
kubectl \
2333
&& yum -y clean all
2434

2535
RUN groupadd pgbackrest -g 2000 && useradd pgbackrest -u 2000 -g 2000
@@ -29,6 +39,7 @@ RUN chmod +x /usr/local/bin/pgo-backrest-repo.sh /usr/local/bin/archive-push-s3.
2939
&& chown -R pgbackrest:pgbackrest /opt/cpm
3040

3141
ADD bin/uid_pgbackrest.sh /opt/cpm/bin
42+
ADD bin/kube-ssh-wrapper.sh /opt/cpm/bin
3243

3344
RUN chmod g=u /etc/passwd && \
3445
chmod g=u /etc/group

centos7/Dockerfile.pgo-backrest-restore.centos7

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ ENV PGVERSION="11" PGDG_REPO="pgdg-redhat-repo-latest.noarch.rpm" PGDG_REPO_DISA
1010

1111
RUN rpm -Uvh https://download.postgresql.org/pub/repos/yum/${PGVERSION}/redhat/rhel-7-x86_64/${PGDG_REPO}
1212

13+
# Kubernetes repository
14+
RUN echo $'[kubernetes] \n\
15+
name=Kubernetes \n\
16+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 \n\
17+
enabled=1 \n\
18+
gpgcheck=1 \n\
19+
repo_gpgcheck=1 \n\
20+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' > /etc/yum.repos.d/kubernetes.repo
21+
1322
RUN yum -y install \
1423
--disablerepo="${PGDG_REPO_DISABLE}" \
1524
--enablerepo="${PGDG_REPO_ENABLE}" \
@@ -19,11 +28,13 @@ RUN yum -y install \
1928
postgresql11-server \
2029
procps-ng \
2130
psmisc \
31+
kubectl \
2232
&& yum -y clean all
2333

2434
RUN mkdir -p /opt/cpm/bin /pgdata && chown -R 26:26 /opt/cpm
2535
ADD bin/pgo-backrest-restore/ /opt/cpm/bin
2636
ADD bin/uid_postgres.sh /opt/cpm/bin
37+
ADD bin/kube-ssh-wrapper.sh /opt/cpm/bin
2738

2839
RUN chmod g=u /etc/passwd && \
2940
chmod g=u /etc/group

conf/postgres-operator/backrest-restore-job.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@
7777
}, {
7878
"name": "PGBACKREST_LOG_PATH",
7979
"value": "/tmp"
80+
}, {
81+
"name": "PGBACKREST_LOG_LEVEL_CONSOLE",
82+
"value": "warn"
83+
}, {
84+
"name": "PGBACKREST_CMD_SSH",
85+
"value": "ssh"
86+
}, {
87+
"name": "CLUSTER_NAME",
88+
"valueFrom": {
89+
"fieldRef": {
90+
"fieldPath": "metadata.labels['pg-cluster']"
91+
}
92+
}
8093
}, {
8194
"name": "NAMESPACE",
8295
"valueFrom": {

conf/postgres-operator/cluster-deployment.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
{{.PgbackrestS3EnvVars}}
8585
"name": "PGHA_DATABASE",
8686
"value": "{{.Database}}"
87+
}, {
88+
"name": "PGHA_CRUNCHYADM",
89+
"value": "true"
8790
}, {
8891
"name": "PATRONI_KUBERNETES_NAMESPACE",
8992
"valueFrom": {

conf/postgres-operator/pgo-backrest-repo-template.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"spec": {
3636
{{.SecurityContext}}
37-
37+
"serviceAccountName": "pgo-backrest",
3838
"containers": [{
3939
"name": "database",
4040
"image": "{{.PGOImagePrefix}}/pgo-backrest-repo:{{.PGOImageTag}}",
@@ -68,6 +68,25 @@
6868
}, {
6969
"name": "PGBACKREST_DB_HOST",
7070
"value": "{{.PGbackrestDBHost}}"
71+
}, {
72+
"name": "PGBACKREST_CMD_SSH",
73+
"value": "ssh"
74+
}, {
75+
"name": "PGBACKREST_LOG_LEVEL_CONSOLE",
76+
"value": "warn"
77+
}, {
78+
"name": "CLUSTER_NAME",
79+
"valueFrom": {
80+
"fieldRef": {
81+
"fieldPath": "metadata.labels['pg-cluster']"
82+
}
83+
}
84+
}, {
85+
"name": "PGO_BACKREST_REPO",
86+
"value": "true"
87+
}, {
88+
"name": "ENABLE_SSHD",
89+
"value": "true"
7190
}],
7291
"volumeMounts": [{
7392
"name": "sshd",

conf/postgres-operator/pgo-pg-role.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
"deletecollection"
2828
]
2929
},
30+
{
31+
"apiGroups":[
32+
""
33+
],
34+
"resources":[
35+
"pods/exec"
36+
],
37+
"verbs":[
38+
"create"
39+
]
40+
},
3041
{
3142
"apiGroups":[
3243
""

rhel7/Dockerfile.pgo-backrest-repo.rhel7

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ ENV PGVERSION="11" BACKREST_VERSION="2.18"
1212
COPY redhat/atomic/pgo_backrest_repo/help.1 /help.1
1313
COPY redhat/atomic/pgo_backrest_repo/help.md /help.md
1414

15+
# Kubernetes repository
16+
RUN echo $'[kubernetes] \n\
17+
name=Kubernetes \n\
18+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 \n\
19+
enabled=1 \n\
20+
gpgcheck=1 \n\
21+
repo_gpgcheck=1 \n\
22+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' > /etc/yum.repos.d/kubernetes.repo
23+
1524
RUN yum -y install \
1625
crunchy-backrest-"${BACKREST_VERSION}" \
1726
hostname \
1827
openssh-clients \
1928
openssh-server \
2029
procps-ng \
2130
psmisc \
31+
kubectl \
2232
&& yum -y clean all
2333

2434
RUN groupadd pgbackrest -g 2000 && useradd pgbackrest -u 2000 -g 2000
@@ -28,6 +38,7 @@ RUN chmod +x /usr/local/bin/pgo-backrest-repo.sh /usr/local/bin/archive-push-s3.
2838
&& chown -R pgbackrest:pgbackrest /opt/cpm
2939

3040
ADD bin/uid_pgbackrest.sh /opt/cpm/bin
41+
ADD bin/kube-ssh-wrapper.sh /opt/cpm/bin
3142

3243
RUN chmod g=u /etc/passwd && \
3344
chmod g=u /etc/group

rhel7/Dockerfile.pgo-backrest-restore.rhel7

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ ENV PGVERSION="11" BACKREST_VERSION="2.18"
1212
COPY redhat/atomic/pgo_backrest_restore/help.1 /help.1
1313
COPY redhat/atomic/pgo_backrest_restore/help.md /help.md
1414

15+
RUN echo $'[kubernetes] \n\
16+
name=Kubernetes \n\
17+
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 \n\
18+
enabled=1 \n\
19+
gpgcheck=1 \n\
20+
repo_gpgcheck=1 \n\
21+
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' > /etc/yum.repos.d/kubernetes.repo
22+
1523
RUN yum -y install \
1624
crunchy-backrest-"${BACKREST_VERSION}" \
1725
openssh-clients \
1826
openssh-server \
1927
postgresql11-server \
2028
procps-ng \
2129
psmisc \
30+
kubectl \
2231
&& yum -y clean all
2332

2433
RUN mkdir -p /opt/cpm/bin /pgdata && chown -R 26:26 /opt/cpm
2534
ADD bin/pgo-backrest-restore/ /opt/cpm/bin
2635
ADD bin/uid_postgres.sh /opt/cpm/bin
36+
ADD bin/kube-ssh-wrapper.sh /opt/cpm/bin
2737

2838
RUN chmod g=u /etc/passwd && \
2939
chmod g=u /etc/group

0 commit comments

Comments
 (0)