티스토리 뷰

카테고리 없음

[도커+쿠버네티스] 1

rediate.will 2021. 4. 10. 13:09

 

도커 설치 >

 

yum -y install yum-utils > yum 설치

 

yum install docker-ce docker-ce-cli containerd.io > 도커 설치

 

systemctl start docker > 도커 일단 스타트 시켜줌 (데몬이므로 항상 올라가 있어야함.)

 

systemctl status docker > 상태 확인

 

systemctl enable docker > 시작시 도커 자동시작하도록 함

 

docker run hello-world > 중간에 "Hello from Docker!"뜨면 됨.

 

ls /var/lib/docker에 다운받은 도커 파일이 저장 되어있음. (로컬 캐시라고 함 - 시간이 지나도 사라지지 않음)

 

docker ps -a > 컨테이너 목록 a옵션은 모두 보여줌.

 

 

 

아파치 컨테이너 100개 만들기 배쉬소스.이거 스크립트 실행은 <bash 파일명>

 

 

100개를 올려도 속도가 느려지거나 안올라가거나 하지도 않음. 포트번호도 다 같은 번호로 쓰는거 가능함.

=>분리된 환경을 달리 주어서 만들기가 쉬움.

 

docker exec -it apache90 /bin/bash > 컨테이너 내부로 들어갈 수 있음.

 

docker run -d --name=mydb --env=MYSQL_ROOT_PASSWORD=asdf mysql:5.5 > 도커허브에서 mariadb 받음. 비번 asdf 

 

도커이미지 -- 실행 --> 컨테이너

 

오후시간 시작!

 

파일 생성 (sudo시 비번 묻지않음)

cd /etc/sudoers.d/

filename : devops

> devops ALL=(ALL) NOPASSWD:ALL

 

usermod -G docker devops > 데브옵스가 도커를 실행할 수 있도록 그룹을 조정해 줌.

 

docker ps > 실행 중인 컨테이너

docker ps -a > 모든 컨테이너 (중단된 컨테이너 포함)

 

docker stop 컨테이너이름 || 컨테이너 아이디 > 컨테이너 중단 (삭제전에 중단해야함)

docker ps -q > 실행중인 컨테이너 값만 보여줌.

docker stop $(docker ps -q) > 모든 컨테이너 중단

docker ps -aq > 모든 컨테이너 아이디 출력

docker rm  $(docker ps -aq) > 모든 컨테이너 삭제

docker images > 이미지 리스트 출력

docker image rm hello-word:latest > 이미지 삭제. (이미지 이름:이미지태그) 태그 생략 가능(디폴트값 latest)

 === docker rmi hello-word

docker rmi -f $(docker images -q) > 강제로 모든 이미지 삭제

 

docker pull centos/httpd-24-centos7

 

docker system df > 디스크 이용상황

 

docker images === docker image ls

docker ps === docker container ps

 

docker run -d httpd > 현재 셀과 분리해서 실행(백그라운드로 실행)

 

docker inspect elastic_saha_컨테이너이름 > 컨테이너 정보

 

docker run -d --name=httpd-24-centos7 centos/httpd-24-centos7 > 이름을 httpd-24-centos7로 지정하고 백그라운드로 올림

 

curl http://172.17.0.3:8080 > 

curl -k https://172.17.0.3:8443 > 인증서 처리를 못하니까 무시하고 접속하라는 옵션이 k

docker pull centos:7 > centos7 다운로드

docker run -d --name=centos7 centos:7 sleep 3600s > 도커 시작할때 sleep 3600s 명령 실행하도록 함 (1시간 뒤에 죽음)

 

[devops@localhost ~]$ docker exec centos71 /bin/hostname
2b42a303776d

[devops@localhost ~]$ docker exec centos71 /bin/cal
     April 2021
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

 

> 컨테이너한테 명령 실행하도록 함

 

