背景:一个.net的微服务,使用k8s方式部署。先将配置文件以configmap形式发布
[root@k8s-master0 dc-stg-ns]# cat tidssettings.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: tidssettings
namespace: dc-stg-ns
data:
tidssettings.json: |
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
.................(略)
}
}
再配置应用及服务
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: xxxx-xxxx
name: xxxx-xxxx
namespace: dc-stg-ns
spec:
replicas: 2
selector:
matchLabels:
k8s-app: xxxx-xxxx
strategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
k8s-app: xxxx-xxxx
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
k8s-app: xxxx-xxxx
topologyKey: kubernetes.io/hostname
containers:
- env:
- name: APP_OPTIONS
value: '-Xms1024m -Xmx1024m -Xss1024k'
volumeMounts:
- mountPath: /app/appsettings.json
name: tidssettingsapp
readOnly: true
subPath: appsettings.json
image: 192.168.xx.xx:8082/xxxx-xxxx:202209300656
imagePullPolicy: IfNotPresent
name: xxxx-xxxx
ports:
- containerPort: 80
protocol: TCP
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 80
timeoutSeconds: 1
volumes:
- configMap:
defaultMode: 420
name: tidssettings
name: tidssettingsapp
imagePullSecrets:
- name: docker-pull-secret
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: xxxx-xxxx
name: xxxx-xxxx
namespace: dc-stg-ns
spec:
ports:
- name: http-80
nodePort: 30224
port: 30224
protocol: TCP
targetPort: 80
selector:
k8s-app: xxxx-xxxx
sessionAffinity: None
type: NodePort
在部署应用后,一直提示出错:Error......unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
看网上的说法大都不靠谱,最后发现问题出在这一行:subPath: appsettings.json
这里subpath是指的源文件,而实际上这里的configmap源文件为:tidssettings.json
因此,改为subPath: tidssettings.json 后故障消除
我们也可以在这里查看这个CM
[root@k8s-master0 dc-stg-ns]# kg cm tidssettings -n dc-stg-ns -o yaml
apiVersion: v1
data:
tidssettings.json: |
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
......
从这里也可以看到,要挂载的配置文件名为 tidssettings.json
网友评论