从零开始搭建Kubernetes集群一:搭建K8s集群
导读
基础环境配置
确保每个节点上 MAC 地址和 product_uuid 的唯一性
您可以使用命令
ip link
或ifconfig -a
来获取网络接口的 MAC 地址可以使用
sudo cat /sys/class/dmi/id/product_uuid
命令对 product_uuid 校验
一般来讲,硬件设备会拥有唯一的地址,但是有些虚拟机的地址可能会重复。Kubernetes 使用这些值来唯一确定集群中的节点。 如果这些值在每个节点上不唯一,可能会导致安装失败。
编辑hosts
master和worker都需要编辑
[root@master-all ~]# cat /etc/hosts 192.168.230.202 master-all 192.168.230.203 Node-1
关闭Swap(交换分区)
master和worker都需要执行
临时关闭
[root@master-all ~]# swapoff -a
永久关闭,需重启
[root@master-all ~]# sed -i 's/\(.*[[:space:]]swap[[:space:]].*\)/#\1/g' /etc/fstab
关闭selinux
master和worker都需要执行
临时关闭
[root@master-all ~]# setenforce 0
永久关闭,需重启
[root@master-all ~]# sed -i 's/\(^SELINUX=\).*/\1disabled/' /etc/selinux/config
关闭防火墙
master和worker都需要执行
[root@master-all ~]# systemctl stop firewalld && systemctl disable firewalld
修改系统参数
master和worker都需要编辑
[root@master-all ~]# vim /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 [root@master-all ~]# modprobe br_netfilter [root@master-all ~]# sysctl -p /etc/sysctl.d/k8s.conf
安装Docker
master和worker都需要安装
[root@master-all ~]# yum -y install docker [root@master-all ~]# systemctl enable docker && systemctl start docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
此处使用阿里云镜像站
[root@Node-1 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://n275i9sm.mirror.aliyuncs.com"] } [root@master-all ~]# systemctl daemon-reload [root@master-all ~]# systemctl restart docker
安装k8s组件
配置国内源repo文件/etc/yum.repos.d/kubernetes.repo
[root@master-all ~]# vim /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg exclude=kube*
安装Kubernetes组件
–disableexcludes=kubernetes
只从指定repo中获取
[root@master-all ~]# yum -y install kubelet kubeadm kubectl --disableexcludes=kubernetes [root@master-all ~]# systemctl enable kubelet && systemctl start kubelet
Master安装
获取初始化文件
[root@master-all ~]# kubeadm config print init-defaults > kubeadm-init.yaml [root@master-all ~]# vim kubeadm-init.yaml
将advertiseAddress: 1.2.3.4修改为本机IP地址
将imageRepository: k8s.gcr.io修改为imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
apiVersion: kubeadm.k8s.io/v1beta2 bootstrapTokens: - groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authentication kind: InitConfiguration localAPIEndpoint: advertiseAddress: 192.168.230.202 bindPort: 6443 nodeRegistration: criSocket: /var/run/dockershim.sock name: master-all taints: - effect: NoSchedule key: node-role.kubernetes.io/master --- apiServer: timeoutForControlPlane: 4m0s apiVersion: kubeadm.k8s.io/v1beta2 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes controllerManager: {} dns: type: CoreDNS etcd: local: dataDir: /var/lib/etcd imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers kind: ClusterConfiguration kubernetesVersion: v1.18.0 networking: dnsDomain: cluster.local serviceSubnet: 10.96.0.0/12 scheduler: {}
拉取images镜像
[root@master-all ~]# kubeadm config images pull --config kubeadm-init.yaml W0411 17:59:25.394894 1717 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 [config/images] Pulled registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7
初始化
[root@master-all ~]# kubeadm init --config kubeadm-init.yaml W0411 18:00:39.037355 2000 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] [init] Using Kubernetes version: v1.18.0 [preflight] Running pre-flight checks [preflight] Pulling images required for setting up a Kubernetes cluster [preflight] This might take a minute or two, depending on the speed of your internet connection [preflight] You can also perform this action in beforehand using 'kubeadm config images pull' [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Starting the kubelet [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "ca" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [master-all kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.230.202] [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [master-all localhost] and IPs [192.168.230.202 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [master-all localhost] and IPs [192.168.230.202 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "sa" key and public key [kubeconfig] Using kubeconfig folder "/etc/kubernetes" [kubeconfig] Writing "admin.conf" kubeconfig file [kubeconfig] Writing "kubelet.conf" kubeconfig file [kubeconfig] Writing "controller-manager.conf" kubeconfig file [kubeconfig] Writing "scheduler.conf" kubeconfig file [control-plane] Using manifest folder "/etc/kubernetes/manifests" [control-plane] Creating static Pod manifest for "kube-apiserver" [control-plane] Creating static Pod manifest for "kube-controller-manager" W0411 18:00:41.149242 2000 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [control-plane] Creating static Pod manifest for "kube-scheduler" W0411 18:00:41.149789 2000 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests" [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s [apiclient] All control plane components are healthy after 15.004506 seconds [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster [upload-certs] Skipping phase. Please see --upload-certs [mark-control-plane] Marking the node master-all as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node master-all as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: abcdef.0123456789abcdef [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy 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/config You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.230.202:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:d3cfcef4ced07d6f648074a15026599db6671a780f9032ec425c3fa75fce8bda
如需清除缓存可执行:
kubeadm reset
拷贝授权文件
[root@master-all ~]# mkdir -p $HOME/.kube [root@master-all ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@master-all ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
网络配置
下载calico配置
需要注意的是, calico.yaml中的IP和kubeadm-init.yaml需要保持一致, 要么初始化前修改kubeadm-init.yaml, 要么初始化后修改calico.yaml.
[root@master-all ~]# wget https://docs.projectcalico.org/v3.11/manifests/calico.yaml [root@master-all ~]# vim calico.yaml value: "10.96.0.0/12"
开始安装
[root@master-all ~]# kubectl apply -f calico.yaml configmap/calico-config created customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created clusterrole.rbac.authorization.k8s.io/calico-node created clusterrolebinding.rbac.authorization.k8s.io/calico-node created daemonset.apps/calico-node created serviceaccount/calico-node created deployment.apps/calico-kube-controllers created serviceaccount/calico-kube-controllers created
等待几分钟后查看Node网络状态
[root@master-all ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master-all Ready master 12m v1.18.1 [root@master-all ~]# kubectl get pod -o wide --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES kube-system calico-kube-controllers-75d56dfc47-6hxnj 1/1 Running 0 10m 10.109.177.70 master-all <none> <none> kube-system calico-node-tpz8m 1/1 Running 1 10m 192.168.230.202 master-all <none> <none> kube-system coredns-546565776c-4pdj9 1/1 Running 2 12m 10.109.177.71 master-all <none> <none> kube-system coredns-546565776c-6bcw8 1/1 Running 2 12m 10.109.177.72 master-all <none> <none> kube-system etcd-master-all 1/1 Running 2 12m 192.168.230.202 master-all <none> <none> kube-system kube-apiserver-master-all 1/1 Running 2 12m 192.168.230.202 master-all <none> <none> kube-system kube-controller-manager-master-all 1/1 Running 1 12m 192.168.230.202 master-all <none> <none> kube-system kube-proxy-ln9kx 1/1 Running 2 12m 192.168.230.202 master-all <none> <none> kube-system kube-scheduler-master-all 1/1 Running 2 12m 192.168.230.202 master-all <none> <none>
Node Worker节点
node节点环境安装请参照 基础环境配置
添加node节点至master
[root@Node-1 ~]# kubeadm join 192.168.230.202:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:d3cfcef4ced07d6f648074a15026599db6671a780f9032ec425c3fa75fce8bda W0411 18:15:25.273383 1583 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set. [preflight] Running pre-flight checks [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service' [preflight] Reading configuration from the cluster... [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Starting the kubelet [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
拷贝授权文件
[root@Node-1 ~]# mkdir -p $HOME/.kube [root@Node-1 ~]# cp -i /etc/kubernetes/kubelet.conf $HOME/.kube/config
验证
[root@Node-1 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master-all Ready master 16m v1.18.1 node-1 Ready <none> 113s v1.18.1
遇到的问题
报错:
failed to run Kubelet: failed to create kubelet: misconfiguration: kubelet cgroup driver: "cgroupfs" is different from docker cgroup driver: "systemd"
解决方法
[root@master-all ~]# sed -i 's/\(.*native.cgroupdriver=\)systemd\(.*\)/\1cgroupfs\2/' /lib/systemd/system/docker.service [root@master-all ~]# systemctl daemon-reload [root@master-all ~]# systemctl restart docker
报错:
[root@master-all ~]# kubectl get pod The connection to the server localhost:8080 was refused - did you specify the right host or port?
解决方法:
[root@master-all ~]# mkdir -p $HOME/.kube [root@master-all ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #如果非root用户需chown授权
报错:
[root@Node-1 ~]# kubeadm join 192.168.230.101:6443 --token n3qdmn.btk974cnv1bfskbt \ > --discovery-token-ca-cert-hash sha256:859476404c5ed23aef3d78e3b8fc09684dde7fbf8ce0ad446774162ccec2e0d1 W0319 09:28:32.847414 24070 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set. [preflight] Running pre-flight checks [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service' error execution phase preflight: [preflight] Some fatal errors occurred: [ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1 [ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1 [preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...` To see the stack trace of this error execute with --v=5 or higher
解决方法:
[root@Node-1 ~]# vim /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 [root@Node-1 ~]# sysctl -p /etc/sysctl.d/k8s.conf
评论(0)