Kubernetes Secrets值验证

一、secrets创建

secrets创建方法常见的有三种:

  • kubectl命令直接创建
  • kubectl命令从文件中获取
  • 通过yaml文件创建

具体如下:

# 方法1:
$ kubectl create secret generic  --from-literal=iamAdminPasswordKey= --namespace 

kubectl create secret generic cncc-iam-secret --from-literal=iamAdminPasswordKey=cncciampasswordvalue --namespace cncc kubectl describe secret cncc-iam-secret -n cncc

# 方法2:
echo -n 'admin' > ./username.txt
echo -n '1f2d1e2e67df' > ./password.txt

kubectl create secret generic db-user-pass \
  --from-file=./username.txt \
  --from-file=./password.txt

# 方法3:
echo -n 'admin' | base64    //YWRtaW4=
echo -n '1f2d1e2e67df' | base64   //MWYyZDFlMmU2N2Rm

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

kubectl apploy -f secrets-test.yaml

二、验证secrets的值

对于创建的secrets值,可以通过运行一个pod,调用创建的值进行确认:

[root@test11-41044 test]# ll
total 8
-rw------- 1 root root 273 Oct 1 06:57 pod.yaml
-rw------- 1 root root 125 Oct 1 06:11 sec.yaml
[root@test11-41044 test]# cat sec.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm

[root@test11-41044 test]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - secretRef:
          name: mysecret
  restartPolicy: Never

[root@test11-41044 test]# kubectl get secrets
NAME                  TYPE                                  DATA   AGE
cncc-db-secret        Opaque                                1      8d
default-secret        kubernetes.io/dockerconfigjson        1      11d
default-token-4pxpx   kubernetes.io/service-account-token   3      11d
mysecret              Opaque                                2      8d
mysql-pass            Opaque                                1      8d
paas.elb              cfe/secure-opaque                     3      11d

查看测试pod的调用日志:

[root@test11-41044 test]# kubectl apply -f pod.yaml
pod/secret-test-pod created
[root@test11-41044 test]# kubectl logs secret-test-pod
WORDPRESS_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT=tcp://10.247.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=secret-test-pod
……
USER_NAME=admin
PASSWORD=1f2d1e2e67df

而如果我们想要查看其他secret对象,只需要更改secretRef对应的name值就可以了。

参考页面:
https://kubernetes.io/docs/concepts/configuration/secret/
https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注