简体   繁体   English

如何在 Ansible 中检索已创建的 Google Kubernetes (GKE) 集群的凭据?

[英]How to retrieve credentials of a created Google Kubernetes (GKE) cluster in Ansible?

I'm creating a cluster and node pool with我正在创建一个集群和节点池

- name: "Create Google Kubernetes Engine Cluster to be setup with with kubectl"
  gcp_container_cluster:
    name: "{{cluster_name}}"
    project: "{{project_id}}"
    auth_kind: "serviceaccount"
    location: "{{cluster_location}}"
    logging_service: "none"
    monitoring_service: "none"
    service_account_contents: "{{service_account_contents}}"
    initial_node_count: 1
  register: cluster
- name: "Create node pool for system pods"
  gcp_container_node_pool:
    name: "default-pool"
    project: "{{project_id}}"
    cluster: "{{ cluster }}"
    auth_kind: "serviceaccount"
    location: "{{cluster_location}}"
    autoscaling:
      enabled: "no"
    initial_node_count: 1
    service_account_contents: "{{service_account_contents}}"
    config:
      machine_type: "g1-small"
      disk_size_gb: 25
      preemptible: "no"
    management:
      auto_repair: "yes"
      auto_upgrade: "yes"

which works fine, however I find no information how to authenticate and thus gain authorization for kubectl commands executed with the Ansible k8s module.这工作正常,但是我没有找到有关如何验证并因此获得使用 Ansible k8s模块执行的kubectl命令授权的信息。

First of all, the documentation is not helpful both in terms of wording and examples.首先,文档在措辞和示例方面都没有帮助。 I didn't find any guide, blogpost or else and feel like I'm the first person performing this tasks with Ansible.我没有找到任何指南、博文或其他内容,我觉得我是第一个使用 Ansible 执行此任务的人。 I examined the returned cluster object (stored with register above) closely, but didn't find anything suitable.我仔细检查了返回的集群 object(与上面的register一起存储),但没有找到任何合适的。 This applies to both gcp_container_cluster and gcp_container_cluster_info .这适用于gcp_container_clustergcp_container_cluster_info

As far as I understand I need to obtain a kubeconfig somehow with an Ansible module which I can use in the k8s module, like I'd do with gcloud container clusters get-credentials .据我了解,我需要使用可以在 k8s 模块中使用的 Ansible 模块以某种方式获取 kubeconfig,就像我对gcloud container clusters get-credentials所做的那样。 I'd like to see if there's a built-in way first before I do a hacky workaround using shell commands in Ansible.我想先看看是否有内置方法,然后再使用 Ansible 中的 shell 命令进行破解。

I'm using Ansible 2.9.10 on Ubuntu 20.04.我在 Ubuntu 20.04 上使用 Ansible 2.9.10。

When you create the cluster using the gcp_container_cluster , the return value will include the path where the kubeconfig file is stored .当您使用gcp_container_cluster创建集群时,返回值将包括存储 kubeconfig 文件的路径 You can refer to this path using your example with {{ cluster.kubectlPath }}.您可以使用带有 {{ cluster.kubectlPath }} 的示例来引用此路径。

When you want to use the k8s module, you can define the kubeconfig file path to use for the module.. It will look something like this:当你想使用k8s模块时,你可以定义用于模块的kubeconfig 文件路径。它看起来像这样:

    - name: "Create Google Kubernetes Engine Cluster to be setup with with kubectl"
      gcp_container_cluster:
        name: "{{cluster_name}}"
        kubectlPath: /path/to/save/config
        [...]
      register: cluster
    - name: "Create k8s resource"
      k8s:
        kubeconfig: "{{ cluster.kubectlPath }}"
        definition:
          [...]

EDIT: I misread the document, in the "response" section it clarifies that this field must be set for the file to be written in this path, there is no default value for this field.编辑:我误读了文档,在“响应”部分中,它阐明了必须为要写入此路径的文件设置此字段,此字段没有默认值。

This seems to be impossible due to https://github.com/ansible/ansible/issues/66096 .由于https://github.com/ansible/ansible/issues/66096这似乎是不可能的。

I circumvented this with我用

- name: "Circumvent https://github.com/ansible/ansible/issues/66096 which makes it impossible to retrieve the
kubecontext retrieved during cluster creation, asked
https://stackoverflow.com/questions/62799952/how-to-update-the-ansible-gcp-container-cluster-module for input"
  block:
    - name: "Create temporary file for gcloud key"
      tempfile: ~
      register: gcloud_key_tempfile
    - name: "Write gcloud key to temporary file"
      copy: content="{{ service_account_contents }}" dest={{ gcloud_key_tempfile.path }}
    - name: "Retrieve k8s credentials through gcloud shell commands"
      shell: |
        gcloud config set project {{ project_id }}
        gcloud config set compute/zone {{ cluster_location }}
        gcloud auth activate-service-account --key-file {{ gcloud_key_tempfile.path }}
        env KUBECONFIG={{kubectl_tempfile.path}} gcloud container clusters get-credentials {{ cluster_name }}
    - set_fact:
        kubeconfig_path: "{{ kubectl_tempfile.path }}"
  always:
    - name: "Remove temporary file for gcloud key"
      file: dest="{{ gcloud_key_tempfile.path }}" state=absent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自定义图像在 Ansible 中创建 Google Kubernetes (GKE) 集群? - How to create Google Kubernetes (GKE) cluster in Ansible with custom image? 如何删除Google Kubernetes集群中的GPU(GKE) - How to remove GPU in Google Kubernetes cluster (GKE) 如何在通过Kubernetes运行的GKE上设置GOOGLE_APPLICATION_CREDENTIALS - How to set GOOGLE_APPLICATION_CREDENTIALS on GKE running through Kubernetes 如何将kubernetes事件从GKE集群传播到谷歌云日志 - How to propagate kubernetes events from a GKE cluster to google cloud log Google Cloud Kubernetes - 如何创建区域或区域 GKE 集群? - Google Cloud Kubernetes - How to create Zonal or Regional GKE cluster? 如何在GKE上扩展Kubernetes集群? - How to scale a Kubernetes cluster on GKE? 如何使用 python 删除 GKE(Google Kubernetes 引擎)集群? - How to delete GKE (Google Kubernetes Engine) cluster using python? Terraform:如何使用命名空间在Google Cloud(GKE)上创建Kubernetes集群? - Terraform: How to create a Kubernetes cluster on Google Cloud (GKE) with namespaces? Google(GKE)中的Kubernetes集群正在扩展节点 - Kubernetes cluster in Google (GKE) is over scaling nodes 如何使用动态和独特的Google凭据对多个GKE Kubernetes群集进行身份验证 - How to authenticate to multiple GKE Kubernetes clusters using dynamic and unique google credentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM