<p>on kubernetes vm Im running for example : <code>kubectl get endpoints</code>
how can I get the same output inside the pod , what should I run within a pod?
I  understood there is a kubeapi but Im new to kubernetes can someone explain how can I use it</p>
<p>this is my clusterrolebinding:</p>
<pre><code>---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: {{ template &quot;elasticsearch.fullname&quot; . }}
  labels:
    app: {{ template &quot;elasticsearch.name&quot; . }}
    chart: &quot;{{ .Chart.Name }}-{{ .Chart.Version }}&quot;
    release: &quot;{{ .Release.Name }}&quot;
    heritage: &quot;{{ .Release.Service }}&quot;
subjects:
- kind: ServiceAccount
  name: {{ template &quot;elasticsearch.serviceAccountName.client&quot; . }}
  namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: {{ template &quot;elasticsearch.fullname&quot; . }}
  apiGroup: rbac.authorization.k8s.io
</code></pre>
<p>clusterrole.yaml:</p>
<hr />
<pre><code>apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name:  {{ template &quot;elasticsearch.fullname&quot; . }}
  labels:
    app: {{ template &quot;elasticsearch.name&quot; . }}
    chart: &quot;{{ .Chart.Name }}-{{ .Chart.Version }}&quot;
    release: &quot;{{ .Release.Name }}&quot;
    heritage: &quot;{{ .Release.Service }}&quot;
rules:
#
# Give here only the privileges you need
#
- apiGroups: [&quot;&quot;]
  resources:
  - pods
  - endpoints
  verbs:
  - get
  - watch
  - list
</code></pre>
<p>serviceaccount:</p>
<pre><code>---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: {{ template &quot;elasticsearch.name&quot; . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    component: &quot;{{ .Values.client.name }}&quot;
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template &quot;elasticsearch.client.fullname&quot; . }}
</code></pre>

NoamiA
<p>You don't have to have   <code>kubectl</code>  installed in pod to access the Kubernetes API. You will be ableto do it with any tool that can make HTTP requests.</p>
<p>The Kubernetes API is a simple HTTP REST API, and all the authentication information that you need is present in the container if it runs as a Pod in the cluster.</p>
<p>To  <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#read-endpoints-v1-core" rel="nofollow noreferrer">get the Endpoints object</a>  named  <code>your-service</code>  from within a container in the cluster, you can do:</p>
<pre><code>$ curl -k -H &quot;Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)&quot; \
  https://kubernetes.default.svc:443/api/v1/namespaces/{namespace}/endpoints/your-service

</code></pre>
<p>Replace  <code>{namespace}</code>  with the namespace of the  <code>your-service</code>  Endpoints resource._</p>
<p>To extract the IP addresses of the returned JSON pipe the output to a tool like  <code>jq</code>:</p>
<pre><code>... | jq -r '.subsets[].addresses[].ip'

</code></pre>
<p><strong>IMPORTANT:</strong>
<strong>The Pod from which you are executing this needs read permissions for the Endpoints resource, otherwise the API request will be denied.</strong></p>
<p>You can do this by creating a ClusterRole, ClusterRoleBinding, and Service Account - set this up once:</p>
<pre><code>$ kubectl create sa endpoint-reader-sa
$ kubectl create clusterrole endpoint-reader-cr --verb=get,list --resource=endpoints
$ kubectl create clusterrolebinding endpoint-reader-crb --serviceaccount=default:endpoint-reader-sa --clusterrole=endpoint-reader-cr
</code></pre>
<p>Next use created ServiceAccount -   <code>endpoint-reader-sa</code>  for the Pod from which you want to execute the above  <code>curl</code>  command by specifying it in the  <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#podspec-v1-core" rel="nofollow noreferrer"><code>pod.spec.serviceAccountName</code></a>  field.</p>
<p><strong>Granting permissions for any different API operations works in the same way.</strong></p>
<p>Source: <a href="https://stackoverflow.com/questions/57913132/get-ip-addresses-of-all-k8s-pods-from-within-a-container">get-pod-ip</a>.</p>
<p>And as also @ITChap mentioned similar answer: <a href="https://stackoverflow.com/questions/42642170/how-to-run-kubectl-commands-inside-a-container">kubectl-from-inside-the-pod</a>.</p>
