Introduction to Kubernetes Engine

Google Kubernetes Engine (GKE) is a managed, production-ready environment for deploying containerized applications. It provides a powerful and flexible way to manage your containerized applications using Kubernetes, an open-source system for automating deployment, scaling, and management of containerized applications.

Key Concepts

  1. Kubernetes: An open-source platform designed to automate deploying, scaling, and operating application containers.
  2. Containers: Lightweight, standalone, executable software packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.
  3. Clusters: A set of nodes (virtual or physical machines) that run containerized applications managed by Kubernetes.
  4. Nodes: Individual machines within a cluster that run containerized applications.
  5. Pods: The smallest deployable units in Kubernetes, which can contain one or more containers.

Setting Up Kubernetes Engine

Step 1: Create a GKE Cluster

  1. Navigate to the GKE Console:

    • Go to the Google Cloud Console.
    • Select "Kubernetes Engine" from the navigation menu.
  2. Create a Cluster:

    • Click on "Create Cluster".
    • Choose the cluster type (Standard or Autopilot).
    • Configure the cluster settings (name, location, number of nodes, machine type, etc.).
    • Click "Create".

Step 2: Install kubectl

kubectl is a command-line tool for interacting with Kubernetes clusters.

  1. Install kubectl:

  2. Configure kubectl to Use Your GKE Cluster:

    • Run the following command to get the cluster credentials:
      gcloud container clusters get-credentials [CLUSTER_NAME] --zone [ZONE] --project [PROJECT_ID]
      

Deploying Applications on GKE

Step 1: Create a Deployment

A Deployment provides declarative updates to applications.

  1. Create a Deployment YAML File:

    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:latest
            ports:
            - containerPort: 80
    
  2. Apply the Deployment:

    kubectl apply -f deployment.yaml
    

Step 2: Expose the Deployment

To make your application accessible from outside the cluster, you need to create a Service.

  1. Create a Service YAML File:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-service
    spec:
      type: LoadBalancer
      selector:
        app: my-app
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
    
  2. Apply the Service:

    kubectl apply -f service.yaml
    

Practical Exercise

Exercise: Deploy a Sample Application on GKE

  1. Create a GKE Cluster:

    • Follow the steps outlined in the "Setting Up Kubernetes Engine" section to create a GKE cluster.
  2. Deploy a Sample Application:

    • Use the following Deployment YAML file to deploy a sample Nginx application:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx-deployment
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx:1.14.2
              ports:
              - containerPort: 80
      
    • Apply the Deployment:

      kubectl apply -f nginx-deployment.yaml
      
  3. Expose the Nginx Deployment:

    • Use the following Service YAML file to expose the Nginx deployment:

      apiVersion: v1
      kind: Service
      metadata:
        name: nginx-service
      spec:
        type: LoadBalancer
        selector:
          app: nginx
        ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      
    • Apply the Service:

      kubectl apply -f nginx-service.yaml
      
  4. Verify the Deployment:

    • Run the following command to get the external IP address of the Service:

      kubectl get services
      
    • Access the Nginx application using the external IP address in your web browser.

Common Mistakes and Tips

  • Incorrect YAML Syntax: Ensure that your YAML files are correctly indented and formatted.
  • Cluster Configuration: Double-check your cluster configuration settings, such as the number of nodes and machine types.
  • Service Exposure: Make sure the Service type is set to LoadBalancer to expose your application externally.

Conclusion

In this section, you learned how to set up and use Google Kubernetes Engine to deploy and manage containerized applications. You created a GKE cluster, deployed a sample application, and exposed it to the internet using a Service. This foundational knowledge will help you manage more complex applications and services on GKE in the future.

© Copyright 2024. All rights reserved