|
2 | 2 | :toc:
|
3 | 3 | :icons:
|
4 | 4 | :linkattrs:
|
5 |
| -:linkcss: |
6 | 5 | :imagesdir: ../../resources/images
|
7 | 6 |
|
8 |
| -== Kubernetes API and CLI |
| 7 | +== Introduction |
9 | 8 |
|
10 | 9 | Now that we have a cluster up and running we can start exploring the Kubernetes CLI via the `kubectl` (pronounced "cube control") command.
|
11 | 10 |
|
@@ -42,6 +41,143 @@ It will show an output similar to:
|
42 | 41 |
|
43 | 42 | If you do not see this output, or receive an error, please ensure that you've followed the steps link:../102-your-first-cluster#multi-master[here] and have a validated cluster.
|
44 | 43 |
|
| 44 | +=== Create your first Pod |
| 45 | + |
| 46 | +This command instantiates an nginx container into your cluster, inside a pod: |
| 47 | + |
| 48 | + $ kubectl run nginx --image=nginx |
| 49 | + deployment "nginx" created |
| 50 | + |
| 51 | +Get the list of deployments: |
| 52 | + |
| 53 | + $ kubectl get deployments |
| 54 | + NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE |
| 55 | + nginx 1 1 1 0 41s |
| 56 | + |
| 57 | +Get the list of running pods: |
| 58 | + |
| 59 | + $ kubectl get pods |
| 60 | + NAME READY STATUS RESTARTS AGE |
| 61 | + nginx-4217019353-pmkzb 1/1 Running 0 1m |
| 62 | + |
| 63 | +Get additional details for the pod by using the `<pod-name>` from the above output: |
| 64 | + |
| 65 | +``` |
| 66 | +$ kubectl describe pod/nginx-4217019353-pmkzb |
| 67 | +kubectl describe pod/nginx-4217019353-pmkzb |
| 68 | +Name: nginx-4217019353-pmkzb |
| 69 | +Namespace: default |
| 70 | +Node: ip-172-20-87-91.us-east-2.compute.internal/172.20.87.91 |
| 71 | +Start Time: Fri, 01 Dec 2017 16:36:48 -0800 |
| 72 | +Labels: pod-template-hash=4217019353 |
| 73 | + run=nginx |
| 74 | +Annotations: kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"nginx-4217019353","uid":"e161abe9-d6f8-11e7-af8f-06c4465216f2","... |
| 75 | + kubernetes.io/limit-ranger=LimitRanger plugin set: cpu request for container nginx |
| 76 | +Status: Running |
| 77 | +IP: 100.96.7.19 |
| 78 | +Created By: ReplicaSet/nginx-4217019353 |
| 79 | +Controlled By: ReplicaSet/nginx-4217019353 |
| 80 | +Containers: |
| 81 | + nginx: |
| 82 | + Container ID: docker://2def6a6dc1594c337748abf6160ff81fab9fa6d734aca02b5f1afc4d395edc6b |
| 83 | + Image: nginx |
| 84 | + Image ID: docker-pullable://nginx@sha256:b81f317384d7388708a498555c28a7cce778a8f291d90021208b3eba3fe74887 |
| 85 | + Port: <none> |
| 86 | + State: Running |
| 87 | + Started: Fri, 01 Dec 2017 16:36:52 -0800 |
| 88 | + Ready: True |
| 89 | + Restart Count: 0 |
| 90 | + Requests: |
| 91 | + cpu: 100m |
| 92 | + Environment: <none> |
| 93 | + Mounts: |
| 94 | + /var/run/secrets/kubernetes.io/serviceaccount from default-token-cqht0 (ro) |
| 95 | +Conditions: |
| 96 | + Type Status |
| 97 | + Initialized True |
| 98 | + Ready True |
| 99 | + PodScheduled True |
| 100 | +Volumes: |
| 101 | + default-token-cqht0: |
| 102 | + Type: Secret (a volume populated by a Secret) |
| 103 | + SecretName: default-token-cqht0 |
| 104 | + Optional: false |
| 105 | +QoS Class: Burstable |
| 106 | +Node-Selectors: <none> |
| 107 | +Tolerations: node.alpha.kubernetes.io/notReady:NoExecute for 300s |
| 108 | + node.alpha.kubernetes.io/unreachable:NoExecute for 300s |
| 109 | +Events: |
| 110 | + Type Reason Age From Message |
| 111 | + ---- ------ ---- ---- ------- |
| 112 | + Normal Scheduled 46s default-scheduler Successfully assigned nginx-4217019353-pmkzb to ip-172-20-87-91.us-east-2.compute.internal |
| 113 | + Normal SuccessfulMountVolume 46s kubelet, ip-172-20-87-91.us-east-2.compute.internal MountVolume.SetUp succeeded for volume "default-token-cqht0" |
| 114 | + Normal Pulling 46s kubelet, ip-172-20-87-91.us-east-2.compute.internal pulling image "nginx" |
| 115 | + Normal Pulled 42s kubelet, ip-172-20-87-91.us-east-2.compute.internal Successfully pulled image "nginx" |
| 116 | + Normal Created 42s kubelet, ip-172-20-87-91.us-east-2.compute.internal Created container |
| 117 | + Normal Started 42s kubelet, ip-172-20-87-91.us-east-2.compute.internal Started container |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +By default, pods are created in a `default` namespace. In addition, a `kube-system` namespace is also reserved for Kubernetes system pods. A list of all the pods in `kube-system` namespace can be displayed as shown: |
| 122 | + |
| 123 | +``` |
| 124 | +$ kubectl get pods --namespace kube-system |
| 125 | +NAME READY STATUS RESTARTS AGE |
| 126 | +dns-controller-3497129722-4pxd6 1/1 Running 0 28d |
| 127 | +etcd-server-events-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 128 | +etcd-server-events-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 129 | +etcd-server-events-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 130 | +etcd-server-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 131 | +etcd-server-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 132 | +etcd-server-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 133 | +kube-apiserver-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 134 | +kube-apiserver-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 135 | +kube-apiserver-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 136 | +kube-controller-manager-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 137 | +kube-controller-manager-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 138 | +kube-controller-manager-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 139 | +kube-dns-1311260920-jgl0m 3/3 Running 0 28d |
| 140 | +kube-dns-1311260920-tvpmp 3/3 Running 0 28d |
| 141 | +kube-dns-autoscaler-1818915203-5kxrb 1/1 Running 0 28d |
| 142 | +kube-proxy-ip-172-20-105-158.us-east-2.compute.internal 1/1 Running 0 28d |
| 143 | +kube-proxy-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 144 | +kube-proxy-ip-172-20-127-251.us-east-2.compute.internal 1/1 Running 0 28d |
| 145 | +kube-proxy-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 146 | +kube-proxy-ip-172-20-63-150.us-east-2.compute.internal 1/1 Running 0 28d |
| 147 | +kube-proxy-ip-172-20-71-14.us-east-2.compute.internal 1/1 Running 0 28d |
| 148 | +kube-proxy-ip-172-20-87-91.us-east-2.compute.internal 1/1 Running 0 28d |
| 149 | +kube-proxy-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 150 | +kube-scheduler-ip-172-20-124-26.us-east-2.compute.internal 1/1 Running 0 28d |
| 151 | +kube-scheduler-ip-172-20-52-35.us-east-2.compute.internal 1/1 Running 0 28d |
| 152 | +kube-scheduler-ip-172-20-94-153.us-east-2.compute.internal 1/1 Running 0 28d |
| 153 | +tiller-deploy-1114875906-k2pj2 1/1 Running 0 28d |
| 154 | +``` |
| 155 | +Again, the exact output may vary but your results should look similar to these. |
| 156 | + |
| 157 | +=== Get logs from the pod |
| 158 | + |
| 159 | +Logs from the pod can be obtained (a fresh nginx does not have logs - check again later once you have accessed the service): |
| 160 | + |
| 161 | + $ kubectl logs <pod-name> |
| 162 | + |
| 163 | +=== Execute a shell on the running pod |
| 164 | + |
| 165 | +This command will open a TTY to a shell in your pod: |
| 166 | + |
| 167 | + $ kubectl get pods |
| 168 | + $ kubectl exec -it <pod-name> /bin/bash |
| 169 | + |
| 170 | +This opens a bash shell and allows you to look around the filesystem of the container. |
| 171 | + |
| 172 | +=== Clean up |
| 173 | + |
| 174 | +Delete all the Kubernetes resources created so far: |
| 175 | + |
| 176 | + $ kubectl delete deployment/nginx |
| 177 | + |
| 178 | + |
| 179 | +In the next sections, we will go into more detail about Pods, Deployments, and other commonly used Kubernetes objects. |
| 180 | + |
45 | 181 | == Pods
|
46 | 182 |
|
47 | 183 | A Pod is the smallest deployable unit that can be created, scheduled, and managed. It’s a logical collection of containers that belong to an application. Pods are created in a namespace. All containers in a pod share the namespace, volumes and networking stack. This allows containers in the pod to "`find`" each other and communicate using `localhost`.
|
|
0 commit comments