Wednesday, May 5, 2021

GKE Cheatsheet

Basics 

------ 

 

Installing gcloud 

exec -l $SHELL 

 

Compute Instances 

----------------- 

 

Auto scaling 

- create image 

- create instance template 

- create instance group (tune auto scaling) 

Spinning Up compute instance via ansible 

1. Create credentials file 

* see "Generatin credentials file" under IAM/Security 

 

2. Install pip packages 

pip install apache-libcloud PyCrypto 

 

3. Create a simple playbook 

--- 

- hosts: localhost 

  connection: local 

  gather_facts: False 

  tasks: 

 

  - name: Create VM instance 

    gce: 

      instance_names: my-test-instance1 

      zone: us-central1-a 

      machine_type: n1-standard-1 

      image: debian-8 

      state: present 

      service_account_email: "639770814480-compute@developer.gserviceaccount.com" 

      credentials_file: "~/Downloads/creds-my-private-cloud-01.json" 

      project_id: "my-private-cloud-01" 

      disk_size: 10 

 

4. Run playbook 

ansible-playbook create_gcp_instance.yml 

Moving an instance to a different zone within same region 

NOTE: 

 
If there are errors regarding reaching static address limit, try to delete some 
static IPs first. 

 

Move can also be done while instance is online. 
 
gcloud compute instances move instance-1 --destination-zone=asia-east1-a 
 

SSH'ing using from local laptop to target using internal IP 

gcloud compute ssh --zone asia-southeast1-a <compute instance name> --internal-ip 

 

IAM/Security 

------------ 

 

Generating credentials file 

- go to: APIs & Services > Credentials 

- choose: Create credentials > Service account key 
  * Compute Engine default service account 

  * JSON format 

 

GKE 

--- 

 

Authenticating to cluster 

gcloud auth activate-service-account --key-file=creds.json 

gcloud container clusters get-credentials my-sample-cluster --zone europe-west2-c 

Adding insecure registry 

1. Update DOCKER_OPTS under /etc/default/docker of each nodes to look like this: 

DOCKER_OPTS="-p /var/run/docker.pid [...] --insecure-registry docker.registry2:5000 --insecure-registry docker.registry2:5001" 

 

2. Drain the node. 

kubectl drain node1 

 

3. Restart docker. 

systemctl restart docker 

 

4. Repeat steps 1 -3 on the remaining nodes. 
 
https://stackoverflow.com/questions/49206139/what-is-the-proper-way-of-adding-insecure-registry-in-google-kubernetes-engine-n 

Scenarios in draining a node 

Scenario 1: 
You drain a node and remove that from a 3 node instance group.. Instance group scaled down to 2 node. 
You update instance group to 3 nodes. So there are now 4 nodes (1 is drained as seen from kubectl). 
You want to remove that drained node. You can simply delete the compute instance directly and wait 
for few minutes. The next time you hit "kubectl get nodes", there will now be only 3 nodes that are 
ready. 

No comments:

Post a Comment