Skip to content

Instantly share code, notes, and snippets.

@nexus166
Forked from jjo/kubectl-root-in-host-nopriv.sh
Last active May 24, 2019 05:12
Show Gist options
  • Select an option

  • Save nexus166/a3aa3977dd02122e64c972d4d289435f to your computer and use it in GitHub Desktop.

Select an option

Save nexus166/a3aa3977dd02122e64c972d4d289435f to your computer and use it in GitHub Desktop.
`privileged: true` + `nsenter`
#!/bin/sh
node=${1}
if [ -n "${node}" ]; then
shift
nodeName=$(kubectl get node ${node} -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}') || exit 1
nodeSelector='"nodeSelector": { "kubernetes.io/hostname": "'${nodeName:?}'" },'
podName=${USER+${USER}-}sudo-${node}
else
nodeSelector=""
podName=${USER+${USER}-}sudo
fi
set -x
kubectl run ${podName:?} --restart=Never -it \
--image overriden --overrides '
{
"spec": {
"hostPID": true,
"hostNetwork": true,
'"${nodeSelector?}"'
"tolerations": [{
"effect": "NoSchedule",
"key": "node-role.kubernetes.io/master"
}],
"containers": [
{
"name": "alpine",
"image": "alpine:latest",
"command": ["nsenter", "--mount=/proc/1/ns/mnt", "--", "/bin/sh"],
"stdin": true,
"tty": true,
"resources": {"requests": {"cpu": "10m"}},
"securityContext": {
"privileged": true
}
}
]
}
}' --rm --attach "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment