运行 使用生命周期钩子的脚本在 kubernetes 中无法正常工作
Running scripts using lifecycle hook does not work properly in kubernetes
我是 kubernetes 环境的新手。我正在使用 minikube 学习 kubernetes。我有一个情况,我必须在PostgreSQL中配置fdw(国外table概念)。
我正在尝试在 kubernetes 中实现这个 fdw 概念。为此,我有两个 pods meta pod,我们在其中创建外部 table 和服务器,Data pod,其中实际 table 存在。要创建元 pod,您应该有一个包含以下脚本的 Docker 图像。
Script.sh
#!/bin/bash
psql -d metap -U papu -c "CREATE EXTENSION if not exists postgres_fdw;"
psql -d metap -U papu -c "CREATE SERVER if not exists dataserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'datap.default.svc.cluster.local', dbname 'datap', port '5432');"
psql -d metap -U papu -c "CREATE USER MAPPING if not exists FOR ais SERVER dataserver OPTIONS (user 'papu', password 'papu');"
psql -d metap -U papu -c "CREATE FOREIGN TABLE if not exists dream (id integer, val text) SERVER dataserver OPTIONS (schema_name 'public', table_name 'dream');"
下面给出了两个 pods 的 yaml 文件。
p-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: p-config
labels:
app: post
data:
POSTGRES_DB: datap
POSTGRES_USER: papu
POSTGRES_PASSWORD: papu
datap.yaml
apiVersion: v1
kind: Service
metadata:
name: datap
labels:
app: datap
spec:
ports:
- port: 5432
name: datap
clusterIP: None
selector:
app: datap
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: datap
spec:
serviceName: "datap"
replicas: 1
selector:
matchLabels:
app: datap
template:
metadata:
labels:
app: datap
spec:
containers:
- name: datap
image: postgres:latest
envFrom:
- configMapRef:
name: p-config
ports:
- containerPort: 5432
name: datap
volumeMounts:
- name: datap
mountPath: /var/lib/postgresql/data
subPath: datap
volumeClaimTemplates:
- metadata:
name: datap
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 2Gi
我的用例是我已经有数据 pod up 和 运行ning,现在我必须通过 运行ning 动态脚本在 meta pod 中创建外部 table。
为此,我正在使用生命周期挂钩。当我运行此配置时,创建了外部table并建立了fdw连接。 但日志显示生命周期挂钩未执行。这是一个错误吗?还是我的配置有问题?
$ kubectl describe pod metap-0
Name: metap-0
Namespace: default
Priority: 0
Node: minikube/10.0.2.15
Start Time: Fri, 20 Sep 2019 15:50:41 +0530
Labels: app=metap
controller-revision-hash=metap-648ddb5465
statefulset.kubernetes.io/pod-name=metap-0
Annotations: <none>
Status: Running
IP: 172.17.0.10
Controlled By: StatefulSet/metap
Containers:
metap:
Container ID:
Image: <script containing image >:latest
Port: 5432/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 20 Sep 2019 15:51:29 +0530
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 20 Sep 2019 15:51:14 +0530
Finished: Fri, 20 Sep 2019 15:51:15 +0530
Ready: True
Restart Count: 2
Environment: <none>
Mounts:
/var/lib/postgresql/data from metap (rw,path="metap")
/var/run/secrets/kubernetes.io/serviceaccount from default-token
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
mpostgredb:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: metap-metap-0
ReadOnly: false
default-token-r2ncm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-r2ncm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 71s (x2 over 71s) default-scheduler pod has unbound immediate PersistentVolumeClaims
Normal Scheduled 69s default-scheduler Successfully assigned default/metap-0 to minikube
Warning FailedPostStartHook 67s kubelet, minikube Exec lifecycle hook ([/bin/sh -c script.sh]) for Container "metap" in Pod "metap-0_default(6a367766-cd7e-4bab-826a-908e33622bcf)" failed - error: command '/bin/sh -c script.sh' exited with 2: psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
, message: "psql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\n"
Warning FailedPostStartHook 35s kubelet, minikube Exec lifecycle hook ([/bin/sh -c script.sh]) for Container "metap" in Pod "metap-0_default(6a367766-cd7e-4bab-826a-908e33622bcf)" failed - error: command '/bin/sh -c script.sh' exited with 1: psql: FATAL: the database system is starting up
psql: FATAL: the database system is starting up
ERROR: server "dataserver" does not exist
ERROR: server "dataserver" does not exist
, message: "psql: FATAL: the database system is starting up\npsql: FATAL: the database system is starting up\nERROR: server \"dataserver\" does not exist\nERROR: server \"dataserver\" does not exist\n"
Normal Killing 35s (x2 over 67s) kubelet, minikube FailedPostStartHook
Warning BackOff 33s (x2 over 34s) kubelet, minikube Back-off restarting failed container
Normal Created 21s (x3 over 68s) kubelet, minikube Created container metap
Normal Started 21s (x3 over 68s) kubelet, minikube Started container metap
Normal Pulling 21s (x3 over 68s) kubelet, minikube Pulling image " <script containing image >:latest"
Normal Pulled 21s (x3 over 68s) kubelet, minikube Successfully pulled image " <script containing image >:latest"
$ kubectl logs metap-0
2019-09-20 10:21:29.500 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-09-20 10:21:29.500 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-09-20 10:21:29.502 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-09-20 10:21:29.514 UTC [22] LOG: database system was shut down at 2019-09-20 10:21:15 UTC
2019-09-20 10:21:29.518 UTC [1] LOG: database system is ready to accept connections
kubectl 版本
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
*** Minikube 版本 ***
minikube version: v1.3.1
commit: ca60a424ce69a4d79f502650199ca2b52f29e631
我已经复现了你的问题,发现问题出在生命周期钩子配置上。
脚本在数据库准备就绪之前执行,这就是它抛出错误的原因:
command '/bin/sh -c script.sh' exited with 2: psql: could not connect to server: No such file or directory,
failed - error: command '/bin/sh -c script.sh' exited with 1: psql: FATAL: the database system is starting up
您应该在脚本的``开头添加 sleep
以便为数据库提供一些准备时间。
示例:
#!/bin/bash
sleep 5
psql -d metap -U papu -c "CREATE EXTENSION if not exists postgres_fdw;"
psql -d metap -U papu -c "CREATE SERVER if not exists dataserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'datap.default.svc.cluster.local', dbname 'datap', port '5432');"
psql -d metap -U papu -c "CREATE USER MAPPING if not exists FOR ais SERVER dataserver OPTIONS (user 'papu', password 'papu');"
psql -d metap -U papu -c "CREATE FOREIGN TABLE if not exists dream (id integer, val text) SERVER dataserver OPTIONS (schema_name 'public', table_name 'dream');
使用 init containers 你可以检查第二个容器是否已经启动,然后 运行ning 然后 运行 脚本
spec:
initContainers:
- name: check-second-ready
image: postgres
command: ['sh', '-c',
'until pg_isready -h connection/url/to/second/conatiner -p 5432;
do echo waiting for database; sleep 2; done;']
containers:
first container config...
------
我是 kubernetes 环境的新手。我正在使用 minikube 学习 kubernetes。我有一个情况,我必须在PostgreSQL中配置fdw(国外table概念)。
我正在尝试在 kubernetes 中实现这个 fdw 概念。为此,我有两个 pods meta pod,我们在其中创建外部 table 和服务器,Data pod,其中实际 table 存在。要创建元 pod,您应该有一个包含以下脚本的 Docker 图像。
Script.sh
#!/bin/bash
psql -d metap -U papu -c "CREATE EXTENSION if not exists postgres_fdw;"
psql -d metap -U papu -c "CREATE SERVER if not exists dataserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'datap.default.svc.cluster.local', dbname 'datap', port '5432');"
psql -d metap -U papu -c "CREATE USER MAPPING if not exists FOR ais SERVER dataserver OPTIONS (user 'papu', password 'papu');"
psql -d metap -U papu -c "CREATE FOREIGN TABLE if not exists dream (id integer, val text) SERVER dataserver OPTIONS (schema_name 'public', table_name 'dream');"
下面给出了两个 pods 的 yaml 文件。
p-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: p-config
labels:
app: post
data:
POSTGRES_DB: datap
POSTGRES_USER: papu
POSTGRES_PASSWORD: papu
datap.yaml
apiVersion: v1
kind: Service
metadata:
name: datap
labels:
app: datap
spec:
ports:
- port: 5432
name: datap
clusterIP: None
selector:
app: datap
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: datap
spec:
serviceName: "datap"
replicas: 1
selector:
matchLabels:
app: datap
template:
metadata:
labels:
app: datap
spec:
containers:
- name: datap
image: postgres:latest
envFrom:
- configMapRef:
name: p-config
ports:
- containerPort: 5432
name: datap
volumeMounts:
- name: datap
mountPath: /var/lib/postgresql/data
subPath: datap
volumeClaimTemplates:
- metadata:
name: datap
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 2Gi
我的用例是我已经有数据 pod up 和 运行ning,现在我必须通过 运行ning 动态脚本在 meta pod 中创建外部 table。
为此,我正在使用生命周期挂钩。当我运行此配置时,创建了外部table并建立了fdw连接。 但日志显示生命周期挂钩未执行。这是一个错误吗?还是我的配置有问题?
$ kubectl describe pod metap-0
Name: metap-0
Namespace: default
Priority: 0
Node: minikube/10.0.2.15
Start Time: Fri, 20 Sep 2019 15:50:41 +0530
Labels: app=metap
controller-revision-hash=metap-648ddb5465
statefulset.kubernetes.io/pod-name=metap-0
Annotations: <none>
Status: Running
IP: 172.17.0.10
Controlled By: StatefulSet/metap
Containers:
metap:
Container ID:
Image: <script containing image >:latest
Port: 5432/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 20 Sep 2019 15:51:29 +0530
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 20 Sep 2019 15:51:14 +0530
Finished: Fri, 20 Sep 2019 15:51:15 +0530
Ready: True
Restart Count: 2
Environment: <none>
Mounts:
/var/lib/postgresql/data from metap (rw,path="metap")
/var/run/secrets/kubernetes.io/serviceaccount from default-token
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
mpostgredb:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: metap-metap-0
ReadOnly: false
default-token-r2ncm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-r2ncm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 71s (x2 over 71s) default-scheduler pod has unbound immediate PersistentVolumeClaims
Normal Scheduled 69s default-scheduler Successfully assigned default/metap-0 to minikube
Warning FailedPostStartHook 67s kubelet, minikube Exec lifecycle hook ([/bin/sh -c script.sh]) for Container "metap" in Pod "metap-0_default(6a367766-cd7e-4bab-826a-908e33622bcf)" failed - error: command '/bin/sh -c script.sh' exited with 2: psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
, message: "psql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\npsql: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\n"
Warning FailedPostStartHook 35s kubelet, minikube Exec lifecycle hook ([/bin/sh -c script.sh]) for Container "metap" in Pod "metap-0_default(6a367766-cd7e-4bab-826a-908e33622bcf)" failed - error: command '/bin/sh -c script.sh' exited with 1: psql: FATAL: the database system is starting up
psql: FATAL: the database system is starting up
ERROR: server "dataserver" does not exist
ERROR: server "dataserver" does not exist
, message: "psql: FATAL: the database system is starting up\npsql: FATAL: the database system is starting up\nERROR: server \"dataserver\" does not exist\nERROR: server \"dataserver\" does not exist\n"
Normal Killing 35s (x2 over 67s) kubelet, minikube FailedPostStartHook
Warning BackOff 33s (x2 over 34s) kubelet, minikube Back-off restarting failed container
Normal Created 21s (x3 over 68s) kubelet, minikube Created container metap
Normal Started 21s (x3 over 68s) kubelet, minikube Started container metap
Normal Pulling 21s (x3 over 68s) kubelet, minikube Pulling image " <script containing image >:latest"
Normal Pulled 21s (x3 over 68s) kubelet, minikube Successfully pulled image " <script containing image >:latest"
$ kubectl logs metap-0
2019-09-20 10:21:29.500 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-09-20 10:21:29.500 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-09-20 10:21:29.502 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-09-20 10:21:29.514 UTC [22] LOG: database system was shut down at 2019-09-20 10:21:15 UTC
2019-09-20 10:21:29.518 UTC [1] LOG: database system is ready to accept connections
kubectl 版本
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
*** Minikube 版本 ***
minikube version: v1.3.1
commit: ca60a424ce69a4d79f502650199ca2b52f29e631
我已经复现了你的问题,发现问题出在生命周期钩子配置上。
脚本在数据库准备就绪之前执行,这就是它抛出错误的原因:
command '/bin/sh -c script.sh' exited with 2: psql: could not connect to server: No such file or directory,
failed - error: command '/bin/sh -c script.sh' exited with 1: psql: FATAL: the database system is starting up
您应该在脚本的``开头添加 sleep
以便为数据库提供一些准备时间。
示例:
#!/bin/bash
sleep 5
psql -d metap -U papu -c "CREATE EXTENSION if not exists postgres_fdw;"
psql -d metap -U papu -c "CREATE SERVER if not exists dataserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'datap.default.svc.cluster.local', dbname 'datap', port '5432');"
psql -d metap -U papu -c "CREATE USER MAPPING if not exists FOR ais SERVER dataserver OPTIONS (user 'papu', password 'papu');"
psql -d metap -U papu -c "CREATE FOREIGN TABLE if not exists dream (id integer, val text) SERVER dataserver OPTIONS (schema_name 'public', table_name 'dream');
使用 init containers 你可以检查第二个容器是否已经启动,然后 运行ning 然后 运行 脚本
spec:
initContainers:
- name: check-second-ready
image: postgres
command: ['sh', '-c',
'until pg_isready -h connection/url/to/second/conatiner -p 5432;
do echo waiting for database; sleep 2; done;']
containers:
first container config...
------