1

我有一个多文档 yaml 文件,如何从中配置 Kubernetes 集群?

使用kops create -f似乎只导入集群定义,而不导入使用 yaml 在附加“文档”中定义的实例组等:

...
    masters: private
    nodes: private

---

apiVersion: kops/v1alpha2
kind: InstanceGroup
...
4

2 回答 2

0

根据 kops 源代码,支持一个文件中的多个部分。也许您需要检查文件是否有错误。

这里是负责解析文件和创建资源的部分。为了使代码更短,我删除了所有的错误检查。如需调查,请参阅原始文件

func RunCreate(f *util.Factory, out io.Writer, c *CreateOptions) error {
    clientset, err := f.Clientset()

    // Codecs provides access to encoding and decoding for the scheme
    codecs := kopscodecs.Codecs //serializer.NewCodecFactory(scheme)

    codec := codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)

    var clusterName = ""
    //var cSpec = false
    var sb bytes.Buffer
    fmt.Fprintf(&sb, "\n")
    for _, f := range c.Filenames {
        contents, err := vfs.Context.ReadFile(f)

        // TODO: this does not support a JSON array
        sections := bytes.Split(contents, []byte("\n---\n"))
        for _, section := range sections {
            defaults := &schema.GroupVersionKind{
                Group:   v1alpha1.SchemeGroupVersion.Group,
                Version: v1alpha1.SchemeGroupVersion.Version,
            }
            o, gvk, err := codec.Decode(section, defaults, nil)

            switch v := o.(type) {

            case *kopsapi.Cluster:
                // Adding a PerformAssignments() call here as the user might be trying to use
                // the new `-f` feature, with an old cluster definition.
                err = cloudup.PerformAssignments(v)
                _, err = clientset.CreateCluster(v)

            case *kopsapi.InstanceGroup:
                clusterName = v.ObjectMeta.Labels[kopsapi.LabelClusterName]
                cluster, err := clientset.GetCluster(clusterName)
                _, err = clientset.InstanceGroupsFor(cluster).Create(v)

            case *kopsapi.SSHCredential:
                clusterName = v.ObjectMeta.Labels[kopsapi.LabelClusterName]
                cluster, err := clientset.GetCluster(clusterName)
                sshCredentialStore, err := clientset.SSHCredentialStore(cluster)
                sshKeyArr := []byte(v.Spec.PublicKey)
                err = sshCredentialStore.AddSSHPublicKey("admin", sshKeyArr)

            default:
                glog.V(2).Infof("Type of object was %T", v)
                return fmt.Errorf("Unhandled kind %q in %s", gvk, f)
            }
        }

    }
    {
        // If there is a value in this sb, this should mean that we have something to deploy
        // so let's advise the user how to engage the cloud provider and deploy
        if sb.String() != "" {
            fmt.Fprintf(&sb, "\n")
            fmt.Fprintf(&sb, "To deploy these resources, run: kops update cluster %s --yes\n", clusterName)
            fmt.Fprintf(&sb, "\n")
        }
        _, err := out.Write(sb.Bytes())
    }
    return nil
}
于 2018-05-08T16:17:51.977 回答
0

这在过去可能是不可能的,但现在是可能的。

以下是我目前对我的 kops 集群进行 IaC 的方式。

我有 2 个文件:

  1. devkube.mycompany.com.cluster.kops.yaml
  2. instancegroups.kops.yaml

第二个文件是您要求的多文档 yaml 文件。
(其中有多个 instance.yaml 由 --- 分隔)。
注意:我已经成功地将这两个 .yaml 文件组合成一个单一的多文档 .yaml 文件并让它正常工作,我就是这样做的,这样我就可以在多个集群上重用 instancegroups.yaml / keep git repo 干了。

我使用以下方法生成了文件:

Bash# kops get cluster --name devkube.mycompany.com -o yaml > devkube.mycompany.com.cluster.kops.yaml
Bash# kops get ig --name devkube.mycompany.com -o yaml > instancegroups.kops.yaml

以下是我如何使用这些文件从 git 配置 kops 集群:

export AWS_PROFILE=devkube-kops
export KOPS_STATE_STORE=s3://kops-state-devkube.mycompany.com
clustername="devkube.mycompany.com"
 
kops replace --name $clustername -f ../kops/qak8s.vibrenthealth.com.cluster.yaml --force
kops replace --name $clustername -f ../kops/instancegroups.yaml --force
kops create secret --name $clustername sshpublickey admin -i ~/.ssh/id_rsa.pub #so the person who provisions the cluster can ssh into the nodes
kops update cluster --name $clustername --yes
于 2019-06-21T05:48:47.020 回答