Portainer.io docker management on ubuntu 20 and set nginx ssl certs

(อันนี้เป็น intro ไม่สนก็ข้ามไปจ๊ะ ) หัวข้อนี้เกิดจากปัญหาที่ผมไม่เคยเล่น Portainer.io + docker  เลยแต่งานล่าสุดเจอ ปัญหาว่าต้อง add ssl certs บน web service ที่เป็น Portainer.io + docker

จึงทำให้ต้องหาข้อมูลแล้วลองทำดู และได้วิธีรวมๆออกมาดังนี้

OS ที่ทดสอบ คือ ubuntu 20 64 bit

1.ติดตั้ง docker

apt update
apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get remove docker docker-engine docker.io containerd runc
apt-cache madison docker-ce
apt-get install docker-ce=5:19.03.10~3-0~ubuntu-focal docker-ce-cli=5:19.03.10~3-0~ubuntu-focal containerd.io
apt-get install docker-ce docker-ce-cli containerd.io
usermod -aG docker $USER
reboot
docker run hello-world
คำสั่งสุดท้ายเป็นการทดสอบ ควรต้องขึ้น 

Response:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

2.ติดตั้ง Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
3.ติดตั้ง Portainer
cd ~/
docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

จากนั้นลองเปิดเว็บเข้า http://<ip เครื่อง>:9000

จากนั้นหน้าเว็บจะให้ ใส่รหัสผ่านใหม่ จากนั้นจะให้เลือก ข้อมูลของของ docker ที่จะ ดึง ในที่นี้ผมเลือกเป็น local
ที่มาข้อ 1-3
https://docs.fuga.cloud/how-to-install-portainer-docker-ui-manager-on-ubuntu-20.04-18.04-16.04
4.pull image nginx
หากอยูที่หน้าแรก ให้เลือก  Endpoints ที่จะใช้งานในที่นี้ของผมคือ local

จากนั้นแทบซ้ายมือจะมีข้อมูลเพิ่ม ให้ไปที่ เมนู images
จากนั้นกรอกข้อมูล เพื่อ pull image ในที่นี้ผมจะใส่ตามนี้
จากนั้น คลิกที่ปุ่ม pull the image แล้วรอนิดไม่นาน
จากนั้นไปที่  เมนู Stacks แล้ว add stack แล้วกรอกตามนี้ 
Name: nginx_proxy (ชื่ออืนก็ได้ตามที่เข้าใจ)
Web editor: ตามด้านล่าง 
version: '2'
services:

 # Nginx webserver
 nginx:
 image: nginx:latest   #จากที่โหลดในเมนู image มา
 restart: always
 volumes: #ที่อยู่แชร์ไฟล์
 - /opt/docker/nginx-config:/etc/nginx/conf.d:ro   #แชร์ไฟล์ config โดยจะอยู่ที่ /opt/docker/nginx-config บน server เรา
 - /opt/docker/tarisga/ssl:/etc/ssl/certs:ro     #แชร์ไฟล์ certs โดยจะอยู่ที่ /opt/docker/tarisga/ssl บน server เรา
 ports:  #port ที่เปิดใช้งาน ของ http และ https
 - "80:80"
 - "443:443"

จากนั้นคลิกที่ปุ่ม Deploy the stack แล้วทดสอบดู 

5.set ssl certs nginx
หาก run nginx ได้แล้ว โดยเช็คได้ที่ stack นั้นๆ แล้วไปที่  Containers จะเห็นว่า run อยู่
แต่หาก เข้าไม่ได้ ให้ไปดู error ที่ Containers นั้นๆ แล้วไปที่ logs

จากนั้นนำ ssl certs ที่จะใช้งาน ไปไว้ที่ /opt/docker/tarisga/ssl
ต่อมาไปที่ /opt/docker/nginx-config แล้วสร้างไฟล์ config โดยในที่นี้จะใช้ชื่อ default.conf โดยมีข้อมูลตามนี้
 server {
 listen 443 ssl http2 default_server;
 #listen [::]:443 ssl http2 default_server;
 server_name _;
 root /usr/share/nginx/html;


 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers ECDHE+RSAGCM:ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:!aNULL!eNull:!EXPORT:!DES:!3DES:!MD5:!DSS;
 ssl_certificate /etc/ssl/certs/xxx.crt;
 ssl_certificate_key /etc/ssl/certs/xxx.key;


 # Load configuration files for the default server block.
 include /etc/nginx/default.d/*.conf;

 location / {
 index index.php index.html index.htm;
 try_files $uri $uri/ =404;
 }
 location ~ \.php$ {
 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 error_page 404 /404.html;
 location = /40x.html {
 }

 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 }
 }
 server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name _;
 root /usr/share/nginx/html;

}

จากเนื้อหาไฟล์ default.conf  ด้านบน จะมี 2 server คือ ของ port 80 เข้าเว็บปกติ กับ port 443 ที่เป็น https
ในส่วนของ port 443 จะมีส่วนกำหนดที่อยู่ของไฟล์ certs โดยที่อยู่จะอิงจาก การสร้าง stack  ในส่วนของ volumes สามารถไปกดดูที่ stack  ที่สร้างได้
โดยในที่นี้คือ /etc/ssl/certs/ แต่หาก Image ของ nginx ไม่ได้มีอะไรเปลี่ยนแปลงมาก ก็ต้องที่นี้ละ

หากเป็น certs ไม่จริงก็ลองทดสอบเลย หากได้อาจจะต้อง accept certs แต่หากเป็น certs จริงๆ ลองแก้ที่ 
C:\Windows\System32\drivers\etc\hosts โดยใส่ ip และเพิ่มโดเมนเราเข้าไป แล้วทดสอบดู


 

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *