Kubernetes学习笔记11

发布于:2024-04-09 ⋅ 阅读:(120) ⋅ 点赞:(0)

k8s集群核心概念:pod:

在K8s集群中是不能直接运行容器的,K8s的最小调度单元是Pod,我们要使用Pod来运行应用程序。

学习目标:

了解pod概念:

了解查看pod方法

了解创建pod方法

了解pod访问方法

了解删除pod方法

课程介绍:

Pod介绍:

Pod是k8s集群能够调度的最小单元。

Pod是容器的封装。

查看Pod:

[root@master1 ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl get pods --namespace default
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
[root@master1 ~]#
[root@master1 ~]#
[root@master1 ~]# kubectl get pod --namespace default
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
[root@master1 ~]#


[root@master1 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   17h
kube-flannel      Active   17h
kube-node-lease   Active   17h
kube-public       Active   17h
kube-system       Active   17h
[root@master1 ~]# kubectl get pod --namespace kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-594649bd75-xjbgr   1/1     Running   0          16h
calico-node-6lz45                          1/1     Running   0          16h
calico-node-fdprn                          1/1     Running   0          16h
calico-node-szs6x                          1/1     Running   0          16h
coredns-558bd4d5db-fmrpg                   1/1     Running   0          17h
coredns-558bd4d5db-fnxdn                   1/1     Running   0          17h
etcd-master1                               1/1     Running   1          17h
kube-apiserver-master1                     1/1     Running   1          17h
kube-controller-manager-master1            1/1     Running   1          17h
kube-proxy-rz26h                           1/1     Running   0          17h
kube-proxy-spf9t                           1/1     Running   1          17h
kube-proxy-x4fq5                           1/1     Running   0          17h
kube-scheduler-master1                     1/1     Running   1          17h

可见,k8s运行的组件,都是以pod形式运行的。

创建pod:

由于网络原因,建议提前准备好容器镜像,本次使用nginx:latest容器镜像。

master默认是不允许把用户运行的pod调度过来的。

1)编写用于创建pod资源清单文件:

[root@master1 ~]# cat 02_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

[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
pod1                     1/1     Running   0          38s

可见pod1创建成功。

[root@master1 ~]# kubectl get pods --namespace default
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h
pod1                     1/1     Running   0          110s

在default命令空间中查看pods。

查看Pod更加详细的信息:

[root@master1 ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP              NODE      NOMINATED NODE   READINESS GATES
nginx-65c4bffcb6-gd9z6   1/1     Running   0          16h     10.244.1.4      worker1   <none>           <none>
pod1                     1/1     Running   0          3m30s   10.244.189.65   worker2   <none>           <none>
[root@master1 ~]#

Pod访问:

可以看到pod1是运行在worker2上。使用curl http://10.244.189.65 暂时还访问不到。

Pod的访问,在实际工作中是不建议访问的,Pod会发生不可预计的变化。

[root@master1 ~]# curl http://10.244.235.130
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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:

可以通过命令行删除,也可通过资源清单YAML文件进行删除。

如果是单个Pod,可以使用命令行进行删除。

如果有控制器,是某一类的应用,我们是不建议使用命令行来进行删除的。

1)kubectl命令行删除:

# 使用kubectl命令行进行删除
[root@master1 ~]# kubectl delete pods pod1
pod "pod1" deleted


[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-86vxw   1/1     Running   0          12m


[root@master1 ~]# kubectl apply -f 02_create_pod.yaml
pod/pod1 created


[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-65c4bffcb6-86vxw   1/1     Running             0          13m
pod1                     0/1     ContainerCreating   0          5s

# 使用资源清单文件进行删除
[root@master1 ~]# kubectl delete -f 02_create_pod.yaml
pod "pod1" deleted


[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-65c4bffcb6-86vxw   1/1     Running   0          14m

然后,我们还需要学习掌握:Pod中容器镜像下载策略、Pod中容器重启策略、Pod健康检查方式、Pod生命周期管理、Pod调度流程、Pod调度约束、Pod故障排除方法等。


网站公告

今日签到

点亮在社区的每一天
去签到