简体   繁体   中英

Kubernetes/kubectl: unable to connect to a server to handle “pods”

I'm new to Kubernetes so I encountered the following issue. These are my steps:

1) I ran etcd :

docker run --volume=/var/etcd:/var/etcd --net=host -d gcr.io/google_containers/etcd:2.0.12 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-addr=0.0.0.0:4001 --data-dir=/var/etcd/data

2) I ran the master container:

docker run \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/dev:/dev \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
--volume=/var/run:/var/run:rw \
--net=host \
--pid=host \
--privileged=true \
-d gcr.io/google_containers/hyperkube:v1.0.1 \
/hyperkube kubelet --containerized --hostname-override="127.0.0.1" --address="0.0.0.0" --api-servers=http://localhost:8080 --config=/etc/kubernetes/manifests

3) I ran the proxy:

docker run -d --net=host --privileged gcr.io/google_containers/hyperkube:v1.0.1 /hyperkube proxy --master=http://127.0.0.1:8080 --v=2

4) I installed kubectl

5) I created this simple pod-file.yml :

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]

and tried to create pod by running:

kubectl create -f pod-file.yml

And I got:

ubuntu@ubuntu:~$ kubectl create -f pod-file.yml
error: could not read an encoded object from pod-file.yml: unable to connect to a server to handle "pods": couldn't read version from server: Get http://localhost:8080/api: dial tcp 127.0.0.1:8080: connection refused

I found it pretty odd so I checked containers I ran earlier:

ubuntu@ubuntu:~$ docker ps
CONTAINER ID        IMAGE                                       COMMAND                  CREATED             STATUS                                                                           PORTS               NAMES
3ae7f094bb01        gcr.io/google_containers/hyperkube:v1.0.1   "/hyperkube proxy ..."   55 minutes ago      Up 55 minutes                                                                                        suspicious_ramanujan
ed841bc6ef26        gcr.io/google_containers/hyperkube:v1.0.1   "/hyperkube kubele..."   57 minutes ago      Up 57 minutes                                                                                        competent_mclean
7408c640a2c8        gcr.io/google_containers/etcd:2.0.12        "/usr/local/bin/et..."   About an hour ago   Up About an hour                                                                                     elated_shaw

So looks like all is ok because all containers are up and running. Okay, I checked open ports in my system ( ubuntu 16.04 ):

ubuntu@ubuntu:~$ sudo netstat -nautp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1315/sshd
tcp        0      0 127.0.0.1:7001          0.0.0.0:*               LISTEN      3209/etcd
tcp        0      0 127.0.0.1:10248         0.0.0.0:*               LISTEN      3324/hyperkube
tcp        0      0 127.0.0.1:10249         0.0.0.0:*               LISTEN      3399/hyperkube
tcp        0      0 127.0.0.1:2380          0.0.0.0:*               LISTEN      3209/etcd
tcp        0    524 172.30.3.114:22         212.98.179.158:35900    ESTABLISHED 3087/sshd: ubuntu [
tcp6       0      0 :::10255                :::*                    LISTEN      3324/hyperkube
tcp6       0      0 :::22                   :::*                    LISTEN      1315/sshd
tcp6       0      0 :::4001                 :::*                    LISTEN      3209/etcd
tcp6       0      0 :::10250                :::*                    LISTEN      3324/hyperkube
udp        0      0 0.0.0.0:68              0.0.0.0:*                           959/dhclient
udp        0      0 0.0.0.0:68              0.0.0.0:*                           796/dhclient

And I found that there is no 8080 open TCP port that kubectl tried to reach. So this is the cause of my issue.

So my question is what container/service/daemon I should run/launch to open this port and to assign web service to it in order to let kubectl use it for this GET request http://localhost:8080/api ?

Any help would be appreciated.

Creating a kubernetes cluster from scratch is a little more complicated. The master kubelet runs a number of pods to make it all go.

If you're looking for an all in one solution use minikube to run in a VM. Otherwise use kubeadm to setup your master and work back from there if you want to see how each component is setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM