Kubernetes Pods

当你创建了一个Deployment后,Kubernetes将创建一个pod来托管应用实例。Pod是一个Kubernetes抽象,它表示一组或多个应用程序容器(如Docker),以及这些容器的一些共享资源。这些资源包括:

  • 共享存储,Volumes
  • 网络,如一个唯一的集群IP地址
  • 怎样启动每一个容器的信息,比如容器镜像版本或使用的具体端口

Pod为特定应用程序的“逻辑主机”建模,可以包含相对紧密耦合的不同应用程序容器。举个例子,一个Pod可能既包含你的Node.js APP,也能包含提供数据给APP服务端发布的容器。Pod中的容器分享IP地址和端口空间,始终位于同一位置并共同调度,并在同一节点上的共享上下文中运行。
Pod在Kubernetes平台中是一个原子单元。当我们创建Deployment时,Deployment创建包含容器的Pod们(不可以直接创建容器)。每一个Pod都和被调度的节点绑定,直到终止(根据重新启动策略)或删除为止。如果节点出现故障,则在集群中的其他可用节点上调度相同的Pods。
A Pod is a group of one or more application containers (such as Docker) and includes shared storage (volumes), IP address and information about how to run them.

1

2

  • kubectl get - list resources
  • kubectl describe - show detailed information about a resource
  • kubectl logs - print the logs from a container in a pod
  • kubectl exec - execute a command on a container in a pod

A node is a worker machine in Kubernetes and may be a VM or physical machine, depending on the cluster. Multiple Pods can run on one Node.

$ kubectl get pods
NAME                                  READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-fb5c67579-6rpgl   1/1     Running   0          9s
$ kubectl describe pods
Name:         kubernetes-bootcamp-fb5c67579-6rpgl
Namespace:    default
Priority:     0
Node:         minikube/172.17.0.43
Start Time:   Wed, 13 Oct 2021 06:53:10 +0000
Labels:       app=kubernetes-bootcamp
              pod-template-hash=fb5c67579
Annotations:  <none>
Status:       Running
IP:           172.18.0.6
IPs:
  IP:           172.18.0.6
Controlled By:  ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://9cce542e4e99443f82fca8ada434c0ae8a54fc87dac81b9bb16ef8eaee3cbde2
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 13 Oct 2021 06:53:13 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hg9sl (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-hg9sl:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-hg9sl
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  30s   default-scheduler  Successfully assigned default/kubernetes-bootcamp-fb5c67579-6rpgl to minikube
  Normal  Pulled     27s   kubelet            Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
  Normal  Created    27s   kubelet            Created container kubernetes-bootcamp
  Normal  Started    27s   kubelet            Started container kubernetes-bootcamp
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-fb5c67579-6rpgl
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-6rpgl | v=1
$ kubectl logs $POD_NAME
Kubernetes Bootcamp App Started At: 2021-10-13T06:53:13.842Z | Running On:  kubernetes-bootcamp-fb5c67579-6rpgl 

Running On: kubernetes-bootcamp-fb5c67579-6rpgl | Total Requests: 1 | App Uptime: 143.844 seconds | Log Time: 2021-10-13T06:55:37.686Z
$ kubectl exec $POD_NAME -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-fb5c67579-6rpgl
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
$ kubectl exec -ti $POD_NAME -- bash
root@kubernetes-bootcamp-fb5c67579-6rpgl:/# cat server.js
var http = require('http');
var requests=0;
var podname= process.env.HOSTNAME;
var startTime;
var host;
var handleRequest = function(request, response) {
  response.setHeader('Content-Type', 'text/plain');
  response.writeHead(200);
  response.write("Hello Kubernetes bootcamp! | Running on: ");
  response.write(host);
  response.end(" | v=1\n");
  console.log("Running On:" ,host, "| Total Requests:", ++requests,"| App Uptime:", (new Date() - startTime)/1000 , "seconds", "| Log Time:",new Date());
}
var www = http.createServer(handleRequest);
www.listen(8080,function () {
    startTime = new Date();;
    host = process.env.HOSTNAME;
    console.log ("Kubernetes Bootcamp App Started At:",startTime, "| Running On: " ,host, "\n" );
});
root@kubernetes-bootcamp-fb5c67579-6rpgl:/# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-6rpgl | v=1
root@kubernetes-bootcamp-fb5c67579-6rpgl:/# exit

Q.E.D.


南七技校练习时长四年的练习生