Ansible运行“ kubectl apply kube-flannel.yml”无效

时间:2019-02-22 01:49:05

标签: kubernetes flannel

这是ansible yml,我在ansible控制节点上运行它以应用主节点的法兰绒网络。而且我是root用户。

- name: Apply flannel network
  shell: sudo kubectl apply -f /tmp/.ansible/files/kube-flannel.yml

Ansible给我这个错误:

"unable to recognize \"/tmp/.ansible/files/kube-flannel.yml\": Get http://localhost:8080/api?timeout=32s: dial tcp [::1]:8080: connect: connection refused"]}

但是,当我手动登录主节点并运行sudo kubectl apply -f /tmp/.ansible/files/kube-flannel.yml时,它可以正常工作。

root@PC:~# kubectl apply -f /tmp/.ansible/files/kube-flannel.yml
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds-amd64 created
daemonset.extensions/kube-flannel-ds-arm64 created
daemonset.extensions/kube-flannel-ds-arm created
daemonset.extensions/kube-flannel-ds-ppc64le created
daemonset.extensions/kube-flannel-ds-s390x created

最后的问题是,如何解决ansible错误?

2 个答案:

答案 0 :(得分:0)

我在ansml yml中添加了以下cmd,则该pod已成功应用。导致具有新ssh会话的ansible访问主节点,环境值已被丢弃。

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

答案 1 :(得分:0)

只需将KUBECONFIG用作环境变量,如下所示:

- name: Apply flannel
  shell: |
    kubectl apply -f  /home/ubuntu/kube-flannel.yml
  environment:
    KUBECONFIG: /home/ubuntu/.kube/config
  when: inventory_hostname == "kube-master"