vcluster icon indicating copy to clipboard operation
vcluster copied to clipboard

HostName and SubDomain should be sync to the physical cluster

Open mayooot opened this issue 10 months ago • 0 comments

Is your feature request related to a problem?

When I use statefulset and headless serivce inside vcluster and access the pod using <pod.hostname>.<pod.subdomain>.<namespace>.svc.cluster.local, it works as expected.

apiVersion: v1
kind: Service
metadata:
  name: nginx-headless
spec:
  clusterIP: None
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-statefulset
spec:
  serviceName: nginx-headless
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      hostname:
      containers:
      - name: nginx
        image: harbor.zetyun.cn/alaya-bugfix/nginx:latest
        ports:
        - containerPort: 80

However, in the physical cluster using the synced pod and serivce, it does not work, the ip is not resolved. I noticed that the subdomain is set to "" in the physical cluster pod, and by reading I found the following logic:

	// if spec.subdomain is set we have to translate the /etc/hosts
	// because otherwise we could get a different hostname as if the pod
	// would be deployed in a non virtual kubernetes cluster
	if pPod.Spec.Subdomain != "" {
		if t.overrideHosts {
			rewritePodHostnameFQDN(pPod, t.defaultImageRegistry, t.overrideHostsImage, pPod.Spec.Hostname, pPod.Spec.Hostname, pPod.Spec.Hostname+"."+pPod.Spec.Subdomain+"."+vPod.Namespace+".svc."+t.clusterDomain)
		}

		pPod.Spec.Subdomain = ""
	}

I changed the code to this, and it works fine, and I was able to access the vcluster's pods on the phsyical cluster via dns.


	// truncate hostname if needed
	if pPod.Spec.Hostname == "" {
		if len(vPod.Name) > 63 {
			pPod.Spec.Hostname = vPod.Name[0:63]
		} else {
			pPod.Spec.Hostname = vPod.Name
		}

		// Kubernetes does not support setting the hostname to a value that
		// includes a '.', therefore we need to rewrite the hostname. This is really bad
		// and wrong, but unfortunately there is currently no other solution as there is
		// no other way to change the container's hostname.
                // EDIT!
		pPod.Spec.Hostname = translate.Default.PhysicalName(strings.TrimSuffix(strings.Replace(pPod.Spec.Hostname, ".", "-", -1), "-"), vPod.Namespace)
	}

        ....

	// if spec.subdomain is set we have to translate the /etc/hosts
	// because otherwise we could get a different hostname as if the pod
	// would be deployed in a non virtual kubernetes cluster
	if pPod.Spec.Subdomain != "" {
		if t.overrideHosts {
			rewritePodHostnameFQDN(pPod, t.defaultImageRegistry, t.overrideHostsImage, pPod.Spec.Hostname, pPod.Spec.Hostname, pPod.Spec.Hostname+"."+pPod.Spec.Subdomain+"."+vPod.Namespace+".svc."+t.clusterDomain)
		}

		// Translate the subdomain using the same method as other resources
                // EDIT!
		pPod.Spec.Subdomain = translate.Default.PhysicalName(pPod.Spec.Subdomain, vPod.Namespace)
	}

$ ping nginx-statefulset-0-x-default-x-vc9i1h3abk7i.nginx-headless-x-default-x-vc9i1h3abk7i.vcluster-vc9i1h3abk7i.svc.cluster.local

I can't understand why I can't sync out the subdomain and if there is another way for me to access the pods inside StatefuSet via dns in the physical cluster.

Which solution do you suggest?

Set the subdomain and hostname for the synced pods.

Which alternative solutions exist?

No response

Additional context

No response

mayooot avatar Feb 20 '25 03:02 mayooot