How does k8s cluster behave when accessed from its pod using kubectl?

2/17/2022

I am working on a project with jenkins where I am already running the jenkins pod and want to run kubectl commands directly from the pod to connect with my host machine in order to do that I followed this SO question about k8s cluster remote access am on windows and have kubectl v1.23.3 installed on a jenkins pod I runned from my host machine k8s.

I managed to verify that running kubectl works properly on the jenkins pod (container):

kubectl create deployment nginx --image=nginx
kubectl create service nodeport nginx --tcp=80:80

when I ran kubectl get all from the jenkins container I get this output:

root@jenkins-64756886f7-2v92n:~/test# kubectl create deployment nginx --image=nginx
kubectl create service nodeport nginx --tcp=80:80
deployment.apps/nginx created
service/nginx created
root@jenkins-64756886f7-2v92n:~/test# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/jenkins-64756886f7-2v92n   1/1     Running   0          37m
pod/nginx-6799fc88d8-kxprv     1/1     Running   0          8s

NAME                      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/jenkins-service   NodePort   10.110.105.78   <none>        8080:30090/TCP   39m
service/nginx             NodePort   10.107.115.5    <none>        80:32355/TCP     8s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/jenkins   1/1     1            1           39m
deployment.apps/nginx     1/1     1            1           8s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/jenkins-64756886f7   1         1         1       37m
replicaset.apps/nginx-6799fc88d8     1         1         1       8s
root@jenkins-64756886f7-2v92n:~/test#

Initially I had Jenkins deployment attached to a namespace called devops-cicd Tested the deployment on my browser and worked fine

and this is the output from my host machine:

PS C:\Users\affes> kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   2d9h

and when I specify the namespace I get the same result as from Jenkins container:

PS C:\Users\affes> kubectl get all -n devops-cicd
NAME                           READY   STATUS    RESTARTS   AGE
pod/jenkins-64756886f7-2v92n   1/1     Running   0          38m
pod/nginx-6799fc88d8-kxprv     1/1     Running   0          93s

NAME                      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/jenkins-service   NodePort   10.110.105.78   <none>        8080:30090/TCP   41m
service/nginx             NodePort   10.107.115.5    <none>        80:32355/TCP     93s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/jenkins   1/1     1            1           41m
deployment.apps/nginx     1/1     1            1           93s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/jenkins-64756886f7   1         1         1       38m
replicaset.apps/nginx-6799fc88d8     1         1         1       93s

I don't know what's causing the resources created on that namespace directly without even specifying the namespace, and is there a possible way to configure something that will allow me to deploy on default namespace instead?

This is my deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops-cicd      
spec:
  selector:
    matchLabels:
      app: jenkins
      workload: cicd
  replicas: 1
  template: 
    metadata:
      namespace: devops-cicd      
      labels:
        app: jenkins 
        workload: cicd        
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts    
        volumeMounts:  
          - name: dockersock
            mountPath: "/var/run/docker.sock"
          - name: docker
            mountPath: "/usr/bin/docker"   
        securityContext: 
            privileged: true     
            runAsUser: 0  # Root                                                       
      restartPolicy: Always       
      volumes:      
      - name: dockersock
        hostPath:
          path: /var/run/docker.sock
      - name: docker
        hostPath:
          path: /usr/bin/docker             
---
apiVersion: v1
kind: Service
metadata:
  namespace: devops-cicd      
  name: jenkins-service
spec:
  selector:
    app: jenkins
    workload: cicd
  ports:
    - name: http
      port: 8080
      nodePort: 30090
  type: NodePort
-- Affes Salem
jenkins
kubernetes

1 Answer

2/17/2022

You may have a different namespace configured by default in the kubectl in the Jenkins pod. You can check it with the following command.

kubectl config view | grep namespace

To change the default namespace to `default, you can run the following command.

kubectl config set-context --current --namespace=default

Please find more details here.

-- RafaƂ Leszko
Source: StackOverflow