南京区域钢板租赁 发表于 2023-1-2 10:12:00

静态pod 创建使用示例详解

一.系统环境

服务器版本docker软件版本Kubernetes(k8s)集群版本CPU架构CentOS Linux release 7.4.1708 (Core)Docker version 20.10.12v1.21.9x86_64Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点
服务器操作系统版本CPU架构进程功能描述k8scloude1/192.168.110.130CentOS Linux release 7.4.1708 (Core)x86_64docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calicok8s master节点k8scloude2/192.168.110.129CentOS Linux release 7.4.1708 (Core)x86_64docker,kubelet,kube-proxy,calicok8s worker节点k8scloude3/192.168.110.128CentOS Linux release 7.4.1708 (Core)x86_64docker,kubelet,kube-proxy,calicok8s worker节点
二.前言

本文介绍静态 Pod,静态 Pod 在指定的节点上由 kubelet 守护进程直接管理,不需要 API 服务器监管。
创建静态pod的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》https://www.jb51.net/article/266741.htm

三.静态pod


3.1 何为静态pod

静态 Pod 在指定的节点上由 kubelet 守护进程直接管理,不需要 API 服务器监管。 与由控制面管理的 Pod(例如,Deployment) 不同;kubelet 监视每个静态 Pod(在它失败之后重新启动)。静态 Pod 始终都会绑定到特定节点的 Kubelet 上。
kubelet 会尝试通过 Kubernetes API 服务器为每个静态 Pod 自动创建一个镜像 Pod。 这意味着节点上运行的静态 Pod 对 API 服务来说是可见的,但是不能通过 API 服务器来控制。 Pod 名称将把以连字符开头的节点主机名作为后缀。
说明:如果你在运行一个 Kubernetes 集群,并且在每个节点上都运行一个静态 Pod, 就可能需要考虑使用 DaemonSet 替代这种方式。静态 Pod 的 spec 不能引用其他 API 对象 (如:ServiceAccount、 ConfigMap、 Secret 等)。

3.2 创建静态pod

静态pod的应用场景为:1.使master能正常启动 2.如果某天我们的master崩溃了,如何让别人知道我们的服务器在维护?
目前该命名空间是没有pod运行的
# kubectl get pods
No resources found in pod namespace.静态pod的创建方法为:写一个yaml文件,然后把yaml文件放在指定目录,会自动根据yaml文件创建pod。有两种方法来指定这个目录:
--pod-manifest-path
/etc/kubernetes/manifests
3.2.1 使用--pod-manifest-path指定静态pod目录

查看kubelet的配置文件位置,可以看到kubelet的配置文件在/usr/lib/systemd/system/kubelet.service
注意:我们是在k8s集群的worker节点k8scloude2上创建静态pod的
# 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
   Active: active (running) since 六 2022-01-15 12:27:34 CST; 5h 30min ago
   Docs: https://kubernetes.io/docs/
Main PID: 947 (kubelet)
   Memory: 122.6M
   CGroup: /system.slice/kubelet.service
         └─947 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --network-plugin=cni --pod-infr...修改kubelet的配置文件/usr/lib/systemd/system/kubelet.service,使用--pod-manifest-path=/etc/kubernetes/kubelet.d指定静态pod目录。
# vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
#--pod-manifest-path=/etc/kubernetes/kubelet.d表示静态pod的目录为/etc/kubernetes/kubelet.d
# cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
# Note: This dropin only works with kubeadm and kubelet v1.11+

Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/kubelet.d"
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创建静态pod目录,并使kubelet配置文件生效
# mkdir /etc/kubernetes/kubelet.d
# systemctl daemon-reload
# systemctl restart kubelet
# 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
   Active: active (running) since 六 2022-01-15 18:02:15 CST; 6s ago
   Docs: https://kubernetes.io/docs/
Main PID: 108844 (kubelet)
   Memory: 30.7M
   CGroup: /system.slice/kubelet.service
         ├─108844 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/kubelet.d --config=/var/lib/ku...
         └─108999 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/kubelet.d --config=/var/lib/ku...进入静态pod的目录,然后创建pod yaml文件
