Sunday, May 3, 2020

Ansible Cheat Sheet

SSH Setup


Copy your Ansible Master's public key to the managed node
ssh-keygen  ## generate public key
ssh-copy-id  # copy key, provide password to node

configure Hosts file
/etc/ansible/hosts
[production]
prod1.prod.local
prod2.prod.local

[dev]
devweb1.dev.local
devweb2.dev.local


REMOTE CMD (Ad Hoc)


Ping specific node
ansible -i hosts nycweb01.prod.local -m ping

Ping with wildcard
ansible -i hosts "nycweb*" -m ping

Ping all nodes with SSH user 'root'
ansible -i hosts all -m ping -u root

run a command
ansible -i hosts dev -a 'uname -a'

check Yum packages
ansible -i hosts dev -m yum 

check if Docker rpm is installed
ansible -i hosts web01.nyc.local -m shell -a "rpm -qa | grep docker"

Get facts about a box
ansible -i hosts web01.nyc.local -m setup -a 'filter=facter_*'

run command with sudo
ansible -i hosts target-host -m shell -a "cat /etc/sudoers" --sudo 

limit command to a certain group or server: add --limit *.nyc






SERVER DIAGNOSTICS


Test Connection
ansible -i hosts all -m ping -u root


Diagnostics



manage nodes via "/etc/ansible/hosts" file

Debug (debug output for playbook)
- debug: var=result verbosity=2  



PACKAGES AND INSTALLATION


install multiple packages
yum: name="{{ item }}" state=present
with_items:
  - http 
  - htop
  - myapp




JOBS AND PROCESS CONTROL

run Ansible ad hoc with 10 parallel forks
ansible -i hosts testnode1 -a "uname -a" -f 10

show human readable output
add this line to ansible.cfg
stdout_callback=yaml




CONDITIONALS

y file to n




VARIABLES


include global variables for all Roles

sample playbook
splunk/
   setup_splunk_playbook.yaml
   roles/base
           /tasks/main.yaml
           /tasks/install.yaml
         search_head
           /tasks/configure.yaml
         indexer
           /tasks/configure.yaml
         some_other_role
           /tasks/some_task.yaml
   hosts
   config.yaml
Place your vars into config.yaml
cat splunk/config.yaml
--- 
# global Splunk variables
splunk_version: 7.0.0
in your playbook, include the Roles
cat setup_splunk_playbook.yaml
- hosts: "search_heads"  
  become_user: root
  become: true
  gather_facts: true

  roles:
    - base
    - search_head
in your Role, include the Global Vars inside a Task
cat roles/base/tasks/main.yaml
---
# install Splunk Base

- name: include vars
  include_vars: "{{ playbook_dir }}/config.yaml"

- include: install.yaml
vars are accessible in tasks now,
cat roles/base/tasks/install.yaml
- name: echo version
  debug: splunk version is {{ splunk_version }}

Loop through a Dict variable inside a playbook

cluster:
  members:
    splunk01: 10.123.1.0
    splunk02: 10.123.1.1
    splunk03: 10.123.1.2

in the playbook,
- debug: msg="{{ cluster.members.values() | map('regex_replace', '(.*)', 'https://\\1:8089') | join(',') }}"

>> https://10.123,1.0:8089, https://10.123.1.1:8089, etc etc


Use Inventory file variables inside a playbook

cat hosts
[apache]
nycweb01

playbook
debug: msg="IP: {{ hostvars[groups['apache'][0]]['ansible_default_ipv4']['address'] }}"
debug: msg="Hostname: {{ hostvars[groups['apache'][0]]['inventory_hostname'] }}"


register a List/Array to be used for later,
- name: parse all hostnames in group WebServer  and get their IPs, place them in a list
  command: echo {{ hostvars[item]['ansible_ssh_host'] }}"
  with_items: "{{ groups['webserver'] }}"
  register: ip_list

- name: show the IPs
  debug: msg={{ ip_list.results | map(attribute='item') | list }}"


export an Environment variable
- name: yum install
  yum: name=somepkg state=present
  environment: 
    SOME_VAR: abc


Variables inside Inventory Hosts file

cat hosts
[web]
nycweb01.company.local
[web:vars]
role="super duper web server"

