在VMWare中创建完整克隆,分别命名为UbuntuNode1
和UbuntuNode2
分别对两个完整克隆的虚拟机进行如下操作,修改主机名称和静态IP
使用root用户登录
打开配置文件vim /etc/cloud/cloud.cfg
修改配置preserve_hostname: true
修改/etc/hostname
,只有一行 node1
或node2
node1
主机
/etc/hostname
node1
node2
主机
/et/hostname
node2
2.确认配置的三台机器的主机名称
$ cat /etc/hosts
$ shutdown -r now
/etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens33:
addresses: [192.168.236.177/24]
dhcp4: false
gateway4: 192.168.236.2
nameservers:
addresses: [192.168.236.2]
optional: true
version: 2
重启ip配置
netplan apply
/etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens33:
addresses: [192.168.236.178/24]
dhcp4: false
gateway4: 192.168.236.2
nameservers:
addresses: [192.168.236.2]
optional: true
version: 2
重启ip配置
netplan apply
/etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens33:
addresses: [192.168.236.179/24]
dhcp4: false
gateway4: 192.168.236.2
nameservers:
addresses: [192.168.236.2]
optional: true
version: 2
重启ip配置
netplan apply
注意: (Master、Node1、Node2都需要配置)
使用root用户登录
打开hosts文件 vim /etc/hosts
输入如下内容
192.168.236.177 master
192.168.236.178 node1
192.168.236.179 node2
重启机器shutdown -r now
$ mkdir /home/itcast/working
$ cd /home/itcast/working/
kubeadm
对应的配置文件,候选操作在home/itcast/working/
目录下使用kubeadm配置文件,通过在配置文件中指定docker仓库地址,便于内网快速部署。
生成配置文件
kubeadm config print init-defaults ClusterConfiguration > kubeadm.conf
kubeadm.conf
中的如下两项:vi kubeadm.conf
# 修改 imageRepository: k8s.gcr.io
# 改为 registry.cn-beijing.aliyuncs.com/imcto
imageRepository: registry.cn-beijing.aliyuncs.com/imcto
# 修改kubernetes版本kubernetesVersion: v1.13.0
# 改为kubernetesVersion: v1.13.1
kubernetesVersion: v1.13.1
kubeadm.conf
中的API服务器地址,后面会频繁使用这个地址。localAPIEndpoint:
advertiseAddress: 192.168.236.177
bindPort: 6443
注意:
192.168.236.177
是master主机的ip地址
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.96.0.0/12
scheduler: {}
这里的10.244.0.0/16
和 10.96.0.0/12
分别是k8s内部pods和services的子网网络,最好使用这个地址,后续flannel网络需要用到。
注意这个 kubeadm.conf的格式问题,很可能出现解析出错的问题。
$ kubeadm config images list --config kubeadm.conf
registry.cn-beijing.aliyuncs.com/imcto/kube-apiserver:v1.13.1
registry.cn-beijing.aliyuncs.com/imcto/kube-controller-manager:v1.13.1
registry.cn-beijing.aliyuncs.com/imcto/kube-scheduler:v1.13.1
registry.cn-beijing.aliyuncs.com/imcto/kube-proxy:v1.13.1
registry.cn-beijing.aliyuncs.com/imcto/pause:3.1
registry.cn-beijing.aliyuncs.com/imcto/etcd:3.2.24
registry.cn-beijing.aliyuncs.com/imcto/coredns:1.2.6
#下载全部当前版本的k8s所关联的镜像
kubeadm config images pull --config ./kubeadm.conf
#初始化并且启动
$ sudo kubeadm init --config ./kubeadm.conf
更多kubeadm配置文件参数详见
kubeadm config print-defaults
k8s启动成功输出内容较多,但是记住末尾的内容
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.236.177:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:e778d3665e52f5a680a87b00c6d54df726c2eda601c0db3bfa4bb198af2262a8
按照官方提示,执行以下操作。
执行如下命令
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
创建系统服务并启动
# 启动kubelet 设置为开机自启动
$ sudo systemctl enable kubelet
# 启动k8s服务程序
$ sudo systemctl start kubelet
NotReady
,证明初始化服务器成功$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 12m v1.13.1
$ kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health": "true"}
目前只有一个master,还没有node,而且是NotReady状态,那么我们需要将node加入到master管理的集群中来。在加入之前,我们需要先配置k8s集群的内部通信网络,这里采用的是flannel网络。
$cd $HOME/working
$wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
编辑这个文件,确保flannel网络是对的,找到net-conf.json
标记的内容是否正确。
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
这个"10.244.0.0/16"和 ./kubeadm.conf中的podsubnet的地址要一致。
应用当前flannel配置文件
itcast@master:~/working$ kubectl apply -f kube-flannel.yml
输出结果如下
root@master:~/working# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/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
安装flannel网络前 执行kubectl get nodes
输出结果如下
itcast@master:~/working$ kubectl get node
NAME STATUS ROLES AGE VERSION
master NotReady master 10m v1.13.1
安装flannel网络后 执行kubectl get nodes
输出结果如下
itcast@master:~/working$ kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready master 10m v1.13.1
此时master已经是Ready
状态了,表示已经配置成功了,那么我们就需要配置node来加入这个集群。
确认关闭swap
apt install -y selinux-utils
swapoff -a
禁止selinux
setenforce 0
确认关闭防火墙
ufw disable
启动k8s后台服务
# 启动kubelet 设置为开机自启动
$ sudo systemctl enable kubelet
# 启动k8s服务程序
$ sudo systemctl start kubelet
将master机器的/etc/kubernetes/admin.conf
传到到node1和node2
登录master
终端
#将admin.conf传递给node1
sudo scp /etc/kubernetes/admin.conf [email protected]:/home/itcast/
##我实际上 sudo scp /etc/kubernetes/admin.conf [email protected]:/root/
#将admin.conf传递给node2
sudo scp /etc/kubernetes/admin.conf [email protected]:/home/itcast/
登录node1
终端,创建基础kube配置文件环境
$ mkdir -p $HOME/.kube
$ sudo cp -i $HOME/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
node2
终端,创建基础kube配置文件环境$ mkdir -p $HOME/.kube
$ sudo cp -i $HOME/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
node1
和node2
分别连接master
加入master集群。这里用的是kubeadm join
指令$ sudo kubeadm join 192.168.236.177:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:e778d3665e52f5a680a87b00c6d54df726c2eda601c0db3bfa4bb198af2262a8
## 地下这个是我的
kubeadm join 192.168.236.177:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:77391e08bf9f802ce3655c2a8bcde21d301b52bea36e7e1a1cbbc1298ad7a4af
这里要注意,使用的hash应该是master
主机 kubeadm init
成功之后生成的hash码。
将master
中的kube-flannel.yml
分别传递给两个node
节点.
#将kube-flannel.yml传递给node1
sudo scp $HOME/working/kube-flannel.yml [email protected]:/home/itcast/
#将kube-flannel.yml传递给node2
sudo scp $HOME/working/kube-flannel.yml [email protected]:/home/itcast/
分别启动flannel
网络
itcast@node1:~$ kubectl apply -f kube-flannel.yml
itcast@node2:~$ kubectl apply -f kube-flannel.yml
itcast@node2:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 35m v1.13.1
node1 Ready <none> 2m23s v1.13.1
node2 Ready <none> 40s v1.13.1