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
gcloudinstalled
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
kubectlcontext 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-contextsYou’ll see a list of available contexts. The Docker Desktop cluster typically has the context name
docker-desktopordocker-for-desktop.To switch to the Docker Desktop context, use:
kubectl config use-context docker-desktopor, if the context is named differently:
kubectl config use-context docker-for-desktopVerify that you’ve switched to the Docker Desktop context:
kubectl config current-contextThis 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:latestRun the following command:
kubectl apply -f nginx-deployment.yamlCheck the status of the deployment:
kubectl get deploymentsStep 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: NodePortRun the following command:
kubectl apply -f nginx-service.yamlCheck the status of the service:
kubectl get svcThis 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 8080Open 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 svcOpen a browser and go to <Eternal IP>
Warning
Don´t forget to delete
- Kubernetes cluster
- Loadbalancer