(共556篇)
全部分类

K8S+Containerd自动化部署脚本
[ Docker ] 

环境: Centos7

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 停止防火墙
systemctl stop firewalld
# 禁止防火墙开机启动
systemctl disable firewalld
# 临时关闭swap分区
swapoff -a
# 永久关闭swap分区
sed -i 's/\(.*swap.*\)/#&/' /etc/fstab

# 临时启用内核模块--桥接防火墙
modprobe br_netfilter

# 临时开启路由转发功能
echo 1 >  /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables

# 设置开机启动桥接防火墙
echo "modprobe br_netfilter" >> /etc/rc.d/rc.local
# 给文件设置可执行权限, 否则开机启动后
chmod +x /etc/rc.d/rc.local

# 设置开机启用路由转发功能
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

# 添加docker的yum仓库源
cat docker-ce.repo > /etc/yum.repos.d/docker-ce.repo

# 修改yum源为中科大
sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.ustc.edu.cn|g' -i.bak /etc/yum.repos.d/CentOS-*.repo

# 添加k8s的yum仓库源
cat k8s.repo > /etc/yum.repos.d/kubernetes.repo

yum makecache

# 更新系统时间
yum install -y ntpdate
ntpdate asia.pool.ntp.org
date

# 安装k8s及containerd包
yum install -y kubeadm kubelet kubectl 
yum install -y containerd.io 

# 创建默认的containerd配置文件
mv /etc/containerd/config.toml /etc/containerd/config.toml.back
containerd config default > /etc/containerd/config.toml
sed -ri '/sand/ s/sandbox_image = (.*)/sandbox_image = "registry.cn-hangzhou.aliyuncs.com\/google_containers\/pause:3.7"/' /etc/containerd/config.toml


systemctl daemon-reload
systemctl enable containerd
systemctl start containerd
systemctl enable kubelet
systemctl start kubelet

# 创建crictl配置文件
crictl config --set runtime-endpoint="unix:///run/containerd/containerd.sock"
crictl config --set timeout=20

# 安装CNI插件
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz

# 添加CNI配置文件
cat << EOF | tee /etc/cni/net.d/10-containerd-net.conflist
{
 "cniVersion": "1.0.0",
 "name": "containerd-net",
 "plugins": [
   {
     "type": "bridge",
     "bridge": "cni0",
     "isGateway": true,
     "ipMasq": true,
     "promiscMode": true,
     "ipam": {
       "type": "host-local",
       "ranges": [
         [{
           "subnet": "10.88.0.0/16"
         }],
         [{
           "subnet": "2001:db8:4860::/64"
         }]
       ],
       "routes": [
         { "dst": "0.0.0.0/0" },
         { "dst": "::/0" }
       ]
     }
   },
   {
     "type": "portmap",
     "capabilities": {"portMappings": true}
   }
 ]
}
EOF

# 重启containerd
systemctl restart containerd


read -p "当前主机是master节点吗(y/n)" is_master
if [[ $is_master == "y" ]]; then
	echo "Init Kube Cluster Master Node"
else
  echo "Child Node Init Completed"
	exit
fi

# 初始化k8s应用 
kubeadm init --v=5 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf

# 安装网络支持插件
kubectl apply -f kube-flannel.yml

echo "Master Node Init Completed"

alias k=kubectl
k cluster-info
k get node
k get ns
k get pod -A