无法使用 kops 在 AWS 上安装 Kubernetes

Unable to install Kubernetes on AWS using kops

我尝试在 Amazon Linux 机器上安装 Kubernetes。我遵循了这些教程中的大量文档和视频,它们很容易安装 kubectl 和 kops,但就我而言,我遵循了相同的步骤,但 kubectl 对我不起作用。

错误:与服务器 localhost:8080 的连接被拒绝 - 您是否指定了正确的主机或端口?我打开了所有必需的端口,但仍然存在错误。

1) kubelet 不是服务,它只是一个二进制可执行文件,因此您的系统上没有任何服务单元文件

2) 你是如何使用kops在aws上部署集群的?我总是使用以下对我有用的步骤:

安装awscli

sudo apt-get install python python-pip
sudo python-pip install awscli

为您的 admin 用户创建 aws 凭据(使用 IAM)并配置您的 awscli 实用程序以使用它们

aws configure

安装kops

curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops

以及kubectl

apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubectl

为 Kubernetes 的存储创建 s3 bucket 名称

aws s3api create-bucket --bucket k8s --region eu-central-1 --create-bucket-configuration LocationConstraint=eu-central-1
aws s3api put-bucket-versioning --region eu-central-1 --bucket k8s --versioning-configuration Status=Enabled
aws s3 ls

在 Route53 中为 Kubernetes 集群创建托管区域(或子域)或使用 Route53 中已有的区域(或子域),例如 test.com

通过 kops 创建集群:

kops create cluster --name=k8s.test.com \
                    --state=s3://k8s \
                    --zones=eu-central-1a \
                    --node-count=2 \
                    --node-size=t2.small \
                    --master-count=1 \
                    --master-size=t2.micro \
                    --master-zones=eu-central-1a \
                    --dns-zone=test.com \
                    --authorization=RBAC \
                    --yes

稍等一下,看看是不是运行:

kops validate cluster --name=k8s.test.com --state=s3://k8s

error: The connection to the server localhost:8080 was refused - did you specify the right host or port?

在大多数情况下,当您的集群的 kubectl 配置设置不正确时,会显示此错误消息。

基本信息

默认情况下,“kubectl”配置文件位于 $HOME/.kube/config 并包含以下部分:

集群信息:

  • hostname/ip:你的 kubernetes master 节点与 运行 kube-apiserver
  • 的端口
  • 连接到 kube-apiserver 所需的证书

    clusters:
    - cluster:
        certificate-authority-data: REDACTED
        server: https://10.156.0.3:6443
      name: kubernetes
    

用户信息:

  • 用户名
  • 用户证书

    users:
    - name: kubernetes-admin
      user:
        client-certificate-data: REDACTED
        client-key-data: REDACTED
    

上下文信息:

  • 集群和用户引用的组合
  • 如果在 kubectl 命令行中未指定上下文,则使用当前上下文

    contexts:
    - context:
        cluster: kubernetes
        user: kubernetes-admin
      name: kubernetes-admin@kubernetes
    current-context: kubernetes-admin@kubernetes
    kind: Config
    preferences: {}
    

用法:

您可以使用以下命令查看当前的 kubectl 配置:

kubectl config view

可以将多个配置文件添加到环境变量中 KUBECONFIG:

export  KUBECONFIG=$KUBECONFIG:config-demo:config-demo-2

您还可以在命令行中指定“kubectl”配置文件:

kubectl --kubeconfig path/to/kubectl.conf get nodes

您可以导出当前的 kubectl 配置并在另一个工作站上使用它:

kubectl config view --flatten

文章:

请在以下文章中找到有关 kubectl 配置和用法的更多信息:

创建方式:

您可以手动创建 kubectl 配置或作为集群创建过程的一部分。

以下是如何为现有 kops 集群(link1, link2)创建 kubectl 配置:

# update cluster will do it automatically after cluster creation. 
# But we expect that if you're part of a team you might share the KOPS_STATE_STORE

# ASSUMPTION:  You have pointed kops to some location where the cluster configurations are stored 
# (I have this in my ~/.bash_profile):
export KOPS_STATE_STORE=s3://example-state-store

# Use kops to get the list of clusters
$ kops get clusters

# Export the configuration of the cluster you care about; this will update your ~/.kube/config file, so kubectl knows about it:
$ kops export kubecfg cluster-foo.example.com

# You can now use kubernetes using the kubectl tool:
$ kubectl get nodes

如果您使用 kubeadm 创建集群,kubectl 配置位于主节点上的 /etc/kubernetes/admin.conf 中,您只需将其复制到主目录即可:

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