系列目錄html
Kubernetes節點架構圖:node
kubernetes組件架構圖:linux
咱們將使用kubeadm部署3個節點的 Kubernetes Cluster,總體結構圖:nginx
無特殊說明如下操做在全部節點執行:git
#master節點: hostnamectl set-hostname k8s-master #node1節點: hostnamectl set-hostname k8s-node1 #node2節點: hostnamectl set-hostname k8s-node2
#修改/etc/hosts文件 cat >> /etc/hosts << EOF 192.168.92.56 k8s-master 192.168.92.57 k8s-node1 192.168.92.58 k8s-node2 EOF #關閉防火牆和selinux systemctl stop firewalld && systemctl disable firewalld sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config && setenforce 0 #關閉swap swapoff -a yes | cp /etc/fstab /etc/fstab_bak cat /etc/fstab_bak |grep -v swap > /etc/fstab
使用chrony同步時間,配置master節點與網絡NTP服務器同步時間,全部node節點與master節點同步時間。github
配置master節點:docker
#安裝chrony: yum install -y chrony #註釋默認ntp服務器 sed -i 's/^server/#&/' /etc/chrony.conf #指定上游公共 ntp 服務器,並容許其餘節點同步時間 cat >> /etc/chrony.conf << EOF 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 EOF #重啓chronyd服務並設爲開機啓動: systemctl enable chronyd && systemctl restart chronyd #開啓網絡時間同步功能 timedatectl set-ntp true
(注意修改master IP地址)bootstrap
#安裝chrony: yum install -y chrony #註釋默認服務器 sed -i 's/^server/#&/' /etc/chrony.conf #指定內網 master節點爲上游NTP服務器 echo server 192.168.92.56 iburst >> /etc/chrony.conf #重啓服務並設爲開機啓動: systemctl enable chronyd && systemctl restart chronyd
全部節點執行chronyc sources命令,查看存在以^*開頭的行,說明已經與服務器時間同步centos
cat <<EOF > /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 EOF # 使配置生效 modprobe br_netfilter sysctl -p /etc/sysctl.d/k8s.conf
因爲ipvs已經加入到了內核的主幹,因此爲kube-proxy開啓ipvs的前提須要加載如下的內核模塊: 在全部的Kubernetes節點執行如下腳本:api
cat > /etc/sysconfig/modules/ipvs.modules <<EOF #!/bin/bash modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF #執行腳本 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
Kubernetes默認的容器運行時仍然是Docker,使用的是kubelet中內置dockershim CRI實現。須要注意的是,Kubernetes 1.13最低支持的Docker版本是1.11.1,最高支持是18.06,而Docker最新版本已是18.09了,故咱們安裝時須要指定版本爲18.06.1-ce。
#配置docker yum源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #安裝指定版本,這裏安裝18.06 yum list docker-ce --showduplicates | sort -r yum install -y docker-ce-18.06.1.ce-3.el7 systemctl start docker && systemctl enable docker
#配置kubernetes.repo的源,因爲官方源國內沒法訪問,這裏使用阿里雲yum源 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 #在全部節點上安裝指定版本 kubelet、kubeadm 和 kubectl yum install -y kubelet-1.13.1 kubeadm-1.13.1 kubectl-1.13.1 #啓動kubelet服務 systemctl enable kubelet && systemctl start kubelet
完整的官方文檔能夠參考: https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/ https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/ Master節點執行初始化:
注意這裏執行初始化用到了- -image-repository選項,指定初始化須要的鏡像源從阿里雲鏡像倉庫拉取。
kubeadm init \ --apiserver-advertise-address=192.168.92.56 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.13.1 \ --pod-network-cidr=10.244.0.0/16
初始化命令說明:
--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.13版本中咱們能夠增長–image-repository參數,默認值是 k8s.gcr.io,將其指定爲阿里雲鏡像地址:registry.aliyuncs.com/google_containers。
--kubernetes-version=v1.13.1
關閉版本探測,由於它的默認值是stable-1,會致使從https://dl.k8s.io/release/stable-1.txt下載最新的版本號,咱們能夠將其指定爲固定版本(最新版:v1.13.1)來跳過網絡請求。
初始化過程以下:
[root@k8s-master ~]# kubeadm init \ > --image-repository registry.aliyuncs.com/google_containers \ > --kubernetes-version v1.13.1 \ > --pod-network-cidr=10.244.0.0/16 [init] Using Kubernetes version: v1.13.1 [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] Activating the kubelet service [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" 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 [192.168.92.56 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 [192.168.92.56 127.0.0.1 ::1] [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 192.168.92.56] [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 "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" [control-plane] Creating static Pod manifest for "kube-scheduler" [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 21.009858 seconds [uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-master" as an annotation [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: 60syk6.vnplamkn3zhwu3s3 [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master 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/ You can now join any number of machines by running the following on each node as root: kubeadm join 192.168.92.56:6443 --token 60syk6.vnplamkn3zhwu3s3 --discovery-token-ca-cert-hash sha256:7d50e704bbfe69661e37c5f3ad13b1b88032b6b2b703ebd4899e259477b5be69 [root@k8s-master ~]#
(注意記錄下初始化結果中的kubeadm join命令,部署worker節點時會用到)
初始化過程說明:
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面咱們已經在全部的節點安裝了 kubectl。Master 初始化完成後須要作一些配置工做,而後 kubectl 就能使用了。 依照 kubeadm init 輸出的最後提示,推薦用 Linux 普通用戶執行 kubectl。
#建立普通用戶並設置密碼123456 useradd centos && echo "centos:123456" | chpasswd centos #追加sudo權限,並配置sudo免密 sed -i '/^root/a\centos ALL=(ALL) NOPASSWD:ALL' /etc/sudoers #保存集羣安全配置文件到當前用戶.kube目錄 su - centos mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config #啓用 kubectl 命令自動補全功能(註銷從新登陸生效) echo "source <(kubectl completion bash)" >> ~/.bashrc
須要這些配置命令的緣由是:Kubernetes 集羣默認須要加密方式訪問。因此,這幾條命令,就是將剛剛部署生成的 Kubernetes 集羣的安全配置文件,保存到當前用戶的.kube 目錄下,kubectl 默認會使用這個目錄下的受權信息訪問 Kubernetes 集羣。 若是不這麼作的話,咱們每次都須要經過 export KUBECONFIG 環境變量告訴 kubectl 這個安全配置文件的位置。 配置完成後centos用戶就可使用 kubectl 命令管理集羣了。
查看集羣狀態:
[centos@k8s-master ~]$ kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health": "true"} [centos@k8s-master ~]$
確認各個組件都處於healthy狀態。 查看節點狀態
[centos@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 36m v1.13.1 [centos@k8s-master ~]$
能夠看到,當前只存在1個master節點,而且這個節點的狀態是 NotReady。 使用 kubectl describe 命令來查看這個節點(Node)對象的詳細信息、狀態和事件(Event):
[centos@k8s-master ~]$ kubectl describe node k8s-master ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Starting 33m kubelet, k8s-master Starting kubelet. Normal NodeHasSufficientMemory 33m (x8 over 33m) kubelet, k8s-master Node k8s-master status is now: NodeHasSufficientMemory Normal NodeHasNoDiskPressure 33m (x8 over 33m) kubelet, k8s-master Node k8s-master status is now: NodeHasNoDiskPressure Normal NodeHasSufficientPID 33m (x7 over 33m) kubelet, k8s-master Node k8s-master status is now: NodeHasSufficientPID Normal NodeAllocatableEnforced 33m kubelet, k8s-master Updated Node Allocatable limit across pods Normal Starting 33m kube-proxy, k8s-master Starting kube-proxy.
經過 kubectl describe 指令的輸出,咱們能夠看到 NodeNotReady 的緣由在於,咱們還沒有部署任何網絡插件,kube-proxy等組件還處於starting狀態。 另外,咱們還能夠經過 kubectl 檢查這個節點上各個系統 Pod 的狀態,其中,kube-system 是 Kubernetes 項目預留的系統 Pod 的工做空間(Namepsace,注意它並非 Linux Namespace,它只是 Kubernetes 劃分不一樣工做空間的單位):
[centos@k8s-master ~]$ kubectl get pod -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES coredns-78d4cf999f-7jdx7 0/1 Pending 0 29m <none> <none> <none> <none> coredns-78d4cf999f-s6mhk 0/1 Pending 0 29m <none> <none> <none> <none> etcd-k8s-master 1/1 Running 0 34m 192.168.92.56 k8s-master <none> <none> kube-apiserver-k8s-master 1/1 Running 0 34m 192.168.92.56 k8s-master <none> <none> kube-controller-manager-k8s-master 1/1 Running 0 34m 192.168.92.56 k8s-master <none> <none> kube-proxy-przwf 1/1 Running 0 34m 192.168.92.56 k8s-master <none> <none> kube-scheduler-k8s-master 1/1 Running 0 34m 192.168.92.56 k8s-master <none> <none> [centos@k8s-master ~]$
能夠看到,CoreDNS依賴於網絡的 Pod 都處於 Pending 狀態,即調度失敗。這固然是符合預期的:由於這個 Master 節點的網絡還沒有就緒。 集羣初始化若是遇到問題,可使用kubeadm reset命令進行清理而後從新執行初始化。
部署網絡插件 要讓 Kubernetes Cluster 可以工做,必須安裝 Pod 網絡,不然 Pod 之間沒法通訊。 Kubernetes 支持多種網絡方案,這裏咱們使用 flannel 執行以下命令部署 flannel: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[centos@k8s-master ~]$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created daemonset.extensions/kube-flannel-ds-amd64 created daemonset.extensions/kube-flannel-ds-arm64 created daemonset.extensions/kube-flannel-ds-arm created daemonset.extensions/kube-flannel-ds-ppc64le created daemonset.extensions/kube-flannel-ds-s390x created [centos@k8s-master ~]$
部署完成後,咱們能夠經過 kubectl get 從新檢查 Pod 的狀態:
[centos@k8s-master ~]$ kubectl get pod -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES coredns-78d4cf999f-7jdx7 1/1 Running 0 11h 10.244.0.3 k8s-master <none> <none> coredns-78d4cf999f-s6mhk 1/1 Running 0 11h 10.244.0.2 k8s-master <none> <none> etcd-k8s-master 1/1 Running 1 11h 192.168.92.56 k8s-master <none> <none> kube-apiserver-k8s-master 1/1 Running 1 11h 192.168.92.56 k8s-master <none> <none> kube-controller-manager-k8s-master 1/1 Running 1 11h 192.168.92.56 k8s-master <none> <none> kube-flannel-ds-amd64-lkf2f 1/1 Running 0 10h 192.168.92.56 k8s-master <none> <none> kube-proxy-przwf 1/1 Running 1 11h 192.168.92.56 k8s-master <none> <none> kube-scheduler-k8s-master 1/1 Running 1 11h 192.168.92.56 k8s-master <none> <none> [centos@k8s-master ~]$
能夠看到,全部的系統 Pod 都成功啓動了,而剛剛部署的flannel網絡插件則在 kube-system 下面新建了一個名叫kube-flannel-ds-amd64-lkf2f的 Pod,通常來講,這些 Pod 就是容器網絡插件在每一個節點上的控制組件。 Kubernetes 支持容器網絡插件,使用的是一個名叫 CNI 的通用接口,它也是當前容器網絡的事實標準,市面上的全部容器網絡開源項目均可以經過 CNI 接入 Kubernetes,好比 Flannel、Calico、Canal、Romana 等等,它們的部署方式也都是相似的「一鍵部署」。 再次查看master節點狀態已經爲ready狀態:
[centos@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 11h v1.13.1 [centos@k8s-master ~]$
至此,Kubernetes 的 Master 節點就部署完成了。若是你只須要一個單節點的 Kubernetes,如今你就可使用了。不過,在默認狀況下,Kubernetes 的 Master 節點是不能運行用戶 Pod 的。
Kubernetes 的 Worker 節點跟 Master 節點幾乎是相同的,它們運行着的都是一個 kubelet 組件。惟一的區別在於,在 kubeadm init 的過程當中,kubelet 啓動後,Master 節點上還會自動運行 kube-apiserver、kube-scheduler、kube-controller-manger 這三個系統 Pod。 在 k8s-node1 和 k8s-node2 上分別執行以下命令,將其註冊到 Cluster 中:
#執行如下命令將節點接入集羣 kubeadm join 192.168.92.56:6443 --token 67kq55.8hxoga556caxty7s --discovery-token-ca-cert-hash sha256:7d50e704bbfe69661e37c5f3ad13b1b88032b6b2b703ebd4899e259477b5be69 #若是執行kubeadm init時沒有記錄下加入集羣的命令,能夠經過如下命令從新建立 kubeadm token create --print-join-command
在k8s-node1上執行kubeadm join :
[root@k8s-node1 ~]# kubeadm join 192.168.92.56:6443 --token 67kq55.8hxoga556caxty7s --discovery-token-ca-cert-hash sha256:7d50e704bbfe69661e37c5f3ad13b1b88032b6b2b703ebd4899e259477b5be69 [preflight] Running pre-flight checks [discovery] Trying to connect to API Server "192.168.92.56:6443" [discovery] Created cluster-info discovery client, requesting info from "https://192.168.92.56:6443" [discovery] Requesting info from "https://192.168.92.56:6443" again to validate TLS against the pinned public key [discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "192.168.92.56:6443" [discovery] Successfully established connection with API Server "192.168.92.56:6443" [join] Reading configuration from the cluster... [join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' [kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" 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] Activating the kubelet service [tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap... [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-node1" as an annotation 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 master to see this node join the cluster. [root@k8s-node1 ~]#
重複執行以上操做將k8s-node2也加進去(注意從新執行kubeadm token create --print-join-command)。 而後根據提示,咱們能夠經過 kubectl get nodes 查看節點的狀態:
[centos@k8s-master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 11h v1.13.1 k8s-node1 Ready <none> 24m v1.13.1 k8s-node2 Ready <none> 4m9s v1.13.1 [centos@k8s-master ~]$
nodes狀態所有爲ready,因爲每一個節點都須要啓動若干組件,若是node節點的狀態是 NotReady,能夠查看全部節點pod狀態,確保全部pod成功拉取到鏡像並處於running狀態:
[centos@k8s-master ~]$ kubectl get pod --all-namespaces -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES kube-system coredns-78d4cf999f-7jdx7 1/1 Running 0 11h 10.244.0.3 k8s-master <none> <none> kube-system coredns-78d4cf999f-s6mhk 1/1 Running 0 11h 10.244.0.2 k8s-master <none> <none> kube-system etcd-k8s-master 1/1 Running 1 12h 192.168.92.56 k8s-master <none> <none> kube-system kube-apiserver-k8s-master 1/1 Running 1 12h 192.168.92.56 k8s-master <none> <none> kube-system kube-controller-manager-k8s-master 1/1 Running 1 12h 192.168.92.56 k8s-master <none> <none> kube-system kube-flannel-ds-amd64-d2r8p 1/1 Running 0 6m43s 192.168.92.58 k8s-node2 <none> <none> kube-system kube-flannel-ds-amd64-d85c6 1/1 Running 0 27m 192.168.92.57 k8s-node1 <none> <none> kube-system kube-flannel-ds-amd64-lkf2f 1/1 Running 0 11h 192.168.92.56 k8s-master <none> <none> kube-system kube-proxy-k8jx8 1/1 Running 0 6m43s 192.168.92.58 k8s-node2 <none> <none> kube-system kube-proxy-n95ck 1/1 Running 0 27m 192.168.92.57 k8s-node1 <none> <none> kube-system kube-proxy-przwf 1/1 Running 1 12h 192.168.92.56 k8s-master <none> <none> kube-system kube-scheduler-k8s-master 1/1 Running 1 12h 192.168.92.56 k8s-master <none> <none> [centos@k8s-master ~]$
這時,全部的節點都已經 Ready,Kubernetes Cluster 建立成功,一切準備就緒。 若是pod狀態爲Pending、ContainerCreating、ImagePullBackOff 都代表 Pod 沒有就緒,Running 纔是就緒狀態。 若是有pod提示Init:ImagePullBackOff,說明這個pod的鏡像在對應節點上拉取失敗,咱們能夠經過 kubectl describe pod 查看 Pod 具體狀況,以確認拉取失敗的鏡像:
[centos@k8s-master ~]$ kubectl describe pod kube-flannel-ds-amd64-d2r8p --namespace=kube-system ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m14s default-scheduler Successfully assigned kube-system/kube-flannel-ds-amd64-lzx5v to k8s-node2 Warning Failed 109s kubelet, k8s-node2 Failed to pull image "quay.io/coreos/flannel:v0.10.0-amd64": rpc error: code = Unknown desc = Error response from daemon: Get https://quay.io/v2/: net/http: TLS handshake timeout Warning Failed 109s kubelet, k8s-node2 Error: ErrImagePull Normal BackOff 108s kubelet, k8s-node2 Back-off pulling image "quay.io/coreos/flannel:v0.10.0-amd64" Warning Failed 108s kubelet, k8s-node2 Error: ImagePullBackOff Normal Pulling 94s (x2 over 2m6s) kubelet, k8s-node2 pulling image "quay.io/coreos/flannel:v0.10.0-amd64"
這裏看最後events輸出內容,能夠看到在下載 image 時失敗,若是網絡質量很差,這種狀況是很常見的。咱們能夠耐心等待,由於 Kubernetes 會重試,咱們也能夠本身手工執行 docker pull 去下載這個鏡像。
[root@k8s-node2 ~]# docker pull quay.io/coreos/flannel:v0.10.0-amd64 v0.10.0-amd64: Pulling from coreos/flannel ff3a5c916c92: Already exists 8a8433d1d437: Already exists 306dc0ee491a: Already exists 856cbd0b7b9c: Already exists af6d1e4decc6: Already exists Digest: sha256:88f2b4d96fae34bfff3d46293f7f18d1f9f3ca026b4a4d288f28347fcb6580ac Status: Image is up to date for quay.io/coreos/flannel:v0.10.0-amd64 [root@k8s-node2 ~]#
若是沒法從 quay.io/coreos/flannel:v0.10.0-amd64 下載鏡像,能夠從阿里雲或者dockerhub鏡像倉庫下載,而後改回原來的tag便可:
docker pull registry.cn-hangzhou.aliyuncs.com/kubernetes_containers/flannel:v0.10.0-amd64 docker tag registry.cn-hangzhou.aliyuncs.com/kubernetes_containers/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64 docker rmi registry.cn-hangzhou.aliyuncs.com/kubernetes_containers/flannel:v0.10.0-amd64
查看master節點下載了哪些鏡像
[centos@k8s-master ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-proxy v1.13.1 fdb321fd30a0 2 weeks ago 80.2MB registry.aliyuncs.com/google_containers/kube-apiserver v1.13.1 40a63db91ef8 2 weeks ago 181MB registry.aliyuncs.com/google_containers/kube-scheduler v1.13.1 ab81d7360408 2 weeks ago 79.6MB registry.aliyuncs.com/google_containers/kube-controller-manager v1.13.1 26e6f1db2a52 2 weeks ago 146MB registry.aliyuncs.com/google_containers/coredns 1.2.6 f59dcacceff4 8 weeks ago 40MB registry.aliyuncs.com/google_containers/etcd 3.2.24 3cab8e1b9802 3 months ago 220MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 11 months ago 44.6MB registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 12 months ago 742kB [centos@k8s-master ~]$
查看node節點下載了哪些鏡像:
[root@k8s-node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-proxy v1.13.1 fdb321fd30a0 2 weeks ago 80.2MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 11 months ago 44.6MB registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 12 months ago 742kB [root@k8s-node1 ~]#
[centos@k8s-master ~]$ kubectl create deployment nginx --image=nginx:alpine deployment.apps/nginx created [centos@k8s-master ~]$ kubectl scale deployment nginx --replicas=2 deployment.extensions/nginx scaled [centos@k8s-master ~]$
驗證Nginx Pod是否正確運行,而且會分配10.244.開頭的集羣IP
[centos@k8s-master ~]$ kubectl get pods -l app=nginx -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-54458cd494-p2qgx 1/1 Running 0 111s 10.244.1.2 k8s-node1 <none> <none> nginx-54458cd494-sdlm7 1/1 Running 0 103s 10.244.2.2 k8s-node2 <none> <none> [centos@k8s-master ~]$
再驗證一下kube-proxy是否正常:
以 NodePort 方式對外提供服務 參考:https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/
[centos@k8s-master ~]$ kubectl expose deployment nginx --port=80 --type=NodePort service/nginx exposed [centos@k8s-master ~]$ kubectl get services nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.108.17.2 <none> 80:30670/TCP 12s [centos@k8s-master ~]$
能夠經過任意 NodeIP:Port 在集羣外部訪問這個服務:
[centos@k8s-master ~]$ curl 192.168.92.56:30670 [centos@k8s-master ~]$ curl 192.168.92.57:30670 [centos@k8s-master ~]$ curl 192.168.92.58:30670
訪問k8s-master ip
訪問k8s-node1 ip
訪問k8s-node2 ip
最後驗證一下dns, pod network是否正常: 運行Busybox並進入交互模式
[centos@k8s-master ~]$ kubectl run -it curl --image=radial/busyboxplus:curl kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. If you don't see a command prompt, try pressing enter. [ root@curl-66959f6557-s5qqs:/ ]$
輸入nslookup nginx查看是否能夠正確解析出集羣內的IP,以驗證DNS是否正常
[ root@curl-66959f6557-s5qqs:/ ]$ nslookup nginx Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: nginx Address 1: 10.108.17.2 nginx.default.svc.cluster.local
經過服務名進行訪問,驗證kube-proxy是否正常
[ root@curl-66959f6557-q472z:/ ]$ curl http://nginx/ <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...... </body> </html> [ root@curl-66959f6557-q472z:/ ]$
分別訪問一下2個Pod的內網IP,驗證跨Node的網絡通訊是否正常
[ root@curl-66959f6557-s5qqs:/ ]$ curl 10.244.1.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...... </body> </html> [ root@curl-66959f6557-s5qqs:/ ]$ curl 10.244.2.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...... </body> </html> [ root@curl-66959f6557-s5qqs:/ ]$
Pod調度到Master節點 出於安全考慮,默認配置下Kubernetes不會將Pod調度到Master節點。查看Taints字段默認配置:
[centos@k8s-master ~]$ kubectl describe node k8s-master ...... Taints: node-role.kubernetes.io/master:NoSchedule
若是但願將k8s-master也看成Node節點使用,能夠執行以下命令,其中k8s-master是主機節點hostname:
kubectl taint node k8s-master node-role.kubernetes.io/master-
修改後Taints字段狀態:
[centos@k8s-master ~]$ kubectl describe node k8s-master ...... Taints: <none>
若是要恢復Master Only狀態,執行以下命令:
kubectl taint node k8s-master node-role.kubernetes.io/master=:NoSchedule
kube-proxy開啓ipvs 修改ConfigMap的kube-system/kube-proxy中的config.conf,mode: 「ipvs」:
[centos@k8s-master ~]$ kubectl edit cm kube-proxy -n kube-system configmap/kube-proxy edited
以後重啓各個節點上的kube-proxy pod:
[centos@k8s-master ~]$ kubectl get pod -n kube-system | grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}' pod "kube-proxy-2w9sh" deleted pod "kube-proxy-gw4lx" deleted pod "kube-proxy-thv4c" deleted [centos@k8s-master ~]$ kubectl get pod -n kube-system | grep kube-proxy kube-proxy-6qlgv 1/1 Running 0 65s kube-proxy-fdtjd 1/1 Running 0 47s kube-proxy-m8zkx 1/1 Running 0 52s [centos@k8s-master ~]$
查看日誌:
[centos@k8s-master ~]$ kubectl logs kube-proxy-6qlgv -n kube-system I1213 09:50:15.414493 1 server_others.go:189] Using ipvs Proxier. W1213 09:50:15.414908 1 proxier.go:365] IPVS scheduler not specified, use rr by default I1213 09:50:15.415021 1 server_others.go:216] Tearing down inactive rules. I1213 09:50:15.461658 1 server.go:464] Version: v1.13.0 I1213 09:50:15.467827 1 conntrack.go:52] Setting nf_conntrack_max to 131072 I1213 09:50:15.467997 1 config.go:202] Starting service config controller I1213 09:50:15.468010 1 controller_utils.go:1027] Waiting for caches to sync for service config controller I1213 09:50:15.468092 1 config.go:102] Starting endpoints config controller I1213 09:50:15.468100 1 controller_utils.go:1027] Waiting for caches to sync for endpoints config controller I1213 09:50:15.568766 1 controller_utils.go:1034] Caches are synced for endpoints config controller I1213 09:50:15.568950 1 controller_utils.go:1034] Caches are synced for service config controller [centos@k8s-master ~]$
日誌中打印出了Using ipvs Proxier,說明ipvs模式已經開啓。
移除節點和集羣 kubernetes集羣移除節點 以移除k8s-node2節點爲例,在Master節點上運行: kubectl drain k8s-node2 --delete-local-data --force --ignore-daemonsets kubectl delete node k8s-node2 上面兩條命令執行完成後,在k8s-node2節點執行清理命令,重置kubeadm的安裝狀態: kubeadm reset 在master上刪除node並不會清理k8s-node2運行的容器,須要在刪除節點上面手動運行清理命令。 若是你想從新配置集羣,使用新的參數從新運行kubeadm init或者kubeadm join便可。
至此3個節點的集羣搭建完成,後續能夠繼續添加node節點,或者部署dashboard、helm包管理工具、EFK日誌系統、Prometheus Operator監控系統、rook+ceph存儲系統等組件。