直接kubectl get nodes会报错:The connection to the server localhost:8080 was refused - did you specify the right host or port?,出现这个问题的原因是kubectl命令需要使用kubernetes-admin来运行,上文输出中提示了解决办法:
1 2 3 4 5 6 7 8
# 创建必要文件 [root@tbh-0001 ~]# mkdir -p $HOME/.kube [root@tbh-0001 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@tbh-0001 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config # 成功运行 [root@tbh-0001 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION tbh-0001 NotReady tbh-0001 5m7s v1.17.4
下面的操作只需要在node节点上执行即可
在tbh-0001上执行后会输出node节点加入集群所需要输入的命令,直接复制即可
1 2 3 4 5 6 7 8 9 10 11
# 将node节点加入集群 [root@tbh-0002 ~]# kubeadm join 192.168.0.181:6443 --token 1p2izv.b6arlpik2red8wyq \ --discovery-token-ca-cert-hash \ sha256:514ef66c3a916ef5a9174e3414140d668e24db612ebd24ca068eedcc288d07e1 # 查看集群状态 此时的集群状态为NotReady,这是因为还没有配置网络插件 [root@tbh-0001 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION tbh-0001 NotReady tbh-00018m10s v1.17.4 tbh-0002 NotReady <none> 11s v1.17.4 tbh-0003 NotReady <none> 4s v1.17.4
# 查看服务状态 [root@tbh-0001 ~]# kubectl get pods,service NAME READY STATUS RESTARTS AGE pod/nginx-6867cdf567-flcfc0/1 ContainerCreating 013s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m service/nginx NodePort 10.99.122.158 <none> 80:31450/TCP 7s
# 4 最后访问下部署的nginx服务 [root@tbh-0001 ~]# curl 192.168.0.181:31450 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you forusing nginx.</em></p> </body> </html>