Kubernetes GKE
Demo - Kubernetes GKE
Install gcloud
CLI
test
random test textBefore deploying to GKE, you need to set up gcloud
(the Google Cloud SDK command-line tool)
- Install python 3
- Install the Google Cloud SDK.
Prepare kubectl
for GKE
https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke
install the gke-gcloud-auth-plugin
gcloud components install gke-gcloud-auth-plugin
Note: gcloud is the recommended way to install the binary on Windows and OS X
You need to have the CLI for GCP
gcloud
installed
Verify
gke-gcloud-auth-plugin --version
For Windows
gke-gcloud-auth-plugin.exe --version
Login to GCP from gcloud CLI
gcloud auth login
Switching between kubectl contexts
If you’re using Docker Desktop’s Kubernetes cluster on macOS or Windows, and you’ve switched your
kubectl
context to another cluster (like GKE), you can switch it back to Docker Desktop’s cluster with the following steps:
First, you can list your available contexts with:
kubectl config get-contexts
You’ll see a list of available contexts. The Docker Desktop cluster typically has the context name
docker-desktop
ordocker-for-desktop
.To switch to the Docker Desktop context, use:
kubectl config use-context docker-desktop
or, if the context is named differently:
kubectl config use-context docker-for-desktop
Verify that you’ve switched to the Docker Desktop context:
kubectl config current-context
This command should now return
docker-desktop
, indicating you’re now working with your Docker Desktop Kubernetes cluster.
Step 1: Setup K8S Cluster
Go to the GCP console and choose
Kubernetes Engine
- Go to Clusters
- Create (AutoPilot)
- Connect
Authenticate kubectl with the cluster:
After your cluster is created, you’ll need to get its credentials to configure
kubectl
:gcloud container clusters get-credentials <cluster> --region <region> --project <project>
Verify that you’ve switched to the GKE context:
kubectl config current-context
Step 2: Deploying the nginx Deployment
Using the deployment YAML file nginx-deployment.yaml
:
nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
Run the following command:
kubectl apply -f nginx-deployment.yaml
Check the status of the deployment:
kubectl get deployments
Step 3: Exposing the nginx Deployment using a Service
Using the service YAML file nginx-service.yaml
:
nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
Run the following command:
kubectl apply -f nginx-service.yaml
Check the status of the service:
kubectl get svc
This will show you the NodePort
on which the nginx
service is exposed.
Unfortunately the service has no external IP making it unaccessible. But there is a way to go directly to a pod with the port-forwarding function in kubectl.
kubectl port-forward service/nginx-service 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
Open a browser and go to localhost:8080
Change the service type to LoadBalancer
and re-apply the service. Verify that a loadbalancer is created
kubectl apply -f nginx-service.yaml
kubectl get svc
Open a browser and go to <Eternal IP>
Warning
Don´t forget to delete
- Kubernetes cluster
- Loadbalancer