【问题标题】:Ansible: Obtain api_token from gce_container_clusterAnsible:从 gce_container_cluster 获取 api_token
【发布时间】:2019-10-13 16:24:23
【问题描述】:

我启动 GCP 集群没有问题,但我不知道如何让 k8s ansible 模块工作。我宁愿让 api_key 验证到 k8s 模块。

我的剧本如下。

- name: Hello k8s
  hosts: all
  tasks:
    - name: Create a cluster
      register: cluster
      gcp_container_cluster:
        name: thecluster
        initial_node_count: 1
        master_auth:
          username: admin
          password: TheRandomPassword
        node_config:
          machine_type: g1-small
          disk_size_gb: 10
          oauth_scopes:
            - "https://www.googleapis.com/auth/compute"
            - "https://www.googleapis.com/auth/devstorage.read_only"
            - "https://www.googleapis.com/auth/logging.write"
            - "https://www.googleapis.com/auth/monitoring"
        zone: europe-west3-c
        project:  second-network-255214 
        auth_kind: serviceaccount
        service_account_file: "{{ lookup('env', 'GOOGLE_CREDENTIALS') }}"
        state: present 
    - name: Show results
      debug: var=cluster
    - name: Create temporary file for CA
      tempfile:
        state: file
        suffix: build
      register: ca_crt
    - name: Save content to file
      copy: 
        content: "{{ cluster.masterAuth.clusterCaCertificate |b64decode }}"
        dest: "{{ ca_crt.path }}"
    - name: Create a k8s namespace
      k8s:
        host: "https://{{ cluster.endpoint }}"
        ca_cert: "{{ ca_crt.path }}"
        api_key: "{{ cluster.HOW_I_GET_THE_API_KEY}}" <<<-- Here is what I want!!!
        name: testing
        api_version: v1
        kind: Namespace
        state: present 

有什么想法吗?

【问题讨论】:

  • 我希望它在cluster.masterAuth 中,我看到你已经debug: 编辑了它,所以你没有在那个数据结构中找到token 吗?
  • 没错。我使用 google_client_config 在 terraform 中实现了相同的任务。我想在 ansible 中会有一个 simikar 模块。
  • 我已尝试使用空的 master_auth 映射并删除 master_auth 条目。也许可以用 k8s_auth 模块完成,但我不知道如何配置它。
  • 不,我的意思是 cluster.masterAuth 已经包含 clusterCaCertificate -- cluster.masterAuth 还包含什么?
  • 如果我在 master_auth 块中传递用户名或密码,除了 clusterCaCertificate 之外,我还会在 masterAuth 中获取它们(用户名和密码)。对于身份验证,我认为不需要用户名和密码。理想的情况是为 GCP 服务帐户获取 access_token。此外,我认为 access_token 不仅在集群上下文中有效,而且在整个 GCP 中都有效。所以与 gcp_container_cluster 无关,但应该有一个 gcp_iam_service_account_token 或一个 oauth 模块可以完成这项工作。

标签: kubernetes ansible google-compute-engine google-kubernetes-engine


【解决方案1】:

我创建了一个解决方法,就是直接调用gcloud:

    - name: Get JWT
      command: gcloud auth application-default print-access-token
      register: api_key

显然,我需要:

  • 安装 GCloud
  • 使用 auth.json 将 envvar 重新定义为 GOOGLE_APPLICATION_CREDENTIALS。

任务直接调用gcloud获取token,无需生成token。我将尝试添加将此功能作为模块添加到 ansible 中,以便与 kubernetes 更好的互操作性。

一旦获得,就可以像这样调用k8s模块:

    - name: Create ClusterRoleBinding
      k8s:
        state: present
        host: "https://{{ cluster.endpoint }}"
        ca_cert: "{{ ca_crt.path }}"
        api_version: rbac.authorization.k8s.io/v1
        api_key: "{{ api_key.stdout }}"
        definition:
          kind: ClusterRoleBinding
          metadata:
            name: kube-system_default_cluster-admin
          subjects:
          - kind: ServiceAccount
            name: default # Name is case sensitive
            namespace: kube-system
          roleRef:
            kind: ClusterRole
            name: cluster-admin
            apiGroup: rbac.authorization.k8s.io

【讨论】:

    【解决方案2】:

    根据the fine manualmasterAuth 包含另外两个字段clientCertificateclientKey,分别对应于client_cert: and client_key: parameters。从那时起,您可以使用非常非常强大的私钥凭据以cluster-admin 身份向集群的端点进行身份验证,然后使用相同的k8s: 任务为自己配置cluster-admin ServiceAccount 令牌,如果你希望这样做。

    您显然也可以在k8s:username:password: 参数中使用masterAuth.usernamemasterAuth.password,这应该同样安全,因为凭据通过HTTPS 传输,但您看起来像您对更高熵的身份验证解决方案更感兴趣。

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2018-08-31
      • 2017-11-10
      • 2016-11-24
      • 2021-08-24
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多