Kubeadm 搭建k8s

网友投稿 244 2022-09-12

Kubeadm 搭建k8s

​一、准备​

​​master(2C/4G,cpu核心数要求大于2)  192.168.19.10  docker、kubeadm、kubelet、kubectl、flannel​​​​​node01(2C/2G)       192.168.19.11     docker、kubeadm、kubelet、kubectl、flannel​​​node02(2C/2G)       192.168.19.17     docker、kubeadm、kubelet、kubectl、flannel​​

​Harbor节点(hub.kgc.com)    192.168.19.18  docker、docker-compose、harbor-offline-v1.2.2​

​​在所有节点上安装Docker和kubeadm​​​​部署Kubernetes Master​​​​部署容器网络插件​​​​部署 Kubernetes Node,将节点加入Kubernetes集群​​​​部署 Dashboard Web 页面,可视化查看Kubernetes资源​​​​部署 Harbor 私有仓库,存放镜像资源​

//所有节点,关闭防火墙规则,关闭selinux,关闭swap交换systemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i 's/enforcing/disabled/' /etc/selinux/config iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -Xswapoff -a sed -ri 's/.*swap.*/#&/' /etc/fstab #交换分区必须要关闭#加载 ip_vs 模块for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done//修改主机名hostnamectl set-hostname masterhostnamectl set-hostname node01hostnamectl set-hostname node02//所有节点修改hosts文件cat >> /etc/hosts << EOF 192.168.132.50 master01 192.168.132.51 node01 192.168.132.52 node02 EOF//调整内核参数cat > /etc/sysctl.d/kubernetes.conf << EOFnet.bridge.bridge-nf-call-ip6tables=1 #开启网桥模式,可将网桥的流量传递给iptables链net.bridge.bridge-nf-call-iptables=1net.ipv6.conf.all.disable_ipv6=1 #关闭ipv6协议net.ipv4.ip_forward=1EOF//生效参数sysctl --system

所有节点安装docker

-------------------- 所有节点安装docker --------------------如果之前配置的是本地源 那么就把配置的本地源文件拖出来重新更新yum缓存cd /etc/yum.repos.d/bak/mv *.repo /etc/yum.repos.d/yum clean all && yum makecacheyum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo yum install -y docker-ce docker-ce-cli containerd.iomkdir /etc/dockercat > /etc/docker/daemon.json <

二、所有节点安装kubeadm,kubelet和kubectl

//定义kubernetes源cat > /etc/yum.repos.d/kubernetes.repo << EOF[kubernetes]name=Kubernetesbaseurl=install -y kubelet-1.20.11 kubeadm-1.20.11 kubectl-1.20.11//开机自启kubeletsystemctl enable kubelet.service#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启kubeadm config images list

​K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启​

三、部署K8S集群

