k8s_concept
Kubernetes
Kubernetes is a software system that allows you to easily deploy and manage containerized applications on top of it. It relies on the features of Linux containers to run heterogeneous applications
without having to know any internal details of these applications and without having to manually deploy these applications on each host.
k8s_cmd
K8s commands
Kubernetes resources are usually created/edited by posting a JSON or YAML manifest
to the Kubernetes REST API endpoint, as with this manifest, you have more control of the resource
, also it is a bit complex, hence K8S provides specific commands to create/edit limited properties of resources.
k8s_faq
Question and Answer about Kubernetes.
nginx_thundering_herd_accept
In computer science, the thundering herd problem occurs when a large number of processes or threads waiting for an event are awoken when that event occurs, but only one process is able to handle the event
http-apache
configure apache conf, tips for apache server
c-language-tips
Overview
parameter passing
x86 always uses stack
to pass function parameter, while x86-64 always uses registers
to pass function if possible(as it has more registers than x86)
ESP (Extended Stack Pointer), always points to the top of the stack. RSP(x86-64)
EBP (Extended Stack Base pointer), like a stack boundary for a function, all stack variables are offset from it.RBP(x86-64)
, EBP bottom of stack which has the highest address.push
- decrease ESP(get a slot), save value to stack(slot, 4 bytes or 8 bytes etc)
pop
- get the value, increase ESP
bash-basic
Bash
Introduction
As bash is old or native way to run commands, there are lots of scripts written with bash, so it’s a must to know bash
and do some basic work.
Better use python instead of bash as you can call bash in python
and use python powerful features
printing
There are several ways for printing and writing multiple lines, echo
is the easy way to do
1 | # print multiple lines |
1 | %%bash |
a b
a
b
a
b
special characters
Special characters here mean shell sees them special but not treated as regular pattern
, if shell treats them as pattern, its meaning may be different. see an example.
1 | $ls |
grep "ab*" ab* search pattern ab*(a, ab matched, abc not match)
from file which starts with ab(abd.c, abc.c all match)
whitespace The most special char in shell is whitespace
, whitespace
is used as separator for parameters, like function parameter, test parameter etc.
; semi-colon is used to separate instructions if at same line
, if each instruction at each line, ;
can be ignored. $ls;date
* any more(0-)
characters #ls ab*
—>file ab matches
? any character, just one
#ls ab?
—>file ab not match!!
[] any character inside it becomes normal except \ [] !
#ls test.[ch]
1 | - indicates different meanings depends on its location |
| command pipe, #ls ab* | xargs cat
() group command, run command in subshell
, #msg=$(echo hello)
& run in background
‘’ keep all(characters) as literal inside, Any char in '' are escaped
, echo 'I\'m a boy' #error
“” quotes string and evaluates variable #echo "$var"
most chars in “” (except [$, `, \]) are escaped
\ escape character
`` run command and get its output, #msg=`echo hello`
Standard wildcards(globing)
Standard wildcards (also known as globing patterns) are used by various command-line utilities to work with multiple files. Standard wildcards are used by nearly any command
(including mv, cp, rm and many others).
? (question mark)
this can represent any single character
. If you specified something at the command line like “hd?” GNU/Linux would look for hda, hdb, hdc and every other letter/number between a-z, 0-9.* (asterisk)
this can represent any number of characters
(including zero, in other words, zero or more characters). If you specified a “cd*” it would use “cda”, “cdrom”, “cdrecord” and anything that starts with “cd” also including “cd” itself. “m*l” could by mill, mull, ml, and anything that starts with an m and ends with an l.[ ] (square brackets)
specifies a range
. If you didm[a,o,u]m it can become: mam, mum, mom
if you did: m[a-d]m it can become anything that starts and ends with m and has any character a to d in between. For example, these would work: mam, mbm, mcm, mdm. This kind of wildcard specifies an “or” relationship (you only need one to match).
{ } (curly brackets, also called brace)
terms are separated by commas and each term must be the name of something or a wildcard
. This wildcard will copy anything that matches either wildcard(s), or exact name(s) (an “or” relationship, one or the other).[!]
This construct is similar to the [ ] construct, except rather than matching any characters inside the brackets,
it'll match any character, as long as it is not listed between the [ and ]
. This is a logical NOT. For example rm myfile[!9] will remove all myfiles* (ie. myfiles1, myfiles2 etc) but won’t remove a file with the number 9 anywhere within it’s name.
variable
Like Python, variable has week type(no need to declare it first
), shell checks its type only when it runs
, like others, shell has local, global, env
variable, if no keyword is specified, default is global!
1 | %%bash |
jason kk
hime
jason kk
jason kk, jason kk, jason kk
11
local /global var
by default, variable is global
, but you can only add local
keyword in function to strict its scope, local
keyword can’t be used outside of a function. but all variables(local global) can be seen in sushell(forked process) as well, but it's another copy of these variables, changed in subshell not see by parent!!!
special var
1 | $? Exit status of last task |
1 | %%bash |
in function c=13
in function a=12
in main: a=
in main: b=20
env var
Env variable is defined outside of the a script, or you can define it in the script, so that all subshells can have it.
1 | %%bash |
hello
/opt/llvm/bin:/home/data/Anaconda3/envs/py3.9/bin:/home/data/Anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/go:/home/go/bin:/root/.yarn_pkg/bin:/usr/lib64:/usr/local/go/bin:/root/.local/bin:/home/data/Anaconda3/bin:/home/data/Anaconda3/sbin
array[pay attention]
you can define an array with one line Fruits=('Apple' 'Banana' 'Orange')
or add item at any slot Fruits[1]="Ab"
or remove one element unset Fruits[1]
, then you can access one item or all items or range items
like below
1 | - all items---echo ${Fruits[@]} |
1 | %%bash |
Apple
Apple Banana Orange
3
5
0
Apple Banana Orange
Apple Banana Orange Watermelon Watermelon
Banana Orange Watermelon Watermelon
Banana Watermelon Watermelon
function
Unsupported declare function parameter like function(a,b), use $1, $2 etc
parameter
1 | $#----number of parameters from command line(not count script name) |
$ ./myspt.sh h1 h2
1 | $#-----2 |
1 | #!/bin/bash |
The implementation of “$“ has always been a problem and realistically should have been replaced with the behavior of “$@”. In almost every case where coders use “$“, they mean “$@”. “$*” can cause bugs and even security holes in your software.
always use “$@” if you want to check all parameters as it’s mostly what you want
definition
no return keyword, output as return
no explicit parameter list
- get the return value by command group
()
or command substitution `` $() is the suggested way
1 | %%bash |
max is: 2
Subshell
()(group command) runs in subshell
, all others run in main shell like {}, (())
.
1 | %%bash |
, 12, 13
substitution[pay attention]
command substitution
1 | files=`ls *` |
variable substitution
1 | ${var:-word} #if var null or unset, return word, unchanged var |
wildcard substitution
1 | $ ls * |
1 | # The pattern matching is always greedy!! |
1 | %%bash |
/path/to/foo
/path/to/foo.o
cpp
foo.cpp
path/to/foo.cpp
foo.cpp
/path/to/bar.cpp
foo.cpp
/path/to/
compare or condition
[] is an old way, used for string and number, file
, here are options for it,
1 | - file -d -c -b -f -e(exist) -r -w -x(executable) |
[[]] supports all options as []
, As a rule of thumb,
- [[ is used for strings and files.
- numbers, use an (())
1 | = Set Equal to |
&& and || used for logical
[[ support pattern matching [[ STRING =~ PATTERN ]]
, it’s regular pattern not globing
Difference:
1 | file="file name" |
Implicit Conversion
The -eq
(integer operators) causes the strings to be interpreted as integers if possible including base conversion
1 | %%bash |
equal
Numeric
flow control
There are several ways to do flow control, like if/while/for, let’s see each of them with example.
1 | %%bash |
a equal 0 or 1
1
a
b
a b
bash-basic.ipynb
0
1
2
3
4
$a matches pattern
signal
Set signal handler with trap command
1 | trap quit 2 3 9 #signal handler |
string(slice of string)[pay attention]
1 | %%bash |
oh
Jo
Joh
ohn
oh
tips
debug shell
check syntax
$bash -n test.sh
just check syntax of test.sh
debug shell
1 | set -x #enable tracing will display all commands and their arguments as they execute. |
redirect
- stdin——————-0
- stdout——————1
- stderr——————2
>
just redirects the stdout
to file without stderr
.
$ls no_file.txt >log.t 2>&1
cd vs pushd/popd
With cd
you need to write the path explicitly, while pushd/popd, you don't need remember the original path
.
1 | #pushd /home/lzq |
() vs (()) vs let vs expr
- () is used for
command groups with subshell
, you canrun any command in subshell
- (()) and let are just for
integer
,not in subshell
- expr is only for
integer operation
as well as expr is a command, so $ is a must for variable!!! $[]
is forinteger operation
as well
NOTE: bash only supports integer operation when do integer operation $ can be omitted for variable
1 | %%bash |
hello
hello
0
10
10
0
1
11
5
============$(())=========
5
15
============$[]=========
5
9
10
15
[] vs [[]]
[ ("test" command) and [[ ("new test" command) are used to evaluate expressions. [[ works only in Bash, Zsh and the Korn shell, and is more powerful; [ and test are available in POSIX shells.
brace expansion
Used to generate list of string or number
with prefix or suffix for each item, prefix or suffix is optional
.
{} linked each item with prefix and suffix if has
, print it as a single string
use case
_{a..f}_
{a,b,c}
1 | %%bash |
a b
_{a, b}_
_a_ _ b_
not_prefix prefix_a_suffix prefix_b_suffix not_suffix
a b c d e f
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10
1
3
5
item1
item2
read a file
you can read a whole file at one time or read it line by line
1 | # read whole into memory at once |
dict in shell [pay attention]
In order to use dict, you must declare it first like this
declare -A sounds
1 | %%bash |
bark
bark howl moo tweet
dog wolf cow bird
4
dog
wolf
cow
bird
bark
howl
moo
tweet
difference with or without “” for variable expansion
In most shells, leaving a variable expansion unquoted is like invoking some sort of implicit split+glob operator.
$var
In another language would be written something like:glob(split($var))
$var
is first split into a list of words according to complex rules involving the $IFS
special parameter (the split part) and then each word resulting of that splitting is considered as a pattern which is expanded.
1 | %%bash |
hello
boy
hello boy
total
40
-rw-r--r--
1
jaluo
root
40733
Feb
22
02:04
bash-basic.ipynb
count
total 40 -rw-r--r-- 1 jaluo root 40733 Feb 22 02:04 bash-basic.ipynb
IFS
IFS: The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>
.
1 | %%bash |
hello
boy
hello
boy
include file
Use source
or .
1 | source /path/s.sh |
random number
$RANDOM
is the random number
1 | %%bash |
15887
27804
do another operation if first command runs ok
1 | %%bash |
ls runs ok
ls runs badly
ls runs ok
ls runs ok
ls runs badly
quotes
for assignment in bash, the righ must be a single word
, so S="a b"
has the same with S=a\ b
, “” to make a string as a whole, like below
1 | for i in "a b" # "a b" as a whole |
check string contains
To check if a string contains a substring
1 | %%bash |
It's there.
It's there.
It's there.
It's there
repeat char(str) n times
1 | %%bash |
==========
==========
==========
echo with color
1 | %%bash |
I [0;31mlove[0m Stack Overflow
Invert boolean variable
1 | %%bash |
true
false
compile bash to binary
1 | $ sudo yum install -y shc |
Ref
http-protocol-connection
Overview
HTTP mostly relies on TCP for its transport protocol, providing a connection between the client and the server. In its infancy(early stage like http1.0), HTTP used a single model to handle such connections. These connections were short-lived: a new one created each time a request needed sending, and closed(server does) once the answer was sent
But in HTTP/1.x, there are several models: short-lived connections
, persistent connections
, and HTTP pipelining(never used in practice, but the foundation of http2.0(pipeline mode)), The persistent-connection model keeps connections opened between successive requests, reducing the time needed to open new connections. The HTTP pipelining model goes one step further, by sending several successive requests without even waiting for an answer
, reducing much of the latency in the network.