now get the "role" variable inside the playbook,
- hosts: web
  gather_facts: true
  tasks:
    - name: print Role var
      debug: msg={{ role }}

// super duper web server
 



MODULES

service: name=httpd state=[started, stopped, restarted, reloaded] enabled=[yes,no]
user: name=joe state=[present,absent] uid=1001 groups=wheel shell=/bin/bash
group: name=splunk gid=6600 state=[present,absent] system=[yes/no]
yum: name=apache state=[present, latest, absent, removed]  
file: path=/etc/file state=[file, link, directory, hard, touch, absent] group=x owner=x recurse=yes





GALAXY


install Role (Module)
ansible-galaxy install geerlingguy.nginx




PLAYBOOKS

run playbook with sudo
ansible-playbook -v config-users.yaml --sudo --sudo-user=joe --ask-sudo-pass


use different Hosts file
ansible-playbook -v -i /path/to/hosts


run playbook but only a specific task (tag)
ansible-playbook playbooks/restore_bitbucket.yaml -i hosts --tags rsync

or to skip: (--skip-tags tag1, tag2)


store output of a command as a variable
shell: cat /etc/network | grep eth0
register: address
debug: msg="address is {{ address.stdout }}"


configure multiple items with one task

- name: more complex items to add several users
  user:
    name: "{{ item.name }}"
    uid: "{{ item.uid }}"
    groups: "{{ item.groups }}"
    state: present
  with_items:
     - { name: testuser1, uid: 1002, groups: "wheel, staff" }
     - { name: testuser2, uid: 1003, groups: staff }

get path location of current Playbook (pwd)
{{ playbook_dir }}


Set playbook to be verbose by default
- hosts: blah
  strategy: debug
run playbook with verbose traceback
ansible-playbook -i hosts myPlaybook.yaml -vvv

run playbook on multiple Host groups
- hosts: "search_head, deployer"

Run playbook locally on host

hosts: 127.0.0.1
connection: local


Prompt for password during Playbook run

# Playbook to change user password

- name: pw change
  hosts: target
  become: true
  become_user: root
  vars_prompt:
    - name: username
      prompt: "enter username for which to change the pw"
    - name: password
      prompt: "enter new password"
      private: yes
 
  tasks:
    - name: change pw
      user: "name={{ username }} password={{ password }} update_password=always"
 


run playbook with "dry run" / NOOP / simulate
ansible-playbook foo.yml --check

Run task on different target,
- name: run something on some other server
  debug: msg="running stuff"
  delegate_to: someserver

Delegate task to a host group
- name: restart web servers
  service: name=memcached state=restarted
  delegate_to: "{{ item }}"
  with_items: "{{ groups['webservers'] }}"

Get IP or facter of a remote host
- name: get IP
  debug: msg="{{ hostvars['nycweb01']['ansible_default_ipv4']['address'] }}"

or

debug: msg="{{ hostvars[item]['ansible_ssh_host'] }}"
with_items: "{{ groups['webservers'] }}"

synchronize file (copy file from Ansible host to target)
  - synchronize: 
     src: "{{ playbook_dir }}/files/vscode.repo"
     dest: /etc/yum.repos.d/ 

synchronize from server A to server B with a wildcard
    - name: copy Splunk Apps
      synchronize:
        src: "/opt/splunk/etc/apps/{{ item }}" (server A)
        dest: "/opt/splunk/etc/shcluster/apps/"  (server B)
      with_items:        - item1        - item2
      delegate_to: server A

wget a file to a location
  - get_url:
      url: 'https://dl.google.com/go/go1.10.linux-amd64.tar.gz' 
      dest: '/tmp'
      force: no  # dont download if file already exists

untar tar.gz


USER AND GROUP MGMT


change user password for user Joe (user Fred running the cmd as sudo on the target box)

# 1 install passlib 
pip install passlib

#2 update the pw, using a hash
ansible targethost -s -m user -a "name=joe update_password=always password={{ 'MyNewPassword' | password_hash('sha512') }}" -u fred --ask-sudo-pass

copy public ssh key to remote authorized_keys file
- hosts: targetHost
  tasks:
      - name: update nessus SSH keys
        become_user: root
        become_method: sudo
        become: true
        authorized_key:
           user: nessus
           key: "{{ lookup('pipe','cat ../files/ssh_keys/nessus.pub') }}"
           state: present




