<p>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 <a href="https://stackoverflow.com/questions/36306904/configure-kubectl-command-to-access-remote-kubernetes-cluster-on-azure">SO question about k8s cluster remote access</a> am on windows and have <code>kubectl v1.23.3</code> installed on a jenkins pod I runned from my host machine k8s.</p>
<p>I managed to verify that running kubectl works properly on the jenkins pod (container):</p>
<pre class="lang-sh prettyprint-override"><code>kubectl create deployment nginx --image=nginx
kubectl create service nodeport nginx --tcp=80:80
</code></pre>
<p>when I ran <code>kubectl get all</code> from the jenkins container I get this output:</p>
<pre><code>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   &lt;none&gt;        8080:30090/TCP   39m
service/nginx             NodePort   10.107.115.5    &lt;none&gt;        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#
</code></pre>
<blockquote>
<blockquote>
<p>Initially I had Jenkins deployment attached to a namespace called <code>devops-cicd</code>
Tested the deployment on my browser and worked fine</p>
</blockquote>
</blockquote>
<p>and this is the output from my host machine:</p>
<pre><code>PS C:\Users\affes&gt; kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   2d9h
</code></pre>
<p>and when I specify the namespace I get the same result as from Jenkins container:</p>
<pre><code>PS C:\Users\affes&gt; 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   &lt;none&gt;        8080:30090/TCP   41m
service/nginx             NodePort   10.107.115.5    &lt;none&gt;        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
</code></pre>
<p>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?</p>
<p>This is my deployment manifest:</p>
<pre class="lang-yaml prettyprint-override"><code>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: &quot;/var/run/docker.sock&quot;
          - name: docker
            mountPath: &quot;/usr/bin/docker&quot;   
        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
</code></pre>

Affes Salem
<p>You may have a different namespace configured by default in the <code>kubectl</code> in the Jenkins pod. You can check it with the following command.</p>
<pre><code>kubectl config view | grep namespace
</code></pre>
<p>To change the default namespace to `default, you can run the following command.</p>
<pre><code>kubectl config set-context --current --namespace=default
</code></pre>
<p>Please find more details <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#setting-the-namespace-preference" rel="nofollow noreferrer">here</a>.</p>
