如何使用多文档yaml文件为Kubernetes配置kops?

时间:2018-05-08 14:43:04

标签: kubernetes kops

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

仅使用kops create -f导入群集定义,并且不会导入使用yaml在其他“文档”中定义的实例组等:

...
    masters: private
    nodes: private

---

apiVersion: kops/v1alpha2
kind: InstanceGroup
...

2 个答案:

答案 0 :(得分:0)

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

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

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
}

答案 1 :(得分:0)

过去可能不可能,但是现在有可能。

这就是我目前如何对Kops集群进行IaC。

我有2个文件:

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

第二个文件是您所请求的多文档yaml文件。
(其中多个instance.yaml的内部由-分隔)。
注意:我已经成功地将这两个.yaml文件都合并到一个多文档.yaml文件中,并且可以正常工作,我就是这样做的,这样我就可以重复使用instancegroups.yaml多个群集/保持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