Introduction to Kubernetes
Kubernetes is an open source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It helps manage workloads efficiently, ensuring high availability and scalability.
Key concepts in Kubernetes
- cluster: A set of nodes (machines) running a containerized application.
- node: A single machine in a Kubernetes cluster (physical or virtual).
- pod: The smallest deployable unit in Kubernetes, containing one or more containers.
- Serve: An abstraction that defines a logical set of Pods and the policies for accessing them.
- deploy: Manage the desired state of Pods and ReplicaSets.
- ConfigMap and Secret: Store configuration data and sensitive information separately.
- namespace: Provides a way to divide cluster resources among multiple consumers.
- Entrance: Manages external access to services in the cluster, usually HTTP.
Set up Kubernetes on Google Cloud
Step 1: Prerequisites
- A Google Cloud account. Register here.
- Install Google Cloud CLI (gcloud) on your local machine. Install gcloud CLI.
- Install kubectl (Kubernetes CLI):
gcloud components install kubectl
Step 2: Create a Google Kubernetes Engine (GKE) cluster
Using Google Cloud Console:
- Go to Google Kubernetes Engine Page.
- Click Create a cluster.
- Select the cluster type (for example, Standard or Autopilot).
- Configure cluster settings such as number of nodes, machine type, and location (region/region).
- Click create Configure the cluster.
Use the command line:
- Enable Kubernetes engine API:
gcloud services enable container.googleapis.com
- Create a GKE cluster:
gcloud container clusters create my-cluster --zone us-central1-a --num-nodes=3
replace my-cluster
Use your desired cluster name and us-central1-a
with your preferred area.
- Authenticate kubectl using clustering:
gcloud container clusters get-credentials my-cluster --zone us-central1-a
Step 3: Verify the cluster
kubectl get nodes
- You should see a list of nodes indicating that the cluster is up and running.
Kubernetes basic operations
1. Deploy the application
Using Google Cloud Console:
- Navigate to workload tab in the Kubernetes Engine section.
- Click deploy.
- Specify the container image (for example,
gcr.io/google-samples/hello-app:1.0
). - Configure deployment settings (e.g. replicas, namespaces).
- Click deploy.
Use kubectl:
kubectl create deployment hello-app --image=gcr.io/google-samples/hello-app:1.0
- Expose the deployment as a service:
kubectl expose deployment hello-app --type=LoadBalancer --port=80 --target-port=8080
- Get the external IP of the service:
kubectl get service hello-app
- Use an external IP to access the application.
2. Extend the application
Using Google Cloud Console:
- Go to workload tab.
- Click the deployment you want to expand.
- Adjust the number of copies and click save.
Use kubectl:
- Scale the deployment to 3 replicas:
kubectl scale deployment hello-app --replicas=3
kubectl get pods
3. Update the application
Using Google Cloud Console:
- Go to workload tab.
- Click on the deployment you want to update.
- Edit the container image to a new version (for example,
gcr.io/google-samples/hello-app:2.0
). - Click save Start the update.
Use kubectl:
- Update image version:
kubectl set image deployment/hello-app hello-app=gcr.io/google-samples/hello-app:2.0
- Monitor release status:
kubectl rollout status deployment/hello-app
4. Delete resources
Using Google Cloud Console:
- Navigate to workload or Services and entrance tab.
- Select the resource you want to delete.
- Click delete and confirm.
Use kubectl:
kubectl delete service hello-app
kubectl delete deployment hello-app
Advanced themes
ConfigMap and Secret
Using Google Cloud Console:
- Go to Configuration mapping or secret tab under Kubernetes Engine.
- Click create and fill in the required details.
Use kubectl:
kubectl create configmap my-config --from-literal=key1=value1
kubectl create secret generic my-secret --from-literal=password=12345
helm chart
- Helm is the package manager for Kubernetes.
- Install the helmet:
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
- Deploy your application using Helm:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-release bitnami/nginx
Monitor and record
Using Google Cloud Console:
- Go to operations tab in the Cloud Console.
- use Indicator browser Monitor cluster performance.
- Under access log Logging > Log Browser View system and workload logs.
Using gcloud CLI:
- Enable logging for your cluster:
gcloud container clusters update my-cluster --logging=SYSTEM,WORKLOAD --monitoring=SYSTEM
clean up
Using Google Cloud Console:
- Navigate to Kubernetes engine Page.
- Select the cluster to delete.
- Click delete and confirm.
Using gcloud CLI:
- Delete the GKE cluster to avoid charges:
gcloud container clusters delete my-cluster --zone us-central1-a
Other resources
By following this guide, you can learn the basics of Kubernetes and effectively use it with Google Cloud both locally and through the Google Cloud Console. Expand your knowledge by exploring advanced features and best practices.