k8s+docker一键安装过程

时间:2024-04-18 20:40:27

环境: k8s 1.20 docker 20.10 centos7.9

#docker安装
yum install -y epel-release
yum install -y yum-utils
yum-config-manager     --add-repo     https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-20.10.6 docker-ce-cli-20.10.6 containerd.io
systemctl start docker
cat >>/etc/docker/daemon.json <<EOF
{
	"registry-mirrors":["https://reg-mirror.qiniu.com"]
	
}
EOF
systemctl daemon-reload
systemctl restart docker
#k8s安装
hostnamectl set-hostname name
cat >>/etc/hosts<<EOF 
192.168.118.190 k8s-master 
192.168.118.191 k8s-node1
192.168.118.192 k8s-node2 
EOF

cat >>etc/sysctl.d/k8s.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1 
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1 
vm.swappiness = 0
EOF

sysctl -p /etc/sysctl.d/k8s.conf

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash 
/etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
swapoff -a
sed -i '/swap/ s/^/#/' /etc/fstab

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg 
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


yum install -y kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6

systemctl enable kubelet

kubeadm init --kubernetes-version=1.20.6 \
--apiserver-advertise-address=192.168.118.190 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--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

wget http://docs.projectcalico.org/v3.8/manifests/calico.yaml


sed -i 's/192.168.0.0/10.244.0.0/g' calico.yaml

kubectl apply -f calico.yaml