Skip to content

GCP Google Kubernetes Engine

ghdrako edited this page Mar 19, 2024 · 4 revisions

GKE is a management platform based on an open source orchestration protocol of the same name (Kubernetes) that essentially groups together your containers for easier discovery, tracking, debugging, and overall management.

Kubernetes (the open source system) facilitates development by automating many aspects of container-based software development such as deployment, scaling, and how containers talk to each other.

you might also be interested in GKE if you're planning on running your application in multiple environments such as in separate development, testing, and production environments or on multi-cloud or hybrid cloud environments, and so on. Multiple environments also help if you want to run different instances in different geographical regions (due to language, legal, and political barriers, and other factors).

GKE is also one of the better options if you want to have continuous integration/continuous deployment (CI/CD) pipelines in your development. Finally, GKE supports network protocols other than HTTP/S, something not available in GAE or Cloud Functions.

Similar to Cloud Run, GKE is based around containers, and if your application isn't containerized, using GKE is out of the question. Also, GKE isn't the most cost-effective compute option available, although it is quite economical if your application has high utilization—that is, it can use all of the resources provisioned to your VMs

gcloud container clusters create bootcamp --num-nodes 5 --scopes "https://www.googleapis.com/auth/projecthosting,storage-rw"

Static persistent volume

Create a persistent zonal disk. Ensure that you use the same zone as your Kubernetes cluster

$ gcloud compute disks create nginx-manual --size 50GB \
--type pd-ssd --zone us-central1-a

create a PersistentVolume resource from it. The manifest file, nginx-manual-pv.yaml, looks like the following:

apiVersion: v1
kind: PersistentVolume
metadata:
name: nginx-manual-pv
labels:
usage: nginx-manual-disk
spec:
capacity:
storage: 50G
accessModes:
- ReadWriteOnce
gcePersistentDisk:
pdName: nginx-manual
fsType: ext4

The spec section contains capacity, accessModes, and the kind of disk it needs to provision. You can specify one or more access modes to a persistent volume:

  • ReadWriteOnce: Only one Pod can read and write to the disk at a time, and therefore you cannot mount such a volume to multiple Pods.
  • ReadOnlyMany: Multiple Pods can read from the same volume at a time, but no Pod can write to the disk.
  • ReadWriteMany: Multiple Pods can read and write to the same volume at once.
$ kubectl apply -f nginx-manual-pv.yaml
$ kubectl get pv

As the persistent volume is now available, we need to create the headless Service resource that will help maintain network identity in the StatefulSet resource. The manifest for the nginx-manual-service.yaml Service resource looks like the following:

apiVersion: v1
kind: Service
metadata:
  name: nginx-manual
  labels:
app: nginx-manual
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx-manual
$ kubectl apply -f nginx-manual-service.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-manual
spec:
selector:
matchLabels:
app: nginx-manual
serviceName: "nginx-manual"
replicas: 1
template:
metadata:
labels:
app: nginx-manual
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: html
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 40Gi
selector:
matchLabels:
usage: nginx-manual-disk

The volumeClaimTemplates section consists of the accessModes, resources, and selector sections. The selector section defines matchLabels, which help to select a particular PersistentVolume resource. In this case, it selects the PersistentVolume resource we defined previously. It also contains the serviceName attribute that defines the headless Service resource it will use.

$ kubectl apply -f nginx-manual-statetfulset.yaml
$ kubectl get pvc
$ kubectl get pv
$ kubectl get pod
$ kubectl exec -it nginx-manual-0 -- /bin/bash

Dynamic provisioning

provisions an SSD within Google Cloud Platform:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd

Tip Keep generic storage class names such as fast, standard, block, and shared, and avoid names specific to the cloud platform. Because storage class names are used in the persistent volume claims, and if you migrate to another cloud provider, you may end up changing a lot of manifests just to avoid confusion.

$ kubectl apply -f fast-storage-class.yaml

Test

Clone this wiki locally