반응형
  • Image 를 export 하는 방법

docker save [option] [tar filename] [image name]

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    fa5269854a5e   2 weeks ago   142MB

docker save -o test.tar fa5269854a5e

 

  • 실행중인 컨테이너를 export 하는 방법

docker export [container name or containter ID] > [tar filename]

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                NAMES
791601bf0587   nginx:latest   "/docker-entrypoint.…"   33 minutes ago   Up 33 minutes   0.0.0.0:80->80/tcp   mystifying_benz

docker export 791601bf0587 > test.tar

 

 

728x90
반응형
반응형

K8S의 container 에 정의되는 args 와 command 에 대한 차이점은 다음과 같다. 

Docker image 빌드시에 ENTRYPOINT 와 CMD 를 정의 할 수 있다.

ENTRYPONT : 컨테이너가 실행될 때 반드시 default 로 실행된다. 따라서 컨테이너가 수행될 때 변경되지 않을 실행명령은 ENTRYPOINT 로 정의하는게 좋다.
CMD : 컨테이너 실행시 파라메터를 추가 하게 되면 추가된 파라메터를 실행시킨다. 

이때 k8s 에서 정의하는 args 는 Docker 이미지의 CMD 에 바인딩 되고 command 는 ENTRYPOINT 에 바인딩 된다. 이름때문에 command가 CMD 에 바인딩된다고 착가하면 안된다.

 

728x90
반응형
반응형

docker 실행시 컨테이너 내부에서 컨테이너 외부 파일을 연결할수 있는 방법이 있다.

docker run 실행시 -v [호스트경로]:[컨테이너경로] 를 추가해주면 호스트 경로와 컨테이너 경로가 연결되게 된다. 한가지 중요한 점은 호스트 경로의 상태가 컨테이너 경로에 덮어써진다는 것이다. 

➜  docker docker run --name nginx-mounts -d -p 8081:80 -v /Users/Workspaces/docker:/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
7d63c13d9b9b: Pull complete 
15641ef07d80: Pull complete 
392f7fc44052: Pull complete 
8765c7b04ad8: Pull complete 
8ddffa52b5c7: Pull complete 
353f1054328a: Pull complete 
Digest: sha256:dfef797ddddfc01645503cef9036369f03ae920cac82d344d58b637ee861fda1
Status: Downloaded newer image for nginx:latest
f1aa047705f563a0db1f76abbadddf74ea2fff7542e55601a84eccf43dc207b4
➜  docker curl localhost:8081                          
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.4</center>
</body>
</html>

위에서는 docker를 실행할 때 /Users/Workspaces/docker 경로를 컨테이너 안에 /usr/share/nginx/html 에 바인드 시켰다. 기본적으로 nginx의 /usr/share/nginx/html 경로에는 index.html 파일이 존재하지만 현재 호스트의 /Users/Workspaces/docker 경로에는 아무것도 없는 빈 디렉토리이기 때문에 403 이 나온다.

 

728x90
반응형
반응형

1. docker inspect container_id
명령어를 치면 굉장히 많은 정보를 확인할 수 있다. 그래서 grep 으로 조회하면 좀 수월하다.
docker inspect container_id | grep IP

"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "192.168.0.1",
"IPPrefixLen": 16,
"IPv6Gateway": "",
        "IPAMConfig": null,
        "IPAddress": "192.168.0.1",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,

2. docker exec -it container_id /bin/bash
위 명령어를 사용하면 컨테이너 내부로 접속이 가능하다. 내부로 접속을 해서 ip addr 이나 ifconfig, hostname -I 등을 사용해서 확인 가능하다. 

3. docker exec container_id command
exec 만 사용을 하고 후에 명령어를 치면 컨테이너 내부에 명령어를 보내 실행이 가능하다. 

 

728x90
반응형
반응형

history 명령어를 통해서 이미지가 어떤 과정을 거쳐 생성되었는지 확인해볼수 있다.

아래와 같이 nginx 의 latest 이미지와 stable 이미지에 대한 내역을 비교해볼수 있다.

