k8s_核心對象及應用kubectl context、pod


k8s是如何創建資源的?

k8s可以通過yml⽂件創建k8s對象(資源),然后發布k8s集群操作。

 

kubectl context

kubectl context是k8s集群的上下⽂對象,存放着集群相關信息。常用查詢kubectl context有如下命令,至於這些信息如何生成的在后續再詳細說明,先大概了解kubectl cintext相關信息如何查詢。

kubectl config view  查看集群信息
kubectl config get-contexts  得到所有集群context對象信息
kubectl cluster-info  查看當前集群情況
 

POD

概念

pod是k8s管理的最小單元,Pod和container的關系,⼀個Pod可以有⼀個或多個container
⼀個Pod內共享⼀個namespace,⽤戶、⽹絡、存儲等。簡單的說,pod⾥⾯如果有多個container,這些container共享⼀個ip,好⽐⼀個系統中起了多個項⽬,共享IP,⽤端⼝區分。如下圖所示

 

pod的定義文件

下面是最簡單的pod定義,完整的pod定義文件的每個字段含義可以自行查閱資料

#應用的api版本
apiVersion: v1
#對象類型 kind種類
kind: Pod
#對象元數據
metadata:
    #pod 名稱
    name: nginx
    #labels標簽配置
    labels:
        app: nginx
#對象詳細信息
spec:
    #容器配置,可以多個
    containers:
    #容器名稱
    - name: nginx
      #鏡像名稱
      image: nginx
      #端口設置
      ports:
      - containerPort: 80
View Code

pod的基本操作

創建pod

[root@k8s-01 pod_demo]# kubectl create -f demo_pod_nginx.yml
pod/nginx created

查詢所有pod資源

[root@k8s-01 pod_demo]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx                              1/1     Running   0          2m51s

查看所有pod資源詳細信息

[root@k8s-01 pod_demo]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
nginx                              1/1     Running   0          3m30s   10.244.2.24   k8s-03   <none>           <none>

查詢指定pod詳細信息

[root@k8s-01 pod_demo]# kubectl describe pods nginx 
Name:         nginx
Namespace:    default
Priority:     0
Node:         k8s-03/192.168.188.133
Start Time:   Sat, 25 Sep 2021 23:13:42 +0800
Labels:       app=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.2.24
Containers:
  nginx:
    Container ID:   docker://15f32743135523f77bd55b76b08dbe34449d3b0991c7f9c2f9d9c57fee51a1d9
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3e
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sat, 25 Sep 2021 23:13:58 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c8jx9 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-c8jx9:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-c8jx9
    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  4m40s  default-scheduler  Successfully assigned default/nginx to k8s-03
  Normal  Pulling    4m39s  kubelet, k8s-03    Pulling image "nginx"
  Normal  Pulled     4m23s  kubelet, k8s-03    Successfully pulled image "nginx"
  Normal  Created    4m23s  kubelet, k8s-03    Created container nginx
  Normal  Started    4m23s  kubelet, k8s-03    Started container nginx
View Code

刪除pod

[root@k8s-01 pod_demo]# kubectl get pods|grep nginx
nginx                              1/1     Running   0          5m32s
[root@k8s-01 pod_demo]# kubectl delete pods nginx
pod "nginx" deleted
[root@k8s-01 pod_demo]# kubectl get pods|grep nginx
[root@k8s-01 pod_demo]# 

pod交互式運行

與docker的交互式運行方式類似,pod的交互式命令如下
kubectl exec -it podname -c containername /bin/bash
#podname可以通過kubectl get pods查詢,-c containername可以不加,默認進入pod的第一個container。加上以后可以指定pod內特定的container。
kubectl describe pods podname可以查詢指定pod的詳細信息,這里面就有該pod包含的所有container信息
 
和docker exec -it containername /bin/bash有什么區別的?
#kubectl創建的pod可能不在本節點上,這時候本節點docker是無法管理pod下的container的,只有到pod所在節點才能使用docker命令操作該container,而kubectl可以在任何安裝kubectl並加入集群的節點上操作任意一個pod任意一個container

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM