Kubernetes 101

    等到容器变成 Running 后,就可以用 kubectl 命令来操作它了,比如

    • kubectl get - 类似于 docker ps,查询资源列表
    • kubectl describe - 类似于 docker inspect,获取资源的详细信息
    • kubectl logs - 类似于 docker logs,获取容器的日志
    • kubectl exec - 类似于 docker exec,在容器内执行一个命令
    1. $ kubectl get pods
    2. NAME READY STATUS RESTARTS AGE
    3. nginx-app-4028413181-cnt1i 1/1 Running 0 6m
    4. $ kubectl exec nginx-app-4028413181-cnt1i ps aux
    5. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
    6. root 1 0.0 0.5 31736 5108 ? Ss 00:19 0:00 nginx: master process nginx -g daemon off;
    7. nginx 5 0.0 0.2 32124 2844 ? S 00:19 0:00 nginx: worker process
    8. root 18 0.0 0.2 17500 2112 ? Rs 00:25 0:00 ps aux
    9. $ kubectl describe pod nginx-app-4028413181-cnt1i
    10. Name: nginx-app-4028413181-cnt1i
    11. Namespace: default
    12. Node: boot2docker/192.168.64.12
    13. Start Time: Tue, 06 Sep 2016 08:18:41 +0800
    14. Labels: pod-template-hash=4028413181
    15. run=nginx-app
    16. Status: Running
    17. IP: 172.17.0.3
    18. Controllers: ReplicaSet/nginx-app-4028413181
    19. Containers:
    20. nginx-app:
    21. Container ID: docker://4ef989b57d0a7638ad9c5bbc22e16d5ea5b459281c77074fc982eba50973107f
    22. Image: nginx
    23. Image ID: docker://sha256:4efb2fcdb1ab05fb03c9435234343c1cc65289eeb016be86193e88d3a5d84f6b
    24. Port: 80/TCP
    25. State: Running
    26. Started: Tue, 06 Sep 2016 08:19:30 +0800
    27. Ready: True
    28. Restart Count: 0
    29. Environment Variables: <none>
    30. Conditions:
    31. Type Status
    32. Initialized True
    33. Ready True
    34. Volumes:
    35. default-token-9o8ks:
    36. Type: Secret (a volume populated by a Secret)
    37. SecretName: default-token-9o8ks
    38. Events:
    39. FirstSeen LastSeen Count From SubobjectPath Type Reason Message
    40. --------- -------- ----- ---- ------------- -------- ------ -------
    41. 8m 8m 1 {default-scheduler} Normal Scheduled Successfully assigned nginx-app-4028413181-cnt1i to boot2docker
    42. 8m 8m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Pulling pulling image "nginx"
    43. 7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Pulled Successfully pulled image "nginx"
    44. 7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Created Created container with docker id 4ef989b57d0a
    45. 7m 7m 1 {kubelet boot2docker} spec.containers{nginx-app} Normal Started Started container with docker id 4ef989b57d0a
    46. $ curl http://172.17.0.3
    47. <!DOCTYPE html>
    48. <html>
    49. <head>
    50. <title>Welcome to nginx!</title>
    51. <style>
    52. body {
    53. width: 35em;
    54. margin: 0 auto;
    55. font-family: Tahoma, Verdana, Arial, sans-serif;
    56. }
    57. </style>
    58. </head>
    59. <body>
    60. <h1>Welcome to nginx!</h1>
    61. <p>If you see this page, the nginx web server is successfully installed and
    62. working. Further configuration is required.</p>
    63. <p>For online documentation and support please refer to
    64. <a href="http://nginx.org/">nginx.org</a>.<br/>
    65. Commercial support is available at
    66. <a href="http://nginx.com/">nginx.com</a>.</p>
    67. <p><em>Thank you for using nginx.</em></p>
    68. </body>
    69. </html>
    70. $ kubectl logs nginx-app-4028413181-cnt1i
    71. 127.0.0.1 - - [06/Sep/2016:00:27:13 +0000] "GET / HTTP/1.0" 200 612 "-" "-" "-"

    前面提到,kubectl run 并不是直接创建一个 Pod,而是先创建一个 Deployment 资源(replicas=1),再由与 Deployment 关联的 ReplicaSet 来自动创建 Pod,这等价于这样一个配置:

    1. kind: Deployment
    2. metadata:
    3. labels:
    4. run: nginx-app
    5. name: nginx-app
    6. namespace: default
    7. spec:
    8. replicas: 1
    9. selector:
    10. matchLabels:
    11. strategy:
    12. rollingUpdate:
    13. maxSurge: 1
    14. maxUnavailable: 1
    15. type: RollingUpdate
    16. template:
    17. metadata:
    18. labels:
    19. run: nginx-app
    20. spec:
    21. containers:
    22. - image: nginx
    23. name: nginx-app
    24. ports:
    25. - containerPort: 80
    26. protocol: TCP
    27. dnsPolicy: ClusterFirst
    28. restartPolicy: Always

    Kubernetes volume 支持非常多的插件,可以根据实际需要来选择:

    • emptyDir
    • hostPath
    • gcePersistentDisk
    • awsElasticBlockStore
    • nfs
    • iscsi
    • flocker
    • glusterfs
    • rbd
    • cephfs
    • gitRepo
    • secret
    • persistentVolumeClaim
    • downwardAPI
    • azureFileVolume
    • vsphereVolume
    1. $ kubectl expose deployment nginx-app --port=80 --target-port=80 --type=NodePort
    2. service "nginx-app" exposed
    3. $ kubectl describe service nginx-app
    4. Name: nginx-app
    5. Namespace: default
    6. Labels: run=nginx-app
    7. Selector: run=nginx-app
    8. Type: ClusterIP
    9. IP: 10.0.0.66
    10. Port: <unset> 80/TCP
    11. NodePort: <unset> 30772/TCP
    12. Endpoints: 172.17.0.3:80
    13. Session Affinity: None

    这样,在 cluster 内部就可以通过 http://10.0.0.66 和 来访问 nginx-app。而在 cluster 外面,则只能通过 http://node-ip:30772 来访问。