我想在容器的MySQL上设置初始数据。 在docker-compose.yml中,这样的代码可以在运行容器时创建初始数据。
#RevWrapper {
clear: both;
margin: auto;
max-width: 1232px;
display: block;
padding-left: calc((100% - 1232px)/2);
padding-right: calc((100% - 1232px)/2);
background-color: #006666;
padding-bottom: 2%;
}
#rev1 {
clear: both;
float: left;
width: 33.3%;
display: block;
padding-bottom: 2%;
padding-top: 2%;
}
#rev2 {
clear: none;
float: left;
width: 33.3%;
display: block;
padding-bottom: 2%;
padding-top: 2%;
}
#rev3 {
clear: none;
float: left;
width: 33.3%;
display: block;
padding-bottom: 2%;
padding-top: 2%;
}
.revimagecenter {
display: block;
width: 85%;
margin-left: auto;
margin-right: auto
}
但是,如何在运行时在Kubernetes上创建初始数据?
答案 0 :(得分:6)
根据MySQL Docker镜像README,与容器启动时数据初始化相关的部分是确保所有初始化文件都挂载到容器的/docker-entrypoint-initdb.d
文件夹中。
您可以在ConfigMap
中定义初始数据,并将相应的卷装入您的广告连播中,如下所示:
apiVersion: extensions/v1beta1
kind: Pod
metadata:
name: mysql
spec:
containers:
- name: mysql
image: mysql
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-initdb
mountPath: /docker-entrypoint-initdb.d
volumes:
- name: mysql-initdb
configMap:
name: mysql-initdb-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-initdb-config
data:
initdb.sql: |
CREATE TABLE friends (id INT, name VARCHAR(256), age INT, gender VARCHAR(3));
INSERT INTO friends VALUES (1, 'John Smith', 32, 'm');
INSERT INTO friends VALUES (2, 'Lilian Worksmith', 29, 'f');
INSERT INTO friends VALUES (3, 'Michael Rupert', 27, 'm');
答案 1 :(得分:0)
你需要像这样创建pv和pvclaim然后部署mysql数据库
kind: PersistentVolume
apiVersion: v1
metadata:
name: sfg-dev-mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sfg-dev-mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
创建秘密:
kubectl create secret generic mysql-secret --from-literal=mysql-root-password=kube1234 --from-literal=mysql-user=testadm --from-literal=mysql-password=kube1234
kubectl create configmap db --from-literal=mysql-database: database
mysql部署:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sfg-dev-mysql-db
labels:
app: sfg-dev-mysql
spec:
strategy:
type: Recreate
template:
metadata:
labels:
app: sfg-dev-mysql
tier: db
spec:
containers:
- image: mysql:8.0.2
name: mysql
env:
- name: MYSQL_DATABASE
valueFrom:
configMapKeyRef:
name: db
key: mysql-database
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-root-password
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-user
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: sfg-dev-mysql-pv-claim
答案 2 :(得分:0)
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-initdb-pv-volume
labels:
type: local
app: mysql
spec:
storageClassName: manual
capacity:
storage: 1Mi
accessModes:
- ReadOnlyMany
hostPath:
path: "/path/to/initdb/sql/scripts"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-initdb-pv-claim
labels:
app: mysql
spec:
storageClassName: manual
accessModes:
- ReadOnlyMany
resources:
requests:
storage: 1Mi
注意:假设您在/path/to/initdb/sql/scripts
中有SQL脚本
/docker-entrypoint-initdb.d
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 3306
volumeMounts:
- mountPath: /docker-entrypoint-initdb.d
name: mysql-initdb
volumes:
- name: mysql-initdb
persistentVolumeClaim:
claimName: mysql-initdb-pv-claim
就是这样。
注意:这也适用于PostgreSQL。