我想在容器的MySQL上设置初始数据。在docker-compose.yml中,此类代码可以在运行容器时创建初始数据。
volumes: - db:/var/lib/mysql - "./docker/mysql/conf.d:/etc/mysql/conf.d" - "./docker/mysql/init.d:/docker-entrypoint-initdb.d"
但是,运行时如何在Kubernetes上创建初始数据?
根据MySQL Docker映像README,与容器启动时的数据初始化有关的部分是确保所有初始化文件都已安装到容器的/docker- entrypoint-initdb.d文件夹中。
/docker- entrypoint-initdb.d
您可以在中定义初始数据ConfigMap,然后将相应的卷挂载到pod中,如下所示:
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');