How to Setup Kubernetes on GCP?

How to Setup Kubernetes on GCP

Kubernetes is an open-source container orchestration platform used for deploying, scaling, and managing containerized applications. Google Cloud Platform (GCP) provides a reliable and scalable environment for deploying Kubernetes clusters. In this article, we will explore how to set up Kubernetes on GCP and deploy a sample application.

Step 1: Creating a GCP Project

To begin, create a new project in the Google Cloud Console. If you don't already have a GCP account, you can sign up for a free trial. Once you have created a new project, enable the Kubernetes Engine API in the APIs & Services section of the Cloud Console.

Step 2: Creating a Kubernetes Cluster

Now that we have a project set up, we can create a Kubernetes cluster. Navigate to the Kubernetes Engine section of the Cloud Console and click "Create Cluster". Configure your cluster with the desired settings, such as the number of nodes and node machine type. You can also choose whether to enable automatic upgrades or not.

Step 3: Installing kubectl

Before we can interact with the Kubernetes cluster, we need to install kubectl, the command-line tool used to manage Kubernetes clusters. Follow the instructions for your operating system in the Kubernetes documentation to install kubectl.

Step 4: Configuring kubectl

After installing kubectl, we need to configure it to connect to our new cluster. Run the following command in your terminal, replacing [CLUSTER_NAME] with the name of your cluster:

gcloud container clusters get-credentials [CLUSTER_NAME]

This command will create a new kubeconfig file and set the current context to the new cluster.

Step 5: Deploying a Sample Application

Now that our cluster is set up and kubectl is configured, we can deploy a sample application. For this example, we will use the guestbook application from the Kubernetes documentation.

First, create a new file called guestbook.yaml and paste in the following YAML:

apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
app: redis
spec:
ports:
- port: 6379
targetPort: redis-server
selector:
app: redis
role: master
---
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
app: redis
spec:
ports:
- port: 6379
targetPort: redis-server
selector:
app: redis
role: slave
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google-samples/gb-frontend:v4
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80
name: php
- name: redis-master
image: gcr.io/google-samples/gb-redismaster:v1
ports:
- containerPort: 6379
name: redis-server
- name: redis-slave
image: gcr.io/google-samples/gb-redis-slave:v1
ports:
- containerPort: 6379
name: redis-server

This YAML file defines the guestbook application as a set of Kubernetes resources, including a Service and a Deployment.

Related Searches and Questions asked:

  • Kubernetes Objects Guide
  • How to Create Namespace in Kubernetes
  • How to Run Kubernetes with Calico
  • How to Run Kubernetes on Windows
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.

    Post a Comment

    0 Comments