# Kubernetes administration on AKS, GKE and EKS

A Bitflow Solutions guide. Web version: https://bitflow-solutions.com/en/guias-tecnicas/kubernetes-administration-aks-gke-eks/

## What you’ll learn

- Connecting to an AKS, GKE or EKS cluster from Windows and switching between clusters without mistakes.
- Checking the status of nodes, pods and services, and diagnosing a failing pod.
- Deploying, scaling and rolling back a deployment with `kubectl`.
- Scaling nodes, carrying out maintenance on a node and upgrading the Kubernetes version on each cloud.

## Prerequisites

- An **AKS**, **GKE** or **EKS** cluster, and permissions to administer it on that cloud.
- **Windows** with **PowerShell** and `winget` (the `kubectl` commands are the same on macOS and Linux).
- For EKS, **eksctl** (see its official installation guide).
- Knowledge of the basic Kubernetes objects: pod, deployment, service and namespace.

## Step by step

### Install the tools

```powershell
# kubectl (common to all three clouds)
winget install -e --id Kubernetes.kubectl

# Your cloud's CLI
winget install -e --id Microsoft.AzureCLI      # Azure (AKS)
winget install -e --id Google.CloudSDK         # Google Cloud (GKE)
winget install -e --id Amazon.AWSCLI           # AWS (EKS)
```

### Connect to the cluster

```powershell
# AKS
az login
az aks get-credentials --resource-group rg-production --name aks-production

# GKE (needs the authentication plugin)
gcloud auth login
gcloud components install gke-gcloud-auth-plugin
gcloud container clusters get-credentials gke-production --region europe-southwest1 --project my-project

# EKS
aws configure
aws eks update-kubeconfig --region eu-south-2 --name eks-production
```

### Contexts

```powershell
kubectl config get-contexts           # all configured clusters
kubectl config current-context        # which one you are in
kubectl config use-context aks-production
kubectl config set-context --current --namespace=orders   # default namespace
```

### Status

```powershell
kubectl get nodes -o wide             # nodes, version, IP and status
kubectl get pods -A                   # all pods in all namespaces
kubectl get pods -n orders -o wide    # pods in a namespace and which node they are on
kubectl get deploy,svc,ingress -n orders
kubectl top nodes                     # CPU and memory usage (needs metrics-server)
kubectl top pods -n orders
```

### Diagnosis

```powershell
kubectl describe pod tasks-api-7c9d8 -n orders     # status, events and reason for failure
kubectl logs tasks-api-7c9d8 -n orders             # container logs
kubectl logs -f deploy/tasks-api -n orders         # follow the logs live
kubectl logs tasks-api-7c9d8 -n orders --previous  # logs of the previous container (after a restart)
kubectl get events -n orders --sort-by=.lastTimestamp
kubectl exec -it tasks-api-7c9d8 -n orders -- sh   # open a shell in the container
kubectl port-forward svc/tasks-api 8080:80 -n orders   # test an internal service at localhost:8080
```

### Deployments

```powershell
kubectl apply -f deployment.yaml                         # create or update from a file
kubectl set image deployment/tasks-api api=ghcr.io/company/tasks-api:1.4.0 -n orders
kubectl rollout status deployment/tasks-api -n orders    # wait for it to finish
kubectl rollout history deployment/tasks-api -n orders
kubectl rollout undo deployment/tasks-api -n orders      # go back to the previous version
kubectl rollout restart deployment/tasks-api -n orders   # restart all pods in an orderly way
kubectl scale deployment/tasks-api --replicas=4 -n orders
```

### Scale nodes

```powershell
# AKS
az aks nodepool scale --resource-group rg-production --cluster-name aks-production --name nodepool1 --node-count 4

# GKE
gcloud container clusters resize gke-production --node-pool default-pool --num-nodes 4 --region europe-southwest1

# EKS (with eksctl)
eksctl scale nodegroup --cluster eks-production --name ng-general --nodes 4
```

### Node maintenance

```powershell
kubectl cordon aks-nodepool1-12345678-vmss000002   # do not schedule new pods on the node
kubectl drain aks-nodepool1-12345678-vmss000002 --ignore-daemonsets --delete-emptydir-data
# ... node maintenance ...
kubectl uncordon aks-nodepool1-12345678-vmss000002 # receives pods again
```

### Upgrade Kubernetes

```powershell
# AKS: available versions and upgrade
az aks get-upgrades --resource-group rg-production --name aks-production --output table
az aks upgrade --resource-group rg-production --name aks-production --kubernetes-version 1.31.2

# GKE: first the control plane, then the node pools
gcloud container clusters upgrade gke-production --master --cluster-version 1.31 --region europe-southwest1
gcloud container clusters upgrade gke-production --node-pool default-pool --region europe-southwest1

# EKS: control plane and then the node groups
eksctl upgrade cluster --name eks-production --approve
eksctl upgrade nodegroup --cluster eks-production --name ng-general
```

### Basic Deployment

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tasks-api
  namespace: orders
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tasks-api
  template:
    metadata:
      labels:
        app: tasks-api
    spec:
      containers:
        - name: api
          image: ghcr.io/company/tasks-api:1.4.0
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              memory: 256Mi
          readinessProbe:
            httpGet:
              path: /
              port: 80
          livenessProbe:
            httpGet:
              path: /
              port: 80
```

### Equivalences

| Task | AKS | GKE | EKS |
|---|---|---|---|
| Connect | az aks get-credentials | gcloud container clusters get-credentials | aws eks update-kubeconfig |
| Scale nodes | az aks nodepool scale | gcloud container clusters resize | eksctl scale nodegroup |
| Upgrade | az aks upgrade | gcloud container clusters upgrade | eksctl upgrade cluster |
| Registry | Azure Container Registry | Artifact Registry | Amazon ECR |

## Common errors

- **CrashLoopBackOff**: The container starts and crashes over and over again. Look at `kubectl logs <pod> --previous`: it is usually an application error, a missing environment variable or secret, or a connection that cannot be established.
- **ImagePullBackOff or ErrImagePull**: It cannot download the image: the name or tag is misspelt, or the cluster does not have permission for the registry. On AKS, `az aks update --resource-group rg --name aks --attach-acr my-registry`; on the others, an `imagePullSecrets` or the node’s IAM permissions.
- **Pod in Pending**: No node can host it. `kubectl describe pod` tells you why: not enough CPU or memory, a `nodeSelector` or toleration that does not match, or a volume that cannot be mounted. Scale the nodes or adjust the `requests`.
- **OOMKilled**: The container exceeded its memory limit and Kubernetes killed it. Increase `limits.memory` or review the application’s usage with `kubectl top pods`.
- **“You must be logged in to the server (Unauthorized)”**: The credentials have expired or your user does not have access. Run the credentials command from step 2 again; on EKS, your IAM user or role must have an *access entry* in the cluster.
- **“gke-gcloud-auth-plugin not found”**: Since version 1.26, kubectl needs Google’s authentication plugin. Install it with `gcloud components install gke-gcloud-auth-plugin` and request the credentials again.
- **“drain” keeps waiting**: A PodDisruptionBudget does not allow more pods of that application to be evicted. Check with `kubectl get pdb -A` and increase the replicas before draining the node.

## Next steps

- Automate deployments with a pipeline (see the CI/CD with GitHub Actions guide).
- Enable autoscaling for pods (Horizontal Pod Autoscaler) and nodes (cluster autoscaler).
- Describe the infrastructure with Terraform to create and update clusters reproducibly.
- Centralise logs and metrics (for example, Prometheus and Grafana) and define alerts.
- Manage deployments with GitOps (Argo CD or Flux).
