[Kubernetes] ローカル環境でのMinikube/kubectlの使用

Minikubeのコマンド

コマンド操作
minikube <command> –help (または -h)Use “minikube <command> –help” for more information about a given command.
minikube sshLog into or run a command on a machine with SSH.
minikube ipRetrieves the IP address of the specified node, and writes it to STDOUT.
minikube profile listLists all valid minikube profiles and detects all possible invalid profiles.
minikube statusGets the status of a local Kubernetes cluster.
minikube startStarts a local Kubernetes cluster
minikube stopStops a local Kubernetes cluster. This command stops the underlying VM or container, but keeps user data intact. The cluster can be started again with the “start” command.
minikube deleteDeletes a local Kubernetes cluster. This command deletes the VM, and removes all associated files.
minikube node addAdds a node to the given cluster config, and starts it.
minikube addons listLists all available minikube addons as well as their current statuses (enabled/disabled)
minikube serviceReturns the Kubernetes URL(s) for service(s) in your local cluster. In the case of multiple URLs they will be printed one at a time.
minikube service listLists the URLs for the services in your local cluster
minikube tunneltunnel creates a route to services deployed with type LoadBalancer and sets their Ingress to their ClusterIP.

kubectlのコマンド

pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)

コマンド操作
kubectl <command> –help (または -h)Use “kubectl <command> –help” for more information about a given command.
kubectl versionPrint the client and server version information for the current context.
kubectl get <resource_name>Display one or many resources.
kubectl describe <resource_name>Show details of a specific resource or group of resources.
kubectl create deployment my-dep –image=nginx –replicas=3Create a deployment named my-dep that runs the nginx image with 3 replicas
kubectl create -f <filename>
kubectl edit deployment my-dep
kubectl logs <pod_name>Print the logs for a container in a pod or specified resource.
kubectl exec -it <pod_name> — /bin/bashExecute a command in a container.
kubectl delete deployment <deployment_name>
kubectl apply -f <file_name>Apply a configuration to a resource by file name or stdin.
kubectl expose <resource_name>

kubectl expose deployment my-nginx –type=NodePort –port=8080
Expose a resource as a new Kubernetes service.
kubectl scale –replicas=<n>Set a new size for a deployment, replica set, replication controller, or stateful set.
kubectl port-forward <pod_name> 5000
kubectl port-forward <pod_name> 8888:5000
Forward one or more local ports to a pod.

kubectlチートシート:https://kubernetes.io/ja/docs/reference/kubectl/cheatsheet/

アプリケーションのデプロイ

Dockerfileからイメージをビルドする

docker image build -t <app_image_name:tag> .
kubectl create -f ./<yaml_file_to_create_pod_for_app>
kubectl get pods

<yaml_file_to_create_pod_for_app>の内容

  • imagePullPolicyなしの場合、STATUSは「ErrImagePull」になる
  • imagePullPolicy: Neverの場合、STATUSは「ImagePullBackOff」になる
kubectl describe pod

シェルをMinikubeのDockerデーモンに向ける

eval $(minikube -p minikube docker-env)

注:上記のevalコマンドが効かない場合、下記のdocker image buildコマンドの代わりに「minikube image build -t <app_image_name:tag> .」でもイメージをビルドできる

docker image build -t <app_image_name:tag> .

<yaml_file_to_create_pod_for_app>に「imagePullPolicy: Never」を追記する

kubectl create -f <yaml_file_to_create_pod_for_app>
kubectl get pods

STATUSが「Running」になっている

Minikube内でアプリを実行する

kubectl exec -it <app_name> -- /bin/bash
curl http://127.0.0.1:5000/
exit

サーバからアプリを実行する

kubectl port-forward <app_pod_name> 5000
curl http://127.0.0.1:5000/