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
- Kubernetes: An open-source platform designed to automate deploying, scaling, and operating application containers.
- 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.
- Clusters: A set of nodes (virtual or physical machines) that run containerized applications managed by Kubernetes.
- Nodes: Individual machines within a cluster that run containerized applications.
- Pods: The smallest deployable units in Kubernetes, which can contain one or more containers.
Setting Up Kubernetes Engine
Step 1: Create a GKE Cluster
-
Navigate to the GKE Console:
- Go to the Google Cloud Console.
- Select "Kubernetes Engine" from the navigation menu.
-
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.
-
Install
kubectl
:- Follow the installation instructions for your operating system from the official Kubernetes documentation.
-
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]
- Run the following command to get the cluster credentials:
Deploying Applications on GKE
Step 1: Create a Deployment
A Deployment provides declarative updates to applications.
-
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
-
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.
-
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
-
Apply the Service:
kubectl apply -f service.yaml
Practical Exercise
Exercise: Deploy a Sample Application on GKE
-
Create a GKE Cluster:
- Follow the steps outlined in the "Setting Up Kubernetes Engine" section to create a GKE cluster.
-
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
-
-
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
-
-
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.
Google Cloud Platform (GCP) Course
Module 1: Introduction to Google Cloud Platform
- What is Google Cloud Platform?
- Setting Up Your GCP Account
- GCP Console Overview
- Understanding Projects and Billing
Module 2: Core GCP Services
Module 3: Networking and Security
Module 4: Data and Analytics
Module 5: Machine Learning and AI
Module 6: DevOps and Monitoring
- Cloud Build
- Cloud Source Repositories
- Cloud Functions
- Stackdriver Monitoring
- Cloud Deployment Manager
Module 7: Advanced GCP Topics
- Hybrid and Multi-Cloud with Anthos
- Serverless Computing with Cloud Run
- Advanced Networking
- Security Best Practices
- Cost Management and Optimization