Gcp
GcpIntermediate

Google Kubernetes Engine (GKE) - From Basics to Advanced

5 min read
gcpcomputekubernetesgkecontainers

TL;DR

Master container orchestration with Google Kubernetes Engine. Learn about cluster management, workload deployment, scaling, monitoring, and best practices for running containerized applications at scale.

Google Kubernetes Engine (GKE) - From Basics to Advanced

Google Kubernetes Engine (GKE) is a managed Kubernetes service that lets you deploy, manage, and scale containerized applications on Google Cloud. This comprehensive guide covers everything from basic concepts to advanced features.

$1

``mermaid

graph TB

subgraph GKE["GKE Cluster"]

direction TB

subgraph ControlPlane["Control Plane"]

direction LR

API["API Server"]

ETCD["etcd"]

SCHED["Scheduler"]

CM["Controller Manager"]

end

subgraph NodePools["Node Pools"]

direction LR

subgraph Pool1["Default Pool"]

N1["Node 1"]

N2["Node 2"]

end

subgraph Pool2["Custom Pool"]

N3["Node 3"]

N4["Node 4"]

end

end

subgraph Workloads["Workload Types"]

direction LR

PODS["Pods"]

DEPLOY["Deployments"]

STS["StatefulSets"]

DS["DaemonSets"]

end

end

subgraph Services["GCP Services"]

direction TB

LB["Load Balancer"]

IAM["IAM"]

LOG["Cloud Logging"]

MON["Cloud Monitoring"]

end

GKE --> Services

classDef primary fill:#4285f4,stroke:#666,stroke-width:2px,color:#fff

classDef secondary fill:#34a853,stroke:#666,stroke-width:2px,color:#fff

classDef tertiary fill:#fbbc05,stroke:#666,stroke-width:2px,color:#fff

class GKE,ControlPlane primary

class NodePools,Workloads secondary

class Services tertiary

`

$1

| Feature | Standard Cluster | Autopilot |

|---------|-----------------|-----------|

| Node Management | Manual | Automated |

| Pricing | Per node | Per pod |

| Control | Full control | Limited control |

| Use Case | Custom workloads | Simple deployments |

| Scaling | Manual/Auto | Fully automated |

$1

$1

`bash

Create standard cluster

gcloud container clusters create my-cluster \

--zone us-central1-a \

--num-nodes 3 \

--machine-type e2-standard-4

Create autopilot cluster

gcloud container clusters create-auto my-autopilot \

--region us-central1

`

$1

`yaml

deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: my-app

spec:

replicas: 3

selector:

matchLabels:

app: my-app

template:

metadata:

labels:

app: my-app

spec:

containers:

- name: my-app

image: gcr.io/PROJECT_ID/my-app:v1

ports:

- containerPort: 8080

resources:

requests:

cpu: "100m"

memory: "128Mi"

limits:

cpu: "200m"

memory: "256Mi"

`

$1

$1

`bash

Create new node pool

gcloud container node-pools create pool-2 \

--cluster my-cluster \

--zone us-central1-a \

--num-nodes 2 \

--machine-type n1-standard-4

Enable autoscaling

gcloud container clusters update my-cluster \

--enable-autoscaling \

--min-nodes 1 \

--max-nodes 5 \

--zone us-central1-a

`

$1

`yaml

node-pool.yaml

apiVersion: container.google.com/v1beta1

kind: NodePool

metadata:

name: custom-pool

spec:

initialNodeCount: 3

autoscaling:

minNodeCount: 1

maxNodeCount: 5

config:

machineType: n1-standard-4

diskSizeGb: 100

oauthScopes:

- "https://www.googleapis.com/auth/cloud-platform"

labels:

env: prod

taints:

- key: dedicated

value: gpu

effect: NoSchedule

`

$1

$1

`yaml

rolling-update.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: my-app

spec:

replicas: 5

strategy:

type: RollingUpdate

rollingUpdate:

maxSurge: 1

maxUnavailable: 1

template:

# ... pod template spec

`

$1

`yaml

statefulset.yaml

apiVersion: apps/v1

kind: StatefulSet

metadata:

name: web

spec:

serviceName: "nginx"

replicas: 3

selector:

matchLabels:

app: nginx

template:

metadata:

