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.
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.
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.
kubectl cluster-infoWe should get an output
Kubernetes control plane is running at https://kubernetes.docker.internal:6443
KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.To view the nodes in the cluster with.
kubectl get nodesWe shoulkd get an output
NAME STATUS ROLES AGE VERSION
docker-desktop Ready master 4d22h v1.19.7If 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.
kubectl create deployment hello-world --image=piinalpin/sample-node-web-appTo check status of the deployment.
kubectl get podsWe should get an output
NAME READY STATUS RESTARTS AGE
hello-world-6b64556c5b-9pzrp 1/1 Running 0 88sOnce 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.
kubectl expose deployment hello-world --port=8080 --type=NodePortThe command below finds the allocated port and executes a HTTP request.
export NODE_PORT=$(kubectl get svc hello-world -o go-template='{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}')
echo "Accessing http://localhost:$NODE_PORT"
curl http://localhost:$NODE_PORTWe should get an output
{"status":"success","message":"Hello World! This api from Node.js"}Kubernetes Dashboard
Deploy latest kubernetes dashboard, you can deploy the dashboard itself with the following single command.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml 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.
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper createdAfterwards, you should have two new pods running on your cluster.
kubectl get pods -AWe should get an output
NAMESPACE NAME READY STATUS RESTARTS AGE
...
kubernetes-dashboard dashboard-metrics-scraper-7b59f7d4df-qkztd 1/1 Running 0 30s
kubernetes-dashboard kubernetes-dashboard-74d688b6bc-s4595 1/1 Running 0 30sRunning 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.
kubectl get secret -n kubernetes-dashboard $(kubectl get serviceaccount default -n kubernetes-dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decodeDetail information dashboard please follow Kubernetes.io
Stopping Kubernetes, by type following script
kubectl delete -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yamlWe should get an output
namespace "kubernetes-dashboard" deleted
serviceaccount "kubernetes-dashboard" deleted
service "kubernetes-dashboard" deleted
secret "kubernetes-dashboard-certs" deleted
secret "kubernetes-dashboard-csrf" deleted
secret "kubernetes-dashboard-key-holder" deleted
configmap "kubernetes-dashboard-settings" deleted
role.rbac.authorization.k8s.io "kubernetes-dashboard" deleted
clusterrole.rbac.authorization.k8s.io "kubernetes-dashboard" deleted
rolebinding.rbac.authorization.k8s.io "kubernetes-dashboard" deleted
clusterrolebinding.rbac.authorization.k8s.io "kubernetes-dashboard" deleted
deployment.apps "kubernetes-dashboard" deleted
service "dashboard-metrics-scraper" deleted
deployment.apps "dashboard-metrics-scraper" deletedSetting 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
nano ~/kubernetes-dashboard.sh#!/bin/bash
showtoken=1
cmd="kubectl proxy"
count=`ps aux | grep -v "grep" | grep -c "$cmd"`
dashboard_yaml="https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml"
msgstarted="-e Kubernetes Dashboard \e[92mstarted\e[0m"
msgstopped="Kubernetes Dashboard stopped"
case $1 in
start)
kubectl apply -f $dashboard_yaml >/dev/null 2>&1
if [ $count = 0 ]; then
nohup $cmd >/dev/null 2>&1 &
echo $msgstarted
else
echo "Kubernetes Dashboard already running"
fi
;;
stop)
showtoken=0
if [ $count -gt 0 ]; then
kill -9 $(pgrep -f "$cmd")
fi
kubectl delete -f $dashboard_yaml >/dev/null 2>&1
echo $msgstopped
;;
status)
found=`kubectl get serviceaccount default -n kubernetes-dashboard 2>/dev/null`
if [[ $count = 0 ]] || [[ $found = "" ]]; then
showtoken=0
echo $msgstopped
else
found=`kubectl get clusterrolebinding default -n kubernetes-dashboard 2>/dev/null`
if [[ $found = "" ]]; then
nopermission=" but user has no permissions."
echo $msgstarted$nopermission
echo 'Run "dashboard start" to fix it.'
else
echo $msgstarted
fi
fi
;;
esac
# Show full command line # ps -wfC "$cmd"
if [ $showtoken -gt 0 ]; then
# Show token
echo "Admin token:"
kubectl get secret -n kubernetes-dashboard $(kubectl get serviceaccount default -n kubernetes-dashboard -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode
echo
echo "Go to:"
echo "http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/"
fiMake this script executable
chmod +x ~/kubernetes-dashboard.shStart the dashboard
./kubernetes-dashboard.sh start Check whether the dashboard is running or not and output the tokens if currently set.
./kubernetes-dashboard.sh statusStop the dashboard
./kubernetes-dashboard.sh stopDeploy 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:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: hello-world
image: piinalpin/sample-node-web-app
ports:
- containerPort: 8080kubectlcreate deploymenthello-worldkubectlcreate 3 replica pods, you can change how many replicas you wantkubectlcreate deployment container frompiinalpin/sample-node-web-appimages and expose container port8080to master
Run YAML file by type following code
kubectl apply -f deployment.yamlCheck the deployment list
kubectl get deploymentsWe should get an output
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 3/3 3 3 20sCheck pods running
kubectl get podsWe shoul get an output
NAME READY STATUS RESTARTS AGE
hello-world-f74567869-lsp74 1/1 Running 0 7m7s
hello-world-f74567869-m5vjd 1/1 Running 0 7m7s
hello-world-f74567869-z7pg7 1/1 Running 0 7m7sThere are three replicas created from YAML file.
Create Service using YAML
Create expose deployment YAML file service.yaml like this:
apiVersion: v1
kind: Service
metadata:
name: hello-world
labels:
app: web
spec:
selector:
app: web
ports:
- protocol: TCP
port: 8080
type: NodePortkubectlcreate service namehello-worldkubectlexpose deployment which is labeled webkubectlcreate connection to port8080with type isNodePort
Execute service YAML file by type following code
kubectl apply -f service.yamlCheck service is running
kubectl get svcWe should get and output
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world NodePort 10.107.135.246 <none> 8080:31302/TCP 8s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d2hCheck port is exposed and we can execute http request
curl http://localhost:<EXPOSED_PORT>We should get an output
{"status":"success","message":"Hello World! This api from Node.js"}To delete service and deployment
kubectl delete -f deployment.yaml
kubectl delete -f service.yamlCreate Deployment and Service on YAML
Create file hello-world.yaml and fill like this:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: hello-world
image: piinalpin/sample-node-web-app
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: hello-world
labels:
app: web
spec:
selector:
app: web
ports:
- protocol: TCP
port: 8080
type: NodePortExecute service YAML file by type following code
kubectl apply -f hello-world.yamlWe should get an output
deployment.apps/hello-world created
service/hello-world createdCheck deployment and service is running
kubectl get deployments
kubectl get pods
kubectl get svcCheck exposed port with http request
curl http://localhost:<EXPOSED_PORT>We should get an output
{"status":"success","message":"Hello World! This api from Node.js"}Create Deployment, Service and Ingress with Load Balancer YAML
Create file my-kubernetes.yaml and fill like this:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-deployment
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: hello-world
image: gcr.io/google-samples/hello-app:2.0
env:
- name: "PORT"
value: "50000"
- name: hello-kubernetes
image: piinalpin/sample-node-web-app
env:
- name: "PORT"
value: "8080"
---
apiVersion: v1
kind: Service
metadata:
name: hello-service
labels:
app: web
spec:
selector:
app: web
ports:
- name: world-port
protocol: TCP
port: 8001
targetPort: 50000
- name: kubernetes-port
protocol: TCP
port: 8002
targetPort: 8080
type: LoadBalancer
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world
spec:
rules:
- http:
paths:
- path: /hello-world
pathType: Prefix
backend:
service:
name: hello-service
port:
number: 8001
- path: /hello-kubernetes
pathType: Prefix
backend:
service:
name: hello-service
port:
number: 8002kubectlcreate deploymenthello-deploymentkubectlcreate 3 replica pods, you can change how many replicas you wantkubectlcreate deployment container fromgcr.io/google-samples/hello-app:2.0images and expose container port50000to masterkubectlcreate deployment container frompiinalpin/sample-node-web-appimages and expose container port8080to masterkubectlcreate service namehello-servicekubectlexpose deployment which is labeled webkubectlcreate port nameworld-portexpose port50000to8001kubectlcreate port namekubernetes-portexpose port8080to8002kubectlcreate typeLoad BalancerkubectlcreateIngressnamedhello-worldkubectlexpose external IPlocalhostkubectlcreate path/hello-worldon port8001kubectlcreate path/hello-kuberneteson port8002
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


