虚拟机实现单Master单Node(创作于2020.10.3)

    科技2022-07-11  114

    master 节点

    ip修改

    Vim /etc/sysconfig/network-scripts/ifcfg-<一般是网卡名> 修改: BOOTPROTO=none ONBOOT=yes 添加: IPADDR= 192.168.80.200 #NAT模式 ip设置的网段一定要和虚拟机设置的相同 NETMASK=255.255.255.0 GATEWAY=192.168.80.2 DNS1=8.8.8.8

    关闭防火墙

    systemctl stop firewalld.service && systemctl disable firewalld.service 关闭selinux vim /etc/selinux/config 修改 SELINUX=disable

    关闭 swap

    vim /etc/fstab 注释下面一条 #/dev/mapper/centos-swap swap swap defaults 0 0

    修改主机名称

    hostnamectl set-hostname master # 和ip 映射有关,缺了会报错

    Master添加host

    vim /etc/hosts 添加 192.168.80.200 master //master节点ip 映射 192.168.80.201 node01 //预留的node节点ip 映射

    Ipv4桥接

    vim /etc/sysctl.d/k8s.conf 添加 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 运行生效 sysctl --system

    重启

    reboot

    查看环境是否生效 防火墙: systemctl status firewalld.service Swap: free : swap 行全为0

    Selinux: getenforce

    主机名: hostname

    时间同步

    yum install -y ntpdate ntpdate time.windows.com

    配置k8s阿里源

    cd /etc/yum.repos.d/ 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 clean all yum repolist

    安装kubeadm , kubelet, kubectl(1.18.0)

    yum install kubectl-1.18.0 kubeadm-1.18.0 kubelet-1.18.0 -y

    可能遇到的错误: 如果出现如上报错,请删除提示版本

    [WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”. Please follow the guide at https://kubernetes.io/docs/setup/cri/

    出现如上错误,请按如下方式处理:

    vim /etc/docker/daemon.json { "registry-mirrors": ["https://c8g9qhfw.mirror.aliyuncs.com"], #这条非必须,是用作国内镜像源加速的 "exec-opts":["native.cgroupdriver=systemd"] #重要的是这一条 } systemctl restart docker

    设置为开机自启 systemctl enable kubelet

    Master 节点 初始化

    kubeadm init \ --apiserver-advertise-address=192.168.80.200 \ #master 主机ip --image-repository registry.aliyuncs.com/google_containers \ #最好默认 --kubernetes-version v1.18.0 \ #和前面yum下载的组件版本相同 --service-cidr=10.1.0.0/16 \ #最好默认 --pod-network-cidr=10.244.0.0/16 #最好默认

    错误:

    [ERROR DirAvailable–var-lib-etcd]: /var/lib/etcd is not empty

    解决方法:

    运行 rm -rf /var/lib/etcd

    [WARNING Hostname]: hostname “k8smaster” could not be reached

    解决方法: cat /etc/hosts 添加ip 映射

    安装成功后根据提示运行:

    kdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown ( i d − u ) : (id -u): (idu):(id -g) $HOME/.kube/config

    添加flannel网络(master)

    wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml

    错误:

    kube-system kube-flannel-ds-xw4xz 0/1 Init:ImagePullBackOff 0 10m

    解决方案:

    导入镜像,修改yml, 重新tag, 重新添加flannel网络。 flannel 镜像下载网盘地址:《国外的太慢,自己准备了一个,版本为0.11[https://pan.baidu.com/s/1VRLjBV2Q1IuAXcmjpNB3Cg](https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml) 密码: itit 以导入镜像:docker load --input flannel.tar 修改yaml: vim kube-flannel.yml #master节点执行 :%s/quay.io/quay-mirror.qiniu.com/g 从新tag: 保证flannel的镜像名和kube-flannel.yml文件中的相同 重新添加flannel网络:kubectl apply -f kube-flannel.yml

    Node节点

    ip修改

    Vim /etc/sysconfig/network-scripts/ifcfg-<一般是网卡名> 修改: BOOTPROTO=none ONBOOT=yes 添加: IPADDR= 192.168.80.201 #NAT模式 ip设置的网段一定要和虚拟机设置的相同 NETMASK=255.255.255.0 GATEWAY=192.168.80.2 DNS1=8.8.8.8

    关闭防火墙

    systemctl stop firewalld.service && systemctl disable firewalld.service 关闭selinux vim /etc/selinux/config 修改 SELINUX=disable

    关闭 swap

    vim /etc/fstab 注释下面一条 #/dev/mapper/centos-swap swap swap defaults 0 0

    修改主机名称

    hostnamectl set-hostname node01 # 和ip 映射有关,缺了会报错

    Master添加host

    vim /etc/hosts 添加 192.168.80.200 master //master节点ip 映射 192.168.80.201 node01 //预留的node节点ip 映射

    Ipv4桥接

    vim /etc/sysctl.d/k8s.conf 添加 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 运行生效 sysctl --system

    重启

    reboot

    查看环境是否生效 防火墙: systemctl status firewalld.service Swap: free : swap 行全为0

    Selinux: getenforce

    主机名: hostname

    时间同步

    yum install -y ntpdate ntpdate time.windows.com

    配置k8s阿里源

    cd /etc/yum.repos.d/ 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 clean all yum repolist

    安装kubeadm , kubelet, kubectl(1.18.0)

    yum install kubectl-1.18.0 kubeadm-1.18.0 kubelet-1.18.0 -y

    可能遇到的错误: 如果出现如上报错,请删除提示版本

    [WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”. Please follow the guide at https://kubernetes.io/docs/setup/cri/

    出现如上错误,请按如下方式处理:

    vim /etc/docker/daemon.json { "registry-mirrors": ["https://c8g9qhfw.mirror.aliyuncs.com"], #这条非必须,是用作国内镜像源加速的 "exec-opts":["native.cgroupdriver=systemd"] #重要的是这一条 } systemctl restart docker

    设置为开机自启 systemctl enable kubelet

    重启相关服务《非必须》

    systemctl start docker.service systemctl enable kubelet.service

    将节点加入集群 kubeadm token create 《因为token每隔一段时间原token就会失效,所以最好在加入新节点之前重新在master创建一个》 查看token :kubeadm token list 查看 discovery-token-ca-cert-hash: openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^.* //’

    kubeadm join <master ip>:6443 --token <TOKEN string> \ --discovery-token-ca-cert-hash sha256:<hash string> Eg: kubeadm join 192.168.80.200:6443 --token st0xod.syjg75lkzdjtweo3 \ --discovery-token-ca-cert-hash sha256:3b6be71de4b53876d3b1823211a25d10bda476d01b7c2dd483b708ff7dc18e2e

    查看Pod《如果节点加入不成功,可用以下命令查看状态》

    kubectl get pods --all-namespaces -o wide kubectl get pod -n kube-system
    Processed: 0.039, SQL: 8