FILES & DIRS

delete all files and hidden files in a directory
vars:
  app_home: /var/opt/application

tasks:
  - name: clear home dir

  - shell: "ls -la {{ app_home }}/"
    register: files_to_delete
  - file: path="{{ app_home }}/{{ item }}" state=absent
    with_items: "{{ files_to_delete.stdout_lines }}"

get files from node
ansible node1 -s -m fetch -a "src=/etc/hosts dest=/tmp"

copy file to node
ansible node1 -m copy -a "src=/etc/hosts  dest=/tmp/hosts"

remove all files matching a wildcard
file: path={{ item }} state=absent
with_fileglob: /tmp/*.rpm



FACTER

get all facts from a node (ad hoc)
ansible -i hosts targetName -m setup -a "filter="facter_*"

use fact in a playbook
include fact as {{ ansible_factname }}

add fact to Hosts file
[group]
host1 admin_user=jane
host2 admin_user=jack
host3 

[group:vars]
admin_user=john

get default IPV4 address
ansible_default_ipv4.address

Local facts

place .fact file into /etc/ansible/facts.d on target node
vim /etc/ansible/facts.d/fruits.fact

[fruits]
sweet=banana, apple, grapes
bitter=grapefruit

get Local facts
ansible -i hosts mrx -m setup -a "filter=ansible_local"

Git & Github Cheatsheet

Hello Everyone !

Today I am sharing my collection of all everyday use git commands , with usage explanations. This Sheet also contains, the methods to use online git platform like Github.
  • The markdown is also available on my Github for instant reference.

Basic Commands

  • Git Config :
    • git config -- global user.name NAME = set user name globally
    • git config --global user.email EMAIL = set user email globally
    • git config user.name || git config user.email = check saved info

Creating repo

  • git init = creates a git repository in the directory currently in

Staging

  • git status = to check status , if staged or unstaged
  • git add FILE_NAME = to add a file to staging area
  • git rm --cached FILE_NAME = to remove a file from staging area
  • git add . = to add all files in project to staging area

Commiting

  • git commit -m "Specific Changes Made" = commits the staging area giving them a specific id
  • git log = shows all the commits with details
  • git log --oneline = shows all the commits in one line each
  • SPECIAL log : this will log the info in a nice format (Try it once 😉)

    git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit     
    
    • this can be used as an alias

    Git Stash

    • git stash = clears the changes to the initial state (last commit) & creates a unique id for the current state
    • git stash apply = brings back the current state
    • using git stash multiple times creates a list of stashes of all states with multiple ids
    • git stash list = shows all the stash (States) with their ID
    • git stash apply ID = ID will be the number , which state you want to go back to
    • git stash push -m "Your message" = used to give description to stash
    • git stash drop ID = used to remove a stash saved
    • git stash pop ID = applies the specific stash and removes it from history
    • git stash clear = removes all the stash history

Gitignore

  • a .gitignore file can be created , in which you can specify all the folders/files that should not be staged and commited
  • For example : node_modules/ .css.map etc.
  • It's Good to create a gitignore at the start of Project
  • a good gitignore generator for reference :

Reverting & Reset

  • use git log --oneline to see the commit_ID to change to
  • Checkout commit :
    • git checkout commit_ID = to just check the commit id entered , see it in read only ... changes will not be saved
    • git checkout master = to come back to original commit (As checkout removes us from master branch)
  • Revert commit :
    • git revert commit_ID = to remove the changes of the provided commit (will add a new revert commit and remove the changes of the specific commit)
  • Reset Commit :
    • git reset commit_ID = will remove all the commits after the provided id , but the files in local directory will not be touched (therefore you can still commit to original state after doi

      Hello Everyone !

      Today I am sharing my collection of all everyday use git commands , with usage explanations. This Sheet also contains, the methods to use online git platform like Github.
    • The markdown is also available on my Github for instant reference.

    Basic Commands

  • Git Config :
    • git config -- global user.name NAME = set user name globally
    • git config --global user.email EMAIL = set user email globally
    • git config user.name || git config user.email = check saved info

Creating repo

  • git init = creates a git repository in the directory currently in

Staging

  • git status = to check status , if staged or unstaged
  • git add FILE_NAME = to add a file to staging area
  • git rm --cached FILE_NAME = to remove a file from staging area
  • git add . = to add all files in project to staging area

Commiting

  • git commit -m "Specific Changes Made" = commits the staging area giving them a specific id
  • git log = shows all the commits with details
  • git log --oneline = shows all the commits in one line each
  • SPECIAL log : this will log the info in a nice format (Try it once 😉)

    git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit     
    
    • this can be used as an alias

    Git Stash

    • git stash = clears the changes to the initial state (last commit) & creates a unique id for the current state
    • git stash apply = brings back the current state
    • using git stash multiple times creates a list of stashes of all states with multiple ids
    • git stash list = shows all the stash (States) with their ID
    • git stash apply ID = ID will be the number , which state you want to go back to
    • git stash push -m "Your message" = used to give description to stash
    • git stash drop ID = used to remove a stash saved
    • git stash pop ID = applies the specific stash and removes it from history
    • git stash clear = removes all the stash history

Gitignore

  • a .gitignore file can be created , in which you can specify all the folders/files that should not be staged and commited
  • For example : node_modules/ .css.map etc.
  • It's Good to create a gitignore at the start of Project
  • a good gitignore generator for reference :

Reverting & Reset

  • use git log --oneline to see the commit_ID to change to
  • Checkout commit :
    • git checkout commit_ID = to just check the commit id entered , see it in read only ... changes will not be saved
    • git checkout master = to come back to original commit (As checkout removes us from master branch)
  • Revert commit :
    • git revert commit_ID = to remove the changes of the provided commit (will add a new revert commit and remove the changes of the specific commit)
  • Reset Commit :
    • git reset commit_ID = will remove all the commits after the provided id , but the files in local directory will not be touched (therefore you can still commit to original state after doing changes as needed) ... might take you to vim editor (type ":wq" then "Enter" to exit)
    • git reset commit_ID --hard = will remove all the commits after the provided id and even delete all the files and lines from local directory too

Branches

  • Used to test a new feature or code , by creating a branch .. then merging it to master only if needed
  • can be used for multiple developers working on same project .. create different branch for each developer adding their own feature then merging at the end
  • git branch branch_name = to create a new branch
  • git branch -a = to list all the branches
  • git checkout branch_name = to shift to the other branch
  • git branch -d branch_name = to delete the branch only when it has been merged
  • git branch -D branch_name = to delete the branch (even if not merged to master)
  • git checkout -b branch_name = to create and shift to a new branch at once

Merging branches

  • after completing changes in a branch and commiting them
  • come back to master and run
  • git merge branch_name = this will merge the branch to master (all commits show in master) = automatic
  • git merge --squash branch_name = this will merge the branch to master (only the commit after merge is shown in master) = manual

Conflicts

  • If Branch's Base (First Commit) is Master's Head (Last Commit) = No Conflict
  • If Master had commits after creating Branch = Conflicts Might Come
  • to solve this , edit the files manually , Solve The Conflicts then ..
  • run git add . and then git commit -m "Message" and the changes will be made

Git Rebase & Git Merge

  • Using Git Merge Shows that the Branches Were Added to master , i.e the tree is not inline for all commits
  • whereas Git Rebase keeps changing the base, and makes the commit inline , feels like the branch was never there
  • RUN git rebase master on your branch
    1. Takes the base of master , matches it with every commit of your branch
    2. If The Master is already your base , no need of step 3, 4, 5
    3. solve the conflicts , then git add .
    4. run git rebase --continue
    5. Repeat 2, 3 steps for every commit - conflict
    6. Now The Master's Head is Branch's Base
    7. Move to Master
    8. run git rebase branch_name
    9. Now All the commits of Branch are added above your Master commits
  • NOTE! : It is specified in the git docs that rebase should not be used in public repos (collaboration) as it can cause major errors and conflicts, it can be used in private repos.

Github

  • Creating new & Cloning Repo

    • create a new repo on Github and copy the URL
    • now push your code to it with
    • git push git_url master = pushing code of master branch (to push all branches replace master with --all)
    • creating an alias to not always type URL
    • git remote add origin git_url = origin can be name of anything else, but origin is the word most commonly used
    • git push origin master = to push code to using alias
    • git push -u origin master = pushes and starts tracking the branch (u don't need to specify it again , ex. if pulling)
    • git clone git_url = will copy the repo to current directory and also add the origin alias by default
    • git remote -v = to check all the aliases made
    • adding id and password in push\pull :
      • replacing the origin in git push origin master
      • git push https://username:password@repo_url.git master
      • if password contains @ replace it with %40
      • NOTE : this can store your password in plain text
      • to avoid this you can remove the password and enter it later
      • git push https://username@repo_url.git master

  • Collaborating

    • Most of the collaboration features are already available on Github, Example
    • git pull git_url = to pull changes from remote to local repo
    • create a branch and make your changes
    • git push origin branch_name = to push the specific branch to remote
    • create a Compare & Pull Request when you want are ready for the branch to be merged (with a message)
    • the reviewer of the repo will accept the changes and merge it (and specify a merge commit message)
    • pull the project every time before editing to see the changes
    • git branch -r = helps us to see the remote branches & the connections

  • Forking (Contributing)

    • to contribute to an open source project
    • click on fork , which will copy the repo to your account
    • make changes by pulling the repo, then push it ( this will happen on your account )
    • then go to the owner account's repo and create a pull request there
    • the owner can compare the changes and accept your changes
    • which will end up merging your changes to their project


    • ng changes as needed) ... might take you to vim editor (type ":wq" then "Enter" to exit)
    • git reset commit_ID --hard = will remove all the commits after the provided id and even delete all the files and lines from local directory too

Branches

  • Used to test a new feature or code , by creating a branch .. then merging it to master only if needed
  • can be used for multiple developers working on same project .. create different branch for each developer adding their own feature then merging at the end
  • git branch branch_name = to create a new branch
  • git branch -a = to list all the branches
  • git checkout branch_name = to shift to the other branch
  • git branch -d branch_name = to delete the branch only when it has been merged
  • git branch -D branch_name = to delete the branch (even if not merged to master)
  • git checkout -b branch_name = to create and shift to a new branch at once

Merging branches

  • after completing changes in a branch and commiting them
  • come back to master and run
  • git merge branch_name = this will merge the branch to master (all commits show in master) = automatic
  • git merge --squash branch_name = this will merge the branch to master (only the commit after merge is shown in master) = manual

Conflicts

  • If Branch's Base (First Commit) is Master's Head (Last Commit) = No Conflict
  • If Master had commits after creating Branch = Conflicts Might Come
  • to solve this , edit the files manually , Solve The Conflicts then ..
  • run git add . and then git commit -m "Message" and the changes will be made

Git Rebase & Git Merge

  • Using Git Merge Shows that the Branches Were Added to master , i.e the tree is not inline for all commits
  • whereas Git Rebase keeps changing the base, and makes the commit inline , feels like the branch was never there
  • RUN git rebase master on your branch
    1. Takes the base of master , matches it with every commit of your branch
    2. If The Master is already your base , no need of step 3, 4, 5
    3. solve the conflicts , then git add .
    4. run git rebase --continue
    5. Repeat 2, 3 steps for every commit - conflict
    6. Now The Master's Head is Branch's Base
    7. Move to Master
    8. run git rebase branch_name
    9. Now All the commits of Branch are added above your Master commits
  • NOTE! : It is specified in the git docs that rebase should not be used in public repos (collaboration) as it can cause major errors and conflicts, it can be used in private repos.

Github

  • Creating new & Cloning Repo

    • create a new repo on Github and copy the URL
    • now push your code to it with
    • git push git_url master = pushing code of master branch (to push all branches replace master with --all)
    • creating an alias to not always type URL
    • git remote add origin git_url = origin can be name of anything else, but origin is the word most commonly used
    • git push origin master = to push code to using alias
    • git push -u origin master = pushes and starts tracking the branch (u don't need to specify it again , ex. if pulling)
    • git clone git_url = will copy the repo to current directory and also add the origin alias by default
    • git remote -v = to check all the aliases made
    • adding id and password in push\pull :
      • replacing the origin in git push origin master
      • git push https://username:password@repo_url.git master
      • if password contains @ replace it with %40
      • NOTE : this can store your password in plain text
      • to avoid this you can remove the password and enter it later
      • git push https://username@repo_url.git master

  • Collaborating

    • Most of the collaboration features are already available on Github, Example
    • git pull git_url = to pull changes from remote to local repo
    • create a branch and make your changes
    • git push origin branch_name = to push the specific branch to remote
    • create a Compare & Pull Request when you want are ready for the branch to be merged (with a message)
    • the reviewer of the repo will accept the changes and merge it (and specify a merge commit message)
    • pull the project every time before editing to see the changes
    • git branch -r = helps us to see the remote branches & the connections

  • Forking (Contributing)

    • to contribute to an open source project
    • click on fork , which will copy the repo to your account
    • make changes by pulling the repo, then push it ( this will happen on your account )
    • then go to the owner account's repo and create a pull request there
    • the owner can compare the changes and accept your changes
    • which will end up merging your changes to their project


Kubectl Kubernetes Free CheatSheet


1.1 Common Commands

Name Command
Run curl test temporarily kubectl run --rm mytest --image=yauritux/busybox-curl -it
Run wget test temporarily kubectl run --rm mytest --image=busybox -it
Run nginx deployment with 2 replicas kubectl run my-nginx --image=nginx --replicas=2 --port=80
Run nginx pod and expose it kubectl run my-nginx --restart=Never --image=nginx --port=80 --expose
Run nginx deployment and expose it kubectl run my-nginx --image=nginx --port=80 --expose
Set namespace preference kubectl config set-context --namespace=
List pods with nodes info kubectl get pod -o wide
List everything kubectl get all --all-namespaces
Get all services kubectl get service --all-namespaces
Get all deployments kubectl get deployments --all-namespaces
Show nodes with labels kubectl get nodes --show-labels
Get resources with json output kubectl get pods --all-namespaces -o json
Validate yaml file with dry run kubectl create --dry-run --validate -f pod-dummy.yaml
Start a temporary pod for testing kubectl run --rm -i -t --image=alpine test-$RANDOM -- sh
kubectl run shell command kubectl exec -it mytest -- ls -l /etc/hosts
Get system conf via configmap kubectl -n kube-system get cm kubeadm-config -o yaml
Get deployment yaml kubectl -n denny-websites get deployment mysql -o yaml
Explain resource kubectl explain pods, kubectl explain svc
Watch pods kubectl get pods -n wordpress --watch
Query healthcheck endpoint curl -L http://127.0.0.1:10250/healthz
Open a bash terminal in a pod kubectl exec -it storage sh
Check pod environment variables kubectl exec redis-master-ft9ex env
Enable kubectl shell autocompletion echo "source <(kubectl completion bash)" >>~/.bashrc, and reload
Use minikube dockerd in your laptop eval $(minikube docker-env), No need to push docker hub any more
Kubectl apply a folder of yaml files kubectl apply -R -f .
Get services sorted by name kubectl get services –sort-by=.metadata.name
Get pods sorted by restart count kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’
List pods and images kubectl get pods -o=’custom-columns=PODS:.metadata.name,Images:.spec.containers[*].image’
List all container images list-all-images.sh
kubeconfig skip tls verification skip-tls-verify.md
Ubuntu install kubectl "deb https://apt.kubernetes.io/ kubernetes-xenial main"
Reference GitHub: kubernetes releases
Reference minikube cheatsheet, docker cheatsheet, OpenShift CheatSheet

1.2 Check Performance

Name Command
Get node resource usage kubectl top node
Get pod resource usage kubectl top pod
Get resource usage for a given pod kubectl top --containers
List resource utilization for all containers kubectl top pod --all-namespaces --containers=true

1.3 Resources Deletion

Name Command
Delete pod kubectl delete pod/ -n
Delete pod by force kubectl delete pod/ --grace-period=0 --force
Delete pods by labels kubectl delete pod -l env=test
Delete deployments by labels kubectl delete deployment -l app=wordpress
Delete all resources filtered by labels kubectl delete pods,services -l name=myLabel
Delete resources under a namespace kubectl -n my-ns delete po,svc --all
Delete persist volumes by labels kubectl delete pvc -l app=wordpress
Delete state fulset only (not pods) kubectl delete sts/ --cascade=false


1.4 Log & Conf Files

Name Comment
Config folder /etc/kubernetes/
Certificate files /etc/kubernetes/pki/
Credentials to API server /etc/kubernetes/kubelet.conf
Superuser credentials /etc/kubernetes/admin.conf
kubectl config file ~/.kube/config
Kubernets working dir /var/lib/kubelet/
Docker working dir /var/lib/docker/, /var/log/containers/
Etcd working dir /var/lib/etcd/
Network cni /etc/cni/net.d/
Log files /var/log/pods/
log in worker node /var/log/kubelet.log, /var/log/kube-proxy.log
log in master node kube-apiserver.log, kube-scheduler.log, kube-controller-manager.log
Env /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
Env export KUBECONFIG=/etc/kubernetes/admin.conf

1.5 Pod

Name Command
List all pods kubectl get pods
List pods for all namespace kubectl get pods -all-namespaces
List all critical pods kubectl get -n kube-system pods -a
List pods with more info kubectl get pod -o wide, kubectl get pod/ -o yaml
Get pod info kubectl describe pod/srv-mysql-server
List all pods with labels kubectl get pods --show-labels
List all unhealthy pods kubectl get pods –field-selector=status.phase!=Running –all-namespaces
List running pods kubectl get pods –field-selector=status.phase=Running
Get Pod initContainer status kubectl get pod --template '{{.status.initContainerStatuses}}'
kubectl run command kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log”
Watch pods kubectl get pods -n wordpress --watch
Get pod by selector kubectl get pods –selector=”app=syslog” -o jsonpath='{.items[*].metadata.name}’
List pods and images kubectl get pods -o=’custom-columns=PODS:.metadata.name,Images:.spec.containers[*].image’
List pods and containers -o=’custom-columns=PODS:.metadata.name,CONTAINERS:.spec.containers[*].name’
Reference Link: kubernetes yaml templates

6 Label & Annontation

Name Command
Filter pods by label kubectl get pods -l owner=denny
Manually add label to a pod kubectl label pods dummy-input owner=denny
Remove label kubectl label pods dummy-input owner-
Manually add annonation to a pod kubectl annotate pods dummy-input my-url=https://dennyzhang.com

1.7 Deployment & Scale

Name Command
Scale out kubectl scale --replicas=3 deployment/nginx-app
online rolling upgrade kubectl rollout app-v1 app-v2 --image=img:v2
Roll backup kubectl rollout app-v1 app-v2 --rollback
List rollout kubectl get rs
Check update status kubectl rollout status deployment/nginx-app
Check update history kubectl rollout history deployment/nginx-app
Pause/Resume kubectl rollout pause deployment/nginx-deployment, resume
Rollback to previous version kubectl rollout undo deployment/nginx-deployment
Reference Link: kubernetes yaml templates, Link: Pausing and Resuming a Deployment

1.8 Quota & Limits & Resource

Name Command
List Resource Quota kubectl get resourcequota
List Limit Range kubectl get limitrange
Customize resource definition kubectl set resources deployment nginx -c=nginx --limits=cpu=200m
Customize resource definition kubectl set resources deployment nginx -c=nginx --limits=memory=512Mi
Reference Link: kubernetes yaml templates

1.9 Service

Name Command
List all services kubectl get services
List service endpoints kubectl get endpoints
Get service detail kubectl get service nginx-service -o yaml
Get service cluster ip kubectl get service nginx-service -o go-template='{{.spec.clusterIP}}’
Get service cluster port kubectl get service nginx-service -o go-template='{{(index .spec.ports 0).port}}’
Expose deployment as lb service kubectl expose deployment/my-app --type=LoadBalancer --name=my-service
Expose service as lb service kubectl expose service/wordpress-1-svc --type=LoadBalancer --name=ns1
Reference Link: kubernetes yaml templates

1.10 Secrets

Name Command
List secrets kubectl get secrets --all-namespaces
Generate secret echo -n 'mypasswd', then redirect to base64 --decode
Get secret kubectl get secret denny-cluster-kubeconfig
Get a specific field of a secret kubectl get secret denny-cluster-kubeconfig -o jsonpath=”{.data.value}”
Create secret from cfg file kubectl create secret generic db-user-pass –from-file=./username.txt
Reference Link: kubernetes yaml templates, Link: Secrets

1.11 StatefulSet

Name Command
List statefulset kubectl get sts
Delete statefulset only (not pods) kubectl delete sts/ --cascade=false
Scale statefulset kubectl scale sts/ --replicas=5
Reference Link: kubernetes yaml templates

1.12 Volumes & Volume Claims

Name Command
List storage class kubectl get storageclass
Check the mounted volumes kubectl exec storage ls /data
Check persist volume kubectl describe pv/pv0001
Copy local file to pod kubectl cp /tmp/my /:/tmp/server
Copy pod file to local kubectl cp /:/tmp/server /tmp/my
Reference Link: kubernetes yaml templates

1.13 Events & Metrics

Name Command
View all events kubectl get events --all-namespaces
List Events sorted by timestamp kubectl get events –sort-by=.metadata.creationTimestamp

1.14 Node Maintenance

Name Command
Mark node as unschedulable kubectl cordon $NDOE_NAME
Mark node as schedulable kubectl uncordon $NDOE_NAME
Drain node in preparation for maintenance kubectl drain $NODE_NAME

1.15 Namespace & Security

Name Command
List authenticated contexts kubectl config get-contexts, ~/.kube/config
Set namespace preference kubectl config set-context --namespace=
Load context from config file kubectl get cs --kubeconfig kube_config.yml
Switch context kubectl config use-context
Delete the specified context kubectl config delete-context
List all namespaces defined kubectl get namespaces
List certificates kubectl get csr
Reference Link: kubernetes yaml templates

1.16 Network

Name Command
Temporarily add a port-forwarding kubectl port-forward redis-134 6379:6379
Add port-forwaring for deployment kubectl port-forward deployment/redis-master 6379:6379
Add port-forwaring for replicaset kubectl port-forward rs/redis-master 6379:6379
Add port-forwaring for service kubectl port-forward svc/redis-master 6379:6379
Get network policy kubectl get NetworkPolicy

1.17 Patch

Name Summary
Patch service to loadbalancer kubectl patch svc $svc_name -p '{"spec": {"type": "LoadBalancer"}}'

1.18 Extenstions

Name Summary
List api group kubectl api-versions
List all CRD kubectl get crd
List storageclass kubectl get storageclass
List all supported resources kubectl api-resources

1.19 Components & Services

1.19.1 Services on Master Nodes

Name Summary
kube-apiserver exposes the Kubernetes API from master nodes
etcd reliable data store for all k8s cluster data
kube-scheduler schedule pods to run on selected nodes
kube-controller-manager node controller, replication controller, endpoints controller, and service account & token controllers

1.19.2 Services on Worker Nodes

Name Summary
kubelet makes sure that containers are running in a pod
kube-proxy perform connection forwarding
Container Runtime Kubernetes supported runtimes: Docker, rkt, runc and any OCI runtime-spec implementation.

1.19.3 Addons: pods and services that implement cluster features

Name Summary
DNS serves DNS records for Kubernetes services
Web UI a general purpose, web-based UI for Kubernetes clusters
Container Resource Monitoring collect, store and serve container metrics
Cluster-level Logging save container logs to a central log store with search/browsing interface

1.19.4 Tools

Name Summary
kubectl the command line util to talk to k8s cluster
kubeadm the command to bootstrap the cluster
kubefed the command line to control a Kubernetes Cluster Federation
Kubernetes Components Link: Kubernetes Components

Tuesday, August 14, 2018

MongoDB: List all collections by size

var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize'] + ")"); }


Improved how to get the collection:
function getReadableFileSizeString(fileSizeInBytes) {
var i = -1;
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
do {
fileSizeInBytes = fileSizeInBytes / 1024;
i++;
} while (fileSizeInBytes > 1024);
return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
};
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db.getCollection(n).stats()); });
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + getReadableFileSizeString(stats[c]['size']) + " (" + getReadableFileSizeString(stats[c]['storageSize']) + ")"); }
x

Tuesday, June 12, 2018

manually load scan dir / load directory in command line php


For checking of the .ini loaded via command line is
php --ini

If scan directory is mentioning the right path of additional loading directory path then  there is nothing to do. You can use it as per your requirement.

If additional scan directory is empty then run the following are the command, you can apply on the command line

PHP_INI_SCAN_DIR=/usr/local/etc/php/7.1/conf.d
export PHP_INI_SCAN_DIR


How to solve mysql ERROR 1118 (42000) Row size too large

  I had this issue with MYSQL 5.7 . The following worked althoug...