Docker私有仓库部署和管理

2023-12-14 05:31:08

一、docker-compose构建lnmp
yum install -y yum-utils device-mapperpersistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker

curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose

docker-compose-lnmp
先创建网络:
docker network create --subnet=172.18.1.0/24 lnmp

vim docker-compose.yml
version: ‘3’
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:

  • 81:80
    networks:
    default:
    ipv4_address: 172.18.1.100
    links:
  • php:php
  • mysql:mysql
    volumes:
  • ./wwwroot:/usr/share/nginx/html

depends_on:

- php

privileged: true
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
ports:

  • 9000:9000
    links:
  • mysql:mysql

depends_on:

- mysql

networks:
default:
ipv4_address: 172.18.1.200
volumes:

  • ./wwwroot:/usr/share/nginx/html
    privileged: true
    mysql:
    hostname: mysql
    build:
    context: ./mysql
    dockerfile: Dockerfile
    ports:
  • 3306:3306
    networks:
    default:
    ipv4_address: 172.18.1.150

command: --character-set-server=utf8

- /usr/bin/mysqld_safe

volumes:

  • ./mysql/conf:/etc/mysql/conf.d

- ./mysql/data:/var/lib/mysql

privileged: true
networks:
default:
external:
name: lnmp

networks:
default:
ipv4_address: 172.18.0.31

networks:
default:
external:
name: mynetwork

docker-compose -f docker-compose.yml up -d

172.18.1.100
nginx:
FROM centos:centos7.9.2009
RUN yum -y update && yum install -y epel-release && yum -y install nginx
RUN rm -rf /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/
EXPOSE 80
CMD [ “/usr/sbin/nginx”,“-g”,“daemon off;” ]

vim nginx.conf

For more information on configuration, see:

* Official English Documentation: http://nginx.org/en/docs/

* Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteu?ser[time_local] “KaTeX parse error: Double superscript at position 34: … '?status b o d y b y t e s s e n t " body_bytes_sent " bodyb?ytess?ent"http_referer” ’
‘“ h t t p u s e r a g e n t " " http_user_agent" " httpu?sera?gent""http_x_forwarded_for”’;

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 4096;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    listen       80;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;
    index     index.php index.html index.htm;

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

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   172.18.1.200:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
    }	
}

Settings for a TLS enabled server.

server {

listen 443 ssl http2;

listen [::]:443 ssl http2;

server_name _;

root /usr/share/nginx/html;

ssl_certificate “/etc/pki/nginx/server.crt”;

ssl_certificate_key “/etc/pki/nginx/private/server.key”;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 10m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

}

172.18.1.200
php:
FROM centos:centos7.9.2009
RUN yum update -y && yum install -y epel-release && yum install php-fpm php-common php-devel php-mysqlnd php-mbstring php-mcrypt -y
RUN sed -i ‘/^daemonize/s/no/yes/g’ /etc/php-fpm.conf
RUN rm -rf /etc/php-fpm.d/www.conf
ADD www.conf /etc/php-fpm.d/
EXPOSE 9000
CMD [ “/usr/sbin/php-fpm”,“-F” ]

vim www.conf
; Start a new pool named ‘www’.
[www]

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; ‘ip.add.re.ss:port’ - to listen on a TCP socket to a specific address on
; a specific port;
; ‘port’ - to listen on a TCP socket to all addresses on a
; specific port;
; ‘/path/to/unix/socket’ - to listen on a unix socket.
; Note: This value is mandatory.
listen = 172.18.1.200:9000

; Set listen(2) backlog. A value of ‘-1’ means unlimited.
; Default Value: -1
;listen.backlog = -1

; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 172.18.1.100

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user’s group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives:
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in ‘idle’
; state (waiting to process). If the number
; of ‘idle’ processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in ‘idle’
; state (waiting to process). If the number
; of ‘idle’ processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to ‘static’ and the
; maximum number of child processes to be created when pm is set to ‘dynamic’.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either ‘static’ or ‘dynamic’
; Note: This value is mandatory.
pm.max_children = 50

