Kubernetes 初始化Pod容器和静态Pod

版权声明: https://blog.csdn.net/Andriy_dangli/article/details/86497075

1、init Container

通常我们希望在真正的应用启动之前去创建一些应用需要的文件、目录。在k8s资源中我们可以通过init container来实现。配置如下:
使用init container判断volume是否挂在成功,并切判断里卖弄是否写入文件

# cat << EOF > init-container.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: init-pod
  namespace: andriy-dang
  labels:
    app: myapp
spec:
  volumes:
  - name: emptydir
    emptyDir: {}
  containers:
  - name: myapp-container
    image: busybox
    command: ["sh", "-c", "if [ -f /test/file ]; then sleep 10000; else exit 1;fi"]
    volumeMounts:
    - mountPath: /test
      name: emptydir
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'echo "hello" > /test/file']
    volumeMounts:
    - mountPath: /test
      name: emptydir
EOF
  
# 创建Pod之后Pod的状态会发生如下变化:
kubectl get pod -n andriy-dang --watch
	NAME       READY     STATUS     RESTARTS   AGE
	init-pod   0/1       Init:0/1   0          3s
	init-pod   0/1       PodInitializing   0         8s
	init-pod   1/1       Running   0         15s

2、static pod

静态pods是直接由特定节点上的kubelet进程来管理,不通过主控节点上的API服务器。静态pod不关联任何replication controller,它由kubelet进程自己来监控,当pod崩溃时重启该pod。对于静态pod没有健康检查。静态pod始终绑定在某一个kubelet,并且始终运行在同一个节点上。

# static-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: static-web
spec:
  containers:
    - name: web
      image: nginx:alpine

修改默认静态pod的发布目录:
1、kubeadm安装的k8s集群,配置文件路径:/var/lib/kubelet/config.yaml,修改文件的倒数第四行

staticPodPath: /etc/kubernetes/manifests

2、二进制文件安装的k8s集群,配置文件路径:/usr/lib/systemd/system/kubelet.service,在文件中添加一行--pod-manifest-path=,例如:

[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=docker.service
Requires=docker.service
[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/usr/local/bin/kubelet \
--address=192.168.8.12 \
--hostname-override=k8s-master \
--experimental-bootstrap-kubeconfig=/etc/kubernetes/bootstrap.kubeconfig \
--kubeconfig=/etc/kubernetes/kubelet.kubeconfig \
--cert-dir=/etc/kubernetes/ssl \
--hairpin-mode promiscuous-bridge \
--allow-privileged=true \
--serialize-image-pulls=false \
--logtostderr=true \
--cgroup-driver=systemd \
--cluster_dns=10.254.0.2 \
--pod-manifest-path=/tmp/staticpod/ \
--cluster_domain=cluster.local \
--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

猜你喜欢

转载自blog.csdn.net/Andriy_dangli/article/details/86497075