使用 kubeadm 安裝單master kubernetes 集群(手動版)
節點信息
修改主機名
基本配置
配置時間同步
修改 node iptables 相關參數
加載 ipvs 相關模塊
安裝 docker
安裝 kubeadm,kubelet,kubectl
部署 master 節點
配置 kubectl
部署網絡插件
部署 worker 節點
安裝 Ingress Controller
快速初始化
卸載Ingress Controller
配置域名解析
WARNING
節點信息
修改主機名
基本配置
配置時間同步
修改 node iptables 相關參數
加載 ipvs 相關模塊
安裝 docker
安裝 kubeadm,kubelet,kubectl
部署 master 節點
配置 kubectl
部署網絡插件
部署 worker 節點
安裝 Ingress Controller
快速初始化
卸載Ingress Controller
配置域名解析
WARNING
節點信息
安裝后的拓撲圖如下:
修改主機名
#master 節點: hostnamectl set-hostname k8s-master #node1 節點: hostnamectl set-hostname k8s-node1 #node2 節點: hostnamectl set-hostname k8s-node2 #node3 節點: hostnamectl set-hostname k8s-node3
基本配置
#Step 1: 修改/etc/hosts 文件 172.16.106.226 k8s-master 172.16.106.209 k8s-node1 172.16.106.239 k8s-node2 172.16.106.205 k8s-node3 #Step 2: 快速復制到其它主機 scp /etc/hosts root@k8s-node1:/etc/ #Step 3: 關閉防火墻和 selinux systemctl stop firewalld && systemctl disable firewalld sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config && setenforce 0 ##Step 4: 關閉 swap swapoff -a yes | cp /etc/fstab /etc/fstab_bak cat /etc/fstab_bak |grep -v swap > /etc/fstab
配置時間同步
使用 chrony 同步時間,配置 master 節點與網絡 NTP 服務器同步時間,所有 node 節點與 master 節點同步時間。
配置 master 節點:
#Step 1: 安裝 chrony: yum install -y chrony #Step 2: 注釋默認 ntp 服務器 sed -i 's/^server/#&/' /etc/chrony.conf #Step 3: 指定上游公共 ntp 服務器,并允許其他節點同步時間 vi /etc/chrony.conf server 0.asia.pool.ntp.org iburst server 1.asia.pool.ntp.org iburst server 2.asia.pool.ntp.org iburst server 3.asia.pool.ntp.org iburst allow all #Step 4: 重啟 chronyd 服務并設為開機啟動: systemctl enable chronyd && systemctl restart chronyd #Step 5: 開啟網絡時間同步功能 timedatectl set-ntp true
配置所有 node 節點:
#Step 1: 安裝 chrony: yum install -y chrony #Step 2: 注釋默認服務器 sed -i 's/^server/#&/' /etc/chrony.conf #Step 3: 指定內網 master 節點為上游 NTP 服務器 echo server 172.16.106.226 iburst >> /etc/chrony.conf #Step 4: 重啟服務并設為開機啟動: systemctl enable chronyd && systemctl restart chronyd
所有節點執行chronyc sources命令,查看存在以^*開頭的行,說明已經與服務器時間同步:
[root@k8s-node1 ~]# chronyc sources 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* k8s-master 3 7 377 53 -51us[ -147us] +/- 22ms
修改 node iptables 相關參數
RHEL / CentOS 7 上的一些用戶報告了由于 iptables 被繞過而導致流量路由不正確的問題。創建 /etc/sysctl.d/k8s.conf 文件,添加如下內容:
#Step 1: 創建配置文件 vi /etc/sysctl.d/k8s.conf vm.swappiness = 0 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 #Step 2: 使配置生效 modprobe br_netfilter sysctl -p /etc/sysctl.d/k8s.conf #Step 3: 快速復制到其它主機 scp /etc/sysctl.d/k8s.conf root@k8s-node1:/etc/sysctl.d/
加載 ipvs 相關模塊
由于 ipvs 已經加入到了內核的主干,所以為 kube-proxy 開啟 ipvs 的前提需要加載以下的內核模塊:
在所有的 Kubernetes 節點執行以下腳本:
#Step 1: 創建腳本 vi /etc/sysconfig/modules/ipvs.modules #!/bin/bash modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 #Step 2: 快速復制到其它主機 scp /etc/sysconfig/modules/ipvs.modules root@k8s-node1:/etc/sysconfig/modules/ #Step 3: 執行腳本 chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
上面腳本創建了/etc/sysconfig/modules/ipvs.modules 文件,保證在節點重啟后能自動加載所需模塊。 使用 lsmod | grep -e ip_vs -e nf_conntrack_ipv4 命令查看是否已經正確加載所需的內核模塊。
接下來還需要確保各個節點上已經安裝了 ipset 軟件包。 為了便于查看 ipvs 的代理規則,最好安裝一下管理工具 ipvsadm。
yum install ipset ipvsadm -y
安裝 docker
Kubernetes 默認的容器運行時仍然是 Docker,使用的是 kubelet 中內置 dockershim CRI 實現.
# step 1: 安裝必要的一些系統工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加軟件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/ docker-ce/linux/centos/docker-ce.repo # Step 3: 更新并安裝 Docker-CE sudo yum makecache fast sudo yum -y install docker-ce docker-ce-selinux # 注意: # 官方軟件源默認啟用了最新的軟件,您可以通過編輯軟件源的方式獲取各個版本的軟件包。例如官方并沒有將測試版本的軟件源置為可用,你可以通過以下方式開啟。同理可以開啟各種測試版本等。 # vim /etc/yum.repos.d/docker-ce.repo # 將 [docker-ce-test] 下方的 enabled=0 修改為 enabled=1 # # 安裝指定版本的 Docker-CE: # Step 3.1: 查找 Docker-CE 的版本: # yum list docker-ce.x86_64 --showduplicates | sort -r # Loading mirror speeds from cached hostfile # Loaded plugins: branch, fastestmirror, langpacks # docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable # docker-ce.x86_64 18.03.1.ce-1.el7.centos @docker-ce-stable # docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable # Available Packages # Step 3.2 : 安裝指定版本的 Docker-CE: (VERSION 例如上面的 18.03.0.ce.1-1.el7.centos) sudo yum -y --setopt=obsoletes=0 install docker-ce-[VERSION] \ docker-ce-selinux-[VERSION] # Step 4: 開啟 Docker 服務 sudo systemctl enable docker && systemctl start docker
卸載老版本的 Docker:
yum remove docker \ docker-common \ docker-selinux \ docker-engine
安裝校驗:
Client: Docker Engine - Community Version: 19.03.11 API version: 1.40 Go version: go1.13.10 Git commit: 42e35e61f3 Built: Mon Jun 1 09:13:48 2020 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.11 API version: 1.40 (minimum version 1.12) Go version: go1.13.10 Git commit: 42e35e61f3 Built: Mon Jun 1 09:12:26 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.13 GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683
安裝 kubeadm,kubelet,kubectl
在各節點安裝 kubeadm,kubelet,kubectl:
kubelet 在群集中所有節點上運行的核心組件, 用來執行如啟動 pods 和 containers 等操作。
kubeadm 引導啟動 k8s 集群的命令行工具,用于初始化 Cluster。
kubectl 是 Kubernetes 命令行工具。通過 kubectl 可以部署和管理應用,查看各種資源,創建、刪除和更新各種組件。
#Step 1: 配置 kubernetes.repo 的源,由于官方源國內無法訪問,這里使用阿里云 yum 源 vi /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 #Step 2: 快速復制到其它主機 scp /etc/yum.repos.d/kubernetes.repo root@k8s-node1:/etc/yum.repos.d/ #Step 3: 更新并安裝 kubelet sudo yum makecache fast #Step 4:在所有節點上安裝 yum install -y kubelet kubeadm kubectl #Step 5: 啟動 kubelet 服務 systemctl enable kubelet && systemctl start kubelet
官方安裝文檔可以參考:
https://kubernetes.io/docs/setup/independent/install-kubeadm/
部署 master 節點
Master 節點執行初始化:
kubeadm init \ --apiserver-advertise-address=172.16.106.226 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.18.1 \ --pod-network-cidr=10.244.0.0/16
注意這里執行初始化用到了- -image-repository 選項,指定初始化需要的鏡像源從阿里云鏡像倉庫拉取。
初始化命令說明:
--apiserver-advertise-address:指明用 Master 的哪個 interface 與 Cluster 的其他節點通信。如果 Master 有多個 interface,建議明確指定,如果不指定,kubeadm 會自動選擇有默認網關的 interface。
--pod-network-cidr:指定 Pod 網絡的范圍。Kubernetes 支持多種網絡方案,而且不同網絡方案對--pod-network-cidr有自己的要求,這里設置為10.244.0.0/16是因為我們將使用 flannel 網絡方案,必須設置成這個 CIDR。
--image-repository:Kubenetes 默認 Registries 地址是k8s.gcr.io,在國內并不能訪問gcr.io,在 1.18 版本中我們可以增加–image-repository參數,默認值是k8s.gcr.io,將其指定為阿里云鏡像地址:registry.aliyuncs.com/google_containers。
--kubernetes-version=v1.18.1:關閉版本探測,因為它的默認值是 stable-1,會導致從https://dl.k8s.io/release/stable-1.txt下載最新的版本號,我們可以將其指定為固定版本(最新版:v1.18.1)來跳過網絡請求。
初始化過程如下:
W0620 11:53:21.635124 21454 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.1 [preflight] Running pre-flight checks [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/ [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 [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.106.226] [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 [k8s-master localhost] and IPs [172.16.106.226 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [172.16.106.226 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" W0620 11:54:02.402998 21454 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" [control-plane] Creating static Pod manifest for "kube-scheduler" W0620 11:54:02.404297 21454 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 17.504415 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 k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: ztz3qu.ee9gdjh32g228l4k [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 172.16.106.226:6443 --token ztz3qu.ee9gdjh32g228l4k \ --discovery-token-ca-cert-hash sha256:24411c65811afb54501be97ad0cf28c87dc9f51ca0ee5c49f71e58b535d91a43
(注意記錄下初始化結果中的 kubeadm join 命令,部署 worker 節點時會用到)
初始化過程說明:
[preflight] kubeadm 執行初始化前的檢查。
[kubelet-start] 生成 kubelet 的配置文件”/var/lib/kubelet/config.yaml”
[certificates] 生成相關的各種 token 和證書
[kubeconfig] 生成 KubeConfig 文件,kubelet 需要這個文件與 Master 通信
[control-plane] 安裝 Master 組件,會從指定的 Registry 下載組件的 Docker 鏡像。
[bootstraptoken] 生成 token 記錄下來,后邊使用 kubeadm join 往集群中添加節點時會用到
[addons] 安裝附加組件 kube-proxy 和 kube-dns。
Kubernetes Master 初始化成功,提示如何配置常規用戶使用 kubectl 訪問集群。
提示如何安裝 Pod 網絡。
提示如何注冊其他節點到 Cluster。
完整的官方文檔可以參考:
https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/
配置 kubectl
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面我們已經在所有的節點安裝了 kubectl。Master 初始化完成后需要做一些配置工作,然后 kubectl 就能使用了。
依照 kubeadm init 輸出的最后提示,推薦用 Linux 普通用戶執行 kubectl。
#Step 1:創建普通用戶 7d 并設置密碼 123456 useradd 7d && echo "7d:123456" | chpasswd 7d #Step 2:追加 sudo 權限,并配置 sudo 免密 sed -i '/^root/a\7d ALL=(ALL) NOPASSWD:ALL' /etc/sudoers #Step 3:保存集群安全配置文件到當前用戶.kube 目錄 su - 7d mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config #Step 4:啟用 kubectl 命令自動補全功能(注銷重新登錄生效) echo "source <(kubectl completion bash)" >> ~/.bashrc
需要這些配置命令的原因是:Kubernetes 集群默認需要加密方式訪問。所以,這幾條命令,就是將剛剛部署生成的 Kubernetes 集群的安全配置文件,保存到當前用戶的.kube 目錄下,kubectl 默認會使用這個目錄下的授權信息訪問 Kubernetes 集群。
如果不這么做的話,我們每次都需要通過 export KUBECONFIG 環境變量告訴 kubectl 這個安全配置文件的位置。
配置完成后 centos 用戶就可以使用 kubectl 命令管理集群了。
查看集群狀態,確認各個組件處于 Healthy 狀態:
[7d@k8s-master ~]$ kubectl get cs NAME STATUS MESSAGE ERROR controller-manager Healthy ok scheduler Healthy ok etcd-0 Healthy {"health":"true"}
查看節點狀態:
[7d@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 159m v1.18.4
可以看到,當前只存在 1 個 master 節點,并且這個節點的狀態是 NotReady。
使用 kubectl describe 命令來查看這個節點(Node)對象的詳細信息、狀態和事件(Event):
[7d@k8s-master ~]$ kubectl describe node k8s-master ...... Conditions: Type Status LastHeartbeatTime LastTransitionTime Reason Message ---- ------ ----------------- ------------------ ------ ------- MemoryPressure False Sat, 20 Jun 2020 14:30:30 +0800 Sat, 20 Jun 2020 11:54:13 +0800 KubeletHasSufficientMemory kubelet has sufficient memory available DiskPressure False Sat, 20 Jun 2020 14:30:30 +0800 Sat, 20 Jun 2020 11:54:13 +0800 KubeletHasNoDiskPressure kubelet has no disk pressure PIDPressure False Sat, 20 Jun 2020 14:30:30 +0800 Sat, 20 Jun 2020 11:54:13 +0800 KubeletHasSufficientPID kubelet has sufficient PID available Ready False Sat, 20 Jun 2020 14:30:30 +0800 Sat, 20 Jun 2020 11:54:13 +0800 KubeletNotReady runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
通過 kubectl describe 指令的輸出,我們可以看到 NodeNotReady 的原因在于,我們尚未部署任何網絡插件。
另外,我們還可以通過 kubectl 檢查這個節點上各個系統 Pod 的狀態,其中,kube-system 是 Kubernetes 項目預留的系統 Pod 的工作空間(Namepsace,注意它并不是 Linux Namespace,它只是 Kubernetes 劃分不同工作空間的單位):
[7d@k8s-master ~]$ kubectl get pod -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES coredns-7ff77c879f-24n99 0/1 Pending 0 162m
可以看到,CoreDNS 依賴于網絡的 Pod 都處于 Pending 狀態,即調度失敗。這當然是符合預期的:因為這個 Master 節點的網絡尚未就緒。
集群初始化如果遇到問題,可以使用 kubeadm reset 命令進行清理然后重新執行初始化。
部署網絡插件
要讓 Kubernetes Cluster 能夠工作,必須安裝 Pod 網絡,否則 Pod 之間無法通信。
Kubernetes 支持多種網絡方案,這里我們使用 flannel
#Step 1:下載部署文件 [7d@k8s-master ~]$ wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml #Step 2:安裝 flannel [7d@k8s-master ~]$ kubectl apply -f kube-flannel.yml podsecuritypolicy.policy/psp.flannel.unprivileged created clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created daemonset.apps/kube-flannel-ds-amd64 created daemonset.apps/kube-flannel-ds-arm64 created daemonset.apps/kube-flannel-ds-arm created daemonset.apps/kube-flannel-ds-ppc64le created daemonset.apps/kube-flannel-ds-s390x created ##Step 3:重新檢測 pod 狀態 [7d@k8s-master ~]$ kubectl get pod -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES coredns-7ff77c879f-24n99 1/1 Running 0 167m 10.244.0.2 k8s-master
可以看到,所有的系統 Pod 都成功啟動了,而剛剛部署的 flannel 網絡插件則在 kube-system 下面新建了一個名叫kube-flannel-ds-amd64-5xhp5的 Pod,一般來說,這些 Pod 就是容器網絡插件在每個節點上的控制組件。
Kubernetes 支持容器網絡插件,使用的是一個名叫 CNI 的通用接口,它也是當前容器網絡的事實標準,市面上的所有容器網絡開源項目都可以通過 CNI 接入 Kubernetes,比如 Flannel、Calico、Canal、Romana 等等,它們的部署方式也都是類似的“一鍵部署” .
再次查看 master 節點狀態已經為 ready 狀態:
[7d@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 167m v1.18.4
至此,Kubernetes 的 Master 節點就部署完成了。如果你只需要一個單節點的 Kubernetes,現在你就可以使用了。不過,在默認情況下,Kubernetes 的 Master 節點是不能運行用戶 Pod 的。
部署 worker 節點
Kubernetes 的 Worker 節點跟 Master 節點幾乎是相同的,它們運行著的都是一個 kubelet 組件。唯一的區別在于,在 kubeadm init 的過程中,kubelet 啟動后,Master 節點上還會自動運行 kube-apiserver、kube-scheduler、kube-controller-manger 這三個系統 Pod。
在 node 節點 上分別執行如下命令,將其注冊到 Cluster 中:
#執行以下命令將節點接入集群 kubeadm join 172.16.106.226:6443 --token ztz3qu.ee9gdjh32g228l4k \ --discovery-token-ca-cert-hash sha256:24411c65811afb54501be97ad0cf28c87dc9f51ca0ee5c49f71e58b535d91a43 #如果執行 kubeadm init 時沒有記錄下加入集群的命令,可以通過以下命令重新創建 kubeadm token create --print-join-command
在 k8s-node1 上執行 kubeadm join:
[root@k8s-node1 ~]# kubeadm join 172.16.106.226:6443 --token ztz3qu.ee9gdjh32g228l4k \ > --discovery-token-ca-cert-hash sha256:24411c65811afb54501be97ad0cf28c87dc9f51ca0ee5c49f71e58b535d91a43 ; W0620 14:44:55.729260 32211 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set. [preflight] Running pre-flight checks [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/ [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.
重復執行以上操作將其他 node 也加進去。
然后根據提示,我們可以通過kubectl get nodes查看節點的狀態:
[7d@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 4h43m v1.18.4 k8s-node1 Ready
nodes 狀態全部為 ready,由于每個節點都需要啟動若干組件,如果 node 節點的狀態是 NotReady,可以查看所有節點 pod 狀態,確保所有 pod 成功拉取到鏡像并處于 running 狀態:
[7d@k8s-master ~]$ kubectl get pod --all-namespaces -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES kube-system coredns-7ff77c879f-24n99 1/1 Running 0 4h46m 10.244.0.2 k8s-master
??注意:
這時,所有的節點都已經 Ready,Kubernetes Cluster 創建成功,一切準備就緒。如果 pod 狀態為 Pending、ContainerCreating、ImagePullBackOff 都表明 Pod 沒有就緒,Running 才是就緒狀態。如果有 pod 提示 Init:ImagePullBackOff,說明這個 pod 的鏡像在對應節點上拉取失敗,我們可以通過 kubectl describe pod 查看 Pod 具體情況,以確認拉取失敗的鏡像。
查看 master 節點下載了哪些鏡像:
[7d@k8s-master ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-proxy v1.18.1 4e68534e24f6 2 months ago 117MB registry.aliyuncs.com/google_containers/kube-apiserver v1.18.1 a595af0107f9 2 months ago 173MB registry.aliyuncs.com/google_containers/kube-controller-manager v1.18.1 d1ccdd18e6ed 2 months ago 162MB registry.aliyuncs.com/google_containers/kube-scheduler v1.18.1 6c9320041a7b 2 months ago 95.3MB quay.io/coreos/flannel v0.12.0-amd64 4e9f801d2217 3 months ago 52.8MB registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 4 months ago 683kB registry.aliyuncs.com/google_containers/coredns 1.6.7 67da37a9a360 4 months ago 43.8MB registry.aliyuncs.com/google_containers/etcd 3.4.3-0 303ce5db0e90 7 months ago 288MB
查看 node 節點下載了哪些鏡像:
[root@k8s-node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-proxy v1.18.1 4e68534e24f6 2 months ago 117MB quay.io/coreos/flannel v0.12.0-amd64 4e9f801d2217 3 months ago 52.8MB registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 4 months ago 683kB
安裝 Ingress Controller
快速初始化
在 master 節點上執行
# 只在 master 節點執行 kubectl apply -f https://kuboard.cn/install-script/v1.18.x/nginx-ingress.yaml
卸載Ingress Controller
在 master 節點上執行
只在您想選擇其他 Ingress Controller 的情況下卸載
# 只在 master 節點執行 kubectl delete -f https://kuboard.cn/install-script/v1.18.x/nginx-ingress.yaml
配置域名解析
將域名 *.demo.yourdomain.com 解析K8S-node1 的 IP 地址 z.z.z.z (也可以是 K8S-node2 的地址 y.y.y.y)
驗證配置
在瀏覽器訪問 a.demo.yourdomain.com,將得到 404 NotFound 錯誤頁面
WARNING
如果打算將 Kubernetes 用于生產環境,請參考此文檔 Installing Ingress Controller,完善 Ingress 的配置
Docker Kubernetes 容器
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。