Not able to create a file using kubectl - kubernetes

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

Related

timed out waiting for the condition on deployments/gatekeeper-controller-manager

I am in K8S, trying my best to explain the issue here
I have followed this link to complete the installation
1 https://open-policy-agent.github.io/gatekeeper/website/docs/install
2 deploy gatekeeper by using
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/release-3.8/deploy/gatekeeper.yaml
3 deploy constraint template
install gatekeeper library gatekeeper-library/library/pod-security-policy/
select a directory (eg: apparmor)
run Kubectl apply -f template.yaml
4 under the apparmor it has a sample folder containing constraint.yaml
run Kubectl apply -f constraint.yaml
I got the error below
Error message:
error: timed out waiting for the condition on deployments/gatekeeper-controller-manager
constrainttemplate.templates.gatekeeper.sh/k8spspapparmor created
error: unable to recognize "samples/psp-apparmor/constraint.yaml": no matches for kind "K8sPSPAppArmor" in version "constraints.gatekeeper.sh/v1beta1"
However, if wait for 1 minute at step 3, then it will success no error.
Assume the command is running fine, but takes time to complete the work. Is there a way to query the command status before the command to be executed?
I have found there is command kubectl wait -- but not sure how to use it.
Thanks in advance!

Kubernetes apply command produces 'wrong encoding error'

I'm trying to execute:
microk8s kubectl apply -f deployment.yaml
and I am always getting:
error: string field contains invalid UTF-8
No matter which file and string as a file path parameter I'm trying to use. Even if I execute:
microk8s kubectl apply -f blablabla
Result is the same.
UPD: I resolved the problem by restarting microk8s service. After restart everything is fine, but I still have no idea what it was.
I have posted Community Wiki answer for better visibility.
As OP has mentioned in the question, he resolved the problem by restarting microk8s service:
I resolved the problem by restarting microk8s service. After restart everything is fine.
This is not a wrong format in the manifest, instead, it's a corrupted cache in $HOME/.kube/
try to delete the cache:
rm -rf $HOME/.kube/http-cache
rm -rf $HOME/.kube/cache

How to backup Helm3 nginx controller configs and update the running LoadBalancer service?

Very new to kubernetes. I've been getting confused by documentation and example differences between Helm2 and 3.
I installed the stable/nginx-ingress chart via helm install app-name stable/nginx-ingress.
1st question:
I need to update the externalTrafficPolicy to Local. I learned later I could have set that during the install process via adding --set controller.service.externalTrafficPolicy=Local to the helm command.
How can I update the LoadBalancer service with the new setting without removing the ingress controller and reinstalling?
2nd question:
Helm3 just downloaded and setup the ingress controller and didn't save anything locally. Is there a way to backup all my my k8s cluster configs (other than the ones I've created manually)?
To upgrade and dump the YAML deployed (for a backup of the ingress release)
helm upgrade <your-release-name> stable/nginx-ingress \
--reuse-values \
--set controller.service.externalTrafficPolicy=Local \
--output yaml
For a public chart you may want to set the --version option to the existing installed version of the chart you used. In case you don't want to any updates from newer versions to be applied along with the setting.
For complete dumps, have a look through this github issue. All options there are a bit dodge though with edge cases. I would recommend having everything re-deployable from something like git, all the way from cluster to apps. Anyone who makes edits by hand can then be shot (Well.. at least have clusters regularly redeployed on them :)
Is there a way to backup all my my k8s cluster configs
kubectl cluster-info dump shows some info about the k8s cluster.
Configs and manifests (yaml files) of k8s itself will be at /etc/kubernetes/ on the master node.
I've been able to dump manifests of all resources in all namespaces in k8s using the following bash script, please edit as needed:
#!/usr/bin/env bash
while read -r line
do
output=$(kubectl get "$line" --all-namespaces -o yaml 2>/dev/null | grep '^items:')
if ! grep -q "\[\]" <<< $output; then
echo -e "\n======== "$line" manifests ========\n"
kubectl get "$line" --all-namespaces -o yaml
fi
done < <(kubectl api-resources | awk '{print $1}' | grep -v '^NAME')
Above bash script was tested with:
k8s v1.16.3
Ubuntu Bionic 18.04.3 OS
bash version version 4.4.20(1)-release (x86_64-pc-linux-gnu)
I suggest not to use the dump/manifests of an existing k8s cluster to create a new k8s cluster, just refer them as backup, and use an installer like Kubeadm to re-install k8s.
I've been getting confused by documentation and example differences between Helm2 and 3.
If you're interested, check helm-2to3 tool - it migrates configs and data from helm 2 to helm 3 using a command like helm 2to3 move config.

docker-compose logs with separate yml file

How do I run docker-compose logs but using a separate yaml config?
For example, if I want to run docker-compose up with a separate file, I can do:
docker-compose -f other-config.yml up
But -f in docker-compose logs points to a service, rather than a file.
For docker-compose, the -f options means to load a different configurations file. for docker-compose logs, the -f option means to follow the logs.
For your situation you need to pass the -f option twice:
docker-compose -f other-config.yml logs -f
You can optionally specify a service name as well at the end.

How to copy a file from host to Kubernetes container?

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