mayurwaghmode / use-to-commands

1 stars 0 forks source link

kubectl commands #5

Open mayurwaghmode opened 1 year ago

mayurwaghmode commented 1 year ago

kubectl run nginx --image=nginx

mayurwaghmode commented 1 year ago

check where the pod is running kubectl get pods -o wide

mayurwaghmode commented 1 year ago

create a Kubernetes secret for storing the username and password

kubectl create secret generic quay-secret --from-literal=username=$USERNAME --from-literal=password=$PASSWORD

Check that the Secret was created:

kubectl get secrets

You can view a description of the Secret:

kubectl describe secrets/quay-secret

Decoding the Secret

kubectl get secret quay-secret -o jsonpath='{.data}'

mayurwaghmode commented 1 year ago

Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)

kubectl run nginx --image=nginx --dry-run=client -o yaml

mayurwaghmode commented 1 year ago

Create a deployment

kubectl create deployment --image=nginx nginx

Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

Generate Deployment with 4 Replicas

kubectl create deployment nginx --image=nginx --replicas=4

You can also scale deployment using the kubectl scale command.

kubectl scale deployment nginx --replicas=4 Another way to do this is to save the YAML definition to a file and modify

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml

mayurwaghmode commented 1 year ago

Service Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379

kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

(This will automatically use the pod's labels as selectors)

Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes:

kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml

mayurwaghmode commented 1 year ago

Expose the hr-web-app as service hr-web-app-service application on port 30082 on the nodes on the cluster.

The web application listens on port 8080.

Name: hr-web-app-service

Type: NodePort

Endpoints: 2

Port: 8080

NodePort: 30082

k expose --type=NodePort deployment hr-web-app --port 80 --name hr-web-app-service --overrides '{ "apiVersion": "v1","spec":{"ports": [{"port":80,"protocol":"TCP","targetPort":80,"nodePort":30082}]}}'

mayurwaghmode commented 1 year ago

Use JSON PATH query to retrieve the osImages of all the nodes

The osImages are under the nodeInfo section under status of each node.

kubectl get nodes -o=jsonpath='{..osImage}'

mayurwaghmode commented 1 year ago

Solution manifest file to create a persistent volume pv-analytics as follows:

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-analytics
spec:
  capacity:
    storage: 100Mi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  hostPath:
      path: /pv/data-analytics
mayurwaghmode commented 1 year ago

Expose the hr-web-app as service hr-web-app-service application on port 30082 on the nodes on the cluster.

The web application listens on port 8080.

kubectl expose deployment hr-web-app --type=NodePort --port=8080 --name=hr-web-app-service --dry-run=client -o yaml > hr-web-app-service.yaml

Now, in generated service definition file add the nodePort field with the given port number under the ports section and create a service.

mayurwaghmode commented 1 year ago

Create a static pod named static-busybox on the controlplane node that uses the busybox image and the command sleep 1000.

kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -oyaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml

mayurwaghmode commented 1 year ago

set current namespace context kubectl config set-context --current --namespace=alpha

mayurwaghmode commented 1 year ago

Print all environment variables present in the container

Kubectl exec -it pod1 -- printenv

mayurwaghmode commented 1 year ago

Delete terminating ns

export NS=argocd
NS=`kubectl get ns |grep Terminating | awk 'NR==1 {print $1}'` && kubectl get namespace "$NS" -o json   | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/"   | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -