Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Docker Commands

Uploaded by

Saar Meir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Docker Commands

Uploaded by

Saar Meir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Install docker

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-


ce.repo
yum -y install docker-ce
systemctl start docker

-it - interactive + terminal


docker run image -create+run
docker create image
docker start image
docker start -a docker ID- with output
docker ps --all
docker system prune - delete all containers
docker logs docker ID -printout output (can replace the -a)
docker stop docker ID - stop docker with grace period
docker container stop $(docker container ls -aq) - stop all containers on the list
docker kill docker ID - stop docker without a grace period
docker exec -it docker ID command -execute commands withing the docker
docker exec -it docker ID sh - open shell (ctrl d exit)
docker build - build an image form a Dockerfile
docker rm - remove a docker
docker images - show images
docker rmi - remove image
docker pull - just download the image without running
docker run -p 80:5000 kodekloud/simple-webapp - map port 5000 to 80
docker run -v /opt/datadir:/var/lib/mysql mysql - store the data in volume
called /opt/datadir
docker inspect docker ID -output of all data of the container in jason format
docker logs docker ID
docker run ubuntu:17.10 cat /etc/*release* - install with specific version (targ
from docker hub)
docker run -d timer - docker runs in the background
docker attach timer - see the dispaly
docker inspect docker ID -see details regarding to docker include ip address
docker container run -d --restart unless-stopped nginx - restart the container
after stopped (not manually)
docker system df - amount of disk space for containers
docker system df -v - the highet ammount
docker container run -dt --rm --name testcontainer busybox ping -c10 google.com -
remove the container after done

kubectl get nodes


kubectl get nodes -o wide - see extended details of nodes
kubectl run nginx --generator=run-pod/v1 nginx --image=nginx -> Create an NGINX Pod
(using --generator)
kubectl describe pod myapp-pod
kubectl create -f file.yml - create pod from yml file
kubectl delete pod webapp - delete pod
kubectl run redis --image=redis123 --generator=run-pod/v1
kubectl edit pod redis -edit the yaml file
kubectl get replicationcontroller
kubectl scale --replicas=6 -f replicaset-definition.yml
kubectl get replicasets - check how many replicasets exist
kubectl describe pods
kubectl delete pod new-replica-set-452mt - delete pod
apply -f /root/replicaset-definition-1.yaml - update replicaset
kubectl delete replicaset replicaset-1 -delete replicaset
kubectl edit replicaset new-replica-set -scale replicaset
kubectl apply -f replicaset-definition-1.yaml

kubectl create -f deploymeny-definition.yml -create deployment from a yaml file


kubectl get deployments
kubectl get all

kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1 -upgrade image


kubectl rollout status deployment/myapp-deployment - check if we have a new version
and update if needed
kubectl rollout history deployment/myapp-deployment
kubectl rollout undo deployment/myapp-deployment - rollout to the old version

Certification tip:

Create an NGINX Pod


kubectl run --generator=run-pod/v1 nginx --image=nginx

Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
kubectl run --generator=run-pod/v1 nginx --image=nginx --dry-run -o yaml

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 -o yaml

Generate Deployment YAML file (-o yaml). Don't create it(--dry-run) with 4 Replicas
(--replicas=4)
kubectl create deployment --image=nginx nginx --dry-run -o yaml > nginx-
deployment.yaml

Save it to a file, make necessary changes to the file (for example, adding more
replicas) and then create the deployment.

kubectl get pods --namespace research - check pods on research namespace

--dry-run: By default as soon as the command is run, the resource will be created.
If you simply want to test your command, use the --dry-run option. This will not
create the resource, instead, tell you whether the resource can be created and if
your command is right.
-o yaml: This will output the resource definition in YAML format on the screen.

Use the above two in combination to generate a resource definition file quickly,
that you can then modify and create resources as required, instead of creating the
files from scratch.

POD
Create an NGINX Pod
kubectl run nginx --image=nginx --restart=Never

Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml

kubectl run redis --image=redis:alpine -l tier=db --generator=run-pod/v1 - create a


pod with label

Deployment
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 -o yaml

--generator=deployment/v1beta1 is deprecated as of Kubernetes 1.16. The recommended


way is to use the kubectl create option instead.

IMPORTANT:

kubectl create deployment does not have a --replicas option. You could first create
it and then scale it using the kubectl scale command
Save it to a file - (If you need to modify or add some other details)

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


deployment.yaml
You can then update the YAML file with the replicas or any other field before
creating the deployment.

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 -o yaml

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


Or
kubectl create service clusterip redis --tcp=6379:6379 --dry-run -o yaml (This
will not use the pods labels as selectors, instead it will assume selectors as
app=redis. You cannot pass in selectors as an option. So it does not work very well
if your pod has a different label set. So generate the file and modify the
selectors before creating the service)

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 --port=80 --name nginx-service --dry-run -o yaml

(This will automatically use the pod's labels as selectors, but you cannot specify
the node port. You have to generate a definition file and then add the node port in
manually before creating the service with the pod.)
Or
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run -o
yaml
(This will not use the pods labels as selectors)

Both the above commands have their own challenges. While one of it cannot accept a
selector the other cannot accept a node port. I would recommend going with the
`kubectl expose` command. If you need to specify a node port, generate a definition
file using the same command and manually input the nodeport before creating the
service.

kubectl get pods --selector env=dev - get pods on environment dev


kubectl taint nodes node01 spray=mortein:NoSchedule - create a taint on the node
with key and value and action

kubectl get pod elephant --output=yaml > elephant.yml - create yaml file from pod
kubectl get daemonsets --all-namespaces - get all deamon
kubectl get pods --all-namespaces - get pods in all namespaces
kubectl run --restart=Never --image=busybox static-busybox --dry-run -o yaml --
command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml - create a
static pos

kubectl top node


kubectl top pod
kubectl logs -f "node_name" "container_name"

kubectl rollout status seployment/myapp-deployment


kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1 - update image
version
kubectl rollout history seployment/myapp-deployment
Recreate - destroy and create new pods
Rollingupdate - remove and create one by one

kubectl rollout undo deployment/myapp-deployment - rollback to old version (old


replicaset within the deployment).

assign variables:

docker run -e APP_COLOR=pink simple-webapp-color - assign new variable


kubectl create configmap <config-name> --from-literal=<key>=<value>

secrets
========
create secret
kubectl create secret generic db-secret --from-literal=DB_Host=sql01 --from-
literal=DB_User=root --from-literal=DB_Password=password123

kubectl exec -it app cat /log/app.log --namespace elastic-stack -inspect a log
within a pod

kubectl drain node-1 - move the pods from node-1 to ther nodesand the node cannot
be scheduled
kubectl uncordon node-1 - the node will be scheduled
kubectl cordon node-1 - the node will not be scheduled

upgrade
======
kubectl drain node-1
apt-get upgrade -y kubeadm=1.12.0-00
apt-get upgrade -y kubelet=1.12.0-00
kuneadm upgrade node config --kunelet-version v1.12.0
systemctl restart kubelet
kubectl uncordon node-1

kubeadm upgrade plan - see the latest version

You might also like