API Server提供了RESTful风格的编程接口,其管理的资源是Kubernetes API中的端点,用于存储某种API对象的集合,例如,内置Pod资源是包含了所有Pod对象的集合。资源对象是用于表现集群状态的实体,常用于描述应于哪个节点进行容器化应用、需要为其配置什么资源以及应用程序的管理策略等,例如,重启、升级及容错机制。另外,一个对象也是一种“意向记录“——一旦创建,Kubernetes就需要一直确保对象始终存在。Pod、Deployment和Service等都是最常用的核心对象。
[root@k8s-master ~]# kubectl get deployment #查看所有deployment控制器对象 NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 66s ###字段说明: NAME 资源对象名称 READY 期望由当前控制器管理的Pod对象副本数及当前已有的Pod对象副本数 UP-TO-DATE 更新到最新版本定义的Pod对象的副本数量,在控制器的滚动更新模式下,表示已经完成版本更新的Pod对象的副本数量 AVAILABLE 当前处于可用状态的Pod对象的副本数量,即可正常提供服务的副本数。 AGE Pod的存在时长
说明:Deployment资源对象通过ReplicaSet控制器实例完成对Pod对象的控制,而非直接控制。另外,通过控制器创建的Pod对象都会被自动附加一个标签。格式为“run=<Controller_Name>”。 [root@k8s-master ~]# kubectl get deployment -o wide #查看deployment控制器对象的详细信息 NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx 1/1 1 1 69m nginx nginx:1.12 run=nginx
[root@k8s-master ~]# kubectl get pods #查看pod资源 NAME READY STATUS RESTARTS AGE nginx-685cc95cd4-9z4f4 1/1 Running 0 72m
[root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-685cc95cd4-9z4f4 1/1 Running 0 73m 10.244.1.12 k8s-node1 <none> <none> ###字段说明: NAME pode资源对象名称 READY pod中容器进程初始化完成并能够正常提供服务时即为就绪状态,此字段用于记录处于就绪状态的容器数量 STATUS pod的当前状态,其值有Pending、Running、Succeeded、Failed和Unknown等其中之一 RESTARTS Pod重启的次数 IP pod的IP地址,通常由网络插件自动分配 NODE pod被分配的节点。
[root@k8s-master ~]# kubectl get pods -o wide #查看pod详细信息 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-685cc95cd4-9z4f4 1/1 Running 0 88m 10.244.1.12 k8s-node1 <none> <none>
[root@k8s-master ~]# curl 10.244.1.12 #kubernetes集群的master节点上访问 <!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 for using nginx.</em></p> </body> </html>
[root@k8s-node2 ~]# curl 10.244.1.12 #kubernetes集群的node节点上访问 <!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 for using nginx.</em></p> </body> </html>
[root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-685cc95cd4-9z4f4 1/1 Running 0 99m 10.244.1.12 k8s-node1 <none> <none>
[root@k8s-master ~]# kubectl delete pods nginx-685cc95cd4-9z4f4 #删除上面的pod pod "nginx-685cc95cd4-9z4f4" deleted
[root@k8s-master ~]# kubectl get pods -o wide #可以看出,当上面pod刚删除,接着deployment控制器又马上创建了一个新的pod,且这次分配在k8s-node2节点上了。 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-685cc95cd4-z5z9p 1/1 Running 0 89s 10.244.2.14 k8s-node2 <none> <none>
[root@k8s-master ~]# curl 10.244.1.12 #访问之前的pod,可以看到已经不能访问 curl: (7) Failed connect to 10.244.1.12:80; 没有到主机的路由 [root@k8s-master ~]# [root@k8s-master ~]# curl 10.244.2.14 #访问新的pod,可以正常访问 <!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 for using nginx.</em></p> </body> </html>
[root@k8s-master ~]# kubectl get svc #查看service对象。或者kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h nginx-svc ClusterIP 10.109.54.136 <none> 80/TCP 41s
# master节点上通过ServiceIP进行访问 [root@k8s-master ~]# curl 10.109.54.136 <!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 for using nginx.</em></p> </body> </html> #新建一个客户端pod进行访问,这里这个客户端使用busybox镜像,且pod副本数量为1个,-it表示进入终端模式。--restart=Never,表示从不重启。 [root@k8s-master ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never If you don't see a command prompt, try pressing enter. / # wget -O - -q 10.109.54.136 #访问上面创建的(service)nginx-svc的IP <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ...... / # / # wget -O - -q nginx-svc #访问上面创建的(service)名称nginx-svc <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>