How to stop/start containers at k3s agent?

10/20/2020

Docker provides the following functions to stop and start the same container.

OP46B1:/ # docker stop 18788407a60c
OP46B1:/ # docker ps -a                                                                                                      
  CONTAINER ID  IMAGE   COMMAND   CREATED       STATUS    PORTS      NAMES                                                                                                                      
  18788407a60c ubuntu:test "/bin/bash" 34 minutes ago Exited (0) 7 seconds ago charming_gagarin
OP46B1:/ # docker start 18788407a60c

But k3s agent does not provide this function. A container stopped by "k3s crictl stop" cannot be restarted by "k3s crictl start". The following error will appear. How to stop and start the same container at k3s agent?

OP46B1:/data # ./k3s-arm64 crictl stop 5485f899c7bb6
  5485f899c7bb6
OP46B1:/data # ./k3s-arm64 crictl ps -a
  CONTAINER  IMAGE  CREATED  STATE NAME  ATTEMPT POD ID
  5485f899c7bb6 b58be220837f0 3 days ago  Exited  pod-webapp86  0 92a94e8eec410
OP46B1:/data# ./k3s-arm64 crictl start 5485f899c7bb6
  FATA[2020-10-20T00:54:04.520056930Z] Starting the container "5485f899c7bb6" failed: rpc error: code = Unknown desc = failed to set starting state for container "5485f899c7bb6f2d294a3a131b33d8f35c9cf84df73cacb7b8af1ee48a591dcf": container is in CONTAINER_EXITED state
-- Wei Yang
containers
k3s
kubernetes

1 Answer

11/12/2020

k3s is a distribution of kubernetes. Kubernetes is an abstraction over the container framework (containerd/docker/etc.). As such, you shouldn't try to control the containers directly using k3s crictl, but instead use the pod abstraction provided by kubernetes.

k3s kubectl get pods -A will list all the pods that are currently running in the k3s instance.1
k3s kubectl delete pod -n <namespace> <pod-selector> will delete the pod(s) specified, which will stop (and delete) their containers.2

-- T0xicCode
Source: StackOverflow