How to create Google Kubernetes (GKE) cluster in Ansible with custom image?

2/23/2021

I've used this pattern in the past to create a GKE and it's worked great, but now I need to define a custom image type to use.

Here's the ansible playbook i'm working with.

- name: GCE
  hosts: localhost
  gather_facts: no
  vars_files:
    - vars/default.yml

  tasks:
    - name: create cluster
      gcp_container_cluster:
        name: "{{ cluster_name }}"
        initial_node_count: "{{ node_count}}"
        initial_cluster_version: "{{ cluster_kubernetes_version }}"
        master_auth:
          username: admin
          password: "{{ cloud_admin }}"
        node_config:
          machine_type:  e2-medium
          disk_size_gb: "{{ disk_size_gb }}"
        location: "{{ cluster_zone}}"
        project: "{{ project }}"
        auth_kind: "{{ auth_kind }}"
        service_account_file: "{{ service_account_file }}"
        state: present
        scopes: "{{ scopes }}"
      register: cluster
    - name: create a node pool
      google.cloud.gcp_container_node_pool:
        name: default-pool
        autoscaling:
          enabled: yes
          min_node_count: "{{ node_count}}"
          max_node_count: "{{ max_node_count }}"
        initial_node_count: "{{ node_count }}"
        cluster: "{{ cluster }}"
        location: "{{ cluster_zone}}"
        config:
          machine_type: e2-medium
          disk_size_gb: "{{ disk_size_gb }}"
        project: "{{ gce_project}}"
        auth_kind: serviceaccount
        service_account_file: "{{ service_account_file }}"
        state: presen

I'm trying to use an E2 based image with 16 cores and 70GB of RAM. The spec don't matter as much as the fact that I can't specify a 'machine type' that's already preconfigured.

Is it possible to still use ansible to create the cluster? Do I need to create a custom image type to reference?

Just to clarify, there are no errors being thrown out. defining the machine_type as e2-medium doesn't allow me to allocate the resources I need and define an instance with the resources required. I'm asking how to say use e2-medium as a base and increase the ram allocation to 70GB or if that is feasible?

-- csgeek
ansible
gcloud
google-kubernetes-engine
kubernetes

1 Answer

2/24/2021

IIUC, you should be able to reference your machine type as e2-custom-16-71680

i.e.:

- name: your-cluster
  google.cloud.gcp_container_cluster:
    ...
    node_config:
      machine_type: e2-custom-16-71680
      disk_size_gb: "{{ disk_size_gb }}"
    ...

The (hidden) documentation for specifying custom machine types:

https://cloud.google.com/compute/docs/instances/creating-instance-with-custom-machine-type#gcloud

-- DazWilkin
Source: StackOverflow