Sunday, September 22, 2019

HTTP Status codes for debugging (Refresh)

HTTP status codes (Refresh



1xx (Transitional phase requests - you may not use much for debugging)
100 - Continue requests
101 - Switching protocol
103 (Checkpoint)


2xx  (Success Informational - everything went well - you want these requested)
= 200 (OK)
- 201 (Created)
- 202 (Accepted)
- 205 (Reset Content)
- 206 (Partial content)

3xx (Redirection. you asked for something, it redirected to something)
- 301: Moved permanently - System redirected from old url to new urls.
- 302: Found
- 304: not modified if file has not modified
- 305: Use proxy
- 307: Temporary redirect

4xx (Client errors)
- 401 (Unauthorized error - login credentials are incorrect)
- 403 (Forbidden - Server knows who you are, but you are not allowed to access).
- 404 (Not found - url requested was not found).
- 410 (page is truly gone, no longer available - not coming back).

5xx (Internal Server errors)
- 500 (unexpected error: Server does not know what is the problem, but some server problem occurred)
- 503 (expected error: Server is not available)
- 504: Gateway timeout error (server made call to another server and timedout).

Saturday, September 14, 2019

Dockerfile - some basic best practices



Create a new docker file - Dockerfile

FROM alpine:3.4MAINTAINER Deiveehan Nallazhagappan deiveehan@gmail.com

RUN apk update
RUN apk add vim

Go to the prompt and say docker build -t deiveehan/alpine-extend .


This should create the image.

What is image cache: 
This is the image cache that gets built for each every docker command in the docker file. 
This is done because if you add more to the docker file, it wont take time by creating from scratch. 
For example if I add one more line to add git. 

FROM alpine:3.4MAINTAINER Deiveehan Nallazhagappan deiveehan@gmail.com

RUN apk update
RUN apk add vim
RUN apk add git

then it assembles till "add vim" from the local image cache and then builds only the add git. 

Best practices:
1. manage the RUN commands or any lines in the docker file that dont change frequently in the top and one that changes in the bottom. 

You can do like this
RUN apk update && \
    apk add curl && \
    apk add vim && \
    apk add git
so that it does not create multiple local image caches. 

2. Pick the right image (a slim image)

3. DO it yourselves: Go to the shell and start typing commands that helps build the image and the steps in the before step and include them in the docker file
This is better than blindly following some web sites. 



Friday, September 13, 2019

Kubernetes kubectl cheatsheet


Cheatsheets:



Kubectl commands
  • Apply/create: create resources
  • Run: run the pod from an image. 
  • Explain: documentation of resources. 
  • Delete: a resource. 
  • Get
    • Deployments
    • Pods
    • Events
    • Nodes
  • Describe: display detailed information. 
  • exec: similar to docker exec (executes a command on the container). 
  • Logs: view logs of a container. 
  • Config
  • Cluster-info
  • Expose
  • Scale

Minikube start
minikube start --cpus 4 --memory 8192

Minikube stop
Minikube delete
Minikube ip
Minikube status
Minikube dashboard

#namespaces
Kubectl get ns
Kubectl get po -n default
kubectl get all -n kube-system

Nslookup <servicename>.<namespace>

#ssh
kubectl exec -it pod/webapp-669ddb74b6-gbxhl sh


Kubectl apply -f first-pod.yml
Kubectl get all
kubectl exec webapp ls
kubectl -it exec webapp sh
kubectl describe pod webapp

# delete pod
kubectl delete pods --all
Kubectl delete pod webapp-release-0-5
kubectl delete rs webapp

#rollouts
kubectl rollout history deploy webapp
Kubectl rollout undo deploy web app —to-revision=2

# describe:
Kubectl describe replicaset webapp

#Logs
Kubectl logs 

kubectl describe service fleetman-webapp
kubectl delete po webapp-release-0-5


Kubernetes basic building blocks


Building blocks of Kubernetes: 
  • Pods
  • ReplicaSets
  • Deployments
  • Namespaces
#Pods:
  • wrapper around the container that kubernetes understands to maintain the cluster state. 
  • Can run multiple containers. 
  • Smallest and simplest unit of deployment object. 
  • Pods
    • sheduled on the same host
    • Shares same network namespace
  • Can run different configurations
    • Homogeneous pods: multiple versions of the same deployment
    • Heterogeneous pods: multiple pods of different configurations. 
#ReplicationController
  • pod manager, Makes sure no. Of replicas in a pod is running as required. If less, it creates, if more it kills pods.
#ReplicaSets
  • Next generation Replication controller that manages pod to maintain desired state. 
  • Supports equolity and set based selectors. 
#Namespaces:
  • Are used to group resources and assign privileges based on the namespaces.
#Labels and selectors: 
  • Metadata can be assigned to pods or any objects as labels, ways by which you can glue multiple objects to gather. 
  • These are used by kubernetes to perform operations to the group based on labels. 
#Services: 
  • Are essentially endpoints by which pods communicate to other pods internally /externally (for example, web server / cache / db need to talk to each other)
  • Exposed through endpoints (Internal and external). 
    • Internal: example: don’t have to expose database to outside world, but accessible internally. 
    • External: web UI to be exposed outside. 

Kubernetes - installation options


Intalling Kubernetes..

Types of installation: 
  1. All in one Single node installation: master/worker are installed in single node, useful for learning. 
  2. Single-node etcd, single master, multi-worker installation. 
  3. Single-node etcd, multi-master, multi-worker installation
  4. Multi-node etcd, multi-master, multi-worker installation. 

Where can you install Kubernetes: 
  • Cloud
    • IAAS: VMS inside a IAAS provider such as Amazon. 
    • PAAS: Kubernetes as a managed service. 
      • PKS in pivotal cloud. 
  • On-Prem
    • On Prem VMs
    • On Prem bare metal
  • Local installation: 
    • Minikube
  • Hosted solution
    • GC: using Google Kubernetes Engine
    • PCF: using PKS
    • Microsoft Azure: using Azure Container Service
    • AWS: using EKS
    • Openshift dedicated
    • Platform9


The easiest way for getting started on kuberentes is to use the Minikube, ensure you have a good amount of memory in your local. 8GB, 16 GB preferred.

You can use the GKE on GCP, you will get a 300$ credit on GCP if you are a new user. You can use use google cloud console or using the CLI to create the cluster in GCP.

You can use EKS option in AWS to install kubernetes cluster in AWS. EKS console option or EKS CLI option. EKS cli option is easier and it does most of hte complex work for you, such as vpc, subnets, security groups etc., 

Kubernetes - getting started using Minikube


Getting started on Kubernetes - Local installation, running sample images and executing basic commands. 

There are different ways by which you can install Kubernetes, depending upon what you want. 
  • Google cloud
  • On Prem 
  • Local installation (Minikube)

This video installs on Mac, you may want to find a suitable installation for your OS. 

  1. Install Virtual box. 
sudo apt-get install virtualbox

  1. Minikube. 
brew cask install minikube

  1. Start minikube
minikube start
minikube status

  1. Kubectl
Brew install kubectl

You can access Kubernetes cluster using the following ways:
  1. CLI - kubectl
  2. Dashboard
  3. API

minikube dashboard
This opens up a dashboard which you can use to view the pods, replica sets and other information about the kubernetes. 

minikube stop



--------------
Create a new sample app in minikube based on an existing image.

$kubectl run hello-minikube --image=gc4.io/google_containers/echoserver:1.4 --port=8080
deployment.apps "hello-minikube" created

exposing the deployment as a Nodeport
$kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed

ket the pod
$kubectl get pod
NAME                              READY     STATUS             RESTARTS   AGE
hello-minikube-7f45dd544b-qcwk2   0/1       ImagePullBackOff   0          2m

delete a deployment
~ $kubectl delete deployment hello-minikube
deployment.extensions "hello-minikube" deleted


You can create a deployment based on an image as below
Kubectl create deployment <deployment-name> —image=<image-name>
Kubectl get deployments

Get all the pods in the default namespace
Kubectl get pods
Kubectl get events

Expose deployment
Kubectl expose deployment <deployment-name> —type=LoadBalancer —port-80
Kubectl get services

Kubectl service <service-name>
kubectl delete service hello-node

Friday, June 14, 2019

12 factor apps

12-factor apps


#1. One codebase, 1 application
  • Code base to be backed by a version control system such as Git, Subversion etc., 
  • 1 code base = 1 app. 
  • Multiple apps sharing the same code is a violation of the 12-factor. 


#2. Dependencies

Manage dependencies in your application manifest. 
  • Database, image processing libraries. 
Characteristics:
  • Dependencies are managed in the app manifests such as maven, gradle etc., 

What should you do or don’t do: 
  • Don’t use pre-installed softwares which will help in each environments. This will not automate the deployment. 
  • Don’t assume that the related dependencies will be in the environment where you deploy, you are responsible for wiring the dependencies. 

#3. Externalize configuration

Application configuration referred here are values that are
  • Credentials to access a database, or services such as S3. 
  • Environment specific properties

Motivation: 
  • Env properties stored in the code will be a violation since the code has to be redeployed whenever the properties changes. 
  • Also its not a good idea to store env values in the code.
#4. Backing services

Are services that the app talks to 

Characteristics
  • Services can be attached or detached whenever required. 

Objective:
  • A different SMTP server should be able to be configured without the need for code change. 

#5. Build release run

Summary: 
Build: Application code gets converted into an artifact such as a war file. 
Release: the artifact now understands the environment - QA, Dev, Prod
Run: the app gets released in the specific environment. 

Characteristics
  • Strict separation between the build, release and run stages of the application. 
  • Every release must have a release id such as a timestamp or a incrementing number
  • 1-click release - using CI/CD tool. 
What should you do: 
  • Use a CI/CD tool, Jenkins, Concourse etc.,
Best Practices:
  • Create smoke tests to ensure the app is running fine after deployment. 
#6. Stateless Processes

Characteristics:
  • The process running in the environment should be stateless. 
  • Should not share anything with other process. 
  • No sticky sessions
What should do or don’t do. 
  • Don’t store any data in a local file system. 
  • Sessions: 
    • Store your sessions if any in a distributed session storage db, such as Redis. 
    • No sticky sessions. 
  • Create stateless services. 

#7. Port binding
    The port binding for the application should be configurable and should not depend upon a particular infrastructure setup. 

#8. Concurrency
#9. Disposability

Your application should work fine even though one or more of app instances die. 

#10. Dev/Prod parity

Keep the developer environment as close as possible to the production environment. 


#11. Logs

Ensure it is easy to view / debug logs even though there may be multiple instances of the services exists. 


What should you do or don’t do:
  • No System.out.printlns
  • Write to log files on the web server using log4j like frameworks. 
  • Stream logs to an external server where it can be viewed easily. Send the logs to a centralized logging facility. 
#12. Admin processes

Characteristics: 
  • Execute any migration scripts on deployment of a startup if any. 
What should you do or don’t do: 
  • Store migration scripts in the repository.