[devops@localhost ~]$ docker exec -it centos71 /bin/bash 
[root@2b42a303776d /]# pwd
/
[root@2b42a303776d /]# cal
     April 2021
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30


[devops@localhost ~]$ docker run -d --name=mycentos centos:7 /bin/bash /bin/sleep 300s
48fd4cffc418797a3f926ba25cf3900e8041df7e9673c69996bd9d1ff5c6971f
[devops@localhost ~]$ docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                       PORTS                NAMES
48fd4cffc418   centos:7                  "/bin/bash /bin/slee…"   11 seconds ago   Exited (126) 9 seconds ago                        mycentos
963b98551e29   centos:7                  "sleep 30s"              31 minutes ago   Exited (0) 31 minutes ago                         centos72
2b42a303776d   centos:7                  "sleep 3600s"            32 minutes ago   Up 32 minutes                                     centos71
7463115e7c4f   centos:7                  "/bin/bash"              37 minutes ago   Exited (0) 37 minutes ago                         centos7
c061e356c2c8   centos/httpd-24-centos7   "container-entrypoin…"   44 minutes ago   Up 44 minutes                8080/tcp, 8443/tcp   httpd-24-centos7
f21837ac5503   httpd                     "httpd-foreground"       53 minutes ago   Up 53 minutes                80/tcp               elastic_saha
f496310d3a83   httpd:2.4                 "httpd-foreground"       55 minutes ago   Exited (0) 54 minutes ago                         objective_franklin

 

[devops@localhost ~]$ docker run -d --name=mycentos1 centos:7 /bin/bash -c '/bin/sleep 300s' > 인자값 전달
be8556ba079ff9ac48d74522a880e8c731bd1d5888671d3c4ee3ca1c7433a5ab
[devops@localhost ~]$ docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS                NAMES
be8556ba079f   centos:7                  "/bin/bash -c '/bin/…"   6 seconds ago    Up 4 seconds                         mycentos1
2b42a303776d   centos:7                  "sleep 3600s"            34 minutes ago   Up 34 minutes                        centos71
c061e356c2c8   centos/httpd-24-centos7   "container-entrypoin…"   46 minutes ago   Up 46 minutes   8080/tcp, 8443/tcp   httpd-24-centos7
f21837ac5503   httpd                     "httpd-foreground"       55 minutes ago   Up 55 minutes   80/tcp               elastic_saha
[devops@localhost ~]$


<컨테이너 삭제 명령 배쉬 만들기>

[devops@localhost bin]$ vi ctrm
[devops@localhost bin]$ cat ctrm
#! /bin/bash
docker container stop $(docker ps -q)
docker container rm $(docker ps -aq)
[devops@localhost bin]$ ctrm
-bash: /home/devops/bin/ctrm: 허가 거부
[devops@localhost bin]$ chmod u+x ctrm


[devops@localhost ~]$ docker logs mydb > 로그 보는거
error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
[devops@localhost ~]$ docker run -d --name=mydb1 --env=MYSQL_ROOT_PASSWORD=asdf mysql:5.5
55eef50ee48a72b321ec3eab9d527e20120e146ad83631587df4257862fe77c5
[devops@localhost ~]$ docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS      NAMES
55eef50ee48a   mysql:5.5   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   3306/tcp   mydb1
[devops@localhost ~]$ docker exec -it mydb1 /bin/bash
root@55eef50ee48a:/# mysql -u root -pasdf
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 

[devops@localhost ~]$ docker run -d --name=mydb4 --env=MYSQL_ROOT_PASSWORD=asdf -e MYSQL_USER=user -e MYSQL_PASSWORD=asdf -e MYSQL_DATABASE=userdb mysql:5.5
8d2ec7d2fc303910eb5672e3ec03396505aaca4bb48ea89575403b310e507368
[devops@localhost ~]$ docker exec -it mydb4 /bin/bash
root@8d2ec7d2fc30:/# mysql -u user -pasdf
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
userdb             |
+--------------------+
2 rows in set (0.00 sec)

