前面两个教程我们已经使用kubekit将K8S搭建起来了。但是,没有将实际使用中需要在K8S上部署我们的容器创建起来的教程,都是耍流氓。所以,经过几番折腾,我回来给自己洗白了。之前一直卡在创建第一个容器上,是因为前面部署的步骤其实是有问题的,只是没有直接显示出来,具体已经在之前的相应博客中进行了更新。
现在我们来说一下如何使用K8S创建我们的第一个容器。都是在终端上劈里啪啦敲命令的,所以就直接上代码了。[[email protected] ~]# kubectl run my-nginx --image=nginx --replicas=2 --port=80 #创建我们的第一个容器命令。这里应该没有作映射到容器外部访问端口,后续我再加强研究
deployment "my-nginx" created
[[email protected] ~]# kubectl get pods #查看我们自己创建的容器运行状态
NAME READY STATUS RESTARTS AGE
my-nginx-4293833666-9vn38 1/1 Running 0 3m
my-nginx-4293833666-bl672 1/1 Running 0 3m
[[email protected] ~]# kubectl get pods --all-namespaces -owide #查看k8S所有容器的状态NAMESPACE NAME READY STATUS RESTARTS AGE IP NODEdefault my-nginx-4293833666-9vn38 1/1 Running 0 5m 10.96.1.5 141node #这个ip我们后面会用到default my-nginx-4293833666-bl672 1/1 Running 0 5m 10.96.1.4 141nodekube-system etcd-bogon 1/1 Running 1 22h 192.168.2.140 bogonkube-system kube-apiserver-bogon 1/1 Running 1 22h 192.168.2.140 bogonkube-system kube-controller-manager-bogon 1/1 Running 1 22h 192.168.2.140 bogonkube-system kube-dns-2425271678-zh6r3 3/3 Running 3 1d 10.96.0.4 bogonkube-system kube-flannel-ds-k7qxx 2/2 Running 1 7m 192.168.2.141 141nodekube-system kube-flannel-ds-kwmk3 2/2 Running 3 1d 192.168.2.140 bogonkube-system kube-proxy-jm9d3 1/1 Running 0 7m 192.168.2.141 141nodekube-system kube-proxy-vz8pr 1/1 Running 1 1d 192.168.2.140 bogonkube-system kube-scheduler-bogon 1/1 Running 1 22h 192.168.2.140 bogonkube-system kubernetes-dashboard-3313488171-2b06x 1/1 Running 1 1d 10.96.0.5 bogon[[email protected] ~]# kubectl describe pod my-nginx-4293833666-lqvjr #查看某个容器的运行日志信息,可用于排错。比如说,下面这个报错No nodes are available。于是怀疑K8S添加的node有问题.所以有了下一条命令Name: my-nginx-4293833666-lqvjrNamespace: defaultNode: <none>。。。。Events: FirstSeen LastSeen Count From SubObjectPath Type Reason Message --------- -------- ----- ---- ------------- -------- ------ ------- 2h 9s 589 default-scheduler Warning FailedScheduling No nodes are available that match all of the following predicates:: PodToleratesNodeTaints (1). [[email protected] ~]# kubectl get nodes #查看K8S的node。之前报错是因为只有一个bogon(这个就是我的K8S服务器本身)。但是在kubekit界面,我明明已经添加了141这个node了,状态也为Deployed。思考再三,看到默认的bogon主机名,突然想到难道是主机名一致,导致识别不到新加的节点。只能试试看了,果断重装并修改hostname,不要问我什么不直接修改hostname就行还那么麻烦要重装。原因很简单,因为k8s已经连上去进行安装了,直接修改hostname可能会导致和之前安装的内容冲突等意想不到的情况。保险起见,直接重装是最明智的选择。毕竟作为一个k8s新手,最好还是不要自己给自己找多余的麻烦了,否则各种报错,一不小心你就会怀疑人生了。果然,修改好主机名后,顺利创建了第一个k8s容器NAME STATUS AGE VERSION141node Ready 57m v1.7.2bogon Ready 2d v1.7.2[[email protected] ~]# ping 10.96.1.5 #正常操作,先ping一下通不通PING 10.96.1.5 (10.96.1.5) 56(84) bytes of data.64 bytes from 10.96.1.5: icmp_seq=1 ttl=63 time=0.520 ms64 bytes from 10.96.1.5: icmp_seq=2 ttl=63 time=0.475 ms^C--- 10.96.1.5 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1000msrtt min/avg/max/mdev = 0.475/0.497/0.520/0.031 ms[[email protected] ~]# curl 10.96.1.5 #访问部署的Nginx服务,可以看到服务正常<!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 andworking. 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 for using nginx.</em></p></body></html>
原文地址:https://www.cnblogs.com/biaopei/p/12052168.html
时间: 2024-10-20 21:49:59