这篇文章以Pod为例,介绍在Kubernetes中创建资源的基本方式。
方式1: 使用命令行方式创建资源
以busybox的镜像为例,使用如下命令即可创建pod
执行命令:kubectl run --generator=run-pod/v1 --image=busybox:latest testbox – sleep 1000
[root@host131 test]# kubectl run --generator=run-pod/v1 --image=busybox:latest testbox -- sleep 1000
pod/testbox created
[root@host131 test]#
结果确认
[root@host131 test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testbox 1/1 Running 0 12s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 test]#
进入此pod之内,然后确认sleep进程
[root@host131 test]# kubectl exec -it testbox sh
/ # hostname
testbox
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sleep 1000
6 root 0:00 sh
12 root 0:00 ps -ef
/ #
方式2: YAML文件
使用YAML文件进行Pod的创建,是最为常见的方式,比如此处可以使用如下的YAML进行Pod的创建
[root@host131 test]# cat testbox.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: testbox
namespace: default
spec:
containers:
- name: testbox-host
image: busybox:latest
command: ["sleep"]
args: ["1000"]
...
[root@host131 test]#
PS: 详细YAML文件格式说明请参看:https://blog.csdn.net/liumiaocn/category_9103221.html
创建和Pod结果确认执行日志如下所示:
[root@host131 test]# kubectl get pods
No resources found in default namespace.
[root@host131 test]# kubectl create -f testbox.yaml
pod/testbox created
[root@host131 test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testbox 1/1 Running 0 9s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 test]#
[root@host131 test]# kubectl exec -it testbox sh
/ # hostname
testbox
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sleep 1000
6 root 0:00 sh
12 root 0:00 ps -ef
/ #
方式3: JSON文件
使用JSON文件也可以进行Pod的创建,比如此处可以使用如下的JSON进行Pod的创建
[root@host131 test]# cat testbox.json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "testbox",
"namespace": "default"
},
"spec": {
"containers": [
{
"name": "testbox-host",
"image": "busybox:latest",
"command": [
"sleep"
],
"args": [
"1000"
]
}
]
}
}
[root@host131 test]#
创建和Pod结果确认执行日志如下所示:
[root@host131 test]# kubectl get pods
No resources found in default namespace.
[root@host131 test]# kubectl create -f testbox.json
pod/testbox created
[root@host131 test]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testbox 1/1 Running 0 8s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 test]#
[root@host131 test]# kubectl exec -it testbox sh
/ # hostname
testbox
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sleep 1000
6 root 0:00 sh
12 root 0:00 ps -ef
/ #
总结
在Kubernetes中很多资源都可以和Pod一样使用命令行、YAML或者JSON方式进行创建,可根据需要进行使用。
转载:https://blog.csdn.net/liumiaocn/article/details/104186715
查看评论