$ kubectl get configmaps
$ kubectl get cm
No resources found in default namespace.
106. 基于字面量创建 configmap
1
2
$ kubectl create cm myconfigmap --from-literal=appname=myapp
configmap/myconfigmap created
107. 查看刚创建的 configmap 的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ kubectl get cm
NAME DATA AGE
myconfigmap 1 91s
$ kubectl describe cm
Name: myconfigmap
Namespace: default
Labels: <none>
Annotations: <none>
Data====appname:
----
myapp
Events: <none>
$ kubectl get cm -o yaml
apiVersion:v1kind:Podmetadata:creationTimestamp:nulllabels:run:secbusyboxname:secbusyboxspec:# Pod 设置securityContext:runAsUser:1000containers:- args:- /bin/sh- -c- sleep 3600;image:busybox# 容器层面的设置securityContext:runAsUser:2000name:secbusyboxresources:{}dnsPolicy:ClusterFirstrestartPolicy:Neverstatus:{}
创建并查看 Pod 中的 id 信息
1
2
3
4
5
6
7
8
9
10
11
$ kubectl delete pod secbusybox
pod "secbusybox" deleted
$ kubectl create -f busybox.yaml
pod/secbusybox created
$ kubectl exec -it secbusybox -- sh
/ $ id
# 和容器层面的设置一样,覆盖掉了 Pod 层面的设置uid=2000gid=0(root)/ $
apiVersion:v1kind:Podmetadata:creationTimestamp:nulllabels:run:nginxname:nginxspec:containers:- image:nginxname:nginxresources:# 设置 CPU 的使用限制requests:cpu:"0.5"limits:cpu:"1"dnsPolicy:ClusterFirstrestartPolicy:Neverstatus:{}
创建并查看 CPU 使用
1
2
3
4
5
6
7
$ kubectl create -f nginx.yaml
pod/nginx created
$ kubectl top pod
NAME CPU(cores) MEMORY(bytes)nginx 0m 0Mi
# 创建 Pod$ kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "echo I am from busybox pod; sleep 3600;"pod/busybox created
# 打印日志$ kubectl logs busybox
I am from busybox pod
138. 将上面 Pod 的日志存入文件 busybox-logs.txt
1
2
3
4
5
$ kubectl logs busybox > busybox-logs.txt
$ cat busybox-logs.txt
I am from busybox pod
kubectl get events --sort-by=.metadata.creationTimestamp
$ kubectl get events --sort-by=.metadata.creationTimestamp > file.log
$ cat file.log
LAST SEEN TYPE REASON OBJECT MESSAGE
16m Normal Killing pod/nginx Stopping container nginx
16m Normal Scheduled pod/nginx Successfully assigned default/nginx to vm-0-2-ubuntu
16m Normal Pulling pod/nginx Pulling image "nginx"15m Normal Started pod/nginx Started container nginx
15m Normal Created pod/nginx Created container nginx
15m Normal Pulled pod/nginx Successfully pulled image "nginx"
140. 创建 imgee 为 alpine 的 Pod 并执行命令每 5s 打印一次日志
1
2
3
4
5
6
7
8
9
10
# 创建 Pod$ kubectl run hello --image=alpine --restart=Never -- /bin/sh -c "while true; do echo 'Hi I am from Alpine'; sleep 5;done"pod/hello created
# 查看日志$ kubectl logs --follow hello
Hi I am from Alpine
Hi I am from Alpine
Hi I am from Alpine
# 创建 Pod$ kubectl create -f https://gist.githubusercontent.com/bbachi/212168375b39e36e2e2984c097167b00/raw/1fd63509c3ae3a3d3da844640fb4cca744543c1c/not-running.yml
pod/not-running created
# 查看 Pod$ kubectl get pod not-running
NAME READY STATUS RESTARTS AGE
not-running 0/1 ImagePullBackOff 0 7s
$ kubectl describe po not-running
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 39s default-scheduler Successfully assigned default/not-running to vm-0-2-ubuntu
Normal SandboxChanged 34s kubelet, vm-0-2-ubuntu Pod sandbox changed, it will be killed and re-created.
Normal Pulling 19s (x2 over 37s) kubelet, vm-0-2-ubuntu Pulling image "ngin" Warning Failed 16s (x2 over 35s) kubelet, vm-0-2-ubuntu Failed to pull image "ngin": rpc error: code= Unknown desc= Error response from daemon: pull access denied for ngin, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
Warning Failed 16s (x2 over 35s) kubelet, vm-0-2-ubuntu Error: ErrImagePull
Normal BackOff 1s (x4 over 33s) kubelet, vm-0-2-ubuntu Back-off pulling image "ngin" Warning Failed 1s (x4 over 33s) kubelet, vm-0-2-ubuntu Error: ImagePullBackOff
# 修改错误kubectl edit pod not-running
or
kubectl set image pod/not-running not-running=nginx
$ kubectl create -f https://gist.githubusercontent.com/bbachi/1f001f10337234d46806929d12245397/raw/84b7295fb077f15de979fec5b3f7a13fc69c6d83/problem-pod.yaml
namespace/namespace1 created
namespace/namespace2 created
namespace/namespace3 created
namespace/namespace4 created
pod/pod1 created
pod/pod2 created
pod/pod3 created
pod/pod4 created
$ kubectl get po --all-namespaces
$ kubectl get po -n namespace2
NAME READY STATUS RESTARTS AGE
pod2 0/1 ImagePullBackOff 0 46s
$ kubectl set image pod/pod2 pod2=nginx -n namespace2
pod/pod2 image updated
$ kubectl get po -n namespace2
NAME READY STATUS RESTARTS AGE
pod2 1/1 Running 0 55s
143. 查看所有 Pod 的 CPU 与内存使用并查找出 CPU 使用率最高的三个
1
2
3
4
5
6
7
8
9
10
11
12
$ kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3
kube-system kube-apiserver-vm-0-4-ubuntu 23m 308Mi
kube-system calico-node-njsrv 23m 29Mi
kube-system calico-node-ltpqg 17m 28Mi
# 将信息写入文件$ kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 > cpu-usage.txt
$ cat cpu-usage.txt
kube-system kube-apiserver-vm-0-4-ubuntu 23m 308Mi
kube-system calico-node-njsrv 23m 29Mi
kube-system calico-node-ltpqg 17m 28Mi
$ kubectl create -f nginx-svc.yaml
service/my-service created
146. 查看 Pod 与 Service
1
2
3
4
5
6
7
8
$ kubectl get po nginx --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 2m17s app=my-nginx
$ kubectl get svc my-service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
my-service ClusterIP 10.104.9.185 <none> 80/TCP 38s app=my-nginx
147. 删除 my-service,并使用 kubectl expose 命令重建
1
2
3
4
5
6
7
8
9
$ kubectl delete svc my-service
service "my-service" deleted
$ kubectl expose po nginx --port=80 --target-port=9376service/nginx exposed
$ kubectl get svc -l app=my-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.109.228.97 <none> 80/TCP 6s
148. 删除上面创建的 Service 并重建,指定 type 为 NodePort
1
2
3
4
5
6
$ kubectl delete svc nginx
service "nginx" deleted
$ kubectl expose po nginx --port=80 --type=NodePort
service/nginx exposed
149. 创建临时 busybox Pod 并通过 nginx Service 访问 Nginx Pod
1
2
3
4
5
6
7
8
9
10
$ kubectl get svc nginx -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx NodePort 10.108.87.189 <none> 80:31305/TCP 2m1s app=my-nginx
$ kubectl run busybox --image=busybox --restart=Never -it --rm -- wget -o- 10.108.87.189:80
Connecting to 10.108.87.189:80 (10.108.87.189:80)saving to 'index.html'index.html 100% |********************************|612 0:00:00 ETA
'index.html' saved
pod "busybox" deleted