部署環境
一個Master節點,兩個Node節點。配置所有節點計算機名,並配置hosts指向,最後配置好統一時間同步服務器。
Master:192.168.6.102(kubernetes-master)
Node1:192.168.6.104(kubernetes-node1)
Node2:192.168.6.105(kubernetes-node2)
系統版本:CentOS Linux release 7.5.1804 (Core)
Docker版本:docker-ce-17.03.0
Kubernetes版本:1.12.0
部署方法:Kubeadm
關閉SELinux,清空防火牆規則
Docker部署
注意:所有節點都需要部署
下載安裝包
cd /usr/local/src/ wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm安裝
yum -y install docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
啓動Docker並配置開機自啓動
systemctl start docker && systemctl enable docker
配置專屬阿里雲Docker官方鏡像倉庫加速器
首先需要註冊一個阿里雲賬號,然後進入“容器鏡像服務”找到鏡像加速器,根據官方文檔說明配置即可
部署Master節點
配置添加yum配置文件
[Kubernetes] name=Kubernetes repo baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg enable=0
安裝軟件:不可能在集羣初始化前啓動,後面在初始化集羣時會自動啓動,但需要設置成開機自啓動,否則在初始化時會有警告信息
yum install kubeadm kubectl systemctl enable kubelet
下載所需要鏡像
kubeadm config images pull
注意:默認情況下此站點在國內無法下載,爲保證能順利安裝,最好是使用牆外的服務器部署好Docker,安裝好kubeadm,然後執行此命令將鏡像下載到本地再打包複製到牆內
準備集羣初始化環境
swapoff -a echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
臨時需要關閉交換分區,否則在初始化的時候會報錯,永久在fstab文件註釋掉交換分區那行即可
打開ipv4透明網橋
開始初始化Master集羣
kubeadm init --pod-network-cidr=10.42.0.0/16 --service-cidr=10.8.0.0/16 --kubernetes-version=v1.12.0 --apiserver-advertise-address=0.0.0.0 --apiserver-bind-port=6443 --ignore-preflight-errors=Swap
--pod-network-cidr: 指定Pod網段
--service-cidr: 指定服務網絡
--kubernetes-version: 指定kubernetes版本,不同時間安裝的kubeadm版本不同,所支持部署的kubernetes版本也不同,如果不支持會有報錯提示
--apiserver-advertise-address: 指定apiserver監聽地址,默認監聽0.0.0.0
--apiserver-bind-port: 指定apiserver監聽端口,默認6443
--ignore-preflight-errors: 忽略指定錯誤信息,默認情況下如果swap打開會報錯,如果關閉了Swap此項可以不指定
如果成功安裝,會顯示大概如下所示信息,一個是配置kubectl控制檯,一個是提示node節點加入方法
[markmaster] Marking the node kubernetes-master as master by adding the label "node-role.kubernetes.io/master=''" [markmaster] Marking the node kubernetes-master as master by adding the taints [node-role.kubernetes.io/master:NoSchedule] [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "kubernetes-master" as an annotation [bootstraptoken] using token: 9jefj3.czyghatvpi8mqamz [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.6.102:6443 --token 9jefj3.czyghatvpi8mqamz --discovery-token-ca-cert-hash sha256:4126634b062c39f2bc3ef3ba5aa717e95a54aa16889ccb820bdb548c961b8d97
此時查看message日誌,如果有類似於如下錯誤提示信息
Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
修改“/var/lib/kubelet/kubeadm-flags.env”配置文件,刪除“--network-plugin=cni”,最後重啓kubelet服務
配置kubectl控制檯
注意:提示推薦我們使用普通用戶執行,所以需要sudo權限,我這裏就直接root
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config此時我們可以使用下面的命令查看Kubernetes的基本信息
kubectl get ns 查看所有名稱空間
kubectl get cs 查看健康狀況
kubectl get nodes 查看所有節點信息
kubectl get pods 查看所有pods信息
部署flannel網絡組件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml sed -i "/10.244/s/244/42/" kube-flannel.yml kubectl apply -f kube-flannel.yml
在部署前需要修改pod網絡爲我們指定的網絡,如果我們使用默認的10.244.0.0/16就無需修改,直接執行下面命令安裝即可
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
注意:在部署flannel的時候也會自動下載相關鏡像,可以先打開yml文件看下需要哪個鏡像先pull下載再執行部署
檢查Master節點是否運行正常