Wednesday, October 24, 2018

Docker Certified Associate Prep Course

Docker Certified Associate Prep Course




Docker Kubernetes Implementation Plan
Plan Follows the below steps:
1.Installation of docker on Centos
2.Storage Driver
3.Docker Swarm
4.Configure Universal Control Plane (UCP)
5.Setting up DTR(Docker Trusted Repository )
6. Pull image, Run a Container and tag an image
7.Docker Services
8.Replicate Service globally
9.Stack Deployment
10.Docker Storage Volumes
11.Networking

Installation of docker on Centos
Device Mapper:
It is used for storage of sub-systems of the Containers.
Step 1:  Installing device-mapper-persistent data Package.
1
$ yum install -y yum-utils device-mapper-persistent-data lvm2
Step 2: Installing the Stable or Production version repo of Docker
1
$ sudo -E yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo   #Downloading & adding Repo to the Docker CE(Comunity Edition)
Step 3: Update the machine
1
$ yum update
Step 4: Install the docker-ce
1
$ yum install docker-ce   #It installs the docker and all dependencies.
Step 5: Enable,Start & Check the status of the docker
1
2
3
$ systemctl enable docker
$ systemctl start docker
$ systemctl status docker
Step 6: Checking docker images as a root
1
$ docker images         #It will shows the docker images present in the root user
Step 7: Running the Docker Commands in Non-root user
In Order to check the docker images as non-root user,add the particular user to docker group ,so that we will checkthe any info.
1
2
3
$ usermod -aG docker user          #Add the user
$ ssh user@Ip       #Logging to the user then we will access the any info
$ docker images
Add a Storage Driver
Selecting the Docker Storage Driver:
Containers support to be abstract & Portable
Step 1: Checking the Docker info
1
$ docker info
Step 2: Checking the storage driver of the docker
1
$ docker info| grep Storage
Storage Driver: Overlay
Step 3: Creating the .json file under /etc/docker directories
1
$ vim daemon.json       #Enter some code
{
“storage-driver” = ”devicemapper”
}          #In CentOs officially supports the device-mapper storage driver
–> Once we change the storage driver The docker images no longer will live (or) it will goes exited state (or) sometimes the images are get deleted when stop & start dokcer after changing the storage-driver
Step 4: Checking the docker images are available or not
1
$ docker images         # It will displays some images before restarting the docker
Step 5: After Creating the json file ,Restart the docker
1
2
$ systemctl stop docker && systemctl start docker
$ docker images          # All images will removed after  restarting the docker
Step 6: Goto the directories
1
$ cd /var/lib/docker      #You can find the device-mapper directory
Docker Swarm
Setting of Swarm(Configure Mnaagers):
–> This is the first step to making a Docker Swarm available for use. Let’s configure our first Swarm Manager and learn how to display the commands and tokens for joining other managers and workers to our cluster.
–> Docker swarm contains manager and node.
–> Node does the work and manager manages the nodes.
Step 1: Initializing the swarm
1
$ docker swarm init --autolock=true
Step 2: Generating a key/token
1
$ docker swarm init  --advertise-addr IP-Address    #Generates a token
–> This step generate the token number, and the current node acts as a manger in swarm
NOTE: Make sure To have a backup of key.
Step 3:  Adding Worker Node
1
$ docker swarm join-token worker        #This generates a token,copy that token and run in to the Worker nodes
Step 4: Add another Manager Node
1
$ docker swarm join-token manager
–> This will generate the token copy the token and run it in a node which you wants to add as a manager.
Step 5: Check whether nodes and managers are added in swarm
–> Run it in manager node:
1
$ docker node ls       # It will display the nodes and managers

                                         Configure Universal Control Plane (UCP)
