Skip to content

Instantly share code, notes, and snippets.

@amshinde
Last active June 12, 2024 19:11
Show Gist options
  • Save amshinde/527beaf8b7b57c8e4c8b66bb3c73026b to your computer and use it in GitHub Desktop.
Save amshinde/527beaf8b7b57c8e4c8b66bb3c73026b to your computer and use it in GitHub Desktop.
  1. Create Pod config json file with privileged set as true in the security_context. Without a priveged pod, containerd does not allow a privileged container to be run in the sandbox.
{
    "metadata": {
        "name": "priv-sandbox",
        "namespace": "default",
        "uid": "0"
    },
    "log_directory": "/tmp",
    "linux": {
        "security_context": {
           "privileged": true
       }
    }
}
  1. Create container json:
cat privileged_container.json 
{
  "metadata": {
    "name": "privileged-container",
    "namespace": "default"
  },
  "image": {
    "image": "busybox"
  },
  "command": [
    "sh",
    "-c",
    "sleep 3600"
  ],
  "mounts": [],
  "log_path": "privileged-container.log",
  "linux": {
    "security_context": {
      "privileged": true
    }
  }
}
  1. Run Pod Sandbox with crictl. Pass the runtime to be used.
POD_ID=$(sudo crictl runp --runtime=kata-qemu pod-config.json)

The above should create a privilged sandbox. If privileged_without_host_devices is set in the containerd config file, the privileged sandbox will be created without passing all host devices.

You can inspect the pod with

sudo crictl inspectp priv-sandbox

You can verify that the kata shim and hypervisor are running at this point with ps aux | grep kata.

  1. Create the container inside the pod and start it:
CONTAINER_ID=$(sudo crictl create $POD_ID privileged_container.json pod-config.json)
sudo crictl start $CONTAINER_ID
sudo crictl ps

If the busybox image is not present on the system, you may need to pull the image with sudo crictl pull busybox. If you get any errors with this step, make sure your crictl config looks like this:

sudo cat    /etc/crictl.yaml 
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 2
debug: true
  1. Stop the container and pod with :
sudo crictl stop $CONTAINER_ID
sudo crictl stopp $POD_ID
sudo crictl rmp $POD_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment