【发布时间】:2022-08-03 02:24:19
【问题描述】:
我正在尝试为我的 mysql 数据库创建一个 StatefulSet 并让它与我的 pod 进行通信。我为此创建了一个部署,但在它的生命周期中,它正在擦除我的数据库。我的问题是我的数据库在 mysql root 用户中使用私有图像和密码,我将发布我的 StatefulSet :
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
app.kubernetes.io/name: mysql
serviceName: mysql
replicas: 3
template:
metadata:
labels:
app: mysql
app.kubernetes.io/name: mysql
spec:
initContainers:
- name: init-mysql
image: rafaelribeirosouza86/shopping:myql
command:
- bash
- \"-c\"
- |
set -ex
# Generate mysql server-id from pod ordinal index.
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
# Add an offset to avoid reserved server-id=0 value.
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
# Copy appropriate conf.d files from config-map to emptyDir.
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/primary.cnf /mnt/conf.d/
else
cp /mnt/config-map/replica.cnf /mnt/conf.d/
fi
volumeMounts:
- name: conf
mountPath: /mnt/conf.d
- name: config-map
mountPath: /mnt/config-map
- name: clone-mysql
image: gcr.io/google-samples/xtrabackup:1.0
command:
- bash
- \"-c\"
- |
set -ex
# Skip the clone if data already exists.
[[ -d /var/lib/mysql/mysql ]] && exit 0
# Skip the clone on primary (ordinal index 0).
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
[[ $ordinal -eq 0 ]] && exit 0
# Clone data from previous peer.
ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
# Prepare the backup.
xtrabackup --prepare --target-dir=/var/lib/mysql
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
imagePullSecrets:
- name: regcred
containers:
- name: mysql
image: rafaelribeirosouza86/shopping:myql
imagePullPolicy: Always
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
resources:
requests:
cpu: 500m
memory: 1Gi
livenessProbe:
exec:
command: [\"mysqladmin\", \"-uroot\", \"-p$MYSQL_ROOT_PASSWORD\", \"ping\"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
# Check we can execute queries over TCP (skip-networking is off).
#command: [\"mysql\", \"-h\", \"127.0.0.1\", \"-e\", \"SELECT 1\"]
command: [\"mysql\", \"-h\", \"127.0.0.1\",\"-uroot\",\"-p$MYSQL_ROOT_PASSWORD\", \"-e\", \"SELECT 1\"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
- name: xtrabackup
image: gcr.io/google-samples/xtrabackup:1.0
ports:
- name: xtrabackup
containerPort: 3307
command:
- bash
- \"-c\"
- |
set -ex
cd /var/lib/mysql
# Determine binlog position of cloned data, if any.
if [[ -f xtrabackup_slave_info && \"x$(<xtrabackup_slave_info)\" != \"x\" ]]; then
# XtraBackup already generated a partial \"CHANGE MASTER TO\" query
# because we\'re cloning from an existing replica. (Need to remove the tailing semicolon!)
cat xtrabackup_slave_info | sed -E \'s/;$//g\' > change_master_to.sql.in
# Ignore xtrabackup_binlog_info in this case (it\'s useless).
rm -f xtrabackup_slave_info xtrabackup_binlog_info
elif [[ -f xtrabackup_binlog_info ]]; then
# We\'re cloning directly from primary. Parse binlog position.
[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
rm -f xtrabackup_binlog_info xtrabackup_slave_info
echo \"CHANGE MASTER TO MASTER_LOG_FILE=\'${BASH_REMATCH[1]}\',\\
MASTER_LOG_POS=${BASH_REMATCH[2]}\" > change_master_to.sql.in
fi
# Check if we need to complete a clone by starting replication.
if [[ -f change_master_to.sql.in ]]; then
echo \"Waiting for mysqld to be ready (accepting connections)\"
until mysql -h 127.0.0.1 -e \"SELECT 1\"; do sleep 1; done
echo \"Initializing replication from clone position\"
mysql -h 127.0.0.1 \\
-e \"$(<change_master_to.sql.in), \\
MASTER_HOST=\'mysql-0.mysql\', \\
MASTER_USER=\'root\', \\
MASTER_PASSWORD=\'$MYSQL_ROOT_PASSWORD\', \\
MASTER_CONNECT_RETRY=10; \\
START SLAVE;\" || exit 1
# In case of container restart, attempt this at-most-once.
mv change_master_to.sql.in change_master_to.sql.orig
fi
# Start a server to send backups when requested by peers.
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \\
\"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root --password=$MYSQL_ROOT_PASSWORD\"
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
resources:
requests:
cpu: 100m
memory: 100Mi
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
imagePullSecrets:
- name: regcred
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [\"ReadWriteOnce\"]
resources:
requests:
storage: 10Gi
这是一个示例,与 link 进行了一些更改
它正常创建但是当我给出一个kubectl 获取 pod它给了我以下错误:初始化:CrashLoopBackOff所以我试着看kubectl 描述 pod mysql-0
在机器上
Normal Created 13s (x3 over 35s) kubelet Created container init-mysql
Normal Started 11s (x3 over 33s) kubelet Started container init-mysql
Warning BackOff 9s (x4 over 27s) kubelet Back-off restarting failed container
在kubectl 描述 sts mysql:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m4s statefulset-controller create Pod mysql-0 in StatefulSet mysql successful
有人知道这个错误吗?
-
你能粘贴 kubectl logs --all-containers mysql-0 的输出吗?
-
每次都会给出不同的错误
++ hostname bash: line 2: hostname: command not found + [[ \'\' =~ -([0-9]+)$ ]] + exit 1 Error from server (BadRequest): container \"clone-mysql\" in pod \"mysql-0\" is waiting to start: PodInitializingError from server (BadRequest): container \"xtrabackup\" in pod \"mysql-0\" is waiting to start: PodInitializing谢谢 -
我运行了
kubectl describe pod mysql-0,它弹出Status: Is Pendingdocker 桌面有问题吗? -
您的 bash 脚本引发错误,退出代码为 1。如果任何 initContainers 失败,Pod 将不会启动。
-
我从网站kubernetes.io/docs/tasks/run-application/… 复制的可能会发生什么,但它仍然给出错误
标签: mysql kubernetes docker-desktop