本文章记录k8s secret 测试mysql时遇到的错误

错误log : mysqladmin: unknown option '--"

实验

实验测试k8s secret时,拿mysql做测试,样例如下

apiVersion: v1
kind: Secret
metadata:
  name: mysqlpassword
type: Opaque
data:
  password: MTIzCg== #密码123

---
apiVersion: v1
kind: Pod
metadata:
  name: mysqlserver
spec:
  containers:
    - name: mysql-server
      image: mysql:5.6
      env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysqlpassword
              key: password

遇坑

应用该样例文件,发现出错

# kubectl apply -f secret-demo.yaml
[root@k8s01 configmap_secret]# kubectl get pod  -owide
NAME          READY   STATUS    RESTARTS   AGE   IP                NODE    NOMINATED NODE   READINESS GATES
mysqlserver   0/1     Error     1          18s   192.168.236.188   k8s02   <none>           <none>

查看日志,报mysqladmin: unknown option ‘–"’

Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2021-04-08 14:23:15 94 [Warning] 'proxies_priv' entry '@ root@mysqlserver' ignored in --skip-name-resolve mode.

2021-04-08 14:23:15+00:00 [Note] [Entrypoint]: Stopping temporary server
mysqladmin: unknown option '--"'
2021-04-08 14:23:15+00:00 [ERROR] [Entrypoint]: Unable to shut down server.

开始以为是样例书写错误,又以为是镜像有问题,结果并非如此,问题出在MYSQL_ROOT_PASSWORD的secret密码的设置上

原因

在我的yaml文件中,secret的密码由base64命令直接生成,如

# echo 123 | base64
MTIzCg==

错误原因就在不能使用该命令生成,正确方式:

# kubectl create secret generic  passwd --from-literal=pass=123 --dry-run -o yaml
W0408 22:57:28.079370   73265 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
data:
  pass: MTIz
kind: Secret
metadata:
  creationTimestamp: null
  name: passwd

由kubectl 导出的命令才是正确的,而base64直接生成的密码是带回车的,此处可以验证

[root@test01 ~]# echo MTIz | base64 -d 
123[root@test01 ~]# 
[root@test01 ~]# echo MTIzCg== | base64 -d 
123

总结

生成密码需要使用kubectl命令导出更合适,不要直接使用base64命令编码填入yaml文件,在市面上较多的教程上,都忽略了这个问题

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