//查看初始化需要的镜像kubeadm config images list//在 master 节点上传 v1.20.11.zip 压缩包至 /opt 目录unzip v1.20.11.zip -d /opt/k8scd /opt/k8s/v1.20.11for i in $(ls *.tar); do docker load -i $i; done//复制镜像和脚本到 node 节点,并在 node 节点上执行脚本加载镜像文件scp -r /opt/k8s root@node01:/optscp -r /opt/k8s root@node02:/opt//初始化kubeadm方法一:kubeadm config print init-defaults > /opt/kubeadm-config.yamlcd /opt/vim kubeadm-config.yaml......11 localAPIEndpoint:12 advertiseAddress: 192.168.132.50 #指定master节点的IP地址13 bindPort: 6443......34 kubernetesVersion: v1.20.11 #指定kubernetes版本号35 networking:36 dnsDomain: cluster.local37 podSubnet: "10.244.0.0/16" #指定pod网段,10.244.0.0/16用于匹配flannel默认网段38 serviceSubnet: 10.96.0.0/16 #指定service网段39 scheduler: {}#末尾再添加以下内容--- apiVersion: kubeproxy.config.k8s.io/v1alpha1kind: KubeProxyConfigurationmode: ipvs #把默认的kube-proxy调度方式改为ipvs模式kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log#--experimental-upload-certs 参数可以在后续执行加入节点时自动分发证书文件,k8sV1.16版本开始替换为 --upload-certs#tee kubeadm-init.log 用以输出日志//查看 kubeadm-init 日志less kubeadm-init.log//kubernetes配置文件目录ls /etc/kubernetes///存放ca等证书和密码的目录ls /etc/kubernetes/pki 方法二:kubeadm init \--apiserver-advertise-address=192.168.132.50 \--image-repository registry.aliyuncs.com/google_containers \--kubernetes-version=v1.20.11 \--service-cidr=10.96.0.0/16 \--pod-network-cidr=10.244.0.0/16 \--token-ttl=0--------------------------------------------------------------------------------------------初始化集群需使用kubeadm init命令,可以指定具体参数初始化,也可以指定配置文件初始化。可选参数:--apiserver-advertise-address:apiserver通告给其他组件的IP地址,一般应该为Master节点的用于集群内部通信的IP地址,0.0.0.0表示节点上所有可用地址--apiserver-bind-port:apiserver的监听端口,默认是6443--cert-dir:通讯的ssl证书文件,默认/etc/kubernetes/pki--control-plane-endpoint:控制台平面的共享终端,可以是负载均衡的ip地址或者dns域名,高可用集群时需要添加--image-repository:拉取镜像的镜像仓库,默认是k8s.gcr.io--kubernetes-version:指定kubernetes版本--pod-network-cidr:pod资源的网段,需与pod网络插件的值设置一致。通常,Flannel网络插件的默认为10.244.0.0/16,Calico插件的默认值为192.168.0.0/16;--service-cidr:service资源的网段--service-dns-domain:service全域名的后缀,默认是cluster.local---------------------------------------------------------------------------------------------方法二初始化后需要修改 kube-proxy 的 configmap,开启 ipvskubectl edit cm kube-proxy -n=kube-system修改mode: ipvs提示:......Your Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configYou should now deploy a pod network to the cluster.Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.132.50:6443 --token rc0kfs.a1sfe3gl4dvopck5 \ --discovery-token-ca-cert-hash sha256:864fe553c812df2af262b406b707db68b0fd450dc08b34efb73dd5a4771d37a2//设定kubectlkubectl需经由API server认证及授权后方能执行相应的管理操作,kubeadm 部署的集群为其生成了一个具有管理员权限的认证配置文件 /etc/kubernetes/admin.conf,它可由 kubectl 通过默认的 “$HOME/.kube/config” 的路径进行加载。mkdir -p $HOME/.kubecp -i /etc/kubernetes/admin.conf $HOME/.kube/configchown $(id -u):$(id -g) $HOME/.kube/config//如果 kubectl get cs 发现集群不健康,更改以下两个文件vim /etc/kubernetes/manifests/kube-scheduler.yaml vim /etc/kubernetes/manifests/kube-controller-manager.yaml# 修改如下内容把--bind-address=127.0.0.1变成--bind-address=192.168.132.50 #修改成k8s的控制节点master01的ip把--port=0 # 搜索port=0,把这一行注释掉systemctl restart kubelet//所有节点部署网络插件flannel方法一://所有节点上传flannel镜像 flannel.tar 到 /opt 目录,master节点上传 kube-flannel.yml 文件cd /optdocker load < flannel.tar//在 master 节点创建 flannel 资源kubectl apply -f kube-flannel.yml 方法二:kubectl apply -f node 节点上执行 kubeadm join 命令加入群集kubeadm join 192.168.132.50:6443 --token rc0kfs.a1sfe3gl4dvopck5 \ --discovery-token-ca-cert-hash sha256:864fe553c812df2af262b406b707db68b0fd450dc08b34efb73dd5a4771d37a2//在master节点查看节点状态kubectl get nodeskubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-bccdc95cf-c9w6l 1/1 Running 0 71mcoredns-bccdc95cf-nql5j 1/1 Running 0 71metcd-master 1/1 Running 0 71mkube-apiserver-master 1/1 Running 0 70mkube-controller-manager-master 1/1 Running 0 70mkube-flannel-ds-amd64-kfhwf 1/1 Running 0 2m53skube-flannel-ds-amd64-qkdfh 1/1 Running 0 46mkube-flannel-ds-amd64-vffxv 1/1 Running 0 2m56skube-proxy-558p8 1/1 Running 0 2m53skube-proxy-nwd7g 1/1 Running 0 2m56skube-proxy-qpz8t 1/1 Running 0 71mkube-scheduler-master 1/1 Running 0 70m//测试 pod 资源创建kubectl create deployment nginx --image=nginxkubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-554b9c67f9-zr2xs 1/1 Running 0 14m 10.244.1.2 node01 //暴露端口提供服务kubectl expose deployment nginx --port=80 --type=NodePortkubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 25hnginx NodePort 10.96.15.132 80:32698/TCP 4s//测试访问curl scale deployment nginx --replicas=3kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-554b9c67f9-9kh4s 1/1 Running 0 66s 10.244.1.3 node01 nginx-554b9c67f9-rv77q 1/1 Running 0 66s 10.244.2.2 node02 nginx-554b9c67f9-zr2xs 1/1 Running 0 17m 10.244.1.2 node01

四、部署 Dashboard

//在 master01 节点上操作#上传 recommended.yaml 文件到 /opt/k8s 目录中cd /opt/k8svim recommended.yaml#默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:kind: ServiceapiVersion: v1metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboardspec: ports: - port: 443 targetPort: 8443 nodePort: 30001 #添加 type: NodePort #添加 selector: k8s-app: kubernetes-dashboard kubectl apply -f recommended.yaml#创建service account并绑定默认cluster-admin管理员集群角色kubectl create serviceaccount dashboard-admin -n kube-systemkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-adminkubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')#使用输出的token登录Dashboardset-hostname hub.kgc.com//所有节点加上主机名映射echo '192.168.132.53 hub.kgc.com' >> /etc/hosts//安装 dockeryum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo yum install -y docker-ce docker-ce-cli containerd.iomkdir /etc/dockercat > /etc/docker/daemon.json < /etc/docker/daemon.json < 443/TCP 10mservice/nginx-deployment ClusterIP 10.96.222.161 30000/TCP 3m15sNAME READY STATUS RESTARTS AGEpod/nginx-deployment-77bcbfbfdc-bv5bz 1/1 Running 0 16spod/nginx-deployment-77bcbfbfdc-fq8wr 1/1 Running 0 16spod/nginx-deployment-77bcbfbfdc-xrg45 1/1 Running 0 3m39syum install ipvsadm -yipvsadm -Lncurl 10.96.222.161:30000kubectl edit svc nginx-deployment25 type: NodePort #把调度策略改成NodePortkubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 10.96.0.1 443/TCP 29mservice/nginx-deployment NodePort 10.96.222.161 30000:32340/TCP 22m浏览器访问:192.168.19.10:32340192.168.19.11:32340192.168.19.17:32340

​安装docker 这里不做描述​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:admin部署k8s
下一篇:如何做好产品整体营销策划?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~