一、基本概念:
1.1、Linux名稱Namespace
? ? ? ? namespace是Linux系統(tǒng)的底層概念,在linux的內(nèi)核中具體實(shí)現(xiàn),內(nèi)核中有不同類型的名稱空間,Docker容器運(yùn)行在同一個(gè)Docker主進(jìn)程,且共享同一個(gè)宿主機(jī)內(nèi)核,通過(guò)名稱空間隔離各個(gè)容器,但是容器在一個(gè)進(jìn)程內(nèi)實(shí)現(xiàn)運(yùn)行指定的服務(wù)環(huán)境,并且還可以保護(hù)宿主機(jī)內(nèi)核不受其它進(jìn)程的干擾和影響,例如:文件系統(tǒng)空間、網(wǎng)絡(luò)空間、進(jìn)程空間等,主要的名稱孔家如下:

1.2、為了避免容器占用完宿主機(jī)的資源,對(duì)容器進(jìn)行資源限制:比如CPU、內(nèi)存、磁盤(pán)、網(wǎng)絡(luò)帶寬等,Linux使用Cgroup實(shí)現(xiàn)上述功能,此外,它還能夠?qū)M(jìn)程進(jìn)行優(yōu)先級(jí)設(shè)置,以及講進(jìn)程掛起和恢復(fù)等操作

