Search code examples
amazon-web-servicesamazon-ec2kubernetescontainerskubectl

Why can't deploy containers on k8s cluster on AWS EC2 correctly?


When I use AWS EC2 deploy k8s cluster, deploy a test container on it:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app-deployment-test
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: app-test
        tier: frontend
    spec:
      containers:
        - name: app
          image: ubuntu
          ports:
            - containerPort: 80
          imagePullPolicy: Always

Got

Back-off restarting failed container
Error syncing pod

error on pods. When describe the special pod: kubectl describe pod app-deployment-test-ccddf7bcc-dqltq, got these messages at tail of the output:

Events:
  Type     Reason                 Age                From                   Message
  ----     ------                 ----               ----                   -------
  Normal   Scheduled              41s                default-scheduler      Successfully assigned app-deployment-test-ccddf7bcc-dqltq to app-instance
  Normal   SuccessfulMountVolume  41s                kubelet, app-instance  MountVolume.SetUp succeeded for volume "default-token-zrf98"
  Normal   Pulling                14s (x3 over 40s)  kubelet, app-instance  pulling image "ubuntu"
  Normal   Pulled                 12s (x3 over 32s)  kubelet, app-instance  Successfully pulled image "ubuntu"
  Normal   Created                12s (x3 over 31s)  kubelet, app-instance  Created container
  Normal   Started                11s (x3 over 31s)  kubelet, app-instance  Started container
  Warning  BackOff                11s (x3 over 27s)  kubelet, app-instance  Back-off restarting failed container
  Warning  FailedSync             11s (x3 over 27s)  kubelet, app-instance  Error syncing pod

What will be the reason it failed?


Solution

  • The default command to ubuntu container is bash. This bash command will run one time and will stop the container, this is the expected behavior.

    If you want to keep the container running add the command and argument below.

    command: ["/bin/sh"] args: ["-c", "while true; do echo hello; sleep 10;done"]

    I recommend you to run the nginx container that will keep running until you kill the container.