我有一个docker容器,可映射用户的本地目录,使其成为持久卷。目录路径可通过环境变量“ MYSQL_DATA_FOLDER”进行自定义。
这在本地运行docker容器时很好用,但是,通过minikube / kubernetes启动容器时,这种方法不再起作用
仅使用docker启动此容器时,我将使用以下命令:
docker-compose run --service-ports --volume=$MYSQL_DATA_FOLDER:/var/lib/mysql --name db --rm database
这是docker-compose.yml文件
version: '3.5'
services:
database:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
volumes:
- mydatavolume:${MYSQL_DATA_FOLDER}
build:
context: .
dockerfile: Dockerfile
volumes:
mydatavolume:
networks:
default:
name: dev_network
driver: bridge
这是环境变量文件(env-variables.env):
MYSQL_ALLOW_EMPTY_PASSWORD=yes
MYSQL_ROOT_PASSWORD=
MYSQL_DATA_FOLDER=/Users/foo/mysql/data
我使用以下命令使用kubernetes创建了一个ConfigMap:
kubectl create configmap db-env --from-env-file=env-variables.env
这是deployment.yml文件:
apiVersion: v1
kind: Service
metadata:
name: db-mysql
labels:
app: db
spec:
ports:
- port: 3306
selector:
app: db
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: db
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: db-mysql
labels:
app: db
spec:
selector:
matchLabels:
app: db
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: db
tier: mysql
spec:
containers:
- image: mysql
name: mysql
envFrom:
- configMapRef:
name: db-env
livenessProbe:
tcpSocket:
port: 3306
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: ${MYSQL_DATA_FOLDER}
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
我想做的是使用环境变量MYSQL_DATA_FOLDER填充mountPath值,以便用户可以对其进行配置。
我希望能够“ git pull”并且无需对文件进行任何后期处理/预处理。
感谢任何线索
答案 0 :(得分:1)
您可以按照以下方式进行操作:
source env.file && cat deployment.yml | sed "s/{{MYSQL_DATA_FOLDER}}/$MYSQL_DATA_FOLDER/g" | kubectl apply -f -
以及您的yaml文件变量,例如:
...
volumeMounts:
- name: mysql-persistent-storage
mountPath: {{MYSQL_DATA_FOLDER}}
...