本文用於指導在Centos虛擬機下部署kubernetes 1.15.0
1.1 安裝VMware虛擬機
本人基於windows操作系統上的Centos虛擬機來搭建K8S環境,軟件版本信息信息如下:
l 宿主機系統:windows 10 家庭版版
l VMware Workstation 版本:15 PRO
l 客戶機系統版本:CentOS 7.6
下載軟件和鏡像:
VMware-workstation-full-15.0.0-10134415.exe
CentOS-7-x86_64-DVD-1611.iso
安裝注意點:
1、 選擇CentOS 7 64位;
2、 處理器配置,如果機器性能本來就不強的話,不要配過多的cpu給虛擬機;
3、 內存配置,建議1G內存。最低512M;
4、 沒有特殊要求的話,選擇橋接網絡或者使用網絡地址轉換;
5、 磁盤類型,推薦使用iscsi磁盤
建議:第一台使用鏡像安裝,另外兩台可克隆安裝,注意克隆安裝后修改IP地址信息。
登錄虛擬機,查看centos版本信息:
[root@master]# uname -a
Linux master 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@master]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
安裝完成后三台虛擬機IP分別為:
192.168.135.144
192.168.135.147
192.168.135.148
安裝后要保證虛擬機之間網絡互通,虛擬機虛宿主機之間網絡互通。
1.2 K8S1.15.0安裝
1.2.1 軟件版本
Docker-ce-18.06.0.ce-3.el7
kubeadm-1.15.0-0.x86_64
kubectl-1.15.0-0.x86_64
kubelet-1.15.0-0.x86_64
kubernetes-cni-0.7.5-0.x86_64
flannel-v0.11.0-amd64
1.2.2 准備階段
修改主機名:
hostnamectl master //192.168.135.144
hostnamectl slave1 //192.168.135.147
hostnamectl slave2 //192.168.135.148
關閉防火牆服務和selinx,避免與docker容器的防火牆規則沖突
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
1.2.3 Master節點安裝
第一步:查看所有倉庫中所有docker版本,並選擇特定版本安裝
查看所有倉庫中所有docker版本
$ yum list docker-ce --showduplicates | sort -r
…
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
…
安裝18.06.1.ce-3.el7版本
[root@master ~]# yum -y install Docker-ce-18.06.0.ce-3.el7
第二步:啟動並加入開機啟動docker:
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl start docker
[root@master ~]# systemctl enable docker
第三步:swap關閉
vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
第四步:安裝Kubeadm
yum -y install kubeadm-1.15.0-0.x86_64 kubectl-1.15.0-0.x86_64 kubelet-1.15.0-0.x86_64 kubernetes-cni-0.7.5-0.x86_64
第五步:設置開機啟動kubelet
systemctl enable kubelet
第六步:加載鏡像
先提前下載鏡像k8s-1.15.0.tar.gz
鏈接: https://pan.baidu.com/s/1AhDsQHUIMd0CQufGteFSXw 提取碼: vshs
上傳到各節點【非自己提供,來自網絡】
docker load -i k8s-1.15.0.tar.gz
先提前下載鏡像flannel-v0.11.0.tar.gz
鏈接: https://pan.baidu.com/s/1QEssOf2yX1taupQT4lTxQg 提取碼: x42r【本自己提供,來自網絡】
docker load -i flannel-v0.11.0.tar.gz
第七部:Master節點初始化
kubeadm init --kubernetes-version=v1.15.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=all
部署成功后會提示:
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.135.144:6443 --token qptprd.q6kpq5gr2vmcul4c \
--discovery-token-ca-cert-hash sha256:e57ea4378ecb188a4187a44936ec1e5fa881a8d95ba6f9fcc8b67771b7cbd085
這里將kubeadm join及后面內容復制下來,后面從節點加入集群的時候會用。
檢查集群狀態:kubectl get cs
檢查節點狀態:kubectl get nodes
節點沒有ready的原因是還沒有安裝flannel網絡
狀態所有的pods狀態:kubectl get pods --all-namespaces
master部署網絡插件flannel
[root@master01 ~]# kubectl apply -f 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 created
檢查節點狀態:kubectl get nodes
狀態所有的pods狀態:kubectl get pods --all-namespaces
1.2.4 安裝kube slave
如下操作在分別在兩個slave上執行
將147和148作為node加入集群
kubeadm join 192.168.135.144:6443 --token qptprd.q6kpq5gr2vmcul4c \
--discovery-token-ca-cert-hash sha256:e57ea4378ecb188a4187a44936ec1e5fa881a8d95ba6f9fcc8b67771b7cbd085
提示如下結果則成功:
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.
在Master上執行kubectl get nodes,查看結果如下:
在Master上執行kubectl get pods --all-namespaces,查看結果如下:
1.3 在slave節點支持kubectl 命令
[root@slave1 taoweizhong]# kubectl get cs
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[root@slave1 taoweizhong]#
在master節點使用遠程復制命令:
scp /etc/kubernetes/admin.conf root@slave1:/etc/kubernetes/
在從節點:
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
1.4 重新初始化
kubeadm reset
1.4.1 master重新生成新的token
[root@master ~]# kubeadm token create
islfov.xfdgpohve01v6073
[root@master ~]# kubeadm token list
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
4vreir.37c5jx9xklz7utdf 23h 2019-08-04T23:44:38-07:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
islfov.xfdgpohve01v6073 23h 2019-08-05T00:00:30-07:00 authentication,signing <none> system:bootstrappers:kubeadm:default-node-token
[root@master ~]#
-------Over------