定义Pod
[root@rourou ~] # vim nginx-busybox.yaml
apiVersion: v1
kind: Pod
metadata: # pod 的源数据信息,可以写多个
name: nginx-busybox # pod 的名字
spec:
containers:
- name: nginx # 容器的名字
image: nginx:alpine # 镜像的名字
ports:
- containerPort: 80
- name: busybox
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
[root@rourou ~] # kubectl create -f nginx-busybox.yaml
查看pod
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-busybox 2/2 Running 0 29m
test-projected-volume 1/1 Running 0 159m
#给查看同样支持3中格式
kubectl get pods -o wide
kubectl get pods -o json
kubectl get pods -o yaml
列出详细信息
kubectl get events
LAST SEEN TYPE REASON OBJECT MESSAGE
<unknown> Normal Scheduled pod/nginx-busybox Successfully assigned default/nginx-busybox to k8s-node2
6m40s Normal Pulled pod/nginx-busybox Container image "nginx:alpine" already present on machine
######################################或者##############################
kubectl describe pods nginx-busybox
进入指定容器
kubectl exec -it nginx-busybox -c nginx sh
或者在容器中执行命令并将及如果返回终端
kubectl exec nginx-busybox -c nginx ls /
#-c 指定容器
多个pods实例
指定节点创建pod
apiVersion: v1
kind: Pod
metadata: # pod 的源数据信息,可以写多个
name: nginx-busybox # pod 的名字
spec:
nodeSelector:
disktype: ssd
containers:
- name: nginx # 容器的名字
image: nginx:alpine # 镜像的名字
ports:
- containerPort: 80
- name: busybox
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
修改pod的容器的hosts文件
apiVersion: v1
kind: Pod
metadata: # pod 的源数据信息,可以写多个
name: nginx-hosts # pod 的名字
spec:
hostAliases:
- ip: "10.0.122.126"
hostnames:
- "rourou1.remote"
- "rourou2.remote"
containers:
- name: nginx # 容器的名字
image: nginx:alpine # 镜像的名字
ports:
- containerPort: 80
- name: busybox
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
#本实验的client与server的版本皆为v1.16.0,得到的/etc/hosts的结果
10.0.122.126 rourou1.remote rourou2.remote
指定namespace创建pod
apiVersion: v1
kind: Pod
metadata:
name: nginx-namespace
namespace: demo
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 8
#指定namespace为demo
kubectl get pods --namespace demo 即可查看指定namespace下的pods
若是使用kubectl get pods则为默认的namespace,不会显示demo下的pods
创建的容器之间共享进程、
apiVersion: v1
kind: Pod
metadata: # pod 的源数据信息,可以写多个
name: nginx-share # pod 的名字
spec:
shareProcessNamespace: true
containers:
- name: nginx # 容器的名字
image: nginx:alpine # 镜像的名字
ports:
- containerPort: 80
- name: busybox
image: busybox
stdin: true
tty: true
#在busybox中可以查看到nginx的进程
创建的容器共享宿主机的进程等
apiVersion: v1
kind: Pod
metadata: # pod 的源数据信息,可以写多个
name: nginx-share-host # pod 的名字
spec:
hostNetwork: true
hostIPC: true
hostPID: true
containers:
- name: nginx # 容器的名字
image: nginx:alpine # 镜像的名字
ports:
- containerPort: 8080
- name: busybox
image: busybox
stdin: true
tty: true
#注意:与主机共享进程时不能指定80端口,否则创建容器时一直处于panding状态
网友评论