Kubernetes笔记
导出现有的资源
1 | for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob) |
热更新deploy
有时候我们修改了ConfigMap,但是代码不支持,肯定不能让程序停止,因此必须支持热更新。命令如下:
1 | kubectl patch deployment [deploy] --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "'`date +%Y%m%d%H%M%S`'" }}}}}' |
拷贝secrets到其他namespace
1 | kubectl get secret gitlab-registry --namespace=revsys-com --export -o yaml |\ |
临时运行一个pod
--restart=Never
代表起一个pod--rm
在终端退出时删除pod-l
给pod打label
1 | kubectl run --rm -it busybox --image sequenceiq/busybox --restart=Never |
获取pod信息
1 | env: |
Scratch Debugger
This is a tool to make debugging containers based on scratch easier. The script
works by bringing up a pod with a statically-linked busybox image on the same
node as the debug target, mounting the node’s root filesystem, and calling
docker directly to copy busybox into the target container. Once the “install” is
complete, the target can be debugged through a standard kubectl exec.
Usage
1 | curl https://raw.githubusercontent.com/kubernetes/contrib/master/scratch-debugger/debug.sh | sh -s -- POD_NAME [-n POD_NAMESPACE -c CONTAINER_NAME] |
POD_NAME
- The name of the pod to debug.POD_NAMESPACE
- The namespace of the target pod (defaults todefault
).CONTAINER_NAME
- The name of the container in the pod to debug (defaults to the first container).
Additionally, the following environment variables can be set:
TMP_SUBDIR
- The subdirectory under/tmp
to install busybox into (defaults todebug-tools
).KUBECONTEXT
- The kubectl context to use (defaults to current context).DEBUGGER_NAME
- The name to use for the debug pod (defaults todebugger
).ARCH
- The architecture Kubernetes is running on (defaults toamd64
).DOCKER_DOWNLOAD_URL
- URL for downloading the docker release.tgz
file
(seedebug.sh
for the default value).
Example
Create a simple pause
pod, which is based off a scratch image and does nothing.
1 | $ kubectl create -f - <<EOF |
Note that we cannot simply exec into the pod, since there isn’t a shell or any
other interactive tools available:
1 | $ kubectl exec -i -t pause -- sh |
So we use the debug.sh
script to copy busybox (which includes many common
tools) into the container:
1 | $ scratch-debugger/debug.sh pause |
The script automatically execs into the pod and starts a shell (ash
) with thePATH
variable set to include the debug tools. After exiting, the tools are
still present in the pod, and we can simply exec back in using the command the
script gave us:
1 | $ kubectl exec -i -t pause -- /tmp/debug-tools/sh -c 'PATH=$PATH:/tmp/debug-tools sh' |
Alternatively, we can just call the debug.sh
script again:
1 | $ scratch-debugger/debug.sh pause |
Once we’ve finished debugging, it’s a good practice to delete the “tainted”
pod. If that is undesirable for some reason, you can simply delete the tools
from the container:
1 | $ kubectl exec pause -- /tmp/debug-tools/rm -r /tmp/debug-tools |
mysql-operator
简化在kubernetes内创建mysql集群(支持MySQL Group Replication)
github