K8S集群的单机部署
一、关闭selinux和firewalld

setenforce 0
systemctl stop firewalld
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl disable firewalld

二、禁用swap

swapoff -a

在/etc/fstab里面把swap行注释

三、修改内核参数和模块

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
 
sysctl --system
 
modprobe br_netfilter
lsmod | grep br_netfilter

四、安装docker

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
systemctl start docker
systemctl enable docker

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://docker.mirrors.ustc.edu.cn","https://tnxkcso1.mirror.aliyuncs.com"],
"log-driver":"json-file",
"log-opts":{ "max-size" :"50m","max-file":"1"}
}
EOF

systemctl daemon-reload
systemctl restart docker

五、设置阿里云仓库、更新yum源并安装kubernetes组件

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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache


yum -y install kubelet-1.19.4 kubeadm-1.19.4 kubectl-1.19.4
systemctl start kubelet
systemctl enable kubelet

六、下载k8s相关镜像并打标签
下载镜像

docker pull kregistry.aliyuncs.com/google_containers/kube-apiserver:v1.19.4
docker pull mirrorgooglecontainers/kube-controller-manager:v1.19.4
docker pull mirrorgooglecontainers/kube-scheduler:v1.19.4
docker pull mirrorgooglecontainers/kube-proxy:v1.19.4
docker pull mirrorgooglecontainers/pause:3.2
docker pull mirrorgooglecontainers/etcd:3.4.13-0
docker pull coredns/coredns:1.7.0

给镜像打标签

docker tag mirrorgooglecontainers/kube-apiserver:v1.19.4 k8s.gcr.io/kube-apiserver:v1.19.4
docker tag mirrorgooglecontainers/kube-controller-manager:v1.19.4  k8s.gcr.io/kube-controller-manager:v1.19.4
docker tag mirrorgooglecontainers/kube-scheduler:v1.19.4 k8s.gcr.io/kube-scheduler:v1.19.4
docker tag mirrorgooglecontainers/kube-proxy:v1.19.4 k8s.gcr.io/kube-proxy:v1.19.4
docker tag mirrorgooglecontainers/pause:3.2 k8s.gcr.io/pause:3.2
docker tag mirrorgooglecontainers/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
docker tag coredns/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0

删除原有镜像

docker rmi kregistry.aliyuncs.com/google_containers/kube-apiserver:v1.19.4
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.19.4
docker rmi mirrorgooglecontainers/kube-scheduler:v1.19.4
docker rmi mirrorgooglecontainers/kube-proxy:v1.19.4
docker rmi mirrorgooglecontainers/pause:3.2
docker rmi mirrorgooglecontainers/etcd:3.4.13-0
docker rmi coredns/coredns:1.7.0

或者使用脚本:
编辑一下脚本

vim image.sh 

url=registry.aliyuncs.com/google_containers
version=v1.19.4
images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
for imagename in ${images[@]} ; do
  docker pull $url/$imagename
  docker tag $url/$imagename k8s.gcr.io/$imagename
  docker rmi -f $url/$imagename
done


chmod 775  image.sh
sh  image.sh    #运行脚本

七、初始化k8s和网络

kubeadm init --kubernetes-version=v1.19.4 --pod-network-cidr=10.100.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
 
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')
kubectl taint node kube-master node-role.kubernetes.io/master-  # 默认k8s的master节点是不能跑pod的业务,需要执行以下命令解除限制

kubectl get pods  --all-namespaces     #查看所有组件
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