I am using ruby kubeclient library and Kubernetes APIs to read file contents from different pod from the current pod. I have set up RBAC and am able to authenticate my Kubernetes API. Followed these docs.
To read the file from the pod from the host I was able to try the below options:
- using
kubectl exec
withcat
command to get file contents (able to see file contents on console) - using
kubectl cp
to copy the file from container to host (able to copy the file in the host)
I want to replicate either of these options to automate this process to run from within the container.
From the container when I run:
curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -X GET "https://kubernetes.default.svc/api/v1/namespaces/mynamespace/pods/mypod"
I am able to get the details of my pod.
But when I tried the above URL with exec
I am getting 400 Bad request errors.
curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -X GET "https://kubernetes.default.svc/api/v1/namespaces/mynamespace/pods/mypod/exec?command=ls"
or
curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -X GET "https://kubernetes.default.svc/api/v1/namespaces/mynamespace/pods/mypod/exec?command=cat&command=/tmp/myfile.txt"
The error I am getting is:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "Upgrade request required",
"reason": "BadRequest",
"code": 400
NOTE: I found StackOverflow thread for this above error: Kubernetes pod exec API - Upgrade request required
The recommendation was to use the CLI tool which supports WebSockets (like wscat or wssh).
I tired replacing curl with ruby rest client:
RestClient::Request.execute( :url => url, :method => :get, :verify_ssl => true, :ssl_ca_file => "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt", :payload => {}.to_json,
:headers => {:Authorization => "Bearer %s" % [access_token]})
But I am getting the same 400 error.
My rbac configuration:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ .Chart.Name }}.{{ .Release.Namespace}}
rules:
- apiGroups: [ "" ]
resources: [ "services", "pods"]
verbs: [ "get", "list" , "watch"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create","delete","get","list","patch","update","watch"]
Is there any equivalent for kubectl exec
or kubectl cp
which I can use via KUBERNETES API? Or is there any sample code that shows how to use kubeclient (ruby client) to copy files in and out of containers?