Skip to content

Instantly share code, notes, and snippets.

@asahicantu
Last active January 13, 2024 19:28
Show Gist options
  • Save asahicantu/a580219b491827c2c611b37bd8404c57 to your computer and use it in GitHub Desktop.
Save asahicantu/a580219b491827c2c611b37bd8404c57 to your computer and use it in GitHub Desktop.
K8s
alias k=kubectl
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
k apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
complete -F __start_kubectl k
export do="--dry-run=client -o yaml"
export dd "--grace-period=0 --force"
k get node node1 -o custom-colums=NODE:.spec.taints
taint=$(k describe node node1 | grep -i taint | awk '{print $2}')
k create cm foo --from-env-file=file.env $do > o.yaml
k delete all --all $dd
#vim
:set tabstop=2
k taint node node1 $taint-
# Get current ip address
curl ifconfig.io
ns = namespace-name
k create ns $ns
k config set-context --current $ns
k config view | grep -i namespace
k set image pod <pod> <container\pod>=<img>
apt install bash-completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> $HOME/.bashrc
k get nodes
k describe node
k taint :taint-name-
k create bb --image busybox --dry-run=client -o yaml -c "while true"
kubectl -A = kubectl --all-namespaces
#export KUBECONFIG=path1:path2:path3
kubectl config set-context --current --namespace=NAMESPACE
k get pods
k get all
k run tmp --restart=Never --rm --image=busybox -i -- wget -O- 10.12.2.15
k run tmp --restart=Never --rm --image=busybox -i -- wget -O- 10.12.2.15
k -n sun create deployment sunny --image=nginx:1.17.3-alpine $do > p2_sunny.yaml
k -n sun expose -h # help
k -n sun expose deployment sunny --name sun-srv --port 9999 --target-port 80
k run tmp --restart=Never --rm -i --image=nginx:alpine -- curl -m 5 sun-srv.sun:9999
k -n earth get all
k -n earth get deploy earth-3cc-web
k -n earth edit deploy earth-3cc-web
k -n earth get pod -l id=earth-3cc-web
k -n earth get pod -l id=earth-3cc-web
k run tmp --restart=Never --rm -i --image=nginx:alpine -- curl -m 5 earth-3cc-web.earth:6363
k run frontend --image=nginx --restart=Never --port=80
k api-resources
k describe
k explain pods.spec
k delete --grace-period=0 --force
#!/bin/bash
alias k=kubectl
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
k apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
complete -F __start_kubectl k
export do="--dry-run=client -o yaml"
export dd "--grace-period=0 --force"
k get node node1 -o custom-colums=NODE:.spec.taints
taint=$(k describe node node1 | grep -i taint | awk '{print $2}')
sudo kubectl completion bash > /etc/bash_completion.d/kubectl
and add this at the end of .bashrc
source /usr/share/bash-completion/bash_completion
source /etc/bash_completion
source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#to verify, if kubectl is working or not, run the following command.
kubectl get pod -A
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
#verify if weave is deployed successfully
kubectl get pods -A
kubeadm token create — print-join-command
#!/bin/bash
# common.sh
# copy this script and run in all master and worker nodes
#i1) Switch to root user [ sudo -i]
#2) Disable swap & add kernel settings
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
#3) Add kernel settings & Enable IP tables(CNI Prerequisites)
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
#4) Install containerd run time
#To install containerd, first install its dependencies.
apt-get update -y
apt-get install ca-certificates curl gnupg lsb-release -y
#Note: We are not installing Docker Here.Since containerd.io package is part of docker apt repositories hence we added docker repository & it's key to download and install containerd.
# Add Docker’s official GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
#Use follwing command to set up the repository:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install containerd
apt-get update -y
apt-get install containerd.io -y
# Generate default configuration file for containerd
#Note: Containerd uses a configuration file located in /etc/containerd/config.toml for specifying daemon level options.
#The default configuration can be generated via below command.
containerd config default > /etc/containerd/config.toml
# Run following command to update configure cgroup as systemd for contianerd.
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
# Restart and enable containerd service
systemctl restart containerd
systemctl enable containerd
#5) Installing kubeadm, kubelet and kubectl
# Update the apt package index and install packages needed to use the Kubernetes apt repository:
apt-get update
apt-get install -y apt-transport-https ca-certificates curl
# Download the Google Cloud public signing key:
curl -fsSL https://dl.k8s.io/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
# Add the Kubernetes apt repository:
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Update apt package index, install kubelet, kubeadm and kubectl, and pin their version:
apt-get update
apt-get install -y kubelet kubeadm kubectl
# apt-mark hold will prevent the package from being automatically upgraded or removed.
apt-mark hold kubelet kubeadm kubectl
# Enable and start kubelet service
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet.service
#!/bin/bash
alias k=kubectl
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
k apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
complete -F __start_kubectl k
export do="--dry-run=client -o yaml"
export dd "--grace-period=0 --force"
k get node node1 -o custom-colums=NODE:.spec.taints
taint=$(k describe node node1 | grep -i taint | awk '{print $2}')
#!/bin/bash
alias k=kubectl
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
k apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
complete -F __start_kubectl k
export do="--dry-run=client -o yaml"
export dd "--grace-period=0 --force"
k get node node1 -o custom-colums=NODE:.spec.taints
taint=$(k describe node node1 | grep -i taint | awk '{print $2}')
@asahicantu
Copy link
Author

kubeadm init

#exit root as normal user
exit
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#to verify, if kubectl is working or not, run the following command.
kubectl get pod -A

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
#verify if weave is deployed successfully
kubectl get pods -A

kubeadm token create — print-join-command

kubeadm join 172.30.20.20:6443 — token cdm6fo.dhbrxyleqe5suy6e
— discovery-token-ca-cert-hash sha256:1fc51686afd16c46102c018acb71ef9537c1226e331840e7d401630b96298e7d

kubeadm join 172.30.20.20:6443 — token cdm6fo.dhbrxyleqe5suy6e
— discovery-token-ca-cert-hash sha256:1fc51686afd16c46102c018acb71ef9537c1226e331840e7d401630b96298e7d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment