Kubernetes on GCP: Setting up a cluster on Google Cloud Platform (with GKE)

Kubernetes on GCP: Setting up a cluster on Google Cloud Platform (with GKE)

Prerequisites

  • A Google Cloud Platform account with administrative access
  • A basic understanding of Kubernetes concepts
  • A local machine with the gcloud and kubectl command-line tools installed

Step 1: Create a GKE Cluster

  • Open the GCP Console and navigate to the GKE console.
  • Click on “Create cluster”.
  • Choose a name for your cluster and select the region and zone where you want to create it.
  • Choose the number and type of nodes you want to create in your cluster.
  • Choose the machine type and size for your nodes.
  • Choose the networking options for your cluster.
  • Review your settings and click on “Create”.

Step 2: Configure kubectl

  • Install the gcloud CLI tool if you haven’t already done so.
  • Run the following command to authenticate kubectl with your GCP account:
  • gcloud auth login
  • This command opens a web page and asks you to log in to your GCP account.
  • Run the following command to configure kubectl to use your GKE cluster:
  • gcloud container clusters get-credentials my-cluster --zone us-central1-a --project my-project
  • Replace my-cluster with the name of your GKE cluster, us-central1-a with the zone where your cluster is located, and my-project with your GCP project ID.
  • This command updates your kubectl configuration to use the GCP account that you used to create your cluster. It also sets the current context to your GKE cluster.

Step 3: Verify Your Cluster

kubectl get nodes

Step 4: Deploy Applications to Your Cluster

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer
kubectl apply -f nginx.yaml

Leave a comment

Your email address will not be published. Required fields are marked *