千家信息网

二进制部署K8s集群

发表于:2024-12-03 作者:千家信息网编辑
千家信息网最后更新 2024年12月03日,二进制部署K8s集群配置nfs服务端NFS_SERVER(){yum install -y nfs-utils rpcbindmkdir -pv /nfssystemctl restart nfs r
千家信息网最后更新 2024年12月03日二进制部署K8s集群

二进制部署K8s集群

配置nfs服务端

NFS_SERVER(){yum install -y nfs-utils rpcbindmkdir -pv /nfssystemctl restart nfs rpcbindsystemctl enable nfs rpcbindecho '/nfs  *(rw,no_root_squash)' > /etc/exportsexportfs -ravcd /nfs}

1、nfs共享之客户端

NFS_CLIENT(){yum install -y nfs-utils rpcbindsystemctl restart nfs rpcbindsystemctl enable nfs rpcbindmkdir -pv /nfsshowmount -e 192.168.11.11mount.nfs 192.168.11.11:/nfs  /nfsdf -hT}

2、主机名设置

HOST_NAME(){read -p 'please inpute hostname(eg.srv11):' NAMEHOSTNAME=${NAME}hostnamectl --static set-hostname $HOSTNAMEhostname#修改hosts文件grep -E 'node|srv|dk.io' /etc/hosts || echo '192.168.11.11   node1  k8s-master  srv11192.168.11.12   node2  k8s-node1   srv12192.168.11.13   node3  k8s-node2   srv13192.168.11.14   node4  k8s-node3   srv14192.168.11.11   dk.io' >> /etc/hostscat /etc/hosts}

3、etcd、flannel安装配置

ETCD_INST(){\cp -fv /nfs/k8s/k8s.repo /etc/yum.repos.d/yum install -y etcd flannel[ -f /etc/etcd/etcd.conf.bak ] || cp -v /etc/etcd/etcd.conf{,.bak}cd /etc/etcd#去掉第5、20、26、27、28行首的#号sed -i 's/localhost/0.0.0.0/' etcd.confsed -i '5s/^#//;20s/^#//;26s/^#//;27s/^#//;28s/^#//;' etcd.confegrep -nv '^#|^$' etcd.confsed -i "9,26s/default/${HOSTNAME}/;" etcd.confsed -i "20,21s/0.0.0.0/${HOSTNAME}/;" etcd.confegrep -nv '^#|^$' etcd.confCluster='ETCD_INITIAL_CLUSTER="srv11=http://srv11:2380,srv12=http://srv12:2380,srv13=http://srv13:2380"'sed -i "26c ${Cluster}" etcd.confegrep -nv '^#|^$' etcd.confsed -i '4s/127.0.0.1/srv11/' /etc/sysconfig/flanneldcat /etc/sysconfig/flanneld

4、启动etcd服务

systemctl enable etcdsystemctl restart etcdetcdctl get /atomic.io/network/config[ $? -eq 0 ] || etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'systemctl enable flanneldsystemctl restart flanneld}

5、安装k8s-master服务器端

K8S_MASTER(){yum install -y docker kubernetessystemctl restart dockersystemctl enable dockerdocker info

6、apiserver、config文件修改脚本

cd /etc/kubernetes/sed -i '11s/^# //;14s/^# //' apiserversed -i '8s/127.0.0.1/0.0.0.0/' apiserversed -i "17s/127.0.0.1/${HOSTNAME}/" apiserversed -i "23s/SecurityContextDeny,ServiceAccount,//" apiservergrep -Ev '^$|^#' apiservercd /etc/kubernetes/sed -i "22s/127.0.0.1/srv11/" configgrep -Ev '^$|^#' config

7、k8s-master启动服务并设置开机自启动

systemctl enable docker.servicesystemctl restart docker.servicesystemctl enable kube-apiserver.servicesystemctl restart kube-apiserver.servicesystemctl enable kube-controller-manager.servicesystemctl restart kube-controller-manager.servicesystemctl enable kube-scheduler.servicesystemctl restart kube-scheduler.servicekubectl get nodes

8、访问http://kube-apiserver:port,查看所有请求url和创建状态

curl http://192.168.11.11:8080 curl http://192.168.11.11:8080/healthz/ping}

9、k8s-node节点部署
#安装docker和kubernets(参考k8s-master的安装方法)

K8S_NODE(){yum install -y docker kubernetessystemctl restart dockersystemctl enable dockerdocker info

10、config、kubelet文件修改脚本

cd /etc/kubernetes/MASTER='srv11'sed -i "22s/127.0.0.1/${MASTER}/" configgrep -Ev '^$|^#' configcd /etc/kubernetes/sed -i "5s/127.0.0.1/0.0.0.0/" kubeletsed -i '8s/^# //' kubeletsed -i "11s/127.0.0.1/${HOSTNAME}/" kubeletsed -i "14s/127.0.0.1/srv11/" kubeletgrep -Ev '^$|^#' kubelet

11、k8s-node1启动服务并设置开机自启动

systemctl enable docker.servicesystemctl restart docker.servicesystemctl enable kubelet.servicesystemctl restart kubelet.servicesystemctl enable kube-proxy.servicesystemctl restart kube-proxy.servicekubectl -s http://srv11:8080 get nodes}

12、k8s集群状态检测

K8S_STAT(){etcdctl member listetcdctl cluster-healthkubectl -s http://srv11:8080 get node }cat <<-EOF---K8S_Cluster_install--1.k8s-master install2.k8s-node install3.k8s-status queryEOFread -p 'please input choice:' Icase $I in1)    NFS_SERVER    read -p 'press enter to continue.'    HOST_NAME    ETCD_INST    read -p 'press enter to continue.'    K8S_MASTER;;2)    NFS_CLIENT    read -p 'press enter to continue.'    HOST_NAME    ETCD_INST    read -p 'press enter to continue.'    K8S_NODE;;3)    K8S_STAT;;*)    echo 'input error choice.'    exit 127;;esac
0