ConfigMapsの作成
$ kubectl create configmap ~
NginxやApacheの設定ファイルをConfigMapに登録し、各ポッドのマニフェストで指定
$ kubectl create configmap my-config --from-file=path/to/bar
$ kubectl create configmap my-config --from-env-file=path/to/bar.env
yaml出力して保存
$ kubectl create configmap nginx-config \
--from-file=nginx.conf \
--dry-run=client -o yaml > nginx-config.yaml
Secretsの作成
$ kubectl create secret generic ~
パスワードやAPIキーなどを含むファイルに関してはSecretとして登録し、各ポッドのマニフェストで指定
$ kubectl create secret generic my-secret --from-file=path/to/bar
$ kubectl create secret generic my-secret --from-env-file=path/to/bar.env
$ kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Get documentation for a resource
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a deployment, replica set, or replication controller
autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
certificate Modify certificate resources
cluster-info Display cluster information
top Display resource (CPU/memory) usage
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
events List events
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash, zsh, fish, or powershell)
Subcommands provided by plugins:
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins
version Print the client and server version information
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
$ kubectl rollout ~
# Rollback to the previous deployment
$ kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
$ kubectl rollout status daemonset/foo
# Restart a deployment
$ kubectl rollout restart deployment/abc
# Restart deployments with the 'app=nginx' label
$ kubectl rollout restart deployment --selector=app=nginx
$ kubectl apply (-f FILENAME | -k DIRECTORY)
# Apply the configuration in pod.json to a pod
$ kubectl apply -f ./pod.json
# Apply resources from a directory containing kustomization.yaml - e.g. dir/kustomization.yaml
$ kubectl apply -k dir/
# Apply the JSON passed into stdin to a pod
$ cat pod.json | kubectl apply -f -
# Apply the configuration from all files that end with '.json'
$ kubectl apply -f '*.json'
# Note: --prune is still in Alpha
# Apply the configuration in manifest.yaml that matches label app=nginx and delete all other resources that are not in the file and match label app=nginx
$ kubectl apply --prune -f manifest.yaml -l app=nginx
$ kubectl edit (RESOURCE/NAME | -f FILENAME)
# Edit the service named 'registry'
$ kubectl edit svc/registry
# Use an alternative editor
$ KUBE_EDITOR="nano" kubectl edit svc/registry
# Edit the job 'myjob' in JSON using the v1 API format
$ kubectl edit job.v1.batch/myjob -o json
# Edit the deployment 'mydeployment' in YAML and save the modified config in its annotation
$ kubectl edit deployment/mydeployment -o yaml --save-config
# Edit the 'status' subresource for the 'mydeployment' deployment
$ kubectl edit deployment mydeployment --subresource='status'
$ kubectl get ~
# List all pods in ps output format
$ kubectl get pods
# List all pods in ps output format with more information (such as node name)
$ kubectl get pods -o wide
# List a single replication controller with specified NAME in ps output format
$ kubectl get replicationcontroller web
# List deployments in JSON output format, in the "v1" version of the "apps" API group
$ kubectl get deployments.v1.apps -o json
# List a single pod in JSON output format
$ kubectl get -o json pod web-pod-13je7
$ kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] – COMMAND [args…]
# Get output from running the 'date' command from pod mypod, using the first container by default
$ kubectl exec mypod -- date
# Get output from running the 'date' command in ruby-container from pod mypod
$ kubectl exec mypod -c ruby-container -- date
# Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod
# and sends stdout/stderr from 'bash' back to the client
$ kubectl exec mypod -c ruby-container -i -t -- bash -il
# List contents of /usr from the first container of pod mypod and sort by modification time
# If the command you want to execute in the pod has any flags in common (e.g. -i),
# you must use two dashes (--) to separate your command's flags/arguments
# Also note, do not surround your command and its flags/arguments with quotes
# unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr")
$ kubectl exec mypod -i -t -- ls -t /usr
# Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default
$ kubectl exec deploy/mydeployment -- date
# Get output from running 'date' command from the first pod of the service myservice, using the first container by default
$ kubectl exec svc/myservice -- date