前面我们详细介绍了Pod的使用,可以通过命令行创建,也可以通过yaml的方式创建。这两种方式都是由ApiService来管理的,会在etcd中保存记录。
但其实还有一种Pod它不由ApiService管理。运行在固定的Node上,由kubelet创建。我们把它叫做Static Pod(静态Pod)
创建静态Pod有两种方式,通过本地的配置文件和http方式
kubelet需要知道创建静态Pod的配置文件在哪个目录。所以需要配置启动参数 --pod-manifest-path
如果不知道kubectl的启动配置文件可以通过 systemctl status kubelet 查看
[root@node02 ~]# systemctl status kubelet
● kubelet.service - kubelet: The Kubernetes Node Agent
Loaded: loaded (/usr/lib/systemd/system/kubelet.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/kubelet.service.d
└─10-kubeadm.conf
上面结果表示通过systemd启动,配置文件在 /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
[root@node02 ~]# cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
通过上面的信息,大概知道配置一个环境变量可以通过 Environment 与 EnvironmentFile 定义,最后通过ExecStart 启动的时候加载定义的环境变量。
于是我们也来新增一条 Environment 并且在最后将定义的环境变量加在 kubelet 后面
Environment="KUBELET_STATIC_POD_PATH=--pod-manifest-path=/root/static_pod"
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS $KUBELET_STATIC_POD_PATH
systemctl restart kubelet
systemctl daemon-reload
先观察下该节点的Pod情况
[root@node02 static_pod]# kubectl get pod -A -o wide|grep node02
kube-flannel kube-flannel-ds-7vbkn 1/1 Running 0 16d 10.0.4.13 node02 <none> <none>
kube-system kube-proxy-mpr55 1/1 Running 0 16d 10.0.4.13 node02 <none> <none>
kubernetes-dashboard kubernetes-dashboard-67b9478795-snqcq 1/1 Running 0 16d 10.244.2.3 node02 <none> <none>
编写一个 kube-busybox.yml 内容如下(也可以时json文件)
apiVersion: v1
kind: Pod
metadata:
name: static-busybox
namespace: default
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent #用于设置镜像拉取策略
command: ["sh","-c","while true;do env | grep app; sleep 3 ;done;"]
将它放置到 /root/static_pod 目录,再次观察Pod
[root@node02 ~]# kubectl get pod -A -o wide|grep node02
default static-busybox-node02 1/1 Running 1 (20s ago) 2s 10.244.2.10 node02 <none> <none>
kube-flannel kube-flannel-ds-7vbkn 1/1 Running 0 16d 10.0.4.13 node02 <none> <none>
kube-system kube-proxy-mpr55 1/1 Running 0 16d 10.0.4.13 node02 <none> <none>
kubernetes-dashboard kubernetes-dashboard-67b9478795-snqcq 1/1 Running 0 16d 10.244.2.3 node02 <none> <none>
发现多了一个 叫 static-busybox-node02的pod,将yml从 /root/static_pod目录移除,等待一小段时间,发现该Pod消失了。
尝试使用kubectl删除
[root@node02 ~]# kubectl delete pod static-busybox-node02
pod "static-busybox-node02" deleted
[root@node02 ~]# kubectl get pod -A -o wide|grep node02
default static-busybox-node02 0/1 Pending 0 2s <none> node02 <none> <none>
发现状态变为Pending,等一小段时间再次查看已经Running了。所以 kubectl 删除不了静态Pod,只能将配置文件移除目录
与配置文件一样,也是要通过添加启动参数,这里的启动参数为 --manifest-url。kubelet就会定期去下载。下载后就跟配置文件方式是一致的了
静态Pod就介绍到这里,后面我们介绍在容器内如何获取Pod信息,比如Pod的ip。
欢迎关注,学习不迷路!