Cloud is in the air, literally !!
Kubernetes has been a game changer, when it comes to scaling your applications and the ease with which you can deploy containers on a cluster.
Learn how easy it is to deploy a container on your very own local kubernetes cluster, no need to worry about getting access to a Azure/AWS/GCP subscription. Just follow along.
Here are the Topics we will cover
- Enable Kubernetes on DockerDesktop
- Deploy a “Hello World” app on your local cluster
- Deploy a NGINX app & access it from local browser using
- Port Forwarding
NOTE: Docker Desktop Stable releases require the Hyper-V feature which is not available in the Windows 10 Home edition.
Enable Kubernetes Cluster on DockerDesktop
You can use other tools such as Minikube or Kind which are also suited for running a single Node Cluster locally.
First and foremost, Enable Kubernetes & Restart Docker Desktop
We now have a single-node cluster running on our systems, you can run
kubectl commands and play around a bit before getting started.
On checking cluster-info shown below, we find that a master is running, now do remember that this is a single-node cluster i.e. master & worker are are on the same node. (A more practical use-case would have a master and many worker nodes to achieve High Availability-HA).
Check namespaces, not that it’s relevant in this context but remember we will be working in the default namespace.
Also, you will find a default ‘kubernetes’ service already running on clusterIP.
Deploy “Hello World” App on the Cluster
Lets start out by deploying a simple “Hello World” application. For this we will create a Deployment and a Service — which connects to the deployment.
Do remember that you can either run kubectl commands directly from a Power shell window or create manifest Yaml file(s) as shown below.
1. Deployment Creation
By creating a manifest file with kind: Deployment, we are essentially creating containers, pods, replicaSets in the background.
apiVersion: apps/v1 kind: Deployment metadata: name: hello spec: selector: matchLabels: app: hello version: 2.0.0 replicas: 2 template: metadata: labels: app: hello version: 2.0.0 spec: containers: - name: hello image: "gcr.io/google-samples/hello-app:2.0" env: - name: "PORT" value: "50001"
Pull a container from gcr.io/google-samples/hello-app:2.0 and
replicas: 2 means that there will be 2 pods running any point of time._
2. Services — Access the deployment
apiVersion: v1 kind: Service metadata: name: hello-service spec: type: NodePort selector: app: hello version: 2.0.0 ports: - name: http protocol: TCP port: 8081 targetPort: 50001 nodePort: 32000
Here the ‘hello-service’ connects to all the pods created above having name ‘hello’ as mentioned by the selector attribute at targetPort=50001
Remember, we are creating a NodePort service to enable access from outside the cluster. From within the cluster, all the pods are accessible through ClusterIP.
Hello World is deployed and is accessible through nodePort, http://localhost:32000/
Hello, world! Version: 2.0.0 Hostname: hello-fdf67c7-b4fs4
Deploying Nginx in 3 steps
We will be deploying an nginx:alpine image (from the public docker hub registry) onto a local kubernetes cluster running on our DockerDesktop.
1. Create Deployment
Lets create nginx deployment, this will create a Deployment and a Pod
kubectl create deploy nginx —image=nginx:alpine
2. Create Service to expose deployment
We want our nginx app to be accessible from any pod within cluster, so we expose it.
kubectl expose deploy nginx --port=80 --target-port=8000
Verify if this nginx app is accessible from any other pod within the cluster, add a ubuntu test pod and run curl on the Pod IP
# lets connect to the nginx app using clusterIP $ kubectl get service nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP 10.106.163.0 <none> 80/TCP 25h # spin up a test ubuntu pod $ kubectl run -i --tty ubuntu --image=ubuntu --restart=Never -- sh # check connectivity through PodIP and Port $ curl http://10.1.0.13:80
BINGO !! we can reach the nginx deployment from within cluster
3. Access the deployment from outside the cluster (locally)
Now we want to make this nginx deployment accessible from outside the cluster.
Do remember that each Pod within a Cluster has a unique IP address, which can be accessed from outside the cluster through a Service, but if you are running cluster locally this can be easily achieved through Port Forwarding.
Port Forwarding will allow you to connect to a Pod on a forwarded(different) port from you local machine, without exposing the Services.
Lets port-forward our nginx deployment to a random port.
$ kubectl port-forward deployment/nginx :80 Forwarding from 127.0.0.1:56271 -> 80 Forwarding from [::1]:56271 -> 80 Handling connection for 56271 Handling connection for 56271 Handling connection for 56271 Handling connection for 56271
Open up your browser and point to the localhost:56271 (forwarded port)
NOTE: Port-Forwarding need to be running all the time for this to work
Hope this blog helps you in getting started with kubernetes.
It’s an open ground, Play around !!
Top comments (0)