; The number of child processes created on startup.
; Note: Used only when pm is set to ‘dynamic’
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 5

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.min_spare_servers = 5

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.max_spare_servers = 35

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify ‘0’. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
; accepted conn - the number of request accepted by the pool;
; pool - the name of the pool;
; process manager - static or dynamic;
; idle processes - the number of idle processes;
; active processes - the number of active processes;
; total processes - the number of idle + active processes.
; The values of ‘idle processes’, ‘active processes’ and ‘total processes’ are
; updated each second. The value of ‘accepted conn’ is updated in real time.
; Example output:
; accepted conn: 12073
; pool: www
; process manager: static
; idle processes: 35
; active processes: 65
; total processes: 100
; By default the status page output is formatted as text/plain. Passing either
; ‘html’ or ‘json’ as a query string will return the corresponding output
; syntax. Example:
; http://www.foo.bar/status
; http://www.foo.bar/status?json
; http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
; anything, but it may not be a good idea to use the .php extension or it
; may conflict with a real PHP file.
; Default Value: not set
;pm.status_path = /status

; The ping URI to call the monitoring page of FPM. If this value is not set, no
; URI will be recognized as a ping page. This could be used to test from outside
; that FPM is alive and responding, or to
; - create a graph of FPM availability (rrd or such);
; - remove a server from a group if it is not responding (load balancing);
; - trigger alerts for the operating team (24/7).
; Note: The value must start with a leading slash (/). The value can be
; anything, but it may not be a good idea to use the .php extension or it
; may conflict with a real PHP file.
; Default Value: not set
;ping.path = /ping

; This directive may be used to customize the response of a ping request. The
; response is formatted as text/plain with a 200 response code.
; Default Value: pong
;ping.response = pong

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the ‘max_execution_time’ ini option
; does not stop script execution for some reason. A value of ‘0’ means ‘off’.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the ‘slowlog’ file. A value of ‘0s’ means ‘off’.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /var/log/php-fpm/www-slow.log

; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024

; Set max core size rlimit.
; Possible Values: ‘unlimited’ or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: chrooting is a great security feature and should be used whenever
; possible. However, all PHP paths will be relative to the chroot
; (error_log, sessions.save_path, …).
; Default Value: not set
;chroot =

; Chdir to this directory at the start. This value must be an absolute path.
; Default Value: current directory or / when chroot
;chdir = /var/www

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
;catch_workers_output = yes

; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; exectute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
;security.limit_extensions = .php .php3 .php4 .php5

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
; php_value/php_flag - you can set classic ini defines which can
; be overwritten from PHP call ‘ini_set’.
; php_admin_value/php_admin_flag - these directives won’t be overwritten by
; PHP call ‘ini_set’
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining ‘extension’ will load the corresponding shared extension from
; extension_dir. Defining ‘disable_functions’ or ‘disable_classes’ will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Default Value: nothing is defined by default except the values in php.ini and
; specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 128M

; Set session path to a directory owned by process user
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

172.18.1.150
mysql
Dockerfile:
FROM centos:centos7.9.2009
RUN yum update -y && yum install -y epel-release && yum -y install mariadb-server mariadb-devel mariadb
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh
RUN /root/db_init.sh
EXPOSE 3306
CMD [ “/usr/bin/mysqld_safe” ]

db_init:
#!/bin/bash
/usr/bin/mysql_install_db --user=mysql
sleep 3
/usr/bin/mysqld_safe &
sleep 3
#涉及到的变量在Dockerfile中都已经声明
mysqladmin -u “ M A R I A D B U S E R " p a s s w o r d " MARIADB_USER" password " MARIADBU?SER"password"MARIADB_PASS”
#授权命令
mysql -u M A R I A D B U S E R ? p MARIADB_USER -p MARIADBU?SER?pMARIADB_PASS -e “grant all privileges on . to ‘ M A R I A D B U S E R ′ @ ′ MARIADB_USER'@'%' identified by ' MARIADBU?SER@MARIADB_PASS’ with grant option;”
mysql -u M A R I A D B U S E R ? p MARIADB_USER -p MARIADBU?SER?pMARIADB_PASS -e “grant all privileges on . to ‘ M A R I A D B U S E R ′ @ ′ l o c a l h o s t ′ i d e n t i f i e d b y ′ MARIADB_USER'@'localhost' identified by ' MARIADBU?SER@localhostidentifiedbyMARIADB_PASS’;”
h=$(hostname)
mysql -u M A R I A D B U S E R ? p MARIADB_USER -p MARIADBU?SER?pMARIADB_PASS -e “USE mysql ; update user set password=password(‘ M A R I A D B P A S S ′ ) w h e r e u s e r = ′ MARIADB_PASS') where user=' MARIADBP?ASS)whereuser=MARIADB_USER’ and host=‘$h’;”

wwwroot
index文件:

