在Kubernetes v1.3.6中使用run测试nginx容器时出现弃用错误

52次阅读
没有评论

问题描述

在尝试运行一些nginx pods时,遇到了弃用错误。

bash-4.4$ kubectl run nginx --image=nginx --port=80 --replicas=3
WARNING: New generator "deployment/apps.v1beta1" specified, but it isn't available. Falling back to "deployment/v1beta1".
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
error: no matches for kind "Deployment" in version "apps/v1beta1"

用户还尝试添加apps generator时遇到了问题。

bash-4.4$ kubectl run nginx --image=nginx --port=80 --replicas=3 --generator=deployment/apps.v1beta1
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
error: no matches for kind "Deployment" in version "apps/v1beta1"

用户不确定发生了什么,他只是尝试了一个简单的hello world。以下是部署和暴露的playbook:

---
- hosts: localhost
  connection: local
  tasks:
    - name: Launch 3 nginx pods
      command: "kubectl run nginx --image=nginx --port=80 --replicas=3"
    - name: Expose nginx
      command: "kubectl expose deployment nginx --type NodePort"
    - name: Get exposed port
      command: "kubectl get svc nginx --output=jsonpath='{range .spec.ports[0]}{.nodePort}'"
      register: result
    - set_fact:
        node_port: "{{ result.stdout }}"
    - debug: msg="Exposed port {{ node_port }}"

以下是集群和版本的一些背景信息:

bash-4.4$ kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-30T21:39:38Z", GoVersion:"go1.11.1", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"3", GitVersion:"v1.3.6", GitCommit:"ae4550cc9c89a593bcda6678df201db1b208133b", GitTreeState:"clean", BuildDate:"2016-08-26T18:06:06Z", GoVersion:"go1.6.2", Compiler:"gc", Platform:"linux/amd64"}
bash-4.4$ kubectl get componentstatus
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-1               Healthy   {"health": "true"}
etcd-2               Healthy   {"health": "true"}
etcd-0               Healthy   {"health": "true"}

用户需要帮助,他正在一个黑客马拉松中卡住了一些人;)

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据错误信息,kubectl run命令的--generator选项已被弃用,并且在将来的版本中将被移除。相反,建议使用kubectl create命令来创建资源。
以下是使用kubectl create命令创建Deployment的步骤:
1. 创建一个YAML文件,命名为nginx-deployment.yaml,并将以下内容复制到文件中:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
  1. 运行以下命令来创建Deployment:
kubectl create -f nginx-deployment.yaml

这将创建一个名为nginx的Deployment,其中包含3个replica的nginx pods。

方案2

如果你仍然想使用kubectl run命令,你可以尝试使用旧版本的--generator选项,如deployment-basic/v1beta1
以下是使用旧版本--generator选项的步骤:
1. 修改playbook中的command命令,将--generator选项更改为deployment-basic/v1beta1

---
- hosts: localhost
  connection: local
  tasks:
    - name: Launch 3 nginx pods
      command: "kubectl run nginx --image=nginx --port=80 --replicas=3 --generator=deployment-basic/v1beta1"
    - name: Expose nginx
      command: "kubectl expose deployment nginx --type NodePort"
    - name: Get exposed port
      command: "kubectl get svc nginx --output=jsonpath='{range .spec.ports[0]}{.nodePort}'"
      register: result
    - set_fact:
        node_port: "{{ result.stdout }}"
    - debug: msg="Exposed port {{ node_port }}"
  1. 运行playbook:
ansible-playbook playbook.yml

这将使用旧版本的--generator选项来创建Deployment。

方案3

如果你的Kubernetes版本较旧,不支持apps/v1 API版本,你可以尝试使用extensions/v1beta1 API版本。
以下是使用extensions/v1beta1 API版本的步骤:
1. 修改playbook中的command命令,将--generator选项更改为deployment/extensions/v1beta1

---
- hosts: localhost
  connection: local
  tasks:
    - name: Launch 3 nginx pods
      command: "kubectl run nginx --image=nginx --port=80 --replicas=3 --generator=deployment/extensions/v1beta1"
    - name: Expose nginx
      command: "kubectl expose deployment nginx --type NodePort"
    - name: Get exposed port
      command: "kubectl get svc nginx --output=jsonpath='{range .spec.ports[0]}{.nodePort}'"
      register: result
    - set_fact:
        node_port: "{{ result.stdout }}"
    - debug: msg="Exposed port {{ node_port }}"
  1. 运行playbook:
ansible-playbook playbook.yml

这将使用extensions/v1beta1 API版本来创建Deployment。

方案4

如果你的Kubernetes版本较旧,不支持apps/v1extensions/v1beta1 API版本,你可以尝试使用kubectl run命令的--api-version选项来指定API版本。
以下是使用--api-version选项的步骤:
1. 修改playbook中的command命令,将--generator选项更改为deployment,并添加--api-version选项:

---
- hosts: localhost
  connection: local
  tasks:
    - name: Launch 3 nginx pods
      command: "kubectl run nginx --image=nginx --port=80 --replicas=3 --generator=deployment --api-version=extensions/v1beta1"
    - name: Expose nginx
      command: "kubectl expose deployment nginx --type NodePort"
    - name: Get exposed port
      command: "kubectl get svc nginx --output=jsonpath='{range .spec.ports[0]}{.nodePort}'"
      register: result
    - set_fact:
        node_port: "{{ result.stdout }}"
    - debug: msg="Exposed port {{ node_port }}"
  1. 运行playbook:
ansible-playbook playbook.yml

这将使用指定的API版本来创建Deployment。

方案5

如果你的Kubernetes版本较旧,不支持apps/v1extensions/v1beta1 API版本,你可以尝试升级你的Kubernetes版本以支持新的API版本。
请参考Kubernetes官方文档以了解如何升级你的Kubernetes集群。

总结

在Kubernetes v1.3.6中,kubectl run命令的--generator选项已被弃用,并且在将来的版本中将被移除。建议使用kubectl create命令来创建资源。如果你仍然想使用kubectl run命令,你可以尝试使用旧版本的--generator选项,如deployment-basic/v1beta1。如果你的Kubernetes版本较旧,不支持新的API版本,你可以尝试使用--api-version选项来指定API版本,或者升级你的Kubernetes版本以支持新的API版本。希望这些解决方案能帮助到你!

正文完