labels:

app: nginx

spec:

containers:

- name: nginx

image: nginx:1.14.2

ports:

- containerPort: 80

volumeMounts:

- name: www

mountPath: /usr/share/nginx/html

volumeClaimTemplates:

- metadata:

name: www

spec:

accessModes: [ "ReadWriteOnce" ]

resources:

requests:

storage: 1Gi

`

$1

$1

`yaml

load-balancer.yaml

apiVersion: v1

kind: Service

metadata:

name: my-app

annotations:

cloud.google.com/neg: '{"ingress": true}'

spec:

type: LoadBalancer

ports:

- port: 80

targetPort: 8080

selector:

app: my-app

`

$1

`yaml

ingress.yaml

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: my-ingress

annotations:

kubernetes.io/ingress.class: "gce"

spec:

rules:

- host: my-app.example.com

http:

paths:

- path: /*

pathType: ImplementationSpecific

backend:

service:

name: my-app

port:

number: 80

`

$1

$1

`yaml

pod-security.yaml

apiVersion: v1

kind: Pod

metadata:

name: secure-pod

spec:

securityContext:

runAsUser: 1000

runAsGroup: 3000

fsGroup: 2000

containers:

- name: secure-container

image: nginx

securityContext:

allowPrivilegeEscalation: false

readOnlyRootFilesystem: true

`

$1

`yaml

network-policy.yaml

apiVersion: networking.k8s.io/v1

kind: NetworkPolicy

metadata:

name: api-allow

spec:

podSelector:

matchLabels:

app: api

policyTypes:

- Ingress

ingress:

- from:

- podSelector:

matchLabels:

role: frontend

ports:

- protocol: TCP

port: 8080

`

$1

$1

`yaml

custom-metrics.yaml

apiVersion: monitoring.googleapis.com/v1

kind: CustomMetric

metadata:

name: my-metric

spec:

type: custom.googleapis.com/my-metric

metricKind: GAUGE

valueType: INT64

labels:

- key: service

valueType: STRING

`

$1

`yaml

logging-config.yaml

apiVersion: v1

kind: ConfigMap

metadata:

name: fluentd-config

data:

fluent.conf: |

@type tail

path /var/log/containers/*.log

pos_file /var/log/fluentd-containers.log.pos

tag kubernetes.*

read_from_head true

@type json

time_key time

time_format %Y-%m-%dT%H:%M:%S.%NZ

`

$1

$1

`yaml

resource-quotas.yaml

apiVersion: v1

kind: ResourceQuota

metadata:

name: compute-resources

spec:

hard:

requests.cpu: "4"

requests.memory: 4Gi

limits.cpu: "8"

limits.memory: 8Gi

`

$1

`yaml

hpa.yaml

apiVersion: autoscaling/v2

kind: HorizontalPodAutoscaler

metadata:

name: my-app-hpa

spec:

scaleTargetRef:

apiVersion: apps/v1

kind: Deployment

name: my-app

minReplicas: 1

maxReplicas: 10

metrics:

- type: Resource

resource:

name: cpu

target:

type: Utilization

averageUtilization: 50

``

$1

1. Cluster Optimization

- Use appropriate machine types

- Implement node auto-provisioning

- Use preemptible VMs where possible

- Configure cluster autoscaling

2. Workload Optimization

- Set resource requests/limits

- Use horizontal pod autoscaling

- Implement pod disruption budgets

- Use node affinity rules

$1

1. High Availability

- Use regional clusters

- Deploy across zones

- Implement pod anti-affinity

- Use PodDisruptionBudgets

2. Security

- Enable Workload Identity

- Use Binary Authorization

- Implement network policies

- Regular security updates

3. Monitoring

- Set up proper alerting

- Monitor cluster health

- Track resource usage

- Implement logging

$1

GKE provides a powerful platform for running containerized applications at scale. Key takeaways:

  • Choose the right cluster type
  • Implement proper security measures
  • Use autoscaling effectively
  • Monitor performance and costs
  • Follow best practices
  • For more information, refer to the [official GKE documentation](https://cloud.google.com/kubernetes-engine/docs).

    Why This Matters

    Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

    Trade-offs to Consider

    Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

    When NOT to Use This

    Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

    Decision Framework

    Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.