شروع کار با kubectl و دستورات پایه

  • مدرس: علی بیگدلی
  • تاریخ انتشار: 1402/06/18
  • تعداد بازدید: 128

دستورات متعددی برای استفاده از k8s وجود دارد که می توان از طریق kubectl این ارتباط را با سرویس مد نظر برقرار کرد درست همانند عملکردی که با داکر داشته اید. اما باید در نظر بگیرید که این دستورات به صورت کلی به دسته های متعددی تقسیم می شوند که در زیر بعضی از دستورات ابتدایی را با هم بررسی خواهیم کرد.

بررسی نسخه

برای اینکه بتوانید نسخه kubectl نصب شده بر روی سیستم خود را ببینید می توانید از دستور زیر استفاده نمایید:

$ kubectl version

Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.2", GitCommit:"7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647", GitTreeState:"clean", BuildDate:"2023-05-17T14:20:07Z", GoVersion:"go1.20.4", Compiler:"gc", Platform:"windows/amd64"}
Kustomize Version: v5.0.1
Server Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.2", GitCommit:"7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647", GitTreeState:"clean", BuildDate:"2023-05-17T14:13:28Z", GoVersion:"go1.20.4", Compiler:"gc", Platform:"linux/amd64"}


$ kubectl version --short
Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
Client Version: v1.27.2
Kustomize Version: v5.0.1
Server Version: v1.27.2

$ kubectl version --output=json
{
  "clientVersion": {
    "major": "1",
    "minor": "27",
    "gitVersion": "v1.27.2",
    "gitCommit": "7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647",
    "gitTreeState": "clean",
    "buildDate": "2023-05-17T14:20:07Z",
    "goVersion": "go1.20.4",
    "compiler": "gc",
    "platform": "windows/amd64"
  },
  "kustomizeVersion": "v5.0.1",
  "serverVersion": {
    "major": "1",
    "minor": "27",
    "gitVersion": "v1.27.2",
    "gitCommit": "7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647",
    "gitTreeState": "clean",
    "buildDate": "2023-05-17T14:13:28Z",
    "goVersion": "go1.20.4",
    "compiler": "gc",
    "platform": "linux/amd64"
  }
}

مشاهده مشخصات cluster

برای اینکه بتوانید مشخصات کلاستری که در آن قرار گرفته است را ببینید می توانید از دستور زیر استفاده نمایید:

$ kubectl cluster-info
Kubernetes control plane is running at https://kubernetes.docker.internal:6443
CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

جمع آوری اطلاعات از علمیات

برای اینکه بتوانید تمامی عملکرد های k8s که در حال اجرا و یا متوقف شده اند، که شامل pod، deployment، services و غیره می شود را ببینید از دستور زیر استفاده می کنید:

$ kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   3m10s

در صورتی که بخواهید فقط عوامل یک مجموعه مثلا سرویس ها را ببینید از دستور زیر استفاده می نمایید:

$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   3m32s

راه اندازی یک deployment 

در ساده ترین حالت برای اجرای یک image و اجرا و ساخت یک container مبتنی بر آن می توانید از دستور زیر استفاده نمایید:

kubectl run [container-name] --image=[image-name]

port forward

برای ایجاد سطح دسترسی به یک پاد از طریق پورت خروجی برنامه مد نظر می توانید از دستور زیر استفاده نمایید:

kubectl port-forward [pod] [ports]

expose port

برای اینکه بخواهید یک پورت را برای یک Deployment و یا یک pod برای دیگر سرویس ها در دسترس قرار دهید می توانید از دستور زیر استفاده نمایید، توجه داشته باشید که استفاده از expose روش های مختلفی دارد:

kubectl expose ...

ساخت

در جهت ساخت یک عامل می توانید از دستور create استفاده کنید که این عامل می تواند یک deployment ، service و یا هر چیز دیگری باشد:

kubectl create [resource]

ایجاد یا تغییر

برای ایجاد و یا تغییر یک عامل در حال اجرا می توانید از دستور apply استفاده نمایید:

kubectl apply [resource]

راهنما

برای اینکه بتوانید برای هر دستور نمونه و یا توضیحاتی از نحوه استفاده آن را دریافت نمایید می توانید از آپشن help استفاده کنید، به نمونه های زیر دقت نمایید:

$ kubectl --help

$ kubectl expose --help

$ kubectl apply --help
Apply a configuration to a resource by file name or stdin. The resource name must be specified. This resource will be
created if it doesn't exist yet. To use 'apply', always create the resource initially with either 'apply' or 'create
--save-config'.

 JSON and YAML formats are accepted.

 Alpha Disclaimer: the --prune functionality is not yet complete. Do not use unless you are aware of what the current
state is. See https://issues.k8s.io/34274.

Examples:
  # 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' - i.e. expand wildcard characters in file names
  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

  # Apply the configuration in manifest.yaml and delete all the other config maps that are not in the file
  kubectl apply --prune -f manifest.yaml --all --prune-allowlist=core/v1/ConfigMap

Available Commands:
  edit-last-applied   Edit latest last-applied-configuration annotations of a resource/object
  set-last-applied    Set the last-applied-configuration annotation on a live object to match the contents of a file
  view-last-applied   View the latest last-applied-configuration annotations of a resource/object

Options:
  ...

Usage:
  kubectl apply (-f FILENAME | -k DIRECTORY) [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).