Docker Commands

 docker login: Logging into docker hub.



docker build -t <Image Name> . : Build docker Image on lacal.



docker images: List of all docker images on local


docker tag user-service nileshkadam222/user-service
To tag a local image to repository image

docker push nileshkadam222/user-service: push the local image to docker hub repository.



docker rmi user-service nileshkadam222/user-service: remove docker image from local

docker run -p 9002:9002 nileshkadam222/user-service: pull image from docker hub and run it on given port

docker ps -a : List out all stop and running containers

docer stop <cobtainer id> : stop container

docker start : start container 



Kubernets 

kubectl version -> check kubernate version

kubectl cluster-info -> View cluster information

kubectl get all -> retrieve information about kubernates pods, deploymets ,services and more

kubectl run [container-name] --image=[image-name] -> simple way to create a deployement for pods

kubectlport-forward [pod] [ports] -> Forward a port to allow external access.

kubectl expose .. -> expose a port for deployement

kubectl create [resource] -> create a resource

kubectl apply [resource]  -> create or modify a resource

---------------------------------------------------------------

Aliasing kubectl

set-Alias -Name k -value kubectl   -> Power shell

alias k="kubectl"   -> Mac/Linux

---------------------------------------------------------------

Creating Pods : 

Pod is the basic execution unit of a kubernates application-the smallest and simplest unit in the kubernates object model that you create or deploy.

- Pod act as enviroment for container

- Smallest Object of the kubernate object

- Organize application parts into pods (Servers , caching ,APIs, Database,)

- pod has IP,Memory, Volumes etc

- scale horizontally by adding pod replicas


kubectl run [podname] --image=nginx:alpine


kubectl create/apply 


kubectl port-forward [name-of-pod] 8080:80   -> enale pod container to be called externally

kubectl delete pod [name-od-pod]

kubectl delete deployement [name-of-pod]



-------------------------------------------------- Defination for pod---------

appVersion: v1 (Kubernates API Version)

kind : pod  (Type of kubernate resource)

metadata: (Metadata about pod)

  name: my-nginx

spec: (Blueprint for the pod)

  containers: (Information about the containers that will run ib the pod)

  - name : my-nginx

    image: nginx:alpine

Creating a pod using YAML :  To Create a pod using YAML use the kubectl create command along with the --filename or -f swith

Kubectl create -f file.pod.yml --dry-run --validate=true'

-- dry-run : try the command and see what it would generate in the output.

--validate=true : Validate the YAML file.


Kubectl create -f file.pod.yml


An alternate way to create or apply changes to a pod from the YAML file.

kubectl apply -f file.pod.yml : replace the existiing pod or create new pod if pod is not exist.


Use --save-config when you want to use kubectl apply in the future

kubectl create -f file.pod.yml --save-config


Delete a pod 

kubectl delete pod [pod name]


delete pod using YAML file that created it

kubectl delete -f file.pod.yml


Get Pod information

kubectl get pod my-nginx -o yaml

kubectl describe pod my-nginx


Execute pod 

kubectl exec my-nginx -it  sh


Edit pod YMAL File

>kubectl edit -f nginx.pod.yml


Pod Health:


Kubernetes relies on Probes to determine the health of a Pod container

Probes is a diagnostic performed periodically by the kubelet on the container

Type of Probes

1.. Liveness probe: can use to determine if a pod is healthy and running as expected

2. Readiness probe: Can be used to determine if a pod should receive the request.

Failed pod containers are recreated by default (restatPolicy default to always)


ExecAction : Execute an action inside the container

TCPSocketAction : TCP Chek against the containers IP address on a specified port

HTTPGetAction : HTTP Get request against container


Probes can have the following results:

- Success

-Failure

- Unkonwn


Liveness Probe :

 livenessProbe: //define liveness probe
      httpGet:
        path: /index.html // check index.html on port 80
        port: 80
      initialDelaySeconds: 15 // Waith 15 seconds
      timeoutSeconds: 2 //Time after 2 seconds
      periodSeconds: 2 //check eery 5 seconds
      failureThreshold: 1 //Allow 1 failure before failing


ExecAction 

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
  labels:
    app: nginx
    rel: stable
spec:
  containers:
  - name : my-nginx
    image: nginx:alpine
   args:
   - /bin/sh
   - -c
   - touch /tmp/healthy; sleep 30;
     rm -f /tmp/healthy; sleep 600

    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 2
    ports:
    - containerPort: 80



TCPSocketAction


 readinessProbe: //define liveness probe
      httpGet:
        path: /index.html  // check index.html on port 80
        port: 80
      initialDelaySeconds: 15 // Waith 15 seconds
      timeoutSeconds: 2 //Time after 2 seconds
      periodSeconds: 2  //check eery 5 seconds
      failureThreshold: 1 //Allow 1 failure before failing


