In this document, I will show you the from the basic, how to write a simple app, build it, containerize it, deploy in kubernetes and expose to external world using Kubernetes services.
install minikube, docker, node,
- minikube status minikube type: Control Plane host: Running kubelet: Running apiserver: Running kubeconfig: Configured
- we are going with angular application.
- check the version or install it.
-
vi index.html
<!DOCTYPE html> <html> <head> <title>HTML Website</title> </head> <body> <h1>Kubernetes Demo</h1> <p>Here you will learn kubernetes.</p> </body> </html>
-
vi Dockerfile FROM python COPY index.html index.html EXPOSE 6100 CMD ["python", "-m","http.server","6100"]
-
docker build .
-
docker images
-
docker run -it -p 6100:6100 48094f98be49
-
docker tag 48094f98be49 docker.io/dharmesh09/html-web:v1
-
docker push docker.io/dharmesh09/html-web:v1
-
now with docker your html application is working well.
-
with kubernetes let's try
-
For kubernetes required
- deployment.yaml
- service.yaml
-
vi deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: html-web labels: app: html-web spec: replicas: 1 selector: matchLabels: app: html-web template: metadata: labels: app: html-web spec: containers: - name: html-web image: docker.io/dharmesh09/html-web:v1 ports: - containerPort: 6100
-
kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES html-web-799dcc499f-4ckt9 1/1 Running 0 5m20s 10.244.0.9 minikube
-
check with this ip pod can communicate or not.
-
minikube ssh
-
curl -L 10.244.0.9:6100 [here you will see your HTML file] docker@minikube:~$ curl -L 10.244.0.9:6100 <title>HTML Website</title>
<h1>Kubernetes Demo</h1> <p>Here you will learn kubernetes.</p> </body> </html>
-
here when your pod is killed or recreated that time it's assigned with new ip address. so, here we need service.yaml becoz it's ip is not chnage that's advantage of it.
-
vi service.yaml apiVersion: v1 kind: Service metadata: name: html-web spec: type: NodePort selector: app: html-web ports:
- protocol: TCP port: 80 targetPort: 6100 nodePort: 31080
-
here nodePort size between: 30k to 32k 20.kubectl apply -f service.yaml
-
kubectl get svc -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR html-web NodePort 10.109.119.202 80:31080/TCP 8s app=html-web kubernetes ClusterIP 10.96.0.1 443/TCP 23h
-
again you can check with now cluster ip your ssh will comunicate or not.
-
minikube ssh
-
docker@minikube:~$ curl -L 192.168.49.2:31080 <title>HTML Website</title>
<h1>Kubernetes Demo</h1> <p>Here you will learn kubernetes.</p> </body> </html>
-
now your pod is 100 time recreated you will get response with this ip's only. that's advantage of k8s.
-
now this ip is only communicate only internal cluster communication.
-
i want to access it on my crome browser.
-
it happened by ingress only.
-
vi ingress.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-wildcard-host spec: rules:
- host: html-web.com
http:
paths:
- pathType: Prefix path: "/" backend: service: name: html-web port: number: 80
- kubectl apply -f ingress.yaml
- kubectl get ingress -n default NAME CLASS HOSTS ADDRESS PORTS AGE ingress-wildcard-host html-web 192.168.49.2 80 21h
done
● ● ● ●