Ansible shell模块执行kubectl错误连接到localhost被拒绝

时间:2018-04-15 21:52:46

标签: kubernetes ansible kubectl

我有一个用于构建kubernetes集群的ansible脚本。如果我通过shell模块运行kubectl

- name: Ensure kube-apiserver-to-kubelet ClusterRole is applied
  shell: "kubectl apply -f kube-apiserver-to-kubelet.yaml"
  delegate_to: controller1
  run_once: true

我收到以下错误“与服务器localhost:8080的连接被拒绝 - 您是否指定了正确的主机或端口?”:

fatal: [controller1 -> 10.240.0.11]: FAILED! => {"changed": true, "cmd": "kubectl apply -f kube-apiserver-to-kubelet.yaml", "delta": "0:00:00.116446", "end": "2018-04-15 21:42:51.023786", "msg": "non-zero return code", "rc": 1, "start": "2018-04-15 21:42:50.907340", "stderr": "The connection to the server localhost:8080 was refused - did you specify the right host or port?", "stderr_lines": ["The connection to the server localhost:8080 was refused - did you specify the right host or port?"], "stdout": "", "stdout_lines": []}

但是,如果我登录controller1(我使用堡垒主机作为节点的代理。)并执行命令它运行没有问题:

kubectl apply -f kube-apiserver-to-kubelet.yaml
clusterrole.rbac.authorization.k8s.io "system:kube-apiserver-to-kubelet" configured
clusterrolebinding.rbac.authorization.k8s.io "system:kube-apiserver" configured

为什么它直接在节点上工作,但不能通过ansible工作,我需要做些什么才能使这次运行不失败?

1 个答案:

答案 0 :(得分:0)

可能没有拿起你的kubeconfig(localhost:8080是未配置的默认值)

我要么使用内置的“k8s_raw”模块,要么在shell脚本中指定--kubeconfig参数