Learning Kubernetes
What is Kubernetes?
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.
More information, please follow this link Kubernetes Overview.
Why we need Kubernetes
Containers are a good way to bundle and run your applications. In a production environment, you need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start. Wouldn’t it be easier if this behavior was handled by a system?
That’s how Kubernetes comes to the rescue! Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, and more. For example, Kubernetes can easily manage a canary deployment for your system.
Kubernetes can provide with :
- Service discovery and load balancing
- Storage orchestration
- Automated rollouts and rollbacks
- Automatic bin packing
- Self healing
- Secret and configuratio management
For more information please follow this link Why you Need Kubernetes.
Start using Kubernetes
Getting Start with Kubernetes on Docker Desktop
Docker Desktop is the easiest way to run Kubernetes on your local machine - it gives you a fully certified Kubernetes cluster and manages all the components for you.
In this lab you’ll learn how to set up Kubernetes on Docker Desktop and run a simple demo app. You’ll gain experience of working with Kubernetes and comparing the app definition syntax to Docker Compose.
Install Docker Desktop from official website, please follow this link Install Docker Desktop. Docker Desktop is freely available in a community edition, for Windows and Mac.
Enable Kubernetes
Kubernetes itself runs in containers. When you deploy a Kubenetes cluster you first install Docker (or another container runtime like containerd) and then use tools like kubeadm which starts all the Kubernetes components in containers. Docker Desktop does all that for you.
Verify Kubernetes Cluster
If you’ve worked with Docker before, you’re used to managing containers with the docker
and docker-compose
command lines. Kubernetes uses a different tool called kubectl
to manage apps - Docker Desktop installs kubectl
for you too.
The cluster can be interacted with using the kubectl CLI. This is the main approach used for managing Kubernetes and the applications running on top of the cluster.
Details of the cluster and its health status is.
|
|
We should get an output
|
|
To view the nodes in the cluster with.
|
|
We shoulkd get an output
|
|
If the node is marked as NotReady then it is still starting the components.
This command shows all nodes that can be used to host our applications. Now we have only one node, and we can see that it’s status is ready (it is ready to accept applications for deployment).
Deploy Container
With a running Kubernetes cluster, containers can now be deployed.
Using kubectl run
, it allows containers to be deployed onto the cluster.
|
|
To check status of the deployment.
|
|
We should get an output
|
|
Once the container is running it can be exposed via different networking options, depending on requirements. One possible solution is NodePort, that provides a dynamic port to a container.
|
|
The command below finds the allocated port and executes a HTTP request.
|
|
We should get an output
|
|
Kubernetes Dashboard
Deploy latest kubernetes dashboard, you can deploy the dashboard itself with the following single command.
|
|
If your cluster is working correctly, you should see an output confirming the creation of a bunch of Kubernetes components like in the example below.
|
|
Afterwards, you should have two new pods running on your cluster.
|
|
We should get an output
|
|
Running kubectl proxy
to accessing the dashbooard, then access this link http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
.
If we get an output
Get token to access dashboard page and paste on authentication page.
|
|
Detail information dashboard please follow Kubernetes.io
Stopping Kubernetes, by type following script
|
|
We should get an output
|
|
Setting Management Script
The steps to deploy or delete the dashboard are not complicated but they can be further simplified.
The following script can be used to start, stop or check the dashboard status.
Create file kubernetes-dashboard.sh
|
|
|
|
Make this script executable
|
|
Start the dashboard
|
|
Check whether the dashboard is running or not and output the tokens if currently set.
|
|
Stop the dashboard
|
|
Deploy Container using YAML
Create a kubernetes deployment YAML to tell kubernetes to manage a set of replicas of that Pod — literally, a ReplicaSet — to make sure that a certain number of them are always available. So we might start our Kubernetes Deployment manifest definition YAML deployment.yaml
like this:
|
|
kubectl
create deploymenthello-world
kubectl
create 3 replica pods, you can change how many replicas you wantkubectl
create deployment container frompiinalpin/sample-node-web-app
images and expose container port8080
to master
Run YAML file by type following code
|
|
Check the deployment list
|
|
We should get an output
|
|
Check pods running
|
|
We shoul get an output
|
|
There are three replicas created from YAML file.
Create Service using YAML
Create expose deployment YAML file service.yaml
like this:
|
|
kubectl
create service namehello-world
kubectl
expose deployment which is labeled webkubectl
create connection to port8080
with type isNodePort
Execute service YAML file by type following code
|
|
Check service is running
|
|
We should get and output
|
|
Check port is exposed and we can execute http request
|
|
We should get an output
|
|
To delete service and deployment
|
|
Create Deployment and Service on YAML
Create file hello-world.yaml
and fill like this:
|
|
Execute service YAML file by type following code
|
|
We should get an output
|
|
Check deployment and service is running
|
|
Check exposed port with http request
|
|
We should get an output
|
|
Create Deployment, Service and Ingress with Load Balancer YAML
Create file my-kubernetes.yaml
and fill like this:
|
|
kubectl
create deploymenthello-deployment
kubectl
create 3 replica pods, you can change how many replicas you wantkubectl
create deployment container fromgcr.io/google-samples/hello-app:2.0
images and expose container port50000
to masterkubectl
create deployment container frompiinalpin/sample-node-web-app
images and expose container port8080
to masterkubectl
create service namehello-service
kubectl
expose deployment which is labeled webkubectl
create port nameworld-port
expose port50000
to8001
kubectl
create port namekubernetes-port
expose port8080
to8002
kubectl
create typeLoad Balancer
kubectl
createIngress
namedhello-world
kubectl
expose external IPlocalhost
kubectl
create path/hello-world
on port8001
kubectl
create path/hello-kubernetes
on port8002
Thankyou
Docker Labs - Getting Started with Kubernetes on Docker Desktop
kubernetes.io - What is Kubernetes?
KataCoda - Launch Single Node Kubernetes Cluster
Mirantis - Introduction to YAML: Creating a Kubernetes deployment
Network Chuck - you need to learn Kubernetes RIGHT NOW!!
kubernetes.io - Exposing an External IP Address to Access an Application in a Cluster UpCloud - How to deploy Kubernetes Dashboard quickly and easily