NOTE: Add Hostname and Ip’s(private ip of linux academy server) in both manager and worker nodes
1
$ vi /etc/hosts
private_ip hostname
private_ip alias_ucp_name
private_ip hostname
Private_ip alias_dtr_name
Example:-
172.31.17.105 ncodeit6.mylabserver.com
172.31.17.105 ucp.example.com
172.31.26.79 ncodeit2.mylabserver.com
172.31.26.79 dtr.example.com~
NOTE: Add public_ip’s of linux academy in local PC
1
$ vi /etc/hosts
public_ip aliasname_ucp
public_ip aliasname_dtr
step 1: Create UCP Container
1
$ docker container run --rm -it --name ucp -v /var/run/docker.sock:/var/run.docker.sock docker/ucp:2.2.4 install –host-address IP-addr --interactive
# it asks for create user name and password:
Admin username:
Admin password:
Confirm Admin Password:
it prompts to create alias, create alias (this alias is used to login on to the UCP)
now you can access in browser on alias name or ip.
                    https://aliasname/login
login with username:
password:
Step 2:   Create a organization, Team, User and add user to a team
–> User Management → users → create user
set the user name and password for the user.
–>Organization &Teams  –> create organization
set the name for organization  and click on create button
–> Click on created organization name –> create a Team –> set Team Name –> click on create team
–> Add a user to the Team
select   organization –> team –> add user –> select the user from the list.

 Setting DTR(Docker Trusted Repository )
Step 1: Run DTR container
1
$ docker run -it --rm docker/dtr install --ucp-node dtr_hostname --ucp-username admin --ucp-url https://ucp_hostname --ucp-insecure-tls
–> Access UCP portal on web- browser
https://ucp_aliasname.com
–> Admin settings –> UCP nodes –> select the node to install the DTR
–> Select Disable TLS Certificate
–> Copy and Run the Command On The Node to Install DTR
–> Check  DTR console on the browser
https://dtr_aliasname.com
–> Goto UCP console
Admin settings → Docker Trusted Repository  # we can see the nodes

Pull an Image, Run a container and Tag image
step 1: Pull from docker hub
1
$ docker pull httpd
step 2: Run a container
1
$ docker run -d --name testweb httpd
step 3: Tag an image
1
$ docker tag httpd myhttpd

Docker Service
Step 1: Pull an image from central hub
1
$ docker pull httpd
Step 2: Run a httpd container
1
$ docker run --d --name testweb httpd
Step 3: Check IPAddress of the container
1
$ docker container inspect testweb | grep IPAddress
Step 4: Check the whether container is running
1
$ elinks http://IP-Address:port
Step 5: Create a docker service
1
2
$ docker service create --name testweb -p 80:80 --replicas 3 httpd           # creates a service on  3 nodes on 80 port
$ docker node ls          # displays all nodes linked with swarm
Step 6: Check Services are up and running.
1
2
$ docker service ls      # displays all services
$ docker service ps testweb   # displays running service on n number of nodes
Step 7: Check whether services are running in all swarm nodes
1
2
3
$ elinks http://node1
$ elinks http://node2
$ elinks http://node3

Replicate Service globally
step 1: create service to all tasks
1
$ docker service create --name testweb -p 5901:80 --mode global --detach=false httpd

step 2: list service in all swarm nodes
1
$ docker service ps testweb

Stack Deployment
Stack Deployment:
Running a container  on different ports in a swarm
Step 1: Creating a DockerFile
1
2
3
$ mkdir Dockerfile
$ cd Dockerfile
$ vi Dockerfile
–> Copy the following code
#testFROM centos:7LABEL maintainer “itzgeek.web@gmail.com”
RUN yum makecache
RUN yum install -y httpd
RUN echo “This Page Designed by ITzGeek, for Docker Build”
CMD [“apache2″,”-DFOREGROUND”]
CMD service httpd restart && /bin/bash
ENTRYPOINT service httpd restart && /bin/bash
ENTRYPOINT [ “/usr/sbin/httpd” ]
CMD [“-D”, “FOREGROUND”]
EXPOSE 80 

