
image.png
定義Pod
[root@rourou ~] # vim nginx-busybox.yaml
apiVersion: v1
kind: Pod
metadata: # pod 的源數(shù)據(jù)信息,可以寫(xiě)多個(gè)
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
列出詳細(xì)信息
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
進(jìn)入指定容器
kubectl exec -it nginx-busybox -c nginx sh
或者在容器中執(zhí)行命令并將及如果返回終端
kubectl exec nginx-busybox -c nginx ls /
#-c 指定容器
多個(gè)pods實(shí)例
指定節(jié)點(diǎn)創(chuàng)建pod
apiVersion: v1
kind: Pod
metadata: # pod 的源數(shù)據(jù)信息,可以寫(xiě)多個(gè)
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 的源數(shù)據(jù)信息,可以寫(xiě)多個(gè)
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"]
#本實(shí)驗(yàn)的client與server的版本皆為v1.16.0,得到的/etc/hosts的結(jié)果
10.0.122.126 rourou1.remote rourou2.remote
指定namespace創(chuàng)建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則為默認(rèn)的namespace,不會(huì)顯示demo下的pods
創(chuàng)建的容器之間共享進(jìn)程、
apiVersion: v1
kind: Pod
metadata: # pod 的源數(shù)據(jù)信息,可以寫(xiě)多個(gè)
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的進(jìn)程
創(chuàng)建的容器共享宿主機(jī)的進(jìn)程等
apiVersion: v1
kind: Pod
metadata: # pod 的源數(shù)據(jù)信息,可以寫(xiě)多個(gè)
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
#注意:與主機(jī)共享進(jìn)程時(shí)不能指定80端口,否則創(chuàng)建容器時(shí)一直處于panding狀態(tài)