kubernetes-Pod

kubernetes-Pod

概念

1
2
在k8s中我们不会直接操作容器,而是把容器包装成Pod在进行管理运行于Node节点上,若干相关容器的组合。Pod内包含的容器运行在同一宿主机上,使用相同的网络命令空间
、ip地址和端口,能够通过localhost进行通信。Pod是k8s进行创建、调度和管理的最小单位,他提供了比容器更高层次的抽象、使得部署和管理更加灵活。一个Pod可以包含一个容器或多个容器。

发布第一个容器化应用

1
2
3
4
5
6
1、有镜像
2、部署应用。
--考虑做不做副本
不做副本就是pod
做副本以deployment/RC/DaemonSet方式去创建
--做了副本访问还需要做一个service,使用访问。

创建Pod

参考文档

1
https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/

使用yaml方式创建

1
2
3
4
5
6
7
8
9
10
11
12
[root@kubernetes-master study]# cat pod-nginx.yaml 
---
apiVersion: v1 #api版本
kind: Pod #创建的资源对象为pod
metadata: #元数据
name: my-nginx #pod的名字
spec: #属性
containers: #定义容器
  - name: my-nginx-web #容器名
    image: daocloud.io/library/nginx #镜像 我这里用的daocloud的镜像
    ports: #端口
      - containerPort: 80 #容器暴漏端口

image-20231107111521934

查看Pod信息
1
2
3
4
# 查看pod信息
[root@kubernetes-master study]# kubectl get pod
NAME       READY   STATUS   RESTARTS   AGE
my-nginx   1/1     Running   0         4m5s

image-20231107112045840

1
2
# 查看详情pod信息
[root@kubernetes-master study]# kubectl get pod -o wide

image-20231107112222120

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 查看pod创建详细信息
[root@kubernetes-master study]# kubectl describe pod my-nginx
Name:             my-nginx
Namespace:       default
Priority:         0
Service Account: default
Node:             kubernetes-node1/10.10.10.22
Start Time:       Tue, 07 Nov 2023 11:18:45 +0800
Labels:           <none>
Annotations:     cni.projectcalico.org/containerID: 89f8c7ab53802b66299950e163f2c3258809dc0e238efc6ee7c02df2587df857
                cni.projectcalico.org/podIP: 10.10.20.230/32
                cni.projectcalico.org/podIPs: 10.10.20.230/32
Status:           Running
IP:               10.10.20.230
IPs:
IP: 10.10.20.230
Containers:
my-nginx-web:
  Container ID:   docker://c56dd1464b60fd9bd7e1545553461adf38a243800a01cad0c366fd25687eb5e4
  Image:         daocloud.io/library/nginx
  Image ID:       docker-pullable://daocloud.io/library/nginx@sha256:1a53eb723d17523512bd25c27299046cfa034cce309f4ed330c943a304513f59
  Port:           80/TCP
  Host Port:     0/TCP
  State:         Running
    Started:     Tue, 07 Nov 2023 11:18:47 +0800
  Ready:         True
  Restart Count: 0
  Environment:   <none>
  Mounts:
    /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tdpfn (ro)
Conditions:
Type             Status
Initialized       True
Ready             True
ContainersReady   True
PodScheduled     True
Volumes:
kube-api-access-tdpfn:
  Type:                   Projected (a volume that contains injected data from multiple sources)
  TokenExpirationSeconds: 3607
  ConfigMapName:           kube-root-ca.crt
  ConfigMapOptional:       <nil>
  DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:             <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                            node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type   Reason     Age   From               Message
----   ------     ----   ----               -------
Normal Scheduled 4m48s default-scheduler Successfully assigned default/my-nginx to kubernetes-node1
Normal Pulling   4m47s kubelet           Pulling image "daocloud.io/library/nginx"
Normal Pulled     4m46s kubelet           Successfully pulled image "daocloud.io/library/nginx" in 529.869685ms (529.888256ms including waiting)
Normal Created   4m46s kubelet           Created container my-nginx-web
Normal Started   4m46s kubelet           Started container my-nginx-web

image-20231107112735671

image-20231107113106939

访问测试
1
2
# 访问podip
[root@kubernetes-master study]# curl 10.10.20.230

image-20231107113329212

进入pod
1
2
3
4
5
6
7
# 进入pod容器中
[root@kubernetes-master study]# kubectl exec -it my-nginx sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

# 查看nginx默认发布目录下文件
# ls /usr/share/nginx/html/              
50x.html index.html
删除pod
1
2
[root@kubernetes-master study]# kubectl  delete -f pod-nginx.yaml 
pod "my-nginx" deleted

使用命令方式生成yaml

# 生成yaml
kubectl run my-nginx-2 --image=daocloud.io/library/nginx --restart=Never --dry-run=client -o yaml > my-nginx-2.yaml

# 创建pod
[root@kubernetes-master study]# kubectl  apply -f pod-nginx-2.yaml 
pod/my-nginx-2 created

# 查看pod
[root@kubernetes-master study]# kubectl  get pod 
NAME         READY   STATUS    RESTARTS   AGE
my-nginx-2   1/1     Running   0          9s

# 查看详细信息
[root@kubernetes-master study]# kubectl  get pod  -o wide  
NAME         READY   STATUS    RESTARTS   AGE   IP             NODE               NOMINATED NODE   READINESS GATES
my-nginx-2   1/1     Running   0          17s   10.10.20.232   kubernetes-node1   <none>           <none>

# 测试访问podip
[root@kubernetes-master study]# curl 10.10.20.232
<!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
[root@kubernetes-master study]# kubectl  delete  -f pod-nginx-2.yaml 
pod "my-nginx-2" deleted