mysql>

 


 

[devops@localhost ~]$ docker inspect httpd > ip정보등 세부정보 볼 수 있음
[
    {
        "Id": "f278a348a9571773e9bad5f93ab1fe5d8e29098dbe7b0fcbea51eadba70769b4",
        "Created": "2021-04-10T08:39:15.105993652Z",
        "Path": "httpd-foreground",
        "Args": [],

 


 

 

 

다시 curl http://172.17.0.2:80 하면 hello container 뜸

 

[devops@localhost ~]$ ctrm
f278a348a957
[devops@localhost ~]$ docker run -d --name=httpd httpd:2.4
344493985e8d34c5652a4ebeb472eb49efee5cdce74208a7b36a1e7c3754fe63
[devops@localhost ~]$ curl http://172.17.0.2
<html><body><h1>It works!</h1></body></html>
[devops@localhost ~]$


컨테이너 레이어는 캐시느낌. 꺼지면 데이터 수정해도 없어짐.

그러면 데이터 계속 유지시킬려면 어떻게 해야하냐?

-> 외부 스토리지를 만들어서 컨테이너 레이어에 마운트 시켜서 사용함.

 

 docker run -d --name=httpd --volume=/var/tmp/mystorage:/usr/local/apache2/htdocs httpd:2.4

 

[devops@localhost ~]$ curl http://172.17.0.2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
<ul></ul>
</body></html>
[devops@localhost ~]$ docker exec -it httpd /bin/bash
root@32e553e0906f:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs  modules
root@32e553e0906f:/usr/local/apache2# cd htdocs/
root@32e553e0906f:/usr/local/apache2/htdocs# ls
root@32e553e0906f:/usr/local/apache2/htdocs# echo "hello apache container" > index.html
root@32e553e0906f:/usr/local/apache2/htdocs# exit
exit
[devops@localhost ~]$ ctrm
32e553e0906f
32e553e0906f
[devops@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[devops@localhost ~]$ docker run -d httpd:2.4
45c41e013457f6e2221836002b9785f9997a26d1d4d589938d774f84d6176959
[devops@localhost ~]$ ctrm
45c41e013457
45c41e013457
[devops@localhost ~]$ docker run -d --name=httpd --volume=/var/temp/mystorate:/usr/local/apache2/htdocs httpd:2.4
9f0a0a3d1c6e0687c879a6eb312a7eabaaa78128ea7b05981bed1a44e7b7689b
[devops@localhost ~]$ curl http://172.17.0.2
hello apache container
[devops@localhost ~]$

 

 

 

 

실습

1-1. centos:7 이미지를 30분 동안 컨테이너가 실행 되게

 >> [devops@localhost ~]$ docker run -d --name=centos centos:7 /bin/bash -c '/bin/sleep 1800s'
f703d41c4644c2e32c7e13faa9cb234d481f0d8ac79e70db588b2691b3f90e00
[devops@localhost ~]$ docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS     NAMES
f703d41c4644   centos:7   "/bin/bash -c '/bin/…"   11 seconds ago   Up 10 seconds             centos

 

1-2. centos:7 컨테이너로 접속해서 net-tools 패키지를 설치

[devops@localhost ~]$ docker exec -it centos /bin/bash
[root@f703d41c4644 /]# yum install net-tools

 

2-1. mariadb latest 버전을 설치하고 db데이터가 영구 보존되도록 호스트 디렉토리에 db데이터가 저장되게

IMAGE            COMMAND                  CREATED          STATUS          PORTS      NAMES
c76d95a257f9   mariadb:latest   "docker-entrypoint.s…"   13 seconds ago   Up 11 seconds   3306/tcp   elegant_sanderson
[devops@localhost db]$ docker exec -it elegant_sanderson /bin/bash
root@c76d95a257f9:/# mysql -u root -pasdf
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.9-MariaDB-1:10.5.9+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> exit
Bye
root@c76d95a257f9:/# exit
exit

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함