kubectl ========= ###### tags: `kubernetes` <kbd>kubectl</kbd> allows you to run commands against Kubernetes clusters.You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. ## how to install ```shell= curl -LO "https://dl.k8s.io/release/v1.26.4/bin/darwin/amd64/kubectl" chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin/kubectl sudo chown root: /usr/local/bin/kubectl alias k=kubectl ``` ## useful "imperative" command lines > ==**explicit instructions**== ### management ```shell= kubectl version -o json kubectl cluster-info kubectl get nodes ``` ### config :::info The loading order follows these rules 1. `--kubeconfig` flag 2. `$KUBECONFIG` environment variable 3. `${HOME}/.kube/config` is used ::: ```shell= kubectl config view kubectl config get-clusters kubectl config get-contexts kubectl config get-users kubectl config use-context minikube kubectl config set-context $(kubectl config current-context) --namespace=dev ``` ### workload ```shell= kubectl api-versions kubectl api-resources kubectl get all -A ``` ```shell= kubectl get pods -n whale \ -l "app=blue-app" \ -o jsonpath="{.items[0].metadata.name}" ``` ### monitor ```shell= $ k top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% controlplane 353m 0% 1060Mi 0% node01 22m 0% 322Mi 0% ``` ```shell= $ k top po NAME CPU(cores) MEMORY(bytes) elephant 20m 32Mi lion 1m 18Mi rabbit 132m 252Mi ``` ### others ```shell= kubectl auth can-i get pods --all-namespaces kubectl auth can-i get pods -n default kubectl auth can-i create pods kubectl auth can-i delete pods kubectl auth can-i create deployments --as=dev-user kubectl auth can-i create pods --as=dev-user --namespace=test kubectl auth can-i list nodes --as=bruce # list 所有權限 kubectl auth can-i --list --as=jenkins ``` ```shell= kubectl set image deployment/nginx nginx=nginx:1.18 ``` ```shell= kubectl label pod/meesaging tier=message --overwrite ``` ## "declarative" command > ==**descibe outcome**== + create the object using `kubectl apply -f` command. ## JSON path ```shell= k get no -o custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu k get po -o jsonpath='{.items[0].spec.containers[0].image}' k get no -o jsonpath='{.items[*].metadata.name}{"\n"}{.items[*].status.capacity.cpu}' k get no --sort-by=.status.capacity.cpu kubectl -n admin2406 get deployment -o custom-columns=DEPLOYMENT:.metadata.name,CONTAINER_IMAGE:.spec.template.spec.containers[].image,READY_REPLICAS:.status.readyReplicas,NAMESPACE:.metadata.namespace --sort-by=.metadata.name > /opt/admin2406_data ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up