简体   繁体   English

SSH 到通过 KOPS 创建的 kubernetes 节点

[英]SSH into kubernetes nodes created through KOPS

I created a Kubernetes cluster through Kops.我通过 Kops 创建了一个 Kubernetes 集群。 The configuration and the ssh keys were in a machine that I don't have access to anymore.配置和 ssh 密钥位于我无法再访问的机器中。 Is it possible to ssh to the nodes through kops even if I have lost the key?即使我丢失了密钥,是否可以通过 kops ssh 到节点? I see there is a command -我看到有一个命令 -

kops get secrets警察得到秘密

This gives me all the secrets.这给了我所有的秘密。 Can I use this to get ssh access to the nodes and how to do it?我可以使用它来获得对节点的 ssh 访问权限以及如何操作吗?

I see the cluster state is stored in S3.我看到集群状态存储在 S3 中。 Does it store the secret key as well?它也存储密钥吗?

You can't recover the private key, but you should be able install a new public key following this procedure:您无法恢复私钥,但您应该可以按照以下步骤安装新的公钥:

kops delete secret --name <clustername> sshpublickey admin
kops create secret --name <clustername> sshpublickey admin -i ~/.ssh/newkey.pub
kops update cluster --yes to reconfigure the auto-scaling groups
kops rolling-update cluster --name <clustername> --yes to immediately roll all the machines so they have the new key (optional)

Taken from this document:取自本文档:

https://github.com/kubernetes/kops/blob/master/docs/security.md#ssh-access https://github.com/kubernetes/kops/blob/master/docs/security.md#ssh-access

This gives me all the secrets.这给了我所有的秘密。 Can I use this to get ssh access to the nodes and how to do it?我可以使用它来获得对节点的 ssh 访问权限以及如何操作吗?

Not really.并不真地。 These are secrets to access the kube-apiserver in the cluster.这些是访问集群中 kube-apiserver 的秘密。 For example, for you to be able to run kubectl commands.例如,为了您能够运行kubectl命令。

I see the cluster state is stored in S3.我看到集群状态存储在 S3 中。 Does it store the secret key as well?它也存储密钥吗?

It's stored in S3 but not the ssh keys to access the servers.它存储在 S3 中,但不是用于访问服务器的 ssh 密钥。 Those are stored in AWS under 'Key Pairs'.这些存储在 AWS 中的“密钥对”下。

密钥对

Unfortunately, you can only get your private key that you can use to log in only once (when you create the keypair).不幸的是,您只能获得只能用于登录一次的私钥(当您创建密钥对时)。 So I think you are out of luck if you don't have the private key.所以我认为如果你没有私钥,你就不走运了。 If you have access to the AWS console you could snapshot the root drive of your instances and recreate your nodes (or control plane) one by one with a different AWS keypair that you have the private key for.如果您有权访问 AWS 控制台,则可以对实例的根驱动器进行快照,并使用您拥有私钥的不同 AWS 密钥对一一重新创建节点(或控制平面)。

In my case when I installed the cluster with Kops I had to run ssh-keygen like below that created id_rsa.pub/pvt keys.在我的情况下,当我使用 Kops 安装集群时,我必须像下面一样运行 ssh-keygen 来创建 id_rsa.pub/pvt 密钥。 This is allowing me to simply do a ssh or这允许我简单地做一个 ssh 或

ssh-keygen
kops create secret --name ${KOPS_CLUSTER_NAME} sshpublickey admin -i ~/.ssh/id_rsa.pub

and then created the cluster with然后创建集群

kops update cluster --name ${KOPS_CLUSTER_NAME} --yes
ssh admin@ec2-13-59-4-99.us-east-2.compute.amazonaws.com

You can run new daemonset with gcr.io/google-containers/startup-script containers, to update the public key on all your nodes, this will help you in case you've a new node spun and will replace the public key in all existing nodes.您可以使用gcr.io/google-containers/startup-script容器运行新的 daemonset,以更新所有节点上的公钥,这将对您有帮助,以防您有一个新节点旋转并将替换所有公钥现有节点。

kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: startup-script
  labels:
    app: startup-script
spec:
  template:
    metadata:
      labels:
        app: startup-script
    spec:
      hostPID: true
      containers:
        - name: startup-script
          image: gcr.io/google-containers/startup-script:v1
          imagePullPolicy: Always
          securityContext:
            privileged: true
          env:
          - name: STARTUP_SCRIPT
            value: |
              #! /bin/bash
              touch /tmp/foo
              #echo "MYPUBLICKEY" > /home/admin/.ssh/authorized_keys
              echo done

replace MYPUBLICKEY with your public key, and the username after home, here admin will get replace depending on what OS you are using.MYPUBLICKEY替换为您的公钥,以及 home 之后的用户名,这里admin将根据您使用的操作系统进行替换。 This will help you access the node via ssh without changing/replacing your existing nodes这将帮助您通过 ssh 访问节点,而无需更改/替换现有节点

You can also add user-data in the ig while performing kops edit ig nodes and add the small one liner to append your public key.您还可以在执行kops edit ig nodes同时在 ig 中添加用户数据,并添加小一行来附加您的公钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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