How to copy a file from host to Kubernetes container? - kubernetes

I want to copy a file from my Ubuntu machine to kube-controller-manager-ubuntu container. Currently I do that like this, but I think it has more straight solution in Kubernetes.
Does anyone know how to copy a file to a Kubernetes container?

it is similar to docker copy.
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
Please refer here for examples and documentation
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#cp

In case you are using namespace then you wanna go like this -
kubectl cp ./file.csv <CONTAINER_ID>:/path/to/copy -n <namespace>
e.g.
kubectl ./file.csv b81dd0b1745c:/usr/cloud_ms/ -n cloud

Related

How do I know which pod a container is running in?

Sorry about a newbie question. I am trying to deploy an image into k3d (a dockerized version of k3s).
k3d image import -c my-cluster registry.gitlab.com/aaa/bbb/ccc/hello123
Now I can see the image on a node:
kubectl get node my-node -o json | grep hello123
However, the documentation doesn't say much about what "import" does. Is my image running? Is it allocated to a pod yet? Where can I find its logs?
If I knew what pod it's running in, I could do kubectl logs. The list of the cluster's pods doesn't show anything relevant.
I am beginning to think my image isn't running yet.
Edit: This if further confirmed by
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
showing nothing relevant.
What's the next step?
You have just pulled the image to the cluster registry, the image has not been yet assigned to a pod.
Once you create a pod with the same image and image tag, it will try to pull from the local registry.
If you could ssh to the k8s node (kubectl get nodes -o wide and ssh user#nodeip), you can run the docker commands like:
docker images
You can expect to see the image that you pulled in the list.
If non of the pods are running the docker ps will return you an empty list.

kubectl cp from a completed pod to local computer

I would like to use kubectl cp to copy a file from a completed pod to my local host(local computer). I used kubectl cp /:/ , however, it gave me an error: cannot exec into a container in a completed pod; current phase is Succeeded error. Is there a way I can copy a file from a completed pod? It does not need to be kubectl cp. Any help appreciated!
Nope. If the pod is gone, it's gone for good. Only possibility would be if the data is stored in a PV or some other external resource. Pods are cattle, not pets.
You can find the files, because the containers of a pod in the state Completed are not deleted, they are just not running.
I am not aware of any way to do it via Kubernetes itself, but here is how to do it if your container runtime is Docker:
$ ssh <node where the pod is>
$ docker ps -a | grep <pod name>
$ docker cp <pod name>:/your/files ./
The files in containers are just overlayfs mounts; if the container still exists, the files still exist.
So if you are using containerd runtime or something else, look at /var/lib/containers or something (don't know where different runtimes do their overlayfs mounts, but it can't not be at the node. you could check if you find out where via $ mount).

How to send the logs from kubernetes pod to host pc

I use k9s to access the bash from the pod where I keep the logs of my project.
Reading the logs with a cat is annoying, so I want to send them to my pc.
How can I do this?
You can use kubectl cp command.
kubectl cp default/<some-pod>:/logs/app.log app.log

Not able to create a file using kubectl

I recently started working on microservices. I am building my docker image and want to deploy it on kubernetes. while creating a pod.yaml file I started getting the below error.
Command :
kubectl create -f podservice.yaml
Error :
error: the path "podcpeservice.yml" does not exist
Tried using the helpfor kubectl create -f help. An example in the help document is
command :
kubectl create -f ./pod.json
Even the above command gives the same error. Not able to figure out what is the problem. tried removing ./
I am using centos 7 on virtual-box with windows 7 as host.
Tushar,
First you need to create the deployment yml file using one of the editor, then pass the file as argument for kubectl command.
eg.
kubernets team already created this deployment file. you use kubectl to deploy.
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml
Somehow the filename was corrupted with unseen symbol so this helped me to get over wrong naming
mv postgres-configmap.yaml\ postgres-configmap.yaml
and then it worked:
kubectl create -f postgres-configmap.yaml
configmap/postgres-config created

Create a deployment from a pod in kubernetes

For a use case I need to create deployments from a pod when a script is being executed from inside the pod.
I am using google container engine for my cluster.
How to configure the container inside the pod to be able to run commands like kubectl create deployment.yaml?
P.S A bit clueless about it at the moment.
Your container is going to need to have kubectl available. There are some container images available, personally I can't vouch for any of them.
Personally I'd probably build my own and download the latest kubectl. A Dockerfile like this is probably a good starting point
FROM alpine:latest
RUN apk --no-cache add curl
RUN curl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
RUN chmod +x /usr/local/bin/kubectl
This will build you a container image with kubectl, so you can then all the kubectl commands you want.