代码之家  ›  专栏  ›  技术社区  ›  Tampa

kubernetes cronjob和更新秘密

  •  0
  • Tampa  · 技术社区  · 6 年前

    下面是我的python脚本,用于更新一个秘密,以便我可以使用kubectl部署到kubernetes。所以效果很好。但是我想创建一个kubernetes cron作业,它将运行一个docker容器来从kubernetes集群中更新一个秘密。我该怎么做?aws的秘密只持续12个小时,我必须从集群内再生,以便我可以拉,如果吊舱坠毁等。。。

    这里有一个我可以在kubernetes中访问的内部api?

    cmd = """aws ecr get-login --no-include-email --region us-east-1 > aws_token.txt"""
    run_bash(cmd)
    
    
    f = open('aws_token.txt').readlines()
    TOKEN = f[0].split(' ')[5]
    
    
    SECRET_NAME = "%s-ecr-registry" % (self.region)
    
    
    cmd = """kubectl delete secret --ignore-not-found %s -n %s""" % (SECRET_NAME,namespace)
    print (cmd)
    run_bash(cmd)
    
    cmd = """kubectl create secret docker-registry %s --docker-server=https://%s.dkr.ecr.%s.amazonaws.com --docker-username=AWS --docker-password="%s" --docker-email="david.montgomery@gmail.com" -n %s """ % (SECRET_NAME,self.aws_account_id,self.region,TOKEN,namespace)
    print (cmd)
    run_bash(cmd)
    
    cmd = "kubectl describe secrets/%s-ecr-registry -n %s" % (self.region,namespace)
    print (cmd)
    run_bash(cmd)
    
    cmd = "kubectl get secret %s-ecr-registry -o yaml -n %s" % (self.region,namespace)
    print (cmd)
    
    0 回复  |  直到 6 年前
        1
  •  2
  •   EvilCreamsicle    5 年前

    碰巧我刚做完这件事。

    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      namespace: {{ namespace }}
      name: ecr-cred-helper
    rules:
    - apiGroups: [""]
      resources:
      - secrets
      - serviceaccounts
      - serviceaccounts/token
      verbs:
      - 'delete'
      - 'create'
      - 'patch'
      - 'get'
    
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: ecr-cred-helper
      namespace: {{ namespace }}
    subjects:
    - kind: ServiceAccount
      name: sa-ecr-cred-helper
      namespace: {{ namespace }}
    roleRef:
      kind: Role
      name: ecr-cred-helper
      apiGroup: ""
    
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: sa-ecr-cred-helper
      namespace: {{ namespace }}
    
    ---
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      annotations:
      name: ecr-cred-helper
      namespace: {{ namespace }}
    spec:
      concurrencyPolicy: Allow
      failedJobsHistoryLimit: 1
      jobTemplate:
        metadata:
          creationTimestamp: null
        spec:
          template:
            metadata:
              creationTimestamp: null
            spec:
              serviceAccountName: sa-ecr-cred-helper
              containers:
              - command:
                - /bin/sh
                - -c
                - |-
                  TOKEN=`aws ecr get-login --region ${REGION} --registry-ids ${ACCOUNT} | cut -d' ' -f6`
                  echo "ENV variables setup done."
                  kubectl delete secret -n {{ namespace }} --ignore-not-found $SECRET_NAME
                  kubectl create secret -n {{ namespace }} docker-registry $SECRET_NAME \
                  --docker-server=https://{{ ECR_REPOSITORY_URL }} \
                  --docker-username=AWS \
                  --docker-password="${TOKEN}" \
                  --docker-email="${EMAIL}"
                  echo "Secret created by name. $SECRET_NAME"
                  kubectl patch serviceaccount default -p '{"imagePullSecrets":[{"name":"'$SECRET_NAME'"}]}' -n {{ namespace }}
                  echo "All done."
                env:
                - name: AWS_DEFAULT_REGION
                  value: eu-west-1
                - name: AWS_SECRET_ACCESS_KEY
                  value: '{{ AWS_SECRET_ACCESS_KEY }}'
                - name: AWS_ACCESS_KEY_ID
                  value: '{{ AWS_ACCESS_KEY_ID }}'
                - name: ACCOUNT
                  value: '{{ AWS_ACCOUNT_ID }}'
                - name: SECRET_NAME
                  value: '{{ imagePullSecret }}'
                - name: REGION
                  value: 'eu-west-1'
                - name: EMAIL
                  value: '{{ ANY_EMAIL }}'
                image: odaniait/aws-kubectl:latest
                imagePullPolicy: IfNotPresent
                name: ecr-cred-helper
                resources: {}
                securityContext:
                  capabilities: {}
                terminationMessagePath: /dev/termination-log
                terminationMessagePolicy: File
              dnsPolicy: Default
              hostNetwork: true
              restartPolicy: Never
              schedulerName: default-scheduler
              securityContext: {}
              terminationGracePeriodSeconds: 30
      schedule: 0 */6 * * *
      successfulJobsHistoryLimit: 3
      suspend: false