CKA [Workoad] – Init Container的使用

Init Container官方文档:https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

根据以上的架构图,Init Container可以作为初始话的container来用。Init Container会在external storage当中获取reports,然后将其存在shared-volume当中,那么当你的app启动之后就能从shared-volume当中读书已经被init container来取的reports。

以下是Init Container的生命周期图,kubelet会运行InitContainer1 , 接着是InitContainer2 最后才是AppContainer,如果说InitContainer1或InitContainer2发生错误,那么AppContainer将无法正常启动。

使用案例

1. kubernetes是先运行check-google,然后再是check-kubernetes,最后才运行app-container

kubectl apply -f pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
  - name: app-container
    image: nginx
  initContainers:
  - name: check-google
    image: alpine/curl
    command: ["ping", "-c", "10", "google.com"]
  - name: check-kubernetes
    image: alpine/curl
    command: ["curl", "kubernetes.io"]

2. 可以使用watch命令来观察pod的变化

kubectl get pods -w

3. 特地执行错误的师范,把curl改成curl11

kubectl apply -f pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
  - name: app-container
    image: nginx
  initContainers:
  - name: check-google
    image: alpine/curl
    command: ["ping", "-c", "10", "google.com"]
  - name: check-kubernetes
    image: alpine/curl
    command: ["curl11", "kubernetes.io"]

结果是失败的,无法运行app-container,因为在check-kubernetes的pod当中执行失败了

【查看pod list】
kubectl get pods

【查看pod的错误内容】
kubectl describe pod myapp-pod

Loading

Facebook评论