As we will interact with Azure, we need a couple of Python libraries to be present in the system.
pip install --user -r requirements_aks.txt
We will also need the Ansible collection for Google Cloud.
ansible-galaxy collection install -r collections/requirements.yml
To authenticate provide the following environment variables GCP_PROJECT
, GCP_AUTH_KIND
, and GCP_SERVICE_ACCOUNT_FILE
.
GCP_PROJECT
: GCP Project ID.GCP_AUTH_KIND
: The type of credential used (application
,machineaccount
, orserviceaccount
).GCP_SERVICE_ACCOUNT_FILE
: The path of a Service Account JSON file ifserviceaccount
is selected as type.
You need to grant resource access to your service account. For example you can assign these roles: Compute Admin
, Kubernetes Engine Admin
, and Service Account User
(iam.serviceAccountUser).
You need to provide your Service Account JSON file, which you point out to with the environmental variable GCP_SERVICE_ACCOUNT_FILE
.
Follow these steps to provision an AKS cluster.
-
Clone this repository:
git clone https://github.com/nleiva/ansible-kubernetes.git
-
Make your GCP Credentials (
GCP_PROJECT
,GCP_AUTH_KIND``, and
GCP_SERVICE_ACCOUNT_FILE) available as environment variables (
export`).
export GCP_PROJECT='...'
export GCP_AUTH_KIND='...'
export GCP_SERVICE_ACCOUNT_FILE='...'
- Run the Playbook and wait a couple of minutes while GKE is being provisioned.
⇨ ansible-playbook main.yml -v --extra-vars "cloud_provider=gcp"
<snip>
TASK [gcp_create_gke : Create GKE cluster] *****************************************************************************************************************************************************
changed: [localhost] => {
"addonsConfig": {
"gcePersistentDiskCsiDriverConfig": {
"enabled": true
},
"kubernetesDashboard": {
"disabled": true
},
"networkPolicyConfig": {}
},
"changed": true,
"clusterIpv4Cidr": "10.108.0.0/14",
"createTime": "2021-08-10T16:59:06+00:00",
"currentMasterVersion": "1.20.8-gke.900",
"currentNodeCount": 2,
"currentNodeVersion": "1.20.8-gke.900",
"databaseEncryption": {
"state": "DECRYPTED"
},
"defaultMaxPodsConstraint": {
"maxPodsPerNode": "110"
},
<snip>
"status": "RUNNING",
"subnetwork": "kubernetes-vpc",
"zone": "us-east4-a"
}
TASK [gcp_create_gke : Create k8s node pool] ***************************************************************************************************************************************************
changed: [localhost] => {
"changed": true,
<snip>
"upgradeSettings": {
"maxSurge": 1
},
"version": "1.20.8-gke.900"
}
PLAY RECAP *************************************************************************************************************************************************************************************
localhost : ok=11 changed=2 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
Authorizing with a service account.
gcloud auth activate-service-account [ACCOUNT] --key-file=[KEY_FILE]
ACCOUNT
: E-mail address of the service account.KEY_FILE
: Path to the Service Account JSON file.
gcloud container clusters get-credentials: Fetch credentials for a running cluster.