快速搭建k8s集群

 
 #修改host
 #master
 hostnamectl set-hostname k8s-1
 #nodes
 hostnamectl set-hostname k8s-2
 hostnamectl set-hostname k8s-3
 
 hostnamectl status
 echo "127.0.0.1   $(hostname)" >> /etc/hosts
 
 printf "##################关闭selinux################## \n"
 sed -i 's/enforcing/disabled/' /etc/selinux/config
 setenforce 0
 printf "##################关闭swap################## \n"
 swapoff -a  
 sed -ri 's/.*swap.*/#&/' /etc/fstab 
 printf "##################配置路由转发################## \n"
 cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
 br_netfilter
 EOF
 echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.d/k8s.conf
 ## 必须 ipv6流量桥接
 echo 'net.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.d/k8s.conf
 ## 必须 ipv4流量桥接
 echo 'net.bridge.bridge-nf-call-iptables = 1' >> /etc/sysctl.d/k8s.conf
 echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
 echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
 echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
 echo "net.ipv6.conf.all.forwarding = 1"  >> /etc/sysctl.d/k8s.conf
 modprobe br_netfilter
 sudo sysctl --system
 printf "##################配置ipvs################## \n"
 cat <<EOF | sudo tee /etc/sysconfig/modules/ipvs.modulesmodprobe -- 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 
 sh /etc/sysconfig/modules/ipvs.modules
 printf "##################安装ipvsadm相关软件################## \n"
 yum install -y ipset ipvsadm
 
 
 printf "##################安装docker容器环境################## \n"
 sudo yum remove docker*
 sudo yum install -y yum-utils
 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 yum install -y docker-ce-19.03.9  docker-ce-cli-19.03.9 containerd.io
 systemctl enable docker
 systemctl start docker
 sudo mkdir -p /etc/docker
 sudo tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"]
 }
 EOF
 sudo systemctl daemon-reload
 sudo systemctl restart docker
 
 docker --v


 printf "##################安装k8s核心包 kubeadm kubelet kubectl################## \n"
 cat <<EOF > /etc/yum.repos.d/kubernetes.repo
 [kubernetes]
 name=Kubernetes
 baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
 enabled=1
 gpgcheck=0
 repo_gpgcheck=0
 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
    http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
 EOF
 ###指定k8s安装版本
 yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0
 ###要把kubelet立即启动。
 systemctl enable kubelet
 systemctl start kubelet
 
 
 #【以下是master节点执行的:】
 #!/bin/bash
 # 阿里云公共镜像仓库地址
 aliyun_repo="registry.cn-hangzhou.aliyuncs.com/google_containers"
 # Kubernetes 镜像列表
 images=(     kube-apiserver:v1.21.0     kube-controller-manager:v1.21.0     kube-scheduler:v1.21.0     kube-proxy:v1.21.0     pause:3.4.1     etcd:3.4.13-0     coredns:1.8.0 )
 # 拉取镜像
 for imageName in ${images[@]}; do     docker pull $aliyun_repo/$imageName; done

 docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0 registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
 
  docker images
  
 # ip a 查看eth0的inet【例如我的是192.168.11.219】
   kubeadm init --apiserver-advertise-address=192.168.11.219 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.21.0 --service-cidr=10.96.0.0/16 --pod-network-cidr=192.178.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
 #复制kubeadm join ... 分别去nodes节点执行
 
 # 安装dashboard,将下面两个文件放在相应目录
 kubectl apply -f /root/recommended.yaml 
 kubectl apply -f /root/dash-admin.yaml 
 
 #获取kubernetes-dashboard的PORT(S)          
  kubectl get svc -A
  
  #https://公网ip+上述端口可打开dashboard页面,token通过执行下面这句获得
  kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
   
  #比较有用的命令
 kubectl get nodes
 kubectl get all -owide
 
 
 【重装:】
 kubeadm reset
 rm -rf $HOME/.kube
 iptables -F
 rm -rf /etc/cni/net.d/*
 systemctl stop kubelet

【谢谢雷丰阳老师!】

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值