• [置顶] kubernetes资源类型--secret和Service Account


    secret

    概念

    secret对象类型主要目的是保存和处理敏感信息/私密数据,比如密码,OAuth tokens,ssh keys等信息。将这些信息放在secret对象中比 直接放在pod或docker image中更安全,也更方便使用。

    一个已经创建好的secrets对象有两种方式被pod对象使用,其一,在container中的volume对象里以file的形式被使用,其二,在pull images时被kubelet使用。

    类型

    Opaque任意字符串,默认类型

    kubernetes.io/service-account-token:作用于ServiceAccount

    kubernetes.io/dockercfg:作用于Docker registry,用户下载docker镜像认证使用

    Opaque

    创建

    文件

        实现把需要加密的内容实现base64编码

    echo -n lykops | base64

    bHlrb3Bz

    echo -n 1qaz2wsx | base64

    MXFhejJ3c3g=

        然后写入lykops-secret.yaml

    apiVersion: v1
    kind: Secret
    metadata:
      name: test-secret
      namespace: default
    type: Opaque
    data:
      password: MXFhejJ3c3g=
      username: bHlrb3Bz

        导入kubectl create -f lykops-secret.yaml

    命令行

    kubectl createsecret generic lykops --secret --from-literal=username=lykops--from-literal=password=1qaz2wsx

    pod引用

    cat << EOF > lykops-secret.yaml
    apiVersion: v1
    kind: Pod
    metadata:
     name: lykops-secret-pod
     labels:
       software: apache
       project: lykops
       app: lykops-secret-pod
       version: v1
    spec:
     containers:
        -name: lykops-secret-pod
         image: web:apache
         command: ['sh' , '/etc/run.sh']
         env:
           - name: SECRET_USERNAME
             valueFrom:
               secretKeyRef:
                 name: lykops-secret
                 key: username
           - name: SECRET_PASSWORD
             valueFrom:
               secretKeyRef:
                 name: lykops-secret
                 key: password
    
    EOF

    kubectl create -f lykops-secret-pod.yaml

    然后进入pod

    kubectl exec -it lykops-secret-pod /bin/bash

    执行env | grep -i '^SECRET_'

    SECRET_USERNAME=lykops

    SECRET_PASSWORD=1qaz2wsx

    imagePullSecrets

    当在需要安全验证的环境中拉取镜像时,需要通过用户名和密码。

    apiVersion: v1
    kind: Secret
    metadata:
     name: myregistrykey
     namespace: awesomeapps
    data:
     .dockerconfigjson:UmVhbGx5IHJlYWxseSByZWVlZWVlZWVlZWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGx5eXl5eXl5eXl5eXl5eXl5eXl5eSBsbGxsbGxsbGxsbGxsbG9vb29vb29vb29vb29vb29vb29vb29vb29vb25ubm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg==
    type: kubernetes.io/dockerconfigjson

    ·或者直接通过命令创建

    kubectl create secret docker-registrymyregistrykey --docker-server=DOCKER_REGISTRY_SERVER--docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD--docker-email=DOCKER_EMAIL

    ·接下来拉取镜像的时候,就可以使用了

    apiVersion: v1
    kind: Pod
    metadata:
     name: foo
     namespace: awesomeapps
    spec:
     containers:
        -name: foo
         image: janedoe/awesomeapp:v1
     imagePullSecrets:
        -name: myregistrykey

    ·其实本质上还是kubelet把这个认证放到了docker的目录下面,如下:

    Service Account

    概念

    Service Account(以下简称SA)的使用场景:运行在pod里的进程需要调用K8S API以及非K8S API的其它服务。SA

    cat ~/.docker/config.json
    {
       "auths": {
           "10.39.0.118": {
               "auth": "Y2hlbm1vOmNtMTM4MTE2NjY3ODY="
           },
           "10.39.0.12:5000": {
               "auth": "dXNlcjAxOjEyMzQ1YQ=="
           },
           "http://10.39.0.12:5000": {
               "auth": "dXNlcjAxOjEyMzQ1YQ=="
           }
        }
    }
    并不是给K8S集群的用户使用的,而是给pod里面的进程使用的,它为pod提供必要的身份认证。

    如果K8S开启了SA(位于/etc/kubernetes/controller-manager的KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"),那么会在每个namespace下面都会创建一个默认的default的SA。

    配置

    当用户在namespace下创建pod时会默认使用sa。

    ……
     volumes:
      -name: default-token-rsf8r
       secret:
         defaultMode: 420
          secretName:default-token-rsf8r

    具体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5OXpaWEoyYVdObFlXTmpiM1Z1ZEM5elpXTnlaWFF1Ym1GdFpTSTZJbVJsWm1GMWJIUXRkRzlyWlc0dGNuTm1PSElpTENKcmRXSmxjbTVsZEdWekxtbHZMM05sY25acFkyVmhZMk52ZFc1MEwzTmxjblpwWTJVdFlXTmpiM1Z1ZEM1dVlXMWxJam9pWkdWbVlYVnNkQ0lzSW10MVltVnlibVYwWlhNdWFXOHZjMlZ5ZG1salpXRmpZMjkxYm5RdmMyVnlkbWxqWlMxaFkyTnZkVzUwTG5WcFpDSTZJakJrWlRJek5UYzFMVEptTURJdE1URmxOeTA1T0dRd0xUVXlOVFJqTkRZeU9HRmtPU0lzSW5OMVlpSTZJbk41YzNSbGJUcHpaWEoyYVdObFlXTmpiM1Z1ZERwa1pXWmhkV3gwT21SbFptRjFiSFFpZlEuSmxuamM0Y0xNYkZrRlJVQjIyWGtFN2t4bTJ1dS1aQm9sUTh4VEdDNmdLOTdSZTVOMzBuY2V0SWJsanVOVWFlaDhtMDk2R19nMHE3cmRvWm5XMTV2OFBVXzNyM1hWWlBNc1lxbGRpZlNJbWtReXFqeEphVlBka3Izam5GWVBkVWNaTmk3MFF3cWtEdm5sMXB4SFRNZTZkTVNPTlExbUVoMHZSbHBhRTdjVWtTVlg5blRzaFVJVTVXWE9wRUxwdTVjVjBHV3ZGeDRDSzR6Umt3clNMdlV5X2d5UGZwLWdYVFZQWU80NkJKSWZtaVhlZGhVaW9nempPN285eGxDbUxQVkhyNkFIZGViNExiTVA1dkJ2MlBSZ2RrMW9keTR0VEdxLVRGU3M2VkNoMTZ4dk5IdTRtRVN5TjZmcXVObzJwYUFOelY4b251aTJuaU4yNTU1TzN4SFdR
    kind: Secret
    metadata:
     annotations:
       kubernetes.io/service-account.name: default
       kubernetes.io/service-account.uid: 0de23575-2f02-11e7-98d0-5254c4628ad9
     name: default-token-rsf8r
     namespace: default
     resourceVersion: "12551"
     selfLink: /api/v1/namespaces/default/secrets/default-token-rsf8r
      uid:75c0a236-2f02-11e7-98d0-5254c4628ad9
    type: kubernetes.io/service-account-token

    上面的内容是经过base64加密过后的,直接进入容器内:

    ls -l /var/run/secrets/kubernetes.io/serviceaccount/
    total 0
    lrwxrwxrwx   1 root     root            13 May  4 23:57 ca.crt -> ..data/ca.crt
    lrwxrwxrwx   1 root     root            16 May  4 23:57 namespace -> ..data/namespace
    lrwxrwxrwx   1 root     root            12 May  4 23:57 token -> ..data/token

    可以看到已将ca.crt 、namespace和token放到容器内了,那么这个容器就可以通过https的请求访问apiserver了。

  • 相关阅读:
    论文阅读 | Generating Natural Language Adversarial Examples through Probability Weighted Word Saliency
    基本原理 | seq2seq概述
    Android复习(三)清单文件中的元素——>supports-gl-texture、supports-screens
    Android复习(三)清单文件中的元素——> provider、receiver、service
    Android复习(三)清单文件中的元素——>path-permission、permission、permission-group、permission-tree
    Android复习(三)清单文件中的元素——>grant-uri-permission、instrumentation、intent-filter、manifest、meta-data
    Android复习(三)清单文件中的元素——>action、activity-alias、category、compatible-screens、data
    Android复习(三)清单文件中的元素——>application
    Android复习(三)清单文件中的元素——> activity
    Android复习(三)清单文件中的元素
  • 原文地址:https://www.cnblogs.com/lykops/p/7348007.html
Copyright © 2020-2023  润新知