其他分享
首页 > 其他分享> > Secret

Secret

作者:互联网

 

 

Secret 存在意义

Secret 解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec中。Secret 可以以 Volume 或者环境变量的方式使用

 

Secret 有三种类型:

/run/secrets/kubernetes.io/serviceaccount 目录中

 

Service Account

Service Account 用来访问 Kubernetes API,由 Kubernetes 自动创建,并且会自动挂载到 Pod的/run/secrets/kubernetes.io/serviceaccount 目录中

$ kubectl run nginx --image nginx
deployment "nginx" created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-3137573019-md1u2 1/1 Running 0 13s
$ kubectl exec nginx-3137573019-md1u2 ls /run/secrets/kubernetes.io/serviceaccount
ca.crt
namespace
token

 

[root@k8s-master01 ~]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE


my-nginx-6cc567cbd6-ll6b6   1/1     Running   0          75s
[root@k8s-master01 ~]# kubectl exec my-nginx-6cc567cbd6-ll6b6 -it -- /bin/sh
# cd /run/secrets/kubernetes.io/serviceaccount
# ls
ca.crt    namespace  token

 

 

 

 

 

 

 

Opaque Secret

创建说明

Opaque 类型的数据是一个 map 类型,要求 value 是 base64 编码格式:

base64是编码格式!


$ echo -n "admin" | base64
YWRtaW4=
$ echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm

 

[root@k8s-master01 ~]# echo -n "admin" | base64
YWRtaW4=
[root@k8s-master01 ~]# echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm
[root@k8s-master01 ~]# echo -n "YWRtaW4=" | base64 -d(解密看看)
admin[root@k8s-master01 ~]#

sec.yml


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

[root@k8s-master01 ~]# mkdir Secret
[root@k8s-master01 ~]# cd Secret/
[root@k8s-master01 Secret]# vim sec.yaml
[root@k8s-master01 Secret]# kubectl apply -f sec.yaml
secret/mysecret created


[root@k8s-master01 Secret]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-zfdww   kubernetes.io/service-account-token   3      11d(这个是默认的,每个名称空间下都有)
mysecret              Opaque                                2      23s
[root@k8s-master01 Secret]#


使用方式

将 Secret 挂载到 Volume 中


apiVersion: v1
kind: Pod
metadata:
labels:
  name: seret-test
name: seret-test
spec:
volumes:(申请使用一个卷)
- name: secrets(卷名)
  secret:(这个卷的使用方案是secret的方案)
  secretName: mysecret
containers:
- image: nginx
  name: db
  volumeMounts:
  - name: secrets
    mountPath: "/etc/secrets"
    readOnly: true

[root@k8s-master01 Secret]# vim pod1.yaml
[root@k8s-master01 Secret]# kubectl apply -f pod1.yaml
pod/seret-test created


[root@k8s-master01 Secret]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-6cc567cbd6-ll6b6   1/1     Running   0          25m
seret-test                  1/1     Running   0          40s
[root@k8s-master01 Secret]#

[root@k8s-master01 Secret]# kubectl exec seret-test -it -- /bin/bash
root@seret-test:/# cd /etc/secrets
root@seret-test:/etc/secrets# ls
password  username
root@seret-test:/etc/secrets# cat password (已经自己解密了!)
1f2d1e2e67dfroot@seret-test:/etc/secrets# cat username
adminroot@seret-test:/etc/secrets#

 

 

将 Secret 导出到环境变量中


apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-deployment
spec:
replicas: 2
  selector:
    matchLabels:
      app: pod-deployment
template:
  metadata:
    labels:
      app: pod-deployment
  spec:
    containers:
    - name: pod-1
      image: nginx
      ports:
      - containerPort: 80
      env:
      - name: TEST_USER
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: TEST_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysecret
              key: password

 

[root@k8s-master01 Secret]# vim env.yaml
[root@k8s-master01 Secret]# kubectl apply -f env.yaml
deployment.apps/pod-deployment created

[root@k8s-master01 Secret]# kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
my-nginx-6cc567cbd6-jnzq6         1/1     Running   0          8m41s
pod-deployment-56cdc45999-4l6pw   1/1     Running   0          28s
pod-deployment-56cdc45999-mg899   1/1     Running   0          28s
[root@k8s-master01 Secret]#

[root@k8s-master01 Secret]# kubectl exec pod-deployment-56cdc45999-4l6pw -it -- /bin/bash
root@pod-deployment-56cdc45999-4l6pw:/# echo $TEST_USER                      
admin
root@pod-deployment-56cdc45999-4l6pw:/# echo $TEST_PASSWORD
1f2d1e2e67df
root@pod-deployment-56cdc45999-4l6pw:/#

 

kubernetes.io/dockerconfigjson

使用 Kuberctl 创建 docker registry 认证的 secret


kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --
docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
secret "myregistrykey" created.

 

在创建 Pod 的时候,通过 imagePullSecrets 来引用刚创建的 myregistrykey


apiVersion: v1
kind: Pod
metadata:
name: foo
spec:
containers:
- name: foo
  image: roc/awangyang:v1
imagePullSecrets:
  - name: myregistrykey

 

 

 

 



标签:kubectl,name,master01,Secret,k8s,root
来源: https://www.cnblogs.com/zypdbk/p/16478834.html