k8s入門-Pod的基本管理

Pod是Kubernetes最小的管理單元,一個Pod可以代表一個運行在集羣中的進程。

一、創建一個nginx的Pod

使用YAML格式來描述一個Pod

[root@k8s-01 ~]# cat nginx-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

YAML文件中定義了版本,類型,名稱和,鏡像和端口。

創建Pod

[root@k8s-01 ~]# kubectl create -f nginx-pod.yaml 
pod/nginx-pod created

查看Pod

[root@k8s-01 ~]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
nginx-pod                   1/1     Running   0          58s

查看Pod更多信息

[root@k8s-01 ~]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP              NODE     NOMINATED NODE   READINESS GATES
nginx-pod                   1/1     Running   0          10m   172.30.64.7     k8s-01   <none>           <none>

不僅可以看到pod的啓動狀態,還能看到pod的IP地址(172.30.64.7)和所被分配的節點(k8s-01)。

查看Pod詳情(排錯用)

[root@k8s-01 ~]# kubectl describe pod nginx-pod
Name:         nginx-pod
Namespace:    default
Priority:     0
Node:         k8s-01/192.168.0.71
Start Time:   Mon, 11 May 2020 15:00:37 +0800
Labels:       app=nginx
Annotations:  <none>
Status:       Running
IP:           172.30.64.7
IPs:
  IP:  172.30.64.7
Containers:
  nginx:
    Container ID:   docker://db49e7059de1bfbd1b0b1af09625de8e728b12eec7790060a45dfc0cd4db4585
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 11 May 2020 15:01:33 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-4cxn2 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-4cxn2:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-4cxn2
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 360s
                 node.kubernetes.io/unreachable:NoExecute for 360s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/nginx-pod to k8s-01
  Normal  Pulling    12m        kubelet, k8s-01    Pulling image "nginx"
  Normal  Pulled     11m        kubelet, k8s-01    Successfully pulled image "nginx"
  Normal  Created    11m        kubelet, k8s-01    Created container nginx
  Normal  Started    11m        kubelet, k8s-01    Started container nginx

查看Pod日誌

[root@k8s-01 ~]# kubectl logs pod/nginx-pod

刪除Pod

[root@k8s-01 ~]# kubectl delete nginx-pod

Pod中的鏡像拉取策略 當kubelet嘗試拉取指定的鏡像時,[imagePullPolicy]和鏡像的標籤會生效。

  • imagePullPolicy: IfNotPresent:僅當鏡像在本地不存在時鏡像才被拉取。
  • imagePullPolicy: Always:每次啓動 pod 的時候都會拉取鏡像。

省略imagePullPolicy,鏡像標籤爲:latest或被省略,Always被應用。 imagePullPolicy被省略,並且鏡像的標籤被指定且不是:latest,IfNotPresent被應用。 imagePullPolicy: Never:鏡像被假設存在於本地。 沒有嘗試拉取鏡像。

二、進入到Pod

怎麼樣進入到Pod中呢?

如果Pod中只有一個容器,可以直接使用下面這個命令:

[root@k8s-01 ~]# kubectl exec -it nginx-pod bash
root@nginx-pod:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

如果Pod中有多個容器,則需要使用``命令如下:

$ kubectl --namespace=kube-system exec -it nginx-pod -c nginx-app bash

其中ube-system爲namespace名稱,nginx-pod爲pod名稱,nginx-app爲pod中的一個容器的名稱。

三、訪問Pod

從上面的信息中可以看到Pod的ip地址爲172.30.64.7,直接用curl訪問下:

[root@k8s-01 ~]# curl 172.30.64.7
<!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>

在集羣中是可以訪問,但是這個nginx部署之後,是給外部提供訪問的,這個時候就要用到kubectl的port-forward了。

可以通過下面這個命令將pod裏面的端口映射到主機上來:

[root@k8s-01 ~]# kubectl port-forward nginx-pod 18080:80
Forwarding from 127.0.0.1:18080 -> 80

可以看到,已經將pod中的80端口映射到了主機的18080端口,現在直接來訪問下:

[root@k8s-01 ~]# curl 127.0.0.1:18080   
<!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>

這裏只是監聽了127.0.0.1的地址,還可以通過增加--address參數來修改監聽地址,具體可以通過kubectl port-forward --help命令進行查看。

參考文章:
http://k8s.unixhot.com/kubernetes/pod.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章