#!/bin/bash
#***************************************************************#
# ScriptName: ck8spreconfig.sh
# Author: xhy
# Create Date: 2025-07-15 01:08
# Modify Author: xhy
# Modify Date: 2025-12-19 00:46
# Version: v1
#***************************************************************#

# openEuler OS
OPENEULER_OS=yes

# Initialize the machine. This needs to be executed on every machine.
rm -f /var/lib/rpm/__db.00*
rpm -vv --rebuilddb
#yum clean all 
#yum makecache
sleep 2s

# Install containerd
CONVERSION=2.2.0
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

if [ "$OPENEULER_OS" = "yes" ]; then
    sed -i 's/\$releasever/9/g' /etc/yum.repos.d/docker-ce.repo
fi

sleep 2s
yum -y install containerd.io-${CONVERSION}

mkdir -p /etc/containerd/certs.d/docker.io
mkdir -p /data/containerd

cat > /etc/containerd/config.toml <<EOF
disabled_plugins = ["io.containerd.internal.v1.restart"]
root = "/data/containerd"
version = 2

[plugins]

  [plugins."io.containerd.grpc.v1.cri"]
#    sandbox_image = "registry.k8s.io/pause:3.10"
    sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.10.1"

    [plugins."io.containerd.grpc.v1.cri".containerd]

      [plugins."io.containerd.grpc.v1.cri".containerd.runtimes]

        [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
          runtime_type = "io.containerd.runc.v2"

          [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
            SystemdCgroup = true

    [plugins."io.containerd.grpc.v1.cri".registry]
      config_path = "/etc/containerd/certs.d"

  [plugins."io.containerd.runtime.v1.linux"]
    shim_debug = true
EOF

cat > /etc/containerd/certs.d/docker.io/hosts.toml <<EOF
server = "https://registry-1.docker.io"

[host."https://image.cloudlayer.icu"]
  capabilities = ["pull", "resolve", "push"]

[host."https://docker.sunzishaokao.com"]
  capabilities = ["pull", "resolve", "push"]

[host."https://docker.m.daocloud.io"]
  capabilities = ["pull", "resolve", "push"]
  
[host."https://docker.1ms.run"]
  capabilities = ["pull", "resolve", "push"]

[host."https://docker.hlmirror.com"]
  capabilities = ["pull", "resolve", "push"]

[host."https://docker.melikeme.cn"]
  capabilities = ["pull", "resolve", "push"]
EOF

# config crictl & containerd 
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

echo 'alias ctrpull="ctr -n k8s.io images pull --hosts-dir /etc/containerd/certs.d"' >> /etc/profile.d/ctr_bash.sh

systemctl restart containerd
systemctl enable containerd --now

# Disable the SELinux.
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

# Turn off and disable the firewalld.
systemctl stop firewalld
systemctl disable firewalld

# Modify related kernel parameters & Disable the swap.
cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness = 0
vm.overcommit_memory = 1
vm.panic_on_oom = 0
net.ipv6.conf.all.disable_ipv6 = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf >&/dev/null
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
echo -e "#/bin/bash \nswapoff -a" > /etc/profile.d/disabled_swap.sh

# Add ipvs modules
cat > /etc/modules-load.d/ipvs.conf <<EOF
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
br_netfilter
overlay
xt_CT
EOF

sysctl --system

systemctl restart systemd-modules-load.service

# Install rpm
yum install -y conntrack curl ipvsadm iproute-tc ipset iptables jq libseccomp pcre2 pcre2-devel sysstat wget
