1、基礎環境介紹html
一、基礎軟硬件環境介紹linux
須要64位的CPUnginx
linux內核版本3.10及以上git
內核支持cggroups 和 namespacegithub
系統版本爲CentOS7.4【centos6也可安裝,可是不穩定】docker
Docker版本爲18.06
shell
K8S版本爲1.16 【k8s版本必須和docker版本兼容】apache
二、查看k8s與docker版本的對應關係json
第一步:登陸githubcentos
https://github.com/kubernetes/kubernetes/releases
第二步:查看指定版本k8s的CHANGELOG文件
第三步:搜索docker version,查看支持的docker版本
三、配置yum源
1)用阿里雲的鏡像便可,路徑爲 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/,以下
2)配置yum源
[root@host1 ~]# cat /etc/yum.repos.d/docker.repo [docker] name=aliyun docker repo enabled=1 gpgcheck=0 baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/
查看是否配置成功
[root@host1 ~]# yum repolist Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.bit.edu.cn * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com repo id repo name status docker aliyun docker repo 58 base/7/x86_64 CentOS-7 - Base 10,097 extras/7/x86_64 CentOS-7 - Extras 307 updates/7/x86_64 CentOS-7 - Updates 997
2、安裝配置docker
一、安裝docker 18.06.0
[root@host1 ~]# yum install docker-ce-18.06.0.ce
二、建立json文件加速docker鏡像下載速度
[root@host1 ~]# mkdir /etc/docker [root@host1 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"] }
三、啓動服務
[root@host1 ~]# systemctl start docker [root@host1 ~]# systemctl status docker
看下docker版本
[root@host1 ~]# docker --version Docker version 18.06.0-ce, build 0ffa825
查看docker信息
[root@host1 ~]# docker info
3、關於鏡像的操做
一、關於防火牆問題
docker是須要啓動iptables這個服務的,可是在centos7中,默認是用firewalld來管理,而不是iptables,因此可能會致使docer使用有問題,由於須要iptables作端口映射。能夠用以下的方式來解決
[root@host1 ~]# systemctl disable firewalld ^C [root@host1 ~]# yum install iptables-services -y ^C [root@host1 ~]# systemctl enable iptables ^C
二、查看本地的鏡像
[root@host1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
三、搜索鏡像,如搜索nginx鏡像
[root@host1 ~]# docker search nginx
從上面的search的結果能夠看到,有些搜索出來的結果就是一個內容,而有些結果是用斜槓分割的
沒有斜槓分隔的,咱們稱之爲頂級倉庫,通常就是官方的
有分隔的稱之爲:用戶倉庫或者像倉庫,通常就是用戶本身註冊帳號,而後作個倉庫,並放一些鏡像給別人用。
四、拉去幾個鏡像
1)拉取nginx鏡像,爲節省帶寬,此處選alpine版本
[root@host1 ~]# docker image pull nginx:1.14-alpine 1.14-alpine: Pulling from library/nginx bdf0201b3a05: Pull complete 3d0a573c81ed: Pull complete 8129faeb2eb6: Pull complete 3dc99f571daf: Pull complete Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7 Status: Downloaded newer image for nginx:1.14-alpine
2)拉取busybox鏡像
[root@host1 ~]# docker image pull busybox Using default tag: latest latest: Pulling from library/busybox 322973677ef5: Pull complete Digest: sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Status: Downloaded newer image for busybox:latest
3)查看本地已經下載的鏡像
[root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest b534869c81f0 2 weeks ago 1.22MB nginx 1.14-alpine 8a2fb25a19f5 8 months ago 16MB
REPOSITORY:倉庫名稱
TAG:標籤
IMAGE ID:惟一ID值
CREATED:什麼時間建立的
SIZE:大小
4)查看鏡像的詳細信息
[root@host1 ~]# docker image ls --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b 2 weeks ago 1.22MB nginx 1.14-alpine sha256:8a2fb25a19f5dc1528b7a3fabe8b3145ff57fe10e4f1edac6c718a3cf4aa4b73 8 months ago 16MB
4、關於容器的操做
一、容器操做包括以下
啓動容器:start
中止容器:stop
強制中止:kill
建立後直接啓動容器:run
暫停容器:pause
取消暫停(繼續運行)容器:unpause
也能夠列出來所有的容器:docker ps或docker container ls
二、啓動容器操做
啓動容器用run命令,包括以下參數
-t:這裏是指定一個終端,若是沒有終端,是沒法登錄這個容器的
-i, --interactive:若是想用交互式訪問,就須要用這個選項
--name string:這裏是指定容器的名字
--rm:當容器中止的時候,自動刪除容器對象
-d, --detach :讓當前的這個容器工做在後臺
--network string :指定容器加入到哪一個網絡,若是不加的話,默認是一個bridge網絡
三、基於busybox啓動容器
[root@host1 ~]# docker run --name bbox -it busybox / #
此時就啓動了容器,同時進入了容器的shell中
此時,在其餘的終端能夠看到busybox容器已經啓動了
[root@host1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc696a322096 busybox "sh" About a minute ago Up About a minute bbox
查看busybox容器中的信息能夠看到此時id爲1的進程是sh而不是init
/ # ps PID USER TIME COMMAND 1 root 0:00 sh 6 root 0:00 ps
在busybox容器中模擬apache
/ # mkdir /data/html -p / # echo "test page">>/data/html/index.html / # / # httpd -f -h /data/html/
也能夠在bash中查看一下容器的詳細信息
[root@host1 ~]# docker inspect bbox2 | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3",
可看到這個容器的地址是172.16.0.3
其實在安裝了docker之後,系統默認生成一個虛擬網卡docker0,物理機就是用docker0和虛擬機通訊的
[root@host1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:d5:cc:54:6d brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:d5ff:fecc:546d/64 scope link valid_lft forever preferred_lft forever 6: veth96dc278@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 10: veth693182e@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
docker0的地址是172.17.0.1
在物理機用curl能夠訪問容器中的apache
[root@host1 ~]# curl 172.17.0.3 test page
三、中止容器與從新啓動容器
執行exit就能夠退出容器
/ # exit [root@host1 ~]#
退出容器後,容器只是中止了,可是依然存在
docker ps:查看正在運行的容器
docker ps -a:查看所有的容器,包括已經中止的
[root@host1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@host1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc696a322096 busybox "sh" 2 hours ago Up 2 hours bbox
重啓啓動這個中止的容器,用start命令,可能用兩個選項
-i:交互模式
-a:附加到終端上
[root@host1 ~]# docker start -i -a bbox
四、刪除已經中止的容器
[root@host1 ~]# docker rm bbox bbox [root@host1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
在建立容器的時候加入rm選項,能夠實現當容器中止的時候,自動刪除
[root@host1 ~]# docker run --rm -it --name bbox2 busybox / # / # exit [root@host1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
在刪除容器時候能夠指定名稱來刪除,也能夠指定容器的ID來刪除
在指定ID的時候,不須要寫所有的ID內容,寫開頭的一部分便可
[root@host1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9c2d1bbe32e8 busybox "sh" 3 seconds ago Exited (0) 1 second ago bbox [root@host1 ~]# docker rm 9c2d 9c2d [root@host1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
下面的命令能夠一次將全部已經中止的容器刪除
[root@host1 ~]# docker rm $(docker ps -a -q)
五、刪除鏡像、導出鏡像、導入鏡像
rmi:刪除本地的鏡像
load:導入鏡像文件
save:導出鏡像文件
查看當前有哪些鏡像
[root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest b534869c81f0 2 weeks ago 1.22MB nginx 1.14-alpine 8a2fb25a19f5 8 months ago 16MB
導出busybox的鏡像到tmp下
[root@host1 ~]# docker save busybox:latest>/tmp/busybox-latest.tar.gz [root@host1 ~]# ls /tmp/ busybox-latest.tar.gz
刪除本地的busybox鏡像
[root@host1 ~]# docker rmi b53 Untagged: busybox:latest Untagged: busybox@sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Deleted: sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b Deleted: sha256:eac247cb7af5edc34d3620e8bce653d4af7d4e3a0427d487a97530c7fac0b841 [root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.14-alpine 8a2fb25a19f5 8 months ago 16MB
從tmp下導入鏡像文件
[root@host1 ~]# docker load</tmp/busybox-latest.tar.gz eac247cb7af5: Loading layer [==================================================>] 1.437MB/1.437MB Loaded image: busybox:latest [root@host1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest b534869c81f0 2 weeks ago 1.22MB nginx 1.14-alpine 8a2fb25a19f5 8 months ago 16MB