–> Save and Close.
Check all images:
1
$ docker images
Step 2: Build a docker image using docker file
1
$ docker build -t myhttpd:v1 # its builds a docker image by using dockerfile version v1
Step 3: Run a container using custom image
1
2
$ docker run -d --name testweb -p 80:80 myhttpd:v1
$ elinks http://node       # it executes testweb on ip
Step 4: First we need to install docker compose in cluster
1
2
3
$ sudo yum install epel-release
$ sudo yum install python-pip                  #Pip is used to upgrade the service
$ sudo pip install docker-compose
–> Here docker compose installs with including pythonpackages
Step 5: Create a docker-compose.yml file
1
$ vi docker-compose.yml
–> Add the below content to the file
version: '3'
services:
  web:
     image: httpd:latest
     deploy:
          replicas: 3
     ports:
          - "6901:80"
web2:
    image: httpd:latest
    deploy:
        replicas: 3
    ports:
         - "6902:80"
–> Save & Close
Step 6: Stack deployment on nodes
1
2
$ docker-compose up --d    #It will up the containers that are running on the host
$ docker stack deploy --compose-file docker-compose.yml mycustomstack     #It will deploy the customimage on all the nodes in a swarm
Storage Volumes
Storage Volume:
To integrate the local host directory to the directory in the container
Step 1: Creating a local volume
1
2
3
4
5
6
$ docker volume ls
$ docker volume create my-mount    #It creates a volume
$ docker volume inspect my-mount |more      #to know in-detail of my-mount
$ cd var/lib/docker/volumes/my-mount/_data
$ Echo “hello from the host” > hostfile.txt     #It creates a hostfile in _data directory
$ Echo “hello world” > internal-mount
Step 2: Creating a Docker volume and mount to the local volume
1
2
3
4
$ docker service create --name testweb --p 80:80 --mount source=my-mount,target=/internal-mount --detach=false --replicas 3 httpd        #Here we are sending that volume tot the httpd inside docker internal mount path
$ docker exec -it httpd id /bin/bash
$ cd internal-amount
$ exit
Networking
Step 1: Search for the networks
1
$ docker ntework ls
Step 2: Run any container
1
$ docker run --name testweb --P 80:80 httpd      #Example:httpd
Step 3: To know on which port the httpd is running
1
$ docker ps a
Step 4: To know on which ip httpd/testweb is running
1
$ docker container inspect testweb |grep IP-Adress
Step 5: Check on the browser whether container is running or not
$ elinks http://IP:PORT
Step 6: Check on which ip network is running or to know particular container running on which IP
1
$ docker container inspect –format=”{{.NetworkSettings.Networks.bridge.IPAddress}}” testweb
Create a New Network:
Step 7: Creating a new network  on the name of devei0
1
2
$ docker network create -d bridge devel0
$ docker network ls    # shows the newly created network
Step 8: Now connect a container to a newly added network
–> This following command connects httpd/testweb to the new network
1
$ docker network connect --ip=IP-Address devel0 testweb    #Give  devel0 IP address here
Step 9: Check whether network is connected to a container or not
1
2
3
$ docker ps a
$ docker container inspect --format=”{{.NetworkSettings.Networks.devel0.IPAddress}}” testweb
$ elinks http://IP
If you want to disconnect from the network:
1
$ docker network disconnect bridge testweb
How to configure docker to use external DNS:
1
$ cat /etc/resolv.conf      #we see the IP-Address of DNS
–> Check the DNS IP in the Container
1
2
$ docker exec --it testweb /bin/bash
$ cat /etc/resov.conf
NOTE: resolv.conf only supports 3 DNS
To run on diff DNS IP’s:
–> Running container on different DNS servers
1
2
$ docker run --name testweb -- dns=8.8.8.8 --dns=8.8.4.4 httpd
$ docker exec --it testweb /bin/bash
Another Method to overwrite DNS:
1
$ sudo vim /etc/docker/daemon.json
{
“dns” = [“8.8.8.8”,8.8.4.4”]
}
–> Save and Close
1
$ sudo systemctl restart docker

No comments:

Post a Comment