一键部署k8s,十分钟搞定
k8s集群快速安装。一键解决你的烦恼
·
一.初始化
我将繁琐的准备配置成了一个bash脚本,注意这里需要修改DNS解析下的nameserver,改为你的ip,在三个节点上执行脚本
#!/bin/bash
echo "1、修改DNS配置如有需要"
chattr -i /etc/resolv.conf
cat <<EOF >> /etc/resolv.conf
nameserver 124.70.16.104
nameserver 121.36.26.129
nameserver 121.36.20.14
EOF
echo "清理一些无用的yum repos"
mkdir -p /etc/yum.repos.d/back && mv /etc/yum.repos.d/ambari* /etc/yum.repos.d/hdp* /etc/yum.repos.d/back
yum clean all
### 关闭swap分区
echo "swapoff ## 临时生效"
swapoff -a
## 永久生效可以编辑/etc/fstab文件 注释掉swap那行
echo "关闭防火墙"
systemctl stop firewalld
systemctl disable --now firewalld
echo "关闭sselinux"
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 内核开启IPv4转发需要开启下面的模块
modprobe br_netfilter
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
EOF
echo "设置docker的目录软连接 /var/lib/docker把docker存储设置到数据盘上"
mkdir -p /data1/docker
ln -s /data1/docker /var/lib
echo "安装docker 依赖 "
yum install yum-utils device-mapper-persistent-data lvm2 -y
### 新增 Docker 仓库。
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum clean all
## 安装 Docker CE.
yum update -y && yum install -y \
containerd.io-1.2.13 \
docker-ce-19.03.11 \
docker-ce-cli-19.03.11
## 创建 /etc/docker 目录。
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": [ "https://1nj0zren.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn", "http://f1361db2.m.daocloud.io", "https://registry.docker-cn.com" ]
}
EOF
sleep 1
systemctl daemon-reload
systemctl restart docker
sudo systemctl enable docker
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
exclude=kube*
EOF
yum install -y kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3 --disableexcludes=kubernetes
sleep 1
kubeadm version
systemctl enable kubelet
systemctl daemon-reload
systemctl restart kubelet
二.集群安装
接下来在master节点
直接init
kubeadm init \
--apiserver-advertise-address=master节点的阿里云公网ip \
--image-repository registry.aliyuncs.com/google_containers
或者init之前先把镜像拉取下来
kubeadm config images pull
如果显示镜像拉取失败,可以采取docker的方式获取镜像
docker pull gotok8s/kube-apiserver:v1.19.0
docker tag gotok8s/kube-apiserver:v1.19.0 k8s.gcr.io/kube-apiserver:v1.19.0
docker pull gotok8s/kube-controller-manager:v1.19.0
docker tag gotok8s/kube-controller-manager:v1.19.0 k8s.gcr.io/kube-controller-manager:v1.19.0
docker pull gotok8s/kube-scheduler:v1.19.0
docker tag gotok8s/kube-scheduler:v1.19.0 k8s.gcr.io/kube-scheduler:v1.19.0
docker pull gotok8s/kube-proxy:v1.19.0
docker tag gotok8s/kube-proxy:v1.19.0 k8s.gcr.io/kube-proxy:v1.19.0
docker pull gotok8s/pause:3.2
docker tag gotok8s/pause:3.2 k8s.gcr.io/pause:3.2
docker pull gotok8s/etcd:3.4.13-0
docker tag gotok8s/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0
docker pull gotok8s/coredns:1.7.0
docker tag gotok8s/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0
init之后再主节点执行
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://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
init成功之后会有一行join命令,直接在其他的node上运行
kubeadm join 你的主节点ip:6443 \
--token 8507uc.o0knircuri8etnw2 \
--discovery-token-ca-cert-hash \
sha256:acc37967fb5b0acf39d7598f8a439cc7dc88f439a3f4d0c9cae88e7901b9d3f
查看节点状态
kubectl get nodes
刚刚加入时是noready状态,等几分钟就ready啦。这里就恭喜你部署完成啦。
更多推荐
已为社区贡献3条内容
所有评论(0)