Rediness Probe : When should a container start receiving traffic?

Liveness prob : when should a container restart?


Deployments:

Replica set is a declarative way to manage pods

Deplyments and replicaSets ensure pods stay running and can be used to scale pods.

Role of ReplicaSets : ReolicaSets act as a pod controller

1. Self Healing mechanism

2. Ensure the requested number of pods are available

3. Provide fault-tolerance

4. Can be used to scale Pods

5. Relies on a Pod template

6. No need to create pods directly


A deploymment manages Pods

1. Pods are managed using ReplicaSets

2. Scales ReplicaSets, which scale pods

3. Supports zero-downtime updates by creating and destroying ReplicaSets

4. Roll back finctionallity

5. Create a unique lables that is assigned to the replicaSets and generate pods

6. YAML is very similar to ReplicaSet


Creating a Deployment


apiVersion: apps/v1 //Kubernetes API version and resourse type(Deployment)
kind: deployment
metadata : //metadata about deployment
  name : frontend
  labels:
    app: my-nginx
    tier: frontend
spec:
  selector: //The selector is used to select the template to use (based on labels)
    matchLabels:
      tier: frontend
  template: /to use to create the pod(node that the selector for matches the label)
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
        - name : my-nginx
          image : nginx:alpine


Create a deployment

use create and apply command

List of all deoloyments

kubectl get deployments


Deployments and Labels

kubectl get deployment --show-labels

list the labels for all deployments using the --show-labels switch

To get information about a deployment with specific lables, use the -l seitch

kubectl get deployment -l app=nginx


Delete Deployment

To delete a Deployment use kubectl delete

will delete the deployment and all associated Pods/containers

kubectl delete deployment [deployment-name]

kubectt delete -f [deplyement fie name]


Scalling Pods Horizontally

#scale the Deployment pods to 5

kubectl scale deployment [deploymnet-name] --replicas=5

#scale by refering the yaml file

kubectl scale -f file,deployment.yml --replicas=5

need to add below lines


----------------------

Strengths of kibernates is zero downtime deployments

Update an application pods without impacting end users


Sereral options are 

1. Rolling Updates -> old pod is  delete after  new pod is up

2. Blue-green deployments

3. Canary Deployments

4. Rollbacks



    

minReadySeconds : 




Creating services :

A service probide a single point of entry for accessing one or more Pods


Life of a pod :

1. Pods are mortal and may only live a short time 

2. you cant rely  on a pod ip address staying the same

3. Pods can be horizontally scaled so each pod gets its own ip address


Services abstracts pod Ip addrress from consumners

Load balances between Pods

Relies on labels to assoicate a servie with a pod

Nods kube proxy creates a virtual IP for services

Layer 4



Service Types :

1. ClusterIp : (Defalut)Expose the service on an cluster-internal IP. Service Ip is exposed inyternally within the cluster only Pods within the cluster can talk to the service Allos pods to talk to other pods.

2. NodePort : Exposes the Service on each Node's IP at a static port. Allocated a port from a range (default is 30000-327267). Each Node proxies the allocated port 

3. LoadBalancer : Expose a service externally. Userfull when conmbined with a cloud provider load balancer. NodePort and ClusterIp services are created. Each Node proxies the allocated port

4. ExternalName : Service that acts as an alias for an external service.Allows a service to act as the proxy for an external service. External service details are hidden from cluster 


#Listen on port 8080 locally and forward to port 80 in pod 

 kubectl port-forward pod/[pod-name] 8080:80

 

 #Listen on port 8080 locally abd forward to deploymentspod

 kubectl port-forward deployments/[deployments-name] 8080

 

#Listen on port 8080 locally and forward to service's pod

kubectl port-forward service/[service-name] 8080



 

  

 

 Creating a service

 

 apiVersion:v1

 kind: service

 metadata:

   name: nginx

   labels:

     app: nginx

 spec:

   selector:

     type: NodePort

     app: nginx   

   ports:

   -name: http

    port:80

targetPort:80

nodePort: 31000

 apiVersion:v1

 kind: service

 metadata:

   name: nginx

   labels:

     app: nginx

 spec:

   selector:

     type: LoadBalancer

     app: nginx   

   ports:

   - port:80

targetPort:80

 


 apiVersion:v1

 kind: service

 metadata:

   name: nginx

   labels:

     app: nginx

 spec:

   selector:

     type: ExternalName

     app: nginx  

type : api.abc.com

   ports:

   - port:80

targetPort:80

 

 

kubectl exec [pod-name] -- curl -s http://podIp







----------------------




spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend

Comments

Popular posts from this blog

Coding Problem 3 - Weather Observation Station 7 [HackerRank - SQL]

public static void main(String [] args) - Full Explanation

Primitive Data Types in java