# cd /etc/kubernetes/kubelet.d/
# ls
# vim pod.yaml
# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
    run: pod
name: pod
spec:
containers:
- image: nginx
    imagePullPolicy: IfNotPresent
    name: pod
    resources: {}
    ports:
    - name: http
      containerPort: 80
      protocol: TCP
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}在k8s集群的master节点上查看pod,在master上可以看到该pod,由于pod.yaml没有指定namespace,默认在default下,可以看到Pod 名称(pod-k8scloude2)是以连字符开头的节点主机名作为后缀。
# kubectl get pods -n default -o wide
NAME             READY   STATUS    RESTARTS   AGE    IP               NODE         NOMINATED NODE   READINESS GATES
pod-k8scloude2   1/1   Running   0          109s   10.244.112.153   k8scloude2   <none>         <none>当把这个pod.yaml文件从静态pod目录移走,pod就消失了
# mv pod.yaml ~/
# ls
#当把这个yaml文件移走,pod消失
# kubectl get pods -n default -o wide
No resources found in default namespace.现在指定静态pod的namespace为pod
# vim pod.yaml
#namespace: pod:指定pod的命名空间
# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
    run: pod
name: pod
namespace: pod
spec:
containers:
- image: nginx
    imagePullPolicy: IfNotPresent
    name: pod
    resources: {}
    ports:
    - name: http
      containerPort: 80
      protocol: TCP
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# ls
pod.yaml查看pod
# kubectl get pods -n pod
NAME             READY   STATUS    RESTARTS   AGE
pod-k8scloude2   1/1   Running   0          6s
3.2.2 静态pod默认目录/etc/kubernetes/manifests

注意:进行这一步的时候,先还原kubelet配置文件/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
查看配置文件,可以发现静态pod默认目录为/etc/kubernetes/manifests
# cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
# Note: This dropin only works with kubeadm and kubelet v1.11+

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
# ls /var/lib/kubelet/config.yaml
/var/lib/kubelet/config.yaml
# cat /var/lib/kubelet/config.yaml | grep manifest
staticPodPath: /etc/kubernetes/manifests
#默认的静态pod的目录为 ls /etc/kubernetes/manifests
# ls /etc/kubernetes/manifests在默认的静态pod目录/etc/kubernetes/manifests/下创建pod yaml文件
#namespace: pod:指定pod的命名空间
# vim ~/pod.yaml
# cat ~/pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
    run: pod
name: pod
namespace: pod
spec:
containers:
- image: nginx
    imagePullPolicy: IfNotPresent
    name: pod
    resources: {}
    ports:
    - name: http
      containerPort: 80
      protocol: TCP
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# cp ~/pod.yaml /etc/kubernetes/manifests/可以发现,静态pod已经创建了
# kubectl get pods -n pod
NAME             READY   STATUS    RESTARTS   AGE
pod-k8scloude2   1/1   Running   0          6s删除yaml文件,静态pod消失
#删除yaml文件
# rm -rf /etc/kubernetes/manifests/pod.yaml
#pod消失
# kubectl get pods -n pod
No resources found in pod namespace.前面几步,静态pod是在k8s集群的worker节点上做的,现在在k8s集群的master节点上做。
注意:如果在/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf里添加了--pod-manifest-path=/etc/kubernetes/kubelet.d,则相应的/etc/kubernetes/manifests/下的yaml文件也要移动到/etc/kubernetes/kubelet.d目录下,不然k8s集群的master节点启动不起来。
可以看到k8s集群的master节点有很多静态pod。
# ls /etc/kubernetes/manifests/
etcd.yamlkube-apiserver.yamlkube-controller-manager.yamlkube-scheduler.yaml以上就是静态pod 创建使用示例详解的详细内容,更多关于静态pod创建使用的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/article/266746.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 静态pod 创建使用示例详解