使用CLI方式可以通过kubectl对Kubernetes进行操作,同时也使用Restful API直接进行操作。这篇文章介绍一下两种方式下进行pod的管理的方法。
环境准备
快速环境搭建建议使用单机版Kubernetes的安装脚本,一键安装,详情可参看:
事前准备
使用docker pull命令下载nginx:latest镜像用于试验。
[root@host132 ~]# docker pull nginx:latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete
a17e64cfe253: Pull complete
e1288088c7a8: Pull complete
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
[root@host132 ~]#
kubectl 方式
创建新的pod
- 创建pod所用的yaml文件
[root@host132 yaml]# cat pod-demo1.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
...
[root@host132 yaml]#
使用kubectl可以直接使用如下命令创建新的pod(名为nginx的pod)
kubectl命令:kubectl create -f pod-demo1.yaml
执行示例信息
[root@host132 yaml]# kubectl get pods
No resources found.
[root@host132 yaml]# kubectl create -f pod-demo1.yaml
pod/nginx created
[root@host132 yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 4s
[root@host132 yaml]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 10s 10.254.224.4 192.168.163.132 <none> <none>
[root@host132 yaml]#
获取所有pod列表信息
使用kubectl可以直接使用如下命令获取pod列表信息
kubectl命令:kubectl get pods -o wide
执行示例信息
[root@host132 ~]# kubectl get pods -o wide
No resources found.
[root@host132 ~]#
获取指定pod信息
使用kubectl可以直接使用如下命令获取指定pod信息(名为nginx的pod)
kubectl命令:kubectl get pods nginx -o wide
执行示例信息
[root@host132 yaml]# kubectl get pods nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 3m26s 10.254.224.4 192.168.163.132 <none> <none>
[root@host132 yaml]#
删除指定的pod
使用kubectl可以直接使用如下命令删除指定的pod(名为nginx的pod)
kubectl命令:kubectl delete pods nginx
执行示例信息
[root@host132 yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 7m36s
[root@host132 yaml]# kubectl delete pods nginx
pod "nginx" deleted
[root@host132 yaml]#
[root@host132 yaml]# kubectl get pods
No resources found.
[root@host132 yaml]#
从运行中的pod中获得yaml或者json的配置信息
使用kubectl可以直接使用如下命令获得运行中的pod的yaml或者json的配置详细信息>kubectl命令(获取yaml信息):kubectl get pods nginx -o yaml
kubectl命令(获取json信息):kubectl get pods nginx -o json
[root@host132 yaml]# kubectl get pods nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2019-08-31T03:38:02Z"
name: nginx
namespace: default
resourceVersion: "37952"
selfLink: /api/v1/namespaces/default/pods/nginx
uid: 00e4d249-bf75-4506-a2dc-a3f51d0bd795
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx-container
ports:
- containerPort: 80
protocol: TCP
resources: {
}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-sr2zb
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: 192.168.163.132
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {
}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-sr2zb
secret:
defaultMode: 420
secretName: default-token-sr2zb
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-08-31T03:38:02Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2019-08-31T03:38:06Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2019-08-31T03:38:06Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2019-08-31T03:38:02Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://7f9635fbe242e255f52078132ad172364b266a9c52f837f2ca0347a2583e6abd
image: nginx:latest
imageID: docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
lastState: {
}
name: nginx-container
ready: true
restartCount: 0
state:
running:
startedAt: "2019-08-31T03:38:06Z"
ho