kubernetes
是容器编排引擎,其负责容器的调度,管理和容器的运行,但
kubernetes
调度最小单位并非 是container
,而是
pod
,
pod
中可包含多个
container
通常集群中不会直接运行
pod
,而是通过各种工作负载的控制器如Deployments
,
ReplicaSets
,
DaemonSets
的方式运行,因为控制器能够保证pod状态的一致性。
pod是kubernetes中运行的最小单元
pod中包含一个容器或者多个容器
pod不会单独使用,需要有工作负载来控制,如D二ployments,StatefulSets,DaemonSets,CroJobs等。
![](https://img-blog.csdnimg.cn/20210809144933789.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTk5Mzk1MQ==,size_16,color_FFFFFF,t_70)
如上图所示,Pod的使用方式通常包含两种:
Pod中运行一个容器,最经常使用的模式,Container封装在pod中调度,两者几乎等同,但k8s不直接管理容器
Pod中运行多个容器,多个容器封装在pod中一起调度,适用于容器之间有数据交互和调用的场景,如app+redis,pod内部共享相同的网络命名空间,存储命名空间,进程命名空间
查看Pod
查看default命名空间中的pod
[root@k8s-master ~]# kubectl get pods
[root@k8s-master ~]# kubectl get pod
[root@k8s-master ~]# kubectl get pod --namespace default
[root@k8s-master ~]# kubectl get pods --namespace default
[root@k8s-master ~]# kubectl get pod -n default
创建Pod
kubernetes交互的方式通常分为四种:
命令行:kubectl和kubernetes交互,完成资源的管理,命令行入门简单,但是只支持部分资源创建
API:通过resfulAPI以http的方式和Kubernetes交互,适用于基于API做二次开发
SDK:提供各种语言原生的SDK,实现各种语言编程接入
YAML:通过易于理解的YAML文件格式,描述资源的定义,功能最丰富,最终转换为json格式
编写用于创建Pod资源清单文件
[root@k8s-master ~]# vim create-pod.yaml
[root@k8s-master ~]# cat create-pod.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: nginx-pod
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: nginxport
containerPort: 80
创建pod应用
[root@k8s-master ~]# kubectl apply -f create-pod.yaml
pod/pod1 created
验证pod应用
[root@k8s-master ~]# kubectl get pods -n default | grep pod1
pod1 1/1 Running 0 70m
[root@k8s-master ~]# kubectl get pods -n default -o wide | grep pod1
pod1 1/1 Running 0 70m 10.224.85.200 k8s-node01 <none> <none>
访问pod
[root@k8s-master ~]# curl http://10.224.85.200
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
删除pod
通过kubectl命令行删除
[root@k8s-master ~]# kubectl delete pods pod1
pod "pod1" deleted
通过资源清单文件删除
[root@k8s-master ~]# kubectl delete -f create-pod.yaml
pod "pod1" deleted