本地搭建k8s1.8环境

此次使用的是Centos7.6的虚拟机2台至少4G内存并可以连接互联网

k8s-master192.168.200.50
k8s-node1192.168.200.51

使用k8s官方的部署工具kubeadm来进行安装

k8s-master节点操作:

#设置ssh免密登陆
ssh-keygen
ssh-copy-id 192.168.200.51
cat >> deploy_k8s-master.sh  << EOF
#!/bin/bash
#这是k8s1.8的在线部署脚本
hostname="k8s-master"
hostip="192.168.200.50"
pre_env(){
#1.关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld

#2.关闭selinux:
sed -i 's/enforcing/disabled/'/etc/selinux/config  # 永久
setenforce 0 # 临时

#3.关闭swap:
swapoff -a  # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab   #永久

#4.根据规划设置主机名:
hostnamectl set-hostname $hostname

#5.在master添加hosts:
cat >>/etc/hosts << EOF
${hostip} k8s-master
192.168.200.51 k8s-node1
151.101.76.133 raw.githubusercontent.com
EOF
cat >/etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables =1
net.bridge.bridge-nf-call-iptables =1
EOF
sysctl --system  # 生效
yum install ntpdate -y
ntpdate time.windows.com
}
Install_software(){
#1.安装Docker
yum install wget -y
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
systemctl enable docker && systemctl start docker
#2.配置镜像下载加速器
cat >/etc/docker/daemon.json << EOF
{
"registry-mirrors":["https://1vdfdb9l.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
#3.添加阿里云YUM软件源并安装kubeadm,kubelet和kubectl
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
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
sed -i -r "s#=#='--feature-gates SupportPodPidsLimit=false --feature-gates SupportNodePidsLimit=false'#g" /etc/sysconfig/kubelet
systemctl enable kubelet && systemctl start kubelet
}
deploy_master(){
touch join_master
#1.初始化集群
kubeadm init \
--apiserver-advertise-address=${hostip} \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 | grep -E "kubeadm join|--d" > join_master.sh
scp join_master.sh root@k8s-node1:~
#2.拷贝kubectl使用的连接k8s认证文件到默认路径
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
}
deploy_cni(){
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sed -i -r "s#quay.io/coreos/flannel:.*-amd64#registry.cn-hangzhou.aliyuncs.com/isys/flannel:v0.12.0-amd64#g" kube-flannel.yml 
kubectl apply -f kube-flannel.yml
}
deploy_dashboard(){
kubectl apply -f https://raw.githubusercontent.com/liyuhang001/k8s-web-nodeport/master/dashboard.yaml
}

pre_env
Install_software
deploy_master
deploy_cni
deploy_dashboard
EOF

执行脚本

chmod +x deploy_k8s-master.sh && bash deploy_k8s-master.sh

node节点操作:

cat >> deploy_k8s-node.sh  << EOF
#!/bin/bash
#这是k8s1.8的在线部署脚本
pre_env(){
#1.关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld

#2.关闭selinux:
sed -i 's/enforcing/disabled/'/etc/selinux/config  # 永久
setenforce 0 # 临时

#3.关闭swap:
swapoff -a  # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab   #永久

#4.根据规划设置主机名:
hostnamectl set-hostname k8s-node1

#5.在master添加hosts:
cat >>/etc/hosts << EOF
192.168.200.50 k8s-master
192.168.200.51 k8s-node1
151.101.76.133 raw.githubusercontent.com
EOF
cat >/etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables =1
net.bridge.bridge-nf-call-iptables =1
EOF
sysctl --system  # 生效
yum install ntpdate -y
ntpdate time.windows.com
}
Install_software(){
#1.安装Docker
yum install wget -y
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
systemctl enable docker && systemctl start docker
#2.配置镜像下载加速器
cat >/etc/docker/daemon.json << EOF
{
"registry-mirrors":["https://1vdfdb9l.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
#3.添加阿里云YUM软件源并安装kubeadm,kubelet和kubectl
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
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
sed -i -r "s#=#='--feature-gates SupportPodPidsLimit=false --feature-gates SupportNodePidsLimit=false'#g" /etc/sysconfig/kubelet
systemctl enable kubelet && systemctl start kubelet
}
join_master(){
chmod +x join_master.sh
bash join_master.sh
}

pre_env
Install_software
join_master

执行脚本

chmod +x deploy_k8s-node.sh && bash deploy_k8s-node.sh

在master节点查询部署情况

[root@k8s-master ~]# kubectl get node
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   4d13h   v1.18.0
k8s-node1    Ready    <none>   4d12h   v1.18.0
Logo

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

更多推荐