kubectl:在没有 yml 文件的情况下创建副本集

kubectl: create replicaset without a yml file

我正在尝试使用 kubernetes 创建一个副本集。这一次,我没有 yml 文件,这就是我尝试使用命令行创建副本集的原因。 为什么 kubectl create replicaset somename --image=nginx 引发错误,如何解决?

您无法使用命令行创建 replicaset。使用 kubectl create 只能创建以下资源:

kubectl create  --help |awk '/Available Commands:/,/^$/'
Available Commands:
  clusterrole         Create a cluster role
  clusterrolebinding  Create a cluster role binding for a particular cluster role
  configmap           Create a config map from a local file, directory or literal value
  cronjob             Create a cron job with the specified name
  deployment          Create a deployment with the specified name
  ingress             Create an ingress with the specified name
  job                 Create a job with the specified name
  namespace           Create a namespace with the specified name
  poddisruptionbudget Create a pod disruption budget with the specified name
  priorityclass       Create a priority class with the specified name
  quota               Create a quota with the specified name
  role                Create a role with single rule
  rolebinding         Create a role binding for a particular role or cluster role
  secret              Create a secret using specified subcommand
  service             Create a service using a specified subcommand
  serviceaccount      Create a service account with the specified name

虽然,您可以使用以下方式创建副本集,但在下面的示例中,kubectl create -f 由 stdout(-) 提供:

echo "apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
" |kubectl create  -f -

您好,希望您享受 kubernetes 之旅!

其实不能直接创建RS,但是如果实在不想用manifest的话,完全可以通过deployment来创建:

❯ kubectl create deployment --image nginx:1.21 --port 80 test-rs
deployment.apps/test-rs created

这里是:

❯ kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
test-rs-5c99c9b8c             1         1         1       15s

bguess