kubenetes辅助脚本hogwarts_k8s.sh

# k apply -f
# kubectl create deployment nginx --image=nginx --port=80
k() {
    kubectl "$@"
}
#kpods
#kpods dns
kpods() {
    local params="${@:-""}"
    kubectl get pod -A | grep "$params"
}

#klog dns
klog() {
    kubectl get po -A | grep "$@" | while read namespace pod other; do kubectl logs $pod -n $namespace; done
}

#kdc dns
kdc() {
    kubectl get po -A | grep "$@" | while read namespace pod other; do kubectl describe pod $pod -n $namespace; done
}

#systemctl_lastlog kubelet
#systemctl_lastlog containerd
systemctl_lastlog() {
    local unit="$@"
    local last=$(journalctl -x -u $unit | grep -E 'systemd\[1\]: Started' | tail -1 | awk '{print $3}')
    journalctl --since $last -ex -u $unit
}

#systemctl_lastlog_error kubelet
#systemctl_lastlog_error containerd
systemctl_lastlog_error() {
    systemctl_lastlog "$@" | grep ': E'
}

#proxy curl https://ceshiren.com
proxy() {
    local proxy=http://127.0.0.1:3128
    http_proxy=$proxy https_proxy=$proxy "$@"
}

docker_install() {
    for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
    proxy apt-get update
    sudo apt-get install -y ca-certificates curl gnupg

    sudo install -m 0755 -d /etc/apt/keyrings
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg

    echo \
        "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |
        sudo tee /etc/apt/sources.list.d/docker.list >/dev/null

    proxy apt-get update
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    sudo docker run hello-world
}
containerd_install() {
    apt-get update
    apt list -a containerd
    apt-get install containerd -y
    containerd --version
    mkdir /etc/systemd/system/containerd.service.d/
    echo '
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:3128"
Environment="HTTPS_PROXY=http://127.0.0.1:3128"
Environment="NO_PROXY=localhost"
' >/etc/systemd/system/containerd.service.d/http-proxy.conf
    systemctl daemon-reload
    systemctl restart containerd
    systemctl status containerd
}

k8s_install() {
    apt-get update
    apt-get install -y apt-transport-https ca-certificates curl
    proxy curl -fsSL https://dl.k8s.io/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
    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
    proxy apt-get update
    proxy apt-get install -y kubelet kubeadm kubectl
    # sudo apt-mark hold kubelet kubeadm kubectl
    crictl config --set runtime-endpoint=unix:///run/containerd/containerd.sock
    crictl ps

    #     cat >>/etc/sysctl.conf <<EOF
    # net.ipv6.conf.all.disable_ipv6 = 1
    # net.ipv6.conf.default.disable_ipv6 = 1
    # EOF
    #     sysctl -p
}

k8s_setup() {
    kubeadm reset -f
    version=$(kubeadm version | grep -o 'GitVersion[^ ]*' | awk -F\" '{print $2}')
    kubeadm config images pull --kubernetes-version $version -v5 # --image-repository registry.aliyuncs.com/google_containers
    #modprobe br_netfilter
    #echo 1 > /proc/sys/net/ipv4/ip_forward
    swapoff -a
    kubeadm init --kubernetes-version $version -v5 --log-file init.log \
        --pod-network-cidr=10.244.0.0/16
    # --apiserver-advertise-address=172.17.102.164 可自动识别,一般不用写
}