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

k8s学习(五) 创建静态pod

程序员文章站 2022-03-12 11:50:43
...

静态 Pod 直接由特定节点上的kubelet进程来管理,不通过 master 节点上的apiserver。无法与我们常用的控制器Deployment或者DaemonSet进行关联,它由kubelet进程自己来监控,当pod崩溃时重启该pod,kubelete也无法对他们进行健康检查。静态 pod 始终绑定在某一个kubelet,并且始终运行在同一个节点上。 kubelet会自动为每一个静态 pod 在 Kubernetes 的 apiserver 上创建一个镜像 Pod(Mirror Pod),因此我们可以在 apiserver 中查询到该 pod,但是不能通过 apiserver 进行控制(例如不能删除)。
创建静态 Pod 有两种方式:配置文件和 HTTP 两种方式。这里采用配置文件的方式创建

1、查看配置文件路径

kubelet通过 kubelet --pod-manifest-path=<路径>来启动kubelet进程,kubelet 定期的去扫描这个目录,根据这个目录下出现或消失的 YAML/JSON 文件来创建或删除静态 pod。
如果你的 kubelet 启动参数中没有配置上面的–pod-manifest-path参数的话,那么添加上这个参数然后重启 kubelet 即可。

[[email protected] manifests]# systemctl status kubelet
● kubelet.service - kubelet: The Kubernetes Node Agent
   Loaded: loaded (/usr/lib/systemd/system/kubelet.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/kubelet.service.d
           └─10-kubeadm.conf
   Active: active (running) since 一 2019-08-26 17:19:37 CST; 21min ago
     Docs: https://kubernetes.io/docs/
 Main PID: 73129 (kubelet)
    Tasks: 20
   Memory: 57.2M
   CGroup: /system.slice/kubelet.service
           └─73129 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --cgroup-driver=systemd --network-plugin=cni --pod-...

8月 26 17:35:00 k8s-node1 kubelet[73129]: W0826 17:35:00.523761   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/memory/libcontainer_83218_systemd_test_default.slice": 0x40000100 == IN_CREATE|...ile or directory
8月 26 17:35:00 k8s-node1 kubelet[73129]: W0826 17:35:00.523800   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/devices/libcontainer_83218_systemd_test_default.slice": 0x40000100 == IN_CREATE...ile or directory
8月 26 17:36:26 k8s-node1 kubelet[73129]: W0826 17:36:26.357946   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/cpu,cpuacct/libcontainer_84114_systemd_test_default.slice": 0x40000100 == IN_CREATE|IN_ISDIR): ...
8月 26 17:36:26 k8s-node1 kubelet[73129]: W0826 17:36:26.358017   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/blkio/libcontainer_84114_systemd_test_default.slice": 0x40000100 == IN_CREATE|I...ile or directory
8月 26 17:36:26 k8s-node1 kubelet[73129]: W0826 17:36:26.364753   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/memory/libcontainer_84114_systemd_test_default.slice": 0x40000100 == IN_CREATE|...ile or directory
8月 26 17:36:26 k8s-node1 kubelet[73129]: W0826 17:36:26.364874   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/devices/libcontainer_84114_systemd_test_default.slice": 0x40000100 == IN_CREATE...ile or directory
8月 26 17:41:10 k8s-node1 kubelet[73129]: W0826 17:41:10.596837   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/cpu,cpuacct/libcontainer_87150_systemd_test_default.slice": 0x40000100 == IN_CR...ile or directory
8月 26 17:41:10 k8s-node1 kubelet[73129]: W0826 17:41:10.596890   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/blkio/libcontainer_87150_systemd_test_default.slice": 0x40000100 == IN_CREATE|I...ile or directory
8月 26 17:41:10 k8s-node1 kubelet[73129]: W0826 17:41:10.607493   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/memory/libcontainer_87150_systemd_test_default.slice": 0x40000100 == IN_CREATE|...ile or directory
8月 26 17:41:10 k8s-node1 kubelet[73129]: W0826 17:41:10.607564   73129 watcher.go:87] Error while processing event ("/sys/fs/cgroup/devices/libcontainer_87150_systemd_test_default.slice": 0x40000100 == IN_CREATE...ile or directory
Hint: Some lines were ellipsized, use -l to show in f

找到Drop-In 获取其中的环境变量

Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path

如果没有添加一下

Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true"

注意的是所以如果我们通过kubeadm的方式来安装的集群环境,对应的kubelet已经配置了我们的静态 Pod 文件的路径,那就是/etc/kubernetes/manifests,所以我们只需要在该目录下面创建一个标准的 Pod 的 JSON 或者 YAML 文件即可。

2、编辑一个Pod static-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: static-nginx
  labels:
    app: static
spec:
  containers:
    - name: web-nginx
      image: nginx
      ports:
        - name: web-nginx
          containerPort: 80

3、查看安装的pod

[[email protected] manifests]# kubectl get pods -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
kube-node-87dcfc9cf-9kj8j   1/1     Running   0          8h    10.244.1.23   k8s-node3   <none>           <none>
kube-node-87dcfc9cf-l2ppz   1/1     Running   0          8h    10.244.2.24   k8s-node2   <none>           <none>
static-nginx-k8s-node1      1/1     Running   0          17m   10.244.0.2    k8s-node1   <none>           <none>

运行curl 10.244.0.2:80

[[email protected] manifests]# curl 10.244.0.2: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>

4、静态pod的删除

无法使用kubectl delete删除pods,删掉会马上重启。通过删除/etc/kubernetes/manifests下对应的yaml或JSON文件删除静态POD

5、补充HTTP方式创建Pods
kubelet 周期地从–manifest-url=参数指定的地址下载文件,并且把它翻译成 JSON/YAML 格式的 pod 定义。此后的操作方式与–pod-manifest-path=相同,kubelet 会不时地重新下载该文件,当文件变化时对应地终止或启动静态 pod。