Kubernetes 101
https://kubernetes.io/docs/user-guide/walkthrough/
This is my self-learning notes for Kubernetes 101.
"For Kubernetes 101, we will cover kubectl, pods, volumes, and multiple containers."
Commands
kubectl create -f pod-nginx.yaml
kubectl get pod
kubectl describe pods/nginx
Create a POD
- Pod definition for pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
- Create pod on your cluster
kubectl create -f pod-nginx.yaml
root@elx744647vq:/home/elluffn/k8s# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2d
root@elx744647vq:/home/elluffn/k8s#
- Get pods
root@elx744647vq:/home/elluffn/k8s# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2d
- Describe a pod
root@elx744647vq:/home/elluffn/k8s# kubectl describe pods/nginx
Name: nginx
Namespace: default
Node: elx744647vq/146.11.23.8
Start Time: Tue, 17 Oct 2017 17:09:57 +0800
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.0.19
Containers:
nginx:
Container ID: docker://8f9f7f682002716b01c39a39def5c77984934c1727e0e675bab83e87c4846d17
Image: nginx:1.7.9
Image ID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port: 80/TCP
State: Running
Started: Tue, 17 Oct 2017 17:14:31 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-9sqlp (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-9sqlp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-9sqlp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.alpha.kubernetes.io/notReady:NoExecute for 300s
node.alpha.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
root@elx744647vq:/home/elluffn/k8s#
Get a shell to a container
https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/
root@elx744647vq:/home/elluffn/k8s# kubectl help exec
Execute a command in a container.
Examples:
# Get output from running 'date' from pod 123456-7890, using the first container by default
kubectl exec 123456-7890 date
# Get output from running 'date' in ruby-container from pod 123456-7890
kubectl exec 123456-7890 -c ruby-container date
# Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890
# and sends stdout/stderr from 'bash' back to the client
kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il
# List contents of /usr from the first container of pod 123456-7890 and sort by modification time.
# If the command you want to execute in the pod has any flags in common (e.g. -i),
# you must use two dashes (--) to separate your command's flags/arguments.
# Also note, do not surround your command and its flags/arguments with quotes
# unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr").
kubectl exec 123456-7890 -i -t -- ls -t /usr
Options:
-c, --container='': Container name. If omitted, the first container in the pod will be chosen
-p, --pod='': Pod name
-i, --stdin=false: Pass stdin to the container
-t, --tty=false: Stdin is a TTY
Usage:
kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
root@elx744647vq:/home/elluffn/k8s# kubectl exec nginx date
Thu Oct 19 09:44:08 UTC 2017
root@elx744647vq:/home/elluffn/k8s#
Create a busybox container via kubectl
$ kubectl run busybox --image=busybox --restart=Never --tty -i --generator=run-pod/v1 --env "POD_IP=$(kubectl get pod nginx -o go-template='{{.status.podIP}}')"
u@busybox$ wget -qO- http://$POD_IP # Run in the busybox container
u@busybox$ exit # Exit the busybox container
$ kubectl delete pod busybox # Clean up the pod we created with "kubectl run"
网友评论