欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

K8S之pod入门和Lable

程序员文章站 2024-03-14 13:42:46
...

K8S之pod入门和Lable

Pod

  • Pod是kubernetes集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。

Pod 可以认为是容器的封装,一个Pod中可以存在一个或者多个容器

Kubernetes在集群启动之后,集群中的各个组件也都是以Pod方式运行的。

[[email protected] ~]# kubectl get pod -n kube-system 
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-7dbc97f587-c8db6   1/1     Running   2          41h
calico-node-2k8tz                          1/1     Running   2          41h
calico-node-f5dmh                          1/1     Running   2          41h
calico-node-w47xc                          1/1     Running   0          25h
coredns-7ff77c879f-bgxft                   1/1     Running   2          44h
coredns-7ff77c879f-txgpk                   1/1     Running   2          44h
etcd-master                                1/1     Running   2          47h
kube-apiserver-master                      1/1     Running   2          47h
kube-controller-manager-master             1/1     Running   4          47h
kube-proxy-p2qpc                           1/1     Running   2          47h
kube-proxy-p4q77                           1/1     Running   0          25h
kube-proxy-skfq5                           1/1     Running   4          46h
kube-scheduler-master                      1/1     Running   5          47h

Pod操作

  • 创建并运行

    kubernetes没有提供单独运行pod的命令,都是通过Pod控制器来实现的

# 命令格式: kubectl run (pod控制器名称) [参数] 
# --image  指定Pod的镜像
# --port   指定端口
# --namespace  指定namespace
[[email protected] ~]# kubectl run nginx --image=nginx:latest --port=80 --namespace dev 
deployment.apps/nginx created
  • 查看pod信息
# 查看Pod基本信息
[[email protected] ~]# kubectl get pods -n dev
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          43s

# 查看Pod的详细信息
[[email protected] ~]# kubectl describe pod nginx -n dev
Name:         nginx
Namespace:    dev
Priority:     0
Node:         node1/192.168.1.105
Start Time:   Wed, 01 Sep 2021 10:29:19 +0800
Labels:       env=test
              version=3.0
Annotations:  cni.projectcalico.org/podIP: 10.244.166.143/32
              cni.projectcalico.org/podIPs: 10.244.166.143/32
Status:       Running
IP:           10.244.166.143
IPs:
  IP:  10.244.166.143
Containers:
  pod:
    Container ID:   docker://ce4bf985af9f01ac31e300becaf47aaf898a60b91936dd09ee1f417f71d34120
    Image:          nginx:latest
    Image ID:       docker-pullable://[email protected]:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 01 Sep 2021 10:29:56 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-spd9p (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-spd9p:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-spd9p
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  15m   default-scheduler  Successfully assigned dev/nginx to node1
  Normal  Pulling    15m   kubelet, node1     Pulling image "nginx:latest"
  Normal  Pulled     14m   kubelet, node1     Successfully pulled image "nginx:latest"
  Normal  Created    14m   kubelet, node1     Created container pod
  Normal  Started    14m   kubelet, node1     Started container pod
  • 访问pod,获取pod运行在哪个节点上
# 获取podIP 和 pod 运行在哪个节点上
[[email protected] ~]#  kubectl get pods -n dev -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          16m   10.244.166.143   node1   <none>           <none>
# 访问pod
 [[email protected] ~]# curl   http://10.244.166.143:80
<!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
[[email protected] ~]# kubectl delete pod nginx -n dev
pod "nginx" deleted

# 此时,显示删除Pod成功,但是再查询,发现又新产生了一个 
[[email protected] ~]# kubectl get pods -n dev
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          21s

# 这是因为当前Pod是由Pod控制器创建的,控制器会监控Pod状况,一旦发现Pod死亡,会立即重建
# 此时要想删除Pod,必须删除Pod控制器

# 先来查询一下当前namespace下的Pod控制器
[[email protected] ~]# kubectl get deploy -n  dev
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           9m7s

# 接下来,删除此PodPod控制器
[[email protected] ~]# kubectl delete deploy nginx -n dev
deployment.apps "nginx" deleted

# 稍等片刻,再查询Pod,发现Pod被删除了
[[email protected] ~]# kubectl get pods -n dev
No resources found in dev namespace.
  • 基于配置文件的创建

vim pod-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:latest
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
  • 创建:
kubectl create -f pod-nginx.yaml
  • 删除:
kubectl delete -f pod-nginx.yaml

Label

  • Label 它的作用就是在资源上添加标识,用来对他们进行区分和选择

  • Label 会以key=value键值对的形式附加到各种对象上,如Node、pod、Service等

  • 一个资源可以定义任意数量的Label,同一个Label也可以被添加到任意数量的资源对象上去

  • Label通常在资源定义时确定,当然也可以在对象创建后动态添加或者删除

  • 为什么要有Label?

    可以实现分组 namespace 实现的分组是 不同的namespace之间是不同的 适合不同项目的分组 一个项目里面的前端和后端也需要分组 但是还要互通 这个时候就可以通过Label分组。

标签定义完毕之后,还要考虑到标签的选择,这就要使用到 Label Selector

  • Label用于给某个资源对象定义标识

  • Label Selector 用于查询和筛选拥有某些标签的资源对象

  • 两种Label Selector

    基于等式的Label Selector

    name = slave: 选择所有包含Label中key="name"且value="slave"的对象
    
    env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象
    

    基于集合的Label Selector

    - name in (master, slave): 选择所有包含Label中的key="name"且value="master""slave"的对象
    
      name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象
    

标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号","进行分隔即可。例如:

name=slave,env!=production

name not in (frontend),env!=production

标签操作

  • 命令方式
# 为pod资源打标签
[[email protected] ~]# kubectl label pod nginx-pod version=1.0 -n dev
pod/nginx-pod labeled

# 为pod资源更新标签
[[email protected] ~]# kubectl label pod nginx-pod version=2.0 -n dev --overwrite
pod/nginx-pod labeled

# 查看标签
[[email protected] ~]# kubectl get pod nginx-pod  -n dev --show-labels
NAME        READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod   1/1     Running   0          10m   version=2.0

# 筛选标签
[[email protected] ~]# kubectl get pod -n dev -l version=2.0  --show-labels
NAME        READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod   1/1     Running   0          17m   version=2.0
[[email protected] ~]# kubectl get pod -n dev -l version!=2.0 --show-labels
No resources found in dev namespace.

#删除标签
[[email protected] ~]# kubectl label pod nginx-pod version- -n dev
pod/nginx-pod labeled
  • 配置文件的方式
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
  labels:
    version: "3.0" 
    env: "test"
spec:
  containers:
  - image: nginx:latest
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

执行命令创建kubectl apply -f pod-nginx.yaml

# 查看
[[email protected] ~]# kubectl get pods  -n dev --show-labels 
NAME    READY   STATUS    RESTARTS   AGE   LABELS
nginx   1/1     Running   0          69m   env=test,version=3.0