$kubectlgetpodsNAMEREADYSTATUSRESTARTSAGEnginx-app-4028413181-cnt1i1/1Running06m$kubectlexecnginx-app-4028413181-cnt1i--psauxUSERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMANDroot10.00.5317365108?Ss00:190:00nginx:masterprocessnginx-gdaemonoff;nginx50.00.2321242844?S00:190:00nginx:workerprocessroot180.00.2175002112?Rs00:250:00psaux$kubectldescribepodnginx-app-4028413181-cnt1iName:nginx-app-4028413181-cnt1iNamespace:defaultNode:boot2docker/192.168.64.12StartTime:Tue,06Sep201608:18:41+0800Labels:pod-template-hash=4028413181 run=nginx-appStatus:RunningIP:172.17.0.3Controllers:ReplicaSet/nginx-app-4028413181Containers:nginx-app:ContainerID:docker://4ef989b57d0a7638ad9c5bbc22e16d5ea5b459281c77074fc982eba50973107fImage:nginxImageID:docker://sha256:4efb2fcdb1ab05fb03c9435234343c1cc65289eeb016be86193e88d3a5d84f6bPort:80/TCPState:RunningStarted:Tue,06Sep201608:19:30+0800Ready:TrueRestartCount:0EnvironmentVariables:<none>Conditions:TypeStatusInitializedTrueReadyTruePodScheduledTrueVolumes:default-token-9o8ks:Type:Secret (a volumepopulatedbyaSecret)SecretName:default-token-9o8ksQoSTier:BestEffortEvents: FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
8m 8m 1 {default-scheduler} Normal Scheduled Successfully assigned nginx-app-4028413181-cnt1i to boot2docker
8m 8m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Pulling pulling image "nginx"
7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Pulled Successfully pulled image "nginx"
7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Created Created container with docker id 4ef989b57d0a
7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Started Started container with docker id 4ef989b57d0a
$curlhttp://172.17.0.3<!DOCTYPEhtml><html><head><title>Welcome to nginx!</title><style>body{width:35em;margin:0auto;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.Furtherconfigurationisrequired.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercialsupportisavailableat<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>$kubectllogsnginx-app-4028413181-cnt1i127.0.0.1-- [06/Sep/2016:00:27:13 +0000]"GET / HTTP/1.0"200612"-""-""-"
使用 yaml 定义 Pod
上面是通过 kubectl run 来启动了第一个 Pod,但是 kubectl run 并不支持所有的功能。在 Kubernetes 中,更经常使用 yaml 文件来定义资源,并通过 kubectl create -f file.yaml 来创建资源。比如,一个简单的 nginx Pod 可以定义为:
前面虽然创建了 Pod,但是在 kubernetes 中,Pod 的 IP 地址会随着 Pod 的重启而变化,并不建议直接拿 Pod 的 IP 来交互。那如何来访问这些 Pod 提供的服务呢?使用 Service。Service 为一组 Pod(通过 labels 来选择)提供一个统一的入口,并为它们提供负载均衡和自动服务发现。比如,可以为前面的 nginx-app 创建一个 service: