一.系统环境
服务器版本 | docker软件版本 | CPU架构 |
---|---|---|
CentOS Linux release 7.4.1708 (Core) | Docker version 20.10.12 | x86_64 |
二.前言
一个pod里可以有一个容器,也可以有多个容器。
一般一个pod里运行一个容器,那一个pod里运行两个容器的意义何在?一个容器是主容器,一个是副容器sidecar,比如nginx容器用来提供服务,另外一个容器使用工具来进行日志分析,两个容器挂载同一个数 据卷,日志分析容器读取数据卷即可分析日志。
创建pod的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/16686769.html
三.创建包含多个容器的pod
3.1 环境介绍
Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点
服务器 | 操作系统版本 | CPU架构 | 进程 | 功能描述 |
---|---|---|---|---|
k8scloude1/192.168.110.130 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master节点 |
k8scloude2/192.168.110.129 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
k8scloude3/192.168.110.128 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
3.2 在一个pod里创建多个容器
生成一个yaml文件,pod里面有2个容器n1,n2
[root@k8scloude1 pod]# vim pod2.yaml
#kind: Pod表示资源类型为Pod labels指定pod标签 metadata下面的name指定pod名字 containers下面全是容器的定义
#image指定镜像名字 imagePullPolicy指定镜像下载策略 containers下面的name指定容器名
#resources指定容器资源(CPU,内存等) dnsPolicy指定DNS策略 restartPolicy容器重启策略
[root@k8scloude1 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1
name: pod1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: n1
resources: {}
- image: nginx
imagePullPolicy: IfNotPresent
name: n2
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
创建pod
[root@k8scloude1 pod]# kubectl apply -f pod2.yaml
pod/pod1 created
查看pod,pod启动失败,原因为使用了同一个镜像,端口80冲突
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 1/2 CrashLoopBackOff 2 40s
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 1/2 Error 3 58s
删除pod
[root@k8scloude1 pod]# kubectl delete pod pod1 --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "pod1" force deleted
对于containers,如果没有指定command参数,则运行的进程为nginx镜像里指定的CMD那个进程,如果有command参数,则运行command指定的进程,不运行nginx镜像里CMD的进程。
command: ["sh","-c","sleep 1000"] 睡眠1000秒,时间到了就结束了,需要进行重启,继续sleep 1000
[root@k8scloude1 pod]# vim pod2.yaml
[root@k8scloude1 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1
name: pod1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: n1
resources: {}
- image: nginx
imagePullPolicy: IfNotPresent
command: ["sh","-c","sleep 1000"]
name: n2
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
创建pod并查看,READY:2/2表示pod里有两个容器,两个容器都启动起来了。
一般一个pod里运行一个容器,那一个pod里运行两个容器的意义何在?则一个容器是主容器,一个是副容器sidecar,比如nginx容器用来提供服务,另外一个容器使用工具来进行日志分析,两个容器挂载同一个数 据卷,日志分析容器读取数据卷即可分析日志。
[root@k8scloude1 pod]# kubectl apply -f pod2.yaml
pod/pod1 created
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 2/2 Running 0 5s
删除pod
[root@k8scloude1 pod]# kubectl delete pod pod1 --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "pod1" force deleted
[root@k8scloude1 pod]# kubectl get pod
No resources found in pod namespace.
command: ["sh","-c","sleep 10"] 修改睡眠时间为10秒
[root@k8scloude1 pod]# vim pod2.yaml
[root@k8scloude1 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1
name: pod1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: n1
resources: {}
- image: nginx
imagePullPolicy: IfNotPresent
command: ["sh","-c","sleep 10"]
name: n2
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
由于restartPolicy: Always,10秒到期之后,pod会自动进行重启
[root@k8scloude1 pod]# kubectl apply -f pod2.yaml
pod/pod1 created
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 2/2 Running 0 5s
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 2/2 Running 2 45s
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 1/2 CrashLoopBackOff 3 2m3s
[root@k8scloude1 pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1 1/2 CrashLoopBackOff 5 4m55s
删除pod
[root@k8scloude1 pod]# kubectl delete -f pod2.yaml --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "pod1" force deleted
[root@k8scloude1 pod]# kubectl get pod
No resources found in pod namespace.