【问题标题】:kubectl run does not create replicacontrollerkubectl run 不创建副本控制器
【发布时间】:2026-01-08 01:45:02
【问题描述】:

在使用 Google Cloud Container 时,我是 Kubernetes 的新手。我只是按照以下教程进行操作:

https://cloud.google.com/container-engine/docs/tutorials/http-balancer http://kubernetes.io/docs/hellonode/#create-your-pod

在这些教程中,我会在运行“kubectl run”后获取副本控制器,但没有副本控制器,因此我无法运行“kubectl expose rc”命令打开端口。

这是我的命令结果:

ChangMatthews-MacBook-Pro:frontend changmatthew$ kubectl run nginx --image=nginx --port=80
deployment "nginx" created

ChangMatthews-MacBook-Pro:frontend changmatthew$ kubectl expose rc nginx --target-port=80 --type=NodePort
Error from server: replicationcontrollers "nginx" not found

这是我运行“kubectl get rc,svc,ingress,deployments,pods”时的结果:

ChangMatthews-MacBook-Pro:frontend changmatthew$ kubectl get rc,svc,ingress,deployments,pods
NAME                    CLUSTER-IP   EXTERNAL-IP   PORT(S)           AGE
kubernetes              10.3.240.1   <none>        443/TCP           12m
NAME                    RULE         BACKEND       ADDRESS           AGE
basic-ingress           -            nginx:80      107.178.247.247   12m
NAME                    DESIRED      CURRENT       UP-TO-DATE        AVAILABLE   AGE
nginx                   1            1             1                 1           11m
NAME                    READY        STATUS        RESTARTS          AGE
nginx-198147104-zgo7m   1/1          Running       0                 11m

我的解决方案之一是创建定义副本控制器的 yaml 文件。但是有没有办法像上面的教程一样通过 kubectl run 命令创建副本控制器?

谢谢,

【问题讨论】:

    标签: docker google-cloud-platform kubernetes


    【解决方案1】:

    现在 kubectl run 创建了一个部署,您可以指定在部署中公开的类型而不是复制控制器:

    kubectl expose deployment nginx --target-port=80 --type=NodePort
    

    【讨论】:

    • kubectl run 没有为我创建部署!
    • 另外仍然无法从外部80端口访问服务。
    【解决方案2】:

    团队可能仍在更新文档以反映 1.2。注意你得到的输出:

    $ kubectl 运行 nginx --image=nginx --port=80
    部署“nginx”已创建

    kubectl run 现在创建一个 deployemtn+replica-set。 要查看这些,您可以分别执行 kubectl get deployment 和 get rs。 部署本质上是一种更好的方式来执行滚动更新服务器端,但还有更多。参见文档:http://kubernetes.io/docs/user-guide/deployments/

    【讨论】:

    【解决方案3】:

    在 1.15.0 版本中,它的工作原理如下。

    root@k8smaster ~]# kubectl run guestbook --image=coolguy/k8s_guestbook:1.0 --port=8080 --generator=run/v1
    kubectl run --generator=run/v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create
    instead.
    ***replicationcontroller/guestbook created***
    

    在 1.19.0 版本中:

    [root@k8smaster ~]# kubectl run guestbook --image=dmsong2008/k8s_guestbook:1.0 --port=8080 --generator=run/v1
    ***Flag --generator has been deprecated, has no effect and will be removed in the future.***
    pod/guestbook created
    

    【讨论】: