大纲

  • 问题
  • 问题原因
  • 如何排查?
  • 注意事项

 

问题

有时候我们看到如下格式错乱的configmap内容:

# kubectl get cm -n test test -oyaml
apiVersion: v1
data:
  test.yaml: "\n\techo \"a\"\n    echo \"b\" \n"
kind: ConfigMap
metadata:
  name: test
  namespace: test
问题原因

问题原因: 文件中某一行结尾有空格,或者中间有特殊字符。

如何排查?

怎么排查问题呢?

先describe cm重定向到文件,就可以看到正常的格式:

#  kubectl describe cm -n test test > a.yaml               

Name:         test$
Namespace:    test$
Labels:       <none>$
Annotations:  <none>$
$
Data$
====$
test.yaml:$
----$
$
  echo "a"$
    echo "b" $
$
Events:  <none>$ 

可以看到"b"后面有个空格。但看不到echo "a"前面的tab键;

找个pod挂载这个configmaps到容器内,如:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    /revision: "2"
  creationTimestamp: "2021-05-10T12:15:21Z"
  generation: 2
  labels:
    app: zzz 
  name: zzz
  namespace: test
  resourceVersion: "186200965"
  selfLink: /apis/apps/v1/namespaces/test/deployments/zzz
  uid: 2f4305ec-d74a-489a-9725-48ce6c3f5c57
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: zzz
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zzz
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /tmp/test/
          name: config
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 493
          name: test
        name: config    

如果容器内有vi命令,进去执行:set invlist,如果进去只读,执行不了命令,或者根本没有vi命令,需要拷贝到主机上再vi进去;

# kubectl exec -ti -n test zzz-8476c5595c-6764n bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@zzz-8476c5595c-6764n:/# cp /tmp/test/test.yaml /test.yaml
root@zzz-8476c5595c-6764n:/# exit
exit
# kubectl cp test/zzz-8476c5595c-6764n:/test.yaml ./test.yaml
tar: Removing leading `/' from member names

由于拷贝挂载文件有问题,所以先复制到/test.yaml,再kubectl cp拷贝。vi进去查看到以下内容:

$
^Iecho "a"$
    echo "b"$

:set invlist

可以看到echo "a"前面有特殊字符^I

注意事项

注意,要用空格,而不是tab键,不管是go、java等客户端调用时,都需注意!!