root@myserver-001:~# docker history nginx:stable
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
c8d03f6b8b91   4 weeks ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      4 weeks ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      4 weeks ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      4 weeks ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      4 weeks ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      4 weeks ago   /bin/sh -c set -x     && addgroup --system -…   63.9MB    
<missing>      4 weeks ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.5.3        0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.20.1     0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      4 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      4 weeks ago   /bin/sh -c #(nop) ADD file:910392427fdf089bc…   69.3MB    

root@myserver-001:~# docker history nginx:latest
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
04661cdce581   45 hours ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  STOPSIGNAL SIGQUIT           0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B        
<missing>      45 hours ago   /bin/sh -c #(nop) COPY file:09a214a3e07c919a…   4.61kB    
<missing>      45 hours ago   /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB    
<missing>      45 hours ago   /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0…   1.96kB    
<missing>      45 hours ago   /bin/sh -c #(nop) COPY file:65504f71f5855ca0…   1.2kB     
<missing>      45 hours ago   /bin/sh -c set -x     && addgroup --system -…   61.1MB    
<missing>      45 hours ago   /bin/sh -c #(nop)  ENV PKG_RELEASE=1~bullseye   0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  ENV NJS_VERSION=0.7.0        0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.21.4     0B        
<missing>      45 hours ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      4 weeks ago    /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      4 weeks ago    /bin/sh -c #(nop) ADD file:16dc2c6d1932194ed…   80.4MB

 

728x90
반응형
반응형

[K8S] ## K8S 설치시 Trouble Shooting

- kubeadm init 했는데 다음과 같이 나오는 경우

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.

Unfortunately, an error has occurred:
timed out waiting for the condition
This error is likely caused by:
- The kubelet is not running
- The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
- 'systemctl status kubelet'
- 'journalctl -xeu kubelet'

Additionally, a control plane component may have crashed or exited when started by the container runtime.
To troubleshoot, list all containers using your preferred container runtimes CLI, e.g. docker.
Here is one example how you may list all Kubernetes containers running in docker:
- 'docker ps -a | grep kube | grep -v pause'
Once you have found the failing container, you can inspect its logs with:
- 'docker logs CONTAINERID'
error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster

 

1. docker Cgroup 이 systemd 로 설정되어있을 경우

/etc/docker/daemon.json 파일에 아래 문구 추가한다.

{
	"exec-opts": ["native.cgroupdriver=systemd"]
}

그리고
systemctl daemon-reload
systemctl restart docker

2. 로그 확인

- 'journalctl -xeu kubelet' 

이 명령어를 실행 하면 아래와 같이 로그가 남았다. 

Apr 30 22:19:38 master kubelet: W0430 22:19:38.226441    2372 cni.go:157] Unable to update cni config: No networks found in /etc/cni/net.d
Apr 30 22:19:38 master kubelet: E0430 22:19:38.226587    2372 kubelet.go:2067] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

 

내 경우에는 위에 2.  /env/environment 설정 에 보면 **no_proxy** 항목에 현재 K8S 가 설치된 Machine 의 IP 를 추가했더니 해결이 되었다.

- kubeadm join 했는데 다음과 같이 나올 경우