<?php echo date("Y-m-d H:i:s")."
\n"; $link=mysql_connect("mysql","root","123456"); if(!$link) echo "FAILD!"; else echo "MySQL is OK!"; phpinfo(); ?>

ctrl+P+Q

request_terminate_timeout = 0

二、consul
服务端节点:Consul-template、Docker 19.03、Compose 、Consul
客户端节点:Docker 19.03、registrator
hostnamectl set-hostname consul && bash
hostnamectl set-hostname registrator && bash
cat >>/etc/hosts<<EOF
192.168.180.210 consul
192.168.180.200 registrator
EOF

consul的安装:
yum install -y yum-utils device-mapperpersistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul

启动服务端:
前台运行:
consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.180.210 -client=0.0.0.0 -node=consul-server01
后台运行:
nohup consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=192.168.180.210 -client=0.0.0.0 -node=consul-server01 &>/var/log/consul.log &
查看集群的信息:
consul members
consul info | grep leader
通过 http api 获取集群信息:
curl 127.0.0.1:8500/v1/status/peers //查看集群 server 成员
curl 127.0.0.1:8500/v1/status/leader // 集 群 Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx 服务信息
curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息

Gliderlabs/Registrator的安装:
yum install -y yum-utils device-mapperpersistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
安装:
docker run -d --name=registrator --net=host -v /var/run/docker.sock:/tmp/docker.sock --restart=always gliderlabs/registrator:latest -ip=192.168.180.200 consul://192.168.180.210:8500
测试服务是否正常:
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:85:80 --name test-03 -h test03 nginx
docker run -itd -p:86:80 --name test-04 -h test04 nginx

-h 容器的主机名

consul-template的安装:
consul服务节点上:
vim /root/consul/nginx.ctmpl
upstream http_backend {
{{range service “nginx”}}
server {{ .Address }}:{{ .Port }};
{{ end }}
}
server {
listen 83;
server_name localhost 192.168.180.210;
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}

安装nginx:
yum install -y epel-release
yum install -y nginx
修改nginx配置文件
vim /etc/nginx/nginx.conf
添加到http下
include /etc/nginx/vhost/*.conf;
mkdir /etc/nginx/vhost

安装consul-template:
yum install -y unzip
unzip consul-template_0.27.0_linux_amd64.zip
启动consul-template
consul-template -consul-addr 192.168.180.210:8500 -template “/root/consul/nginx.ctmpl:/etc/nginx/vhost/kgc.conf:/usr/sbin/nginx -s reload” --log-level=info

编辑配置文件:
vim /etc/nginx/vhost/kgc.conf
upstream http_backend {
server 192.168.180.200:84;
server 192.168.180.200:83;
}
server {
listen 83;
server_name localhost 192.168.180.210;
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
测试:
docker run -itd -p:87:80 --name test-05 -h test05 nginx

docker logs -f test-01
docker logs -f test-02
docker logs -f test-03
docker logs -f test-04
docker logs -f test-05

三、私有仓库的搭建:
1、两个节点安装docker
hostnamectl set-hostname harbor

vim /etc/hosts

192.168.180.210 harbor

192.168.180.200 client

systemctl stop firewalld && systemctl disable firewalld

sed -i ‘/^SELINUX=/s/enforcing/disabled/’ /etc/selinux/config && setenforce 0
yum install -y yum-utils device-mapperpersistent-data lvm2 wget
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io -y
镜像加速

vim /etc/docker/daemon.json

{
“registry-mirrors”: [“https://6bs5y5lw.mirror.aliyuncs.com”]
}

systemctl start docker && systemctl enable docker
2、harbor节点安装docker-compose
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
3、harbor节点部署Harbor
wget http://harbor.orientsoft.cn/harbor-1.2.2/harbor-offline-installer-v1.2.2.tgz
cd /usr/local/harbor

cp harbor.yml.tmpl harbor.yml

vim harbor.yml

hostname: 192.168.180.210

https related config

https:

# https port for harbor, default is 443

port: 443

# The path of cert and key files for nginx

certificate: /your/certificate/path

private_key: /your/private/key/path

sh /usr/local/harbor/install.sh

在客户端访问时

docker login -u admin -p harbor12345 http://192.168.180.210
出现拒接连接时,调整下面文件

vim /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd --insecure-registry 192.168.180.210
systemctl daemon-reload

systemcl restart docker

文章来源:https://blog.csdn.net/drrui520/article/details/134968964
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。