二、Docker安裝
2.1、系統(tǒng):ubuntu 20.4
2.2、docker軟件版本:docker-ce-19.03.15
2.3、軟件安裝:
2.3.1、關(guān)閉防火墻:
?ufw disable
2.3.2、參數(shù)優(yōu)化:
cat >>/etc/security/limits.conf<<EOF
*? ? ? ? ? ? ? ? -? ? ? nofile? ? ? ? ? 65535
*? ? ? ? ? ? ? ? -? ? ? nproc? ? ? ? ? 65535
EOF
cat >>/etc/sysctl.conf<<EOF
kernel.pid_max=4194303
fs.file-max=1000000
vm.max_map_count=262144
net.netfilter.nf_conntrack_max=2097152
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000? ? 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.ipv4.tcp_max_orphans = 16384
net.core.netdev_max_backlog = 16384
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
net.ipv4.ip_nonlocal_bind=1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness = 0
EOF
2.3.3、安裝軟件
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
apt-get -y update
apt install docker-ce=5:19.03.15~3-0~ubuntu-focal docker-ce-cli=5:19.03.15~3-0~ubuntu-focal
2.3.4、設(shè)置docker配置文件
cat<<EOF > /etc/docker/daemon.json
{
? "exec-opts": ["native.cgroupdriver=systemd"],
? "log-driver": "json-file",
? "log-opts": {
? ? "max-size": "100m"
? },
? "storage-driver": "overlay2",
? "storage-opts": [
? ? "overlay2.override_kernel_check=true"
? ],
? "registry-mirrors": ["https://2efvplft.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
查看docker設(shè)定信息
docker info
Client:
Debug Mode: false
Server:
Containers: 0
? Running: 0
? Paused: 0
? Stopped: 0
Images: 0
Server Version: 19.03.15
Storage Driver: overlay2
? Backing Filesystem: extfs
? Supports d_type: true
? Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: systemd
Plugins:
? Volume: local
? Network: bridge host ipvlan macvlan null overlay
? Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: fec3683
Security Options:
? apparmor
? seccomp
? Profile: default
Kernel Version: 5.11.0-27-generic
Operating System: Ubuntu 20.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.81GiB
Name: CNCF-K8S-MD
ID: Q6MC:ZVNA:MEUS:RASN:7CYM:HNBX:UKWM:PEY3:ZTVY:GW76:UOU7:XMJF
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
? 127.0.0.0/8
Registry Mirrors:
? https://2efvplft.mirror.aliyuncs.com/
Live Restore Enabled: false
三、Docker常用命令
拉取鏡像:docker pull 鏡像
docker pull? nginx
上傳鏡像:docker push 鏡像
docker push nginx
運(yùn)行容器:
docker run -d --rm -p 81:80 nginx??
-d是后臺(tái)運(yùn)行,--rm是容器結(jié)束后自動(dòng)刪除,-p 映射端口:8081為主機(jī)端口、8080為容器中的服務(wù)端口? bash為容器運(yùn)行的命令
root@CNCF-K8S-MD:~# docker run -d --rm -p 81:80 nginx
ab827b65bdaa002aa8d0a094e3d1bd5110ad7f8668a40b13cb9fe4f62be029d9
root@CNCF-K8S-MD:~# docker ps -a
CONTAINER ID? ? ? ? IMAGE? ? ? ? ? ? ? COMMAND? ? ? ? ? ? ? ? ? CREATED? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? ? NAMES
ab827b65bdaa? ? ? ? nginx? ? ? ? ? ? ? "/docker-entrypoint.…"? 5 seconds ago? ? ? Up 4 seconds? ? ? ? 0.0.0.0:81->80/tcp? hopeful_galois
查看主機(jī)上的容器:docker ps -a,-a是顯示所有容器,不帶-a是只顯示運(yùn)行中的容器
顯示主機(jī)上的鏡像:docker images
為鏡像打標(biāo)簽: docker tag 源標(biāo)簽? 目標(biāo)標(biāo)簽
root@CNCF-K8S-MD:~# docker tag nginx registry.test.com/nginx:v1
root@CNCF-K8S-MD:~# docker images
REPOSITORY? ? ? ? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED? ? ? ? ? ? SIZE
nginx? ? ? ? ? ? ? ? ? ? latest? ? ? ? ? ? ? f6987c8d6ed5? ? ? ? 5 days ago? ? ? ? ? 141MB
registry.test.com/nginx? v1? ? ? ? ? ? ? ? ? f6987c8d6ed5? ? ? ? 5 days ago? ? ? ? ? 141MB
centos? ? ? ? ? ? ? ? ? ? latest? ? ? ? ? ? ? 5d0da3dc9764? ? ? ? 3 months ago? ? ? ? 231MB
鏡像刪除:docker rmi -f 鏡像名稱或ID
docker rmi nginx
root@CNCF-K8S-MD:~# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254
四、HABOR安裝
4.1、主機(jī)系統(tǒng):系統(tǒng):ubuntu 20.4
4.2、docker軟件版本:docker-ce-19.03.15
4.3、IP地址:10.0.0.3/24
4.4、軟件下載地址:https://github.com/goharbor/harbor/releases
4.5、軟件配置
tar xf harbor-offline-installer-v2.3.5.tgz
cd harbor/
cp harbor.yml.tmpl harbor.yml
編輯harbor.yml文件
hostname: reg.mydomain.com 改為hostname: 10.0.0.3;harbor_admin_password: Harbor12345改為arbor_admin_password: 1234567,1234567是登錄harbor網(wǎng)頁(yè)時(shí)用戶admin的密碼;data_volume: /data其中/data改為你需要的目錄,本次不更改;注釋掉下面的內(nèi)容,不啟用https
#https:
? #? port: 443
? # certificate: /your/certificate/path
? #private_key: /your/private/key/path
4.6、下載docker-composer
sudo curl-L"https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname-s)-$(uname-m)" -o /usr/local/bin/docker-compose
chmod +x?/usr/local/bin/docker-compose
4.7、安裝harbor
./install.sh?--with-trivy --with-chartmuseum
最后出現(xiàn)? ----Harbor has been installed and started successfully.----表示安裝成功
ss -lntup |grep 80
tcp? ? LISTEN? 0? ? ? ? 128? ? ? ? ? ? 127.0.0.1:6010? ? ? ? ? 0.0.0.0:*? ? ? users:(("sshd",pid=41680,fd=11))? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
tcp? ? LISTEN? 0? ? ? ? 16384? ? ? ? ? ? ? ? ? *:80? ? ? ? ? ? ? ? ? *:*? ? ? users:(("docker-proxy",pid=44727,fd=4))? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
tcp? ? LISTEN? 0? ? ? ? 128? ? ? ? ? ? ? ? [::1]:6010? ? ? ? ? ? ? [::]:*? ? ? users:(("sshd",pid=41680,fd=10))?
4.8、harbor使用
登錄網(wǎng)頁(yè),用戶名admin,密碼1234567

新建項(xiàng)目



修改client的docker配置(/etc/docker/daemon.json),
在"registry-mirrors": ["https://2efvplft.mirror.aliyuncs.com"]后面加,然后添加:? ?“insecure-registries”: [“10.0.0.3”]
重新加載和重啟docker
登錄:
docker login 10.0.0.3
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
給要上傳的鏡像打標(biāo)簽
docker tag nginx 10.0.0.3/test/nginx:v1
上傳鏡像
root@CNCF-K8S-MD:/opt/harbor# docker pull 10.0.0.3/test/nginx:v1
Error response from daemon: unknown: repository test/nginx not found
root@CNCF-K8S-MD:/opt/harbor# docker push 10.0.0.3/test/nginx:v1
The push refers to repository [10.0.0.3/test/nginx]
51a4ac025eb4: Pushed
4ded77d16e76: Pushed
32359d2cd6cd: Pushed
4270b63061e5: Pushed
5f5f780b24de: Pushed
2edcec3590a4: Pushed
v1: digest: sha256:2e87d9ff130deb0c2d63600390c3f2370e71e71841573990d54579bc35046203 size: 157

下載鏡像測(cè)試:
root@CNCF-K8S-MD:/opt/harbor# docker rmi 10.0.0.3/test/nginx:v1
Untagged: 10.0.0.3/test/nginx:v1
Untagged: 10.0.0.3/test/nginx@sha256:2e87d9ff130deb0c2d63600390c3f2370e71e71841573990d54579bc35046203
root@CNCF-K8S-MD:/opt/harbor# docker pull 10.0.0.3/test/nginx:v1
v1: Pulling from test/nginx
Digest: sha256:2e87d9ff130deb0c2d63600390c3f2370e71e71841573990d54579bc35046203
Status: Downloaded newer image for 10.0.0.3/test/nginx:v1
10.0.0.3/test/nginx:v1
安裝完成