[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists
    [ERROR Port-10250]: Port 10250 is in use
    [ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher

    VM 에서 다음과 같이 kubeadm join 을 했는데 에러가 났을경우 (node가 2개인데 2번째 노드 구성시)

    kubeadm reset 을 한번 실행하고 다시 join 명령어를 실행해준다.

728x90
반응형
반응형

Tasks

An application image with a v1 tag has been pushed to the gcr.io repository
A new Kubernetes cluster exists (zone: us-central1-a)
Check that an application has been deployed to the cluster
Test that a service exists that responds to requests like Echo-app

 

1. 클러스터 생성


gcloud beta container --project "qwiklabs-gcp-00-337f72711928" clusters create "echo-cluster" --zone "us-central1-a" --no-enable-basic-auth --cluster-version "1.14.10-gke.17" --machine-type "n1-standard-2" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --metadata disable-legacy-endpoints=true --scopes "
https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "3" --enable-stackdriver-kubernetes --enable-ip-alias --network "projects/qwiklabs-gcp-00-337f72711928/global/networks/default" --subnetwork "projects/qwiklabs-gcp-00-337f72711928/regions/us-central1/subnetworks/default" --default-max-pods-per-node "110" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair

 

클러스터 생성은 Cloud Console 에서 직접 하면 된다.

name 은 "echo-cluster" , machine-type 은 "n1-standard-2" 로 만들어 준다.

 

2. tar file 을 Google Cloud Storage 에서 복사한 후 압축을 푼다.

 

- Google Cloud Strage 에서 복사

gsutil cp gs://qwiklabs-gcp-00-337f72711928/echo-web.tar.gz .

- 압축 풀기
tar -xvfz echo-web.tar

 

 

3. Docker Build 및 Image Push

- Docker Build

- Image 확인

echo-app 이 생성된 것을 확인할 수 있다.

 

- GCR(Google Cloud Registry) 에 push 하기 위해서 Tag 를 변경해준다. 

export PROJECT_ID=$(gcloud info --format='value(config.project)')

docker tag echo-app:v1 gcr.io/${PROJECT_ID}/echo-app:v1

- Image 를 Cloud Registry 에 push

중간에 나는 project id 를 그대로 써줬는데 위에서 export 를 했기 때문에 ${PROEJCT_ID} 로 써줘도 된다.

 

4. GKE 에 Deploy 하기

Cloud Console  의 Container Registry 를 확인해 보면 위와 같이 push한 이미지를 확인 할 수 있다.

오른쪽에 ... 메뉴를 눌러보면 Deploy to GKE 버튼을 볼수 있다. 그걸 클릭한다. 

 

Image 는 "Exsiting conatiner images" 를 선택하고 좀전에 생성한 이미지를 선택해준다.

 

Application name 은 "echo-web" 이라고 해준다.

라벨에도 key에 app, value 에 echo-web 이라고 넣어준다.

Cluster 는 위에서 생성한 "echo-cluster" 를 선택한다.

그리고 "Deploy" 클릭.

 

5. 외부에서 접속 할수 있도록 expose 하기

 

Cloud Console 에서 Kubernetes Engine > Workloads 메뉴로 가면 echo-web 의 detail 정보로 들어가면 위와 같이 화면이 나온다.

화면 상단에 있는 Expose 버튼을 누른다.

 

port 를 80 으로 넣어주고 Target port 를 8080 으로 넣고 Expose 버튼을 누른다.

 

Expose 가 완료되면 위와같이 External endpoint 가 나타난다.

 

주소로 접속을 하게 되면 위와 같이 결과를 볼 수 잇다.

Cloud console 메뉴를 이용해서 만들었는데 직접 kubectl 명령어를 활용해서 만들어 보는것도 좋은 공부가 될것 같다.

 

 

 

 

728x90
반응형
반응형

 

Spring boot Application 을 Dockerfile 로 build 할 때에 profile 값을 전달 해야해서 찾아본 것을 적어보았다.

profile 값을 전달하기 위해서는 다음과 같은 방법들이 있다. 

 

1.Dockerfile 에 profile 값을 넣고 build 를 한다.

FROM java:8
ADD target/app.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=dev","-jar","/app.jar"]

2.Docker run 할때 환경 변수로 전달한다.

docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:lates

3.DockerCompose 로 전달한다.

version: "3"
services:
	rest-api:
	image: rest-api:0.0.1
	ports:
		- "8080:8080"
	environment:
		- "SPRING_PROFILES_ACTIVE=dev"

 

여기에서 난 1번을 선택해서 적용을 했는데 한가지 Dockerfile 에 전달되는 profile 값이 변경 될수 있도록 하고 싶었다. 그럴려면 docker build 시점에 값을 넘겨줘야 하는데 document를 찾아보니 ARG 를 사용하게 되면 docker build 시점에 값을 넘길수가 있다.

FROM ubuntu
VOLUME /tmp
ADD app.jar app.jar
RUN bash -c 'touch ./app.jar'
ARG SPRING_PROFILES_ACTIVE
RUN echo $SPRING_PROFILES_ACTIVE
ENV SPRING_PROFILES_ACTIVE=$SPRING_PROFILES_ACTIVE
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar","./app.jar"]
docker build --build-arg SPRING_PROFILES_ACTIVE=dev .

결과적으로 docker build 시에 --build-arg 로 SPRING_PROFILES_ACTIVE 값을 넘겨주고 Dockerfile 에서는 전달 받은 SPRING_PROFILES_ACTIVE 값을 ENV 로 등록을하면 ENTRYPOINT 에 -D 옵션을 넣어서 실행하지 않아도 자동으로 profile 이 적용되서 실행이 된다.

 


참고자료

https://docs.docker.com/engine/reference/builder/#arg

https://stackoverflow.com/questions/43707770/spring-boot-in-docker

 

728x90
반응형
반응형

로컬 또는 git에서 빌드한 파일을 AWS에 ubuntu 로 파일 옮기는게 귀찮아서 jenkins 로 받아서 가져올수 있도록 만들어봤다..

이곳에 가면 바로 설치할 수 있는 가이드가 있다.


https://jenkins.io/doc/book/installing/#on-macos-and-linux


docker run \

  -u root \

  --rm \

  -d \

  -p 49000:8080 \

  -v jenkins-data:/var/jenkins_home \

  -v /var/run/docker.sock:/var/run/docker.sock \

  jenkinsci/blueocean


각각 의 라인 별로 설명도 잘 되어있다. 나는 우선 host port를 8080으로 사용하기 싫어서 49000번으로 설정했다.


CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                NAMES

ac6489e4bc34        jenkinsci/blueocean   "/sbin/tini -- /usr/…"   3 seconds ago       Up 2 seconds        50000/tcp, 0.0.0.0:49000->8080/tcp   suspicious_benz

e99420ff2ee4        mysql                 "docker-entrypoint.s…"   24 hours ago        Up 24 hours         0.0.0.0:3306->3306/tcp, 33060/tcp    mysql-db



run 할때 이름을 따로 안줘서 이상한 이름이 들어가 있으니 이름을 바꿔보자.


docker rename suspicious_benz jenkins


ONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                NAMES

ac6489e4bc34        jenkinsci/blueocean   "/sbin/tini -- /usr/…"   10 minutes ago      Up 10 minutes       50000/tcp, 0.0.0.0:49000->8080/tcp   jenkins

e99420ff2ee4        mysql                 "docker-entrypoint.s…"   24 hours ago        Up 24 hours         0.0.0.0:3306->3306/tcp, 33060/tcp    mysql-db


이렇게 정상적으로 이름이 바뀌었다.


Host Url 의 49000번 포트로 접속을 하면 Jenkins 가 기동된 모습이 나온다. 그런데 아직 정상적인 모습은 아니다.



Administrator password 를 넣으라는데 어디서 찾지?? 다음 명령어를 통해 jenkins container 안으로 들어간다.


 docker exec -it jenkins bash


container 로 접속 한 후에 저 캡쳐한 사진을 보면 경로가 나와있다. 


/var/jenkins_home/secrets/initialAdminPassword


이 파일 안에 password 가 있다. password 를 복사해서 붙여넣고 Continue를 누르면 된다.



난 첫번째꺼 선택하고 진행했다.

이것저것 설치되면서 진행이 된다.


admin 계정으로 생성할 정보를 넣은 후 Save and Continue를 누른다.



접속 URL 을 다시 안내해준다.



이렇게 해서 Jenkins 설치가 마무리 되었다.


728x90
반응형

'Development > Build' 카테고리의 다른 글

[Gradle] gradle build error tool.jar... valid JDK  (0) 2018.10.19
[Gradle]Ubuntu 에서 Gradle 설치하기  (0) 2018.10.19
Mac 에서 gradle 설치  (0) 2016.07.04
Maven 라이브러리 추출  (0) 2016.01.06
Maven clean, install, build??  (0) 2015.05.12

+ Recent posts