Last active
November 19, 2018 14:13
-
-
Save Yoshyn/97d16ee4a71cf6ad4f928ebb34eb9d3c to your computer and use it in GitHub Desktop.
.Manage Kubernete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Contains : | |
# * attachment-rc.yml : How to attach a console to a process inside a pod | |
# * script.sh : Set of script for managing pods easily | |
# * minikube_on-mac.sh : Install minikube on macos | |
# Strace & Docker | |
# https://medium.com/@rothgar/how-to-debug-a-running-docker-container-from-a-separate-container-983f11740dc6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: attachment | |
spec: | |
containers: | |
- name: count | |
image: ubuntu | |
stdin: true | |
tty: true | |
args: [bash, -c, | |
'for ((i = 0; i < 20 ; i++)); do echo "$i: $(date)"; sleep 1; read; done'] | |
# Config related to docker-for-mac only to check configmap & volume | |
envFrom: | |
- configMapRef: | |
name: env-config | |
volumeMounts: | |
- name: data | |
mountPath: /host_data/ | |
volumes: | |
- name: data | |
hostPath: | |
path: /Users/.../kubernetes/data | |
# File credentials/env-config.yaml | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: env-config | |
namespace: default | |
data: | |
SAMPLE_ENV_VAR: SOME_VALUE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to attach a console to a script running on a pod in kubernetes : | |
kubectl create -f attachment-rc.yml | |
kubectl attach -ti attachment | |
kubectl delete -f attachment-rc.yml | |
This can be usefull for binding (debugging) purpose. | |
## check configmap & volume with docker-for-mac | |
# Create the credential | |
kubectl create -f ./credentials/env-config.yaml --context docker-for-desktop | |
# Launch the pod | |
kubectl create -f attachment-rc.yml --context=docker-for-desktop | |
# Verify that all is ok | |
kubectl get pods --context=docker-for-desktop | |
kubectl describe pod --context=docker-for-desktop | |
# Connect to the pod | |
kubectl exec -it --context docker-for-desktop attachment bash | |
# Verify that the credential is set : | |
> env | grep SAMPLE_ENV_VAR | |
> touch /host_data/toto # verify the file exist also on the host system | |
# IF NEEDED, delete the pod | |
kubectl delete -f attachment-rc.yml --context=docker-for-desktop --grace-period=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brew install hyperkit | |
brew install docker-machine-driver-hyperkit | |
sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-hyperkit/bin/docker-machine-driver-hyperkit | |
sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-hyperkit/bin/docker-machine-driver-hyperkit | |
#rm -rf ~/.minikube | |
minikube stop | |
minikube delete | |
echo "Removing hyperkit.pid file if present (https://github.com/kubernetes/minikube/issues/1926)..."; | |
if [ -f ~/.minikube/machines/minikube/hyperkit.pid ]; then | |
sudo rm ~/.minikube/machines/minikube/hyperkit.pid | |
sudo rm -rf /var/db/dhcpd_leases | |
fi | |
echo "Configuring Minikube resource usage..."; | |
machine_cpus="$(sysctl -n hw.ncpu)"; | |
machine_memory="$(expr $(sysctl -n hw.memsize) / 1048576)"; # Bytes to megabytes conversion (x/2^20) | |
minikube_cpus="$(expr ${machine_cpus} / 2)"; | |
minikube_memory="$(expr ${machine_memory} / 2)"; | |
echo "Machine resources: ${machine_cpus} CPUs / ${machine_memory}MB Memory"; | |
echo "Configure Minikube to use 50%: ${minikube_cpus} CPUs / ${minikube_memory}MB Memory"; | |
echo "Running 'minikube start --vm-driver=hyperkit --cpus ${minikube_cpus} --memory ${minikube_memory}'..." | |
minikube start --vm-driver=hyperkit --cpus ${minikube_cpus} --memory ${minikube_memory} --v=10 --kubernetes-version v1.9.11 --mount --mount-string /Users:/Users --profile minihyperkit; | |
minikube addons enable ingress; | |
echo "Minikube started. Status:"; | |
minikube status; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# kube_console : Easy connect to your pods | |
# dependance : fzf, highlight | |
kube_console() { | |
local window_cols=`tput cols` | |
local window_lines=`tput lines` | |
local context=${$(kubectl config view -o jsonpath='{range .contexts[*]}{@.name}{"\t"}(cluster={@.context.cluster}, namespace={@.context.namespace}, user={@.context.user}){"\n"}{end}' | sort | fzf --header="Select a context :" --exit-0 --select-1 --height 20% | awk '{print $1}'):-vagrant} | |
local pods=$(kubectl get pods --context=$context -o=jsonpath='{.items[?(@.status.phase=="Running")].metadata.name}') | |
local pod=$(echo $pods | tr ' ' '\n' | fzf --header='Select a pod :' --preview="kubectl describe pods {} --context=$context | highlight -O ansi --syntax=yaml" | awk '{print $1}') | |
local shell=$(kubectl exec $pod --context=$context -i -t -- find /bin /usr/bin -regex '^\(/usr\)*/bin/\(fish\|ash\|sh\)$' | awk '{ print length($0) " " $0; }' | sort -r -n | cut -d ' ' -f 2- | head -1 | tr -d '\r') | |
local shell_cmd="${@:-"\"$shell\""}" | |
local CMD=(kubectl exec -ti $pod env COLUMNS=$window_cols LINES=$window_lines --context=$context -- /bin/sh -c "$shell_cmd") | |
echo "Connection to $pod on $context with the following command : " | |
echo $CMD | |
$CMD | |
} | |
# kube_logs : Easy get logs from your pods | |
# dependance : fzf, stern, highlight | |
kube_logs() { | |
local context=${$(kubectl config view -o jsonpath='{range .contexts[*]}{@.name}{"\t"}(cluster={@.context.cluster}, namespace={@.context.namespace}, user={@.context.user}){"\n"}{end}' | sort | fzf --header="Select a context :" --exit-0 --select-1 --height 20% | awk '{print $1}'):-vagrant} | |
local application=$(kubectl get pods --context=$context -o=jsonpath='{.items[?(@.status.phase=="Running")].metadata.labels.app}' | tr ' ' '\n' | sort | uniq | fzf --header='Application label :' --preview="echo \"Pod(s) : \n\" &&kubectl get pods --context=$context --selector app={} && echo \"\nHost(s) : \n\" && kubectl get ingress {} --context=$context -o jsonpath='{.spec.rules[*].host}'") | |
local CMD | |
CMD=(stern $application --context=$context --selector app=$application --timestamps --tail 50 --color always --exclude ".path.:.(/health/all|/api(?:/v\d+)?/public/build_version).") | |
echo "Get aggregated logs for $application query on $context with the following command : " | |
echo $CMD | |
$CMD | |
} | |
# kube_attach : Attach to a pod that has stdin && tty enable | |
kube_attach() { | |
local available_pods_data=$(kubectl get pods -o=jsonpath="{range .items[*]}[{.metadata.name}, {.metadata.namespace}, available_{.spec.containers[*]['.stdin','.tty']}_]{end}" | tr "][" '\n' | grep -i "available_true true_") | |
local pod=$(echo "$available_pods_data" | tr "," " " | awk '{print $1}' | fzf --header='Select a pod :') | |
local CMD | |
CMD=(kubectl attach -ti $pod) | |
echo "Attach to pod : $pod on default context & default namespace with the following command : " | |
echo $CMD | |
$CMD | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment