Kubernetes 初始化容器及静态Pod和标签
初始化容器
kubernetes 1.3版本引入了init container 初始化容器特性。主要用于在启动应用容器(app container)前来启动一个或多个初始化容器,作为应用容器的一个基础。
init 1 --> init 2 --> 所有的初始化容器加载运行完成后,才能运行 应用容器 --> app container
实战举例
# 查看要修改的内核参数
[root@kmaster ~]# sysctl -a|grep vm.overcommit_ratio
vm.overcommit_ratio = 50
# 输出yaml文件
[root@kmaster ~]# kubectl run initpod --image centos --image-pull-policy IfNotPresent --dry-run=client -o yaml -- sleep 3600> /root/pod_yaml/initpod.yaml
[root@kmaster ~]# cd pod_yaml/
[root@kmaster pod_yaml]# ls
initpod.yaml
# 修改yaml文件
[root@kmaster pod_yaml]# vim initpod.yaml
[root@kmaster pod_yaml]# cat initpod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: initpod
name: initpod
spec:
containers:
- args:
- sleep
- "3600"
image: centos
imagePullPolicy: IfNotPresent
name: initpod
resources: {}
initContainers:
- name: initpod1
image: alpine
imagePullPolicy: IfNotPresent
command: ["/sbin/sysctl","-w","vm.overcommit_ratio=55"]
securityContext:
privileged: true
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# 编排Pod
[root@kmaster pod_yaml]# kubectl apply -f initpod.yaml
pod/initpod created
# 查看Pod
[root@kmaster pod_yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
initpod 1/1 Running 0 14s 10.244.69.210 knode2 <none> <none>
# 查看knode2上是否已经修改
[root@knode2 ~]# sysctl -a|grep vm.overcommit_ratio
vm.overcommit_ratio = 55
# 输出yaml文件
[root@kmaster pod_yaml]# kubectl run initpod2 --image centos --image-pull-policy IfNotPresent --dry-run=client -o yaml -- sleep 3600 > /root/pod_yaml/initpod2.yaml
[root@kmaster pod_yaml]# ls
initpod2.yaml initpod.yaml
# 修改yaml文件
[root@kmaster pod_yaml]# vim initpod2.yaml
[root@kmaster pod_yaml]# cat initpod2.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: initpod2
name: initpod2
spec:
containers:
- args:
- sleep
- "3600"
image: centos
imagePullPolicy: IfNotPresent
name: initpod2
resources: {}
volumeMounts:
- name: testdir
mountPath: /test
initContainers:
- name: alpine
image: alpine
imagePullPolicy: IfNotPresent
command: ["sh","-c","echo 123 > /initest/test.txt"]
volumeMounts:
- name: testdir
mountPath: /initest
volumes:
- name: testdir
emptyDir: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# 编排Pod
[root@kmaster pod_yaml]# kubectl apply -f initpod2.yaml
pod/initpod2 created
# 查看状态
[root@kmaster pod_yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
initpod 1/1 Running 0 32m 10.244.69.210 knode2 <none> <none>
initpod2 1/1 Running 0 2m25s 10.244.195.149 knode1 <none> <none>
# 进入容器查看是否挂载成功
[root@kmaster pod_yaml]# kubectl exec -it pods/initpod2 -- bash
Defaulted container "initpod2" out of: initpod2, alpine (init)
[root@initpod2 /]# cd /test/
[root@initpod2 test]# ls
test.txt
[root@initpod2 test]# cat test.txt
123
[root@initpod2 test]#
静态Pod
注意不要在master上操作,因为master上跑的是集群核心静态pod,在node上去做
# 创建一个目录
[root@knode1 ~]# mkdir /etc/kubernetes/test
# 编辑配置文件
[root@knode1 ~]# vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
[root@knode1 ~]# cat /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
# 在环境变量参数里面加上 --pod-manifest-path=/etc/kubernetes/test ,指向创建的目录
# 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 --pod-manifest-path=/etc/kubernetes/test"
# 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
# 重启k8s
[root@knode1 ~]# systemctl daemon-reload
[root@knode1 ~]# systemctl restart kubelet.service
# 在test目录里面编写yaml文件
[root@knode1 ~]# cd /etc/kubernetes/test
[root@knode1 test]# kubectl run pod1 --image nginx --image-pull-policy IfNotPresent --dry-run=client -o yaml > pod1.yaml
[root@knode1 test]# cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: pod1
name: pod1
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: pod1
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@knode1 test]#
[root@knode1 test]# ls
pod1.yaml
# 去master上查询,就会看到一个静态pod
[root@kmaster ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod1-knode1 0/1 ContainerCreating 0 14s
# 一旦移走了yaml文件,静态pod也将被删除
[root@knode1 test]# mv pod1.yaml /tmp/
[root@knode1 test]# ls
[root@kmaster ~]# kubectl get pod -o wide
No resources found in kongshuo namespace.