凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... ·...

34
凝思容器系统使用手册 北京凝思软件股份有限公司 Beijing Linx Software Co.,Ltd

Upload: others

Post on 19-Sep-2019

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册

北京凝思软件股份有限公司Beijing Linx Software Co.,Ltd

Page 2: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

目 录1 软件安装............................................................................................12 基础操作............................................................................................4

2.1 服务管理......................................................................................................42.2 查看帮助......................................................................................................5

3 基础管理............................................................................................53.1 容器管理......................................................................................................53.2 仓库管理....................................................................................................173.3 数据卷管理................................................................................................18

4 访问控制..........................................................................................215 端口映射..........................................................................................236 域名管理..........................................................................................247 网桥管理..........................................................................................258 平台基础命令....................................................................................26

8.1 自动补全....................................................................................................268.2 kubectl 上下文和配置...............................................................................26

9 容器管理..........................................................................................279.1 创建对象....................................................................................................279.2 查找和显示资源.........................................................................................289.3 更新资源....................................................................................................299.4 修补资源....................................................................................................309.5 编辑资源....................................................................................................309.6 缩放资源....................................................................................................319.7 删除资源....................................................................................................319.8 与运行中的 Pod 交互.................................................................................319.9 与节点和集群交互.....................................................................................32

表格

北京凝思软件股份有限公司 第 I 页

Page 3: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

1 软件安装准备工作:

获取 kubernetes 和 docker 安装包:

$ echo "192.168.22.233 linx.freenas.cn" >> /etc/hosts

$ wget -c -t 0 http://linx.freenas.cn:8080/kubernetes/kubernetes-bins.tar.gz

$ cd docker

$ wget -c -t 0 http://linx.freenas.cn:8080/kubernetes/docker-installer.tar.gz

确认 Deploy_K8s 目录中有 kubernetes-bins.tar.gz,Deploy_K8s/docker 目录中有 docker-installer.tar.gz

搭建 harbor 仓库:

harbor 仓库搭建参考 README.harbor 文档,harbor 仓库搭建完成后要将/data/cert/ca.crt 拷贝到每台节点

的/etc/docker/certs.d/hub.linx.com (需先在各节点创建该目录);并参照harbor 的使用手册将 pause-amd64、

calico-node、k8s-dns-dnsmasq-nanny-amd64、k8s-dns-sidecar-

amd64、k8s-dns-kube-dns-amd64、k8s-dashboard、heapster-amd64、heapster-influxdb-amd64、heapster-grafana-amd64 镜

像上传到 k8s 仓库,将 nginx-ingress-controller、defaultbackend-amd64 镜像上传到 k8s-ingress-controller 仓库。

使用手册下载:wget -c -t 0 http://linx.freenas.cn:8080/kubernetes/harbor/凝思容器仓库使用手册.odt

镜像下载:wget -c -t 0 http://linx.freenas.cn:8080/kubernetes/harbor/images.tar.gz

1)首先安装 docker(所有节点)$ cd docker

$ echo "linx_serial=docker 的序列号" > docker_sn.conf

$ ./install.sh

查看 docker 服务状态:

$ service docker status

2)准备 hosts(所有节点)

北京凝思软件股份有限公司 第 1 页

Page 4: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

配置 host,使每个节点都可以通过 hostName 解析到 ip 地址$ vi /etc/hosts

加入如下片段(ip 地址和 hostName 替换成实际值)

192.168.22.11 master1

192.168.22.12 master2

192.168.22.13 master3

192.168.22.14 node1

192.168.22.14 node2

192.168.22.10 VIP

192.168.22.100 hub.linx.com

3) 准备 k8s 配置文件(所有节点)$ cd Deploy_K8s (主目录)

$ vim config.properties

其中:NODE_IP 填写当前节点的 ip

ETCD_ENDPOINTS 填写 https://${VIP}:2381

MASTER_IP 填写${VIP}

MASTER1_IP、MASTER2_IP、MASTER3_IP 分别填写各主节点的 IP

4) 同步根证书(主节点)生成根证书(所有主节点)$ cd Deploy_K8s (主目录)

$ ./gen-config.sh

将一台主节点的根证书同步到其余所有节点(包含其余主节点,工作节点需先创建/etc/kubernetes/ca 目录)

$ scp /etc/kubernetes/ca/* root@hostName:/etc/kubernetes/ca/

5) 搭建 haproxy、keepalived 环境(主节点)cd haproxy

北京凝思软件股份有限公司 第 2 页

Page 5: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

$ ./install.sh master/backup (所有主节点中设置一台 master,其余设置为backup)

检查: 在设置为 master 的机器上执行$ ip addr (能看到集群的 VIP 地址)

6) 部署主节点所有主节点:$ cd Deploy_K8s (主目录)

$ ./install.sh master

将一台主节点的 etcd 证书同步到其他主节点:

$ scp /etc/kubernetes/ca/etcd/* root@hostName:/etc/kubernetes/ca/etcd/

启动 etcd 服务(所有主节点):

$ service etcd start

检查 etcd:(能看到所有主节点)

$ etcdctl \

--ca-file=/etc/kubernetes/ca/ca.pem \

--cert-file=/etc/kubernetes/ca/etcd/etcd.pem \

--key-file=/etc/kubernetes/ca/etcd/etcd-key.pem \

--endpoints https://192.168.23.110:2381 \

member list

7) 部署工作节点所有工作节点:

$ cd Deploy_K8s (主目录)

$ ./install.sh worker

8) 同步 kubeconfig 到工作节点(主节点)将一台主节点的 kubeconfig 同步到所有工作节点:

$ scp /etc/kubernetes/* root@hostName:/etc/kubernetes/

$ kubectl create clusterrolebinding kubelet-bootstrap \

--clusterrole=system:node-bootstrapper --user=kubelet-bootstrap

北京凝思软件股份有限公司 第 3 页

Page 6: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

检查所有工作节点 kubelet 服务状态:

$ service kubelet status

9) 批准工作节点加入集群(主节点)在一台主节点执行:

$ kubectl get csr|grep 'Pending' | awk '{print $1}'| xargs kubectl certificate approve

$ kubectl get nodes (能看到所有工作节点,且状态为 Ready)

10)部署 dahsboard、ingress 服务(主节点)为所有主节点添加 label:

$ kubectl label node 192.168.23.111 custom/ingress-controller-ready=true

部署服务:

$ cd yaml/

$ ./install.sh

检验服务部署是否成功:

$ kubectl get ds -n ingress-nginx (能看到 nginx-ingress-controller 信息)

$ kubectl get ingress --all-namespaces (能看到 dashboard.linx.cn 信息)

浏览器网页端登录验证: (任意主机)

$ vim /etc/hosts

添加: 192.168.23.110 dashboard.linx.cn (部署时替换实际 VIP 或任一主节点的 IP)

浏览器访问 url: https://dashboard.linx.cn (能访问到 kubernetes)

2 基础操作2.1 服务管理

操作系统:Linx6.0.80

1. 运行$ systemctl start docker

北京凝思软件股份有限公司 第 4 页

Page 7: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

2. 停止$ systemctl stop docker

3. 重启$ systemctl restart docker

2.2 查看帮助$ docker

或者$ docker help

3 基础管理3.1 容器管理

1) 创建语法:docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

描述:Create a new container

参数:域名映射: --add-host list Add a custom host-to-IP mapping

(host:ip)

绑入出错:-a, --attach list Attach to STDIN, STDOUT or STDERR

添系兼容: --cap-add list Add Linux capabilities

移系兼容: --cap-drop list Drop Linux capabilities

编号文件:--cidfile string Write the container ID to the file

添加主机:--device list Add a host device to the container

限读频率:--device-read-bps list Limit read rate (bytes per second) from a device (default [])

限读频率:--device-read-iops list Limit read rate (IO per second) froma device (default [])

限写频率:--device-write-bps list Limit write rate (bytes per second) toa device (default [])

北京凝思软件股份有限公司 第 5 页

Page 8: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

限写频率:--device-write-iops list Limit write rate (IO per second) to a device (default [])

跳过验证:--disable-content-trust Skip image verification (default true)

域名解服:--dns list Set custom DNS servers

域名解参:--dns-option list Set DNS options

域名解域:--dns-search list Set custom DNS search domains

镜像入口:--entrypoint string Overwrite the default ENTRYPOINT of the image

环境变量:-e, --env list Set environment variables

环境变量:--env-file list Read in a file of environment variables

暴露端口:--expose list Expose a port or a range of ports

添加群组:--group-add list Add additional groups to join

健康检查:--health-cmd string Command to run to check health

健检周期:--health-interval duration Time between running the check (ms|s|m|h) (default 0s)

健检通知:--health-retries int Consecutive failures needed to report unhealthy

健检通知: --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)

健检超时:--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)

主机名字:-h, --hostname string Container host name

容初始化:Run an init inside the container that forwards signals and reaps processes

绑定输入:-i, --interactive Keep STDIN open even if not attached

主机地址:--ip string IPv4 address (e.g., 172.30.100.104)

主机地址:--ip6 string IPv6 address (e.g., 2001:db8::33)

--ipc string IPC mode to use

容器隔离:--isolation string Container isolation technology

缓存限制:--kernel-memory bytes Kernel memory limit

北京凝思软件股份有限公司 第 6 页

Page 9: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

容器标签:-l, --label list Set meta data on a container

标签文件:--label-file list Read in a line delimited file of labels

容器互联:--link list Add link to another container

容器互联:--link-local-ip list Container IPv4/IPv6 link-local addresses

日志驱动:--log-driver string Logging driver for the container

日驱选项:--log-opt list Log driver options

机器地址:--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)

缓存限制:--memory bytes Memory limit

缓存限制:--memory-reservation bytes Memory soft limit

缓存限制:--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap

缓存限制:--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)

文件挂载:--mount mount Attach a filesystem mount to the container

容器名字:--name string Assign a name to the container

连接网络:--network string Connect a container to a network (default "default")

网络别名:--network-alias list Add network-scoped alias for the container

健检关闭:--no-healthcheck Disable any container-specified HEALTHCHECK

--oom-kill-disable Disable OOM Killer

--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)

容编前缀:--pid string PID namespace to use

容编限制:--pids-limit int Tune container pids limit (set -1 for unlimited)

扩展权限:--privileged Give extended privileges to this container

给主容端:-p, --publish list Publish a container's port(s) to the host

北京凝思软件股份有限公司 第 7 页

Page 10: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

给主容端:-P, --publish-all Publish all exposed ports to random ports

文件只读:--read-only Mount the container's root filesystem as read only

重启策略:--restart string Restart policy to apply when a container exits (default "no")

退出删容:--rm Automatically remove the container when it exits

缓存目录:--runtime string Runtime to use for this container

安全选项:--security-opt list Security Options

--shm-size bytes Size of /dev/shm

关容信号:--stop-signal string Signal to stop a container (default "SIGTERM")

关容超时:--stop-timeout int Timeout (in seconds) to stop a container

存驱选项:--storage-opt list Storage driver options for the container

--sysctl map Sysctl options (default map[])

挂载目录:--tmpfs list Mount a tmpfs directory

-t, --tty Allocate a pseudo-TTY

非限列表:--ulimit ulimit Ulimit options (default [])

用户标识:-u, --user string Username or UID (format: <name|uid>[:<group|gid>])

用户名缀: --userns string User namespace to use

--uts string UTS namespace to use

绑数据卷:-v, --volume list Bind mount a volume

数卷驱动:--volume-driver string Optional volume driver for the container

绑数据卷:--volumes-from list Mount volumes from the specified container(s)

工作目录:-w, --workdir string Working directory inside the container

$ docker images

北京凝思软件股份有限公司 第 8 页

Page 11: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

用仓库+标签确定镜像$ docker create -it --name centos6_container centos:centos6

docker create -it --name centos6_container 6a77ab6655b9 bash

$ docker ps -a

创数据卷+挂载目录+指定名字docker create -it --name centos6_container -v /src/webapp:/opt/webapp

centos:centos6

2) 启动语法:docker start [OPTIONS] CONTAINER [CONTAINER...]

描述:Start one or more stopped containers

参数:绑定输出:-a

覆拆卸钥: --detach-keys string

绑定输入:-i

$ docker start apache

$ docker start ubuntu

$ docker start ubuntu -i

进入语法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

描述:Run a command in a running container

-d, --detach Detached mode: run command in the background

--detach-keys string Override the key sequence for detaching a container

-e, --env list Set environment variables

-i, --interactive Keep STDIN open even if not attached

--privileged Give extended privileges to the command

-t, --tty Allocate a pseudo-TTY

-u, --user string Username or UID (format: <name|uid>[:<group|gid>])

北京凝思软件股份有限公司 第 9 页

Page 12: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

-w, --workdir string Working directory inside the container

进入方式 1:关闭的容器语法:docker start [OPTIONS] CONTAINER [CONTAINER...]

描述:Start one or more stopped containers

方式 2:运行中容器语法:docker attach [OPTIONS] CONTAINER

描述:Attach local standard input, output, and error streams to a running container

参数:莫绑输入: --no-stdin

覆拆卸钥: --detach-keys string

代理信号:--sig-proxy

$ docker attach apache

方式 3:运行中容器语法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

描述:Run a command in a running container

参数:示例:docker container attach $(docker ps -l --format=' {{.ID}}')

3)退出方式 1:关闭容器$ exit

方式 2:不关容器$ Ctrl+P+Q

4)停止北京凝思软件股份有限公司 第 10 页

Page 13: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

语法:docker stop [OPTIONS] CONTAINER [CONTAINER...]

描述:Stop one or more running containers

参数延迟几秒:-t int

$ docker stop apache

$ docker stop ubuntu -t 15

语法:docker kill [OPTIONS] CONTAINER [CONTAINER...]

描述:Kill one or more running containers

参数:发送信号:-s string

5)移除语法:docker rm [OPTIONS] CONTAINER [CONTAINER...]

描述:Remove one or more containers

参数:强制删除:-f

删数链接:-l

删数据卷:-v

停止状态$ docker rm apache

运行状态$ docker rm -f apache

$ docker rm -f ubuntu

$ docker rm -f 2d3c7afae625

6)导出(备份)语法: docker export [OPTIONS] CONTAINER

描述:Export a container's filesystem as a tar archive

北京凝思软件股份有限公司 第 11 页

Page 14: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

参数:文件名字:-o string

$ docker export apache > ubuntu.tar

$ docker export apache -o ubuntu.tar

7)导入(恢复)语法:docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

描述:Import the contents from a tarball to create a filesystem image

参数:配置文件:-c, --change list Apply Dockerfile instruction to the created

image

注释信息:-m, --message string Set commit message for imported image

$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0

8)运行语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

描述:Run a command in a new container

参数:后台运行:-d

端口映射:-p

环境变量:-e

缓存大小:-m

交互终端:-it

主机名字:-h

退出关闭:-rm

工作目录:-w

容器名字:--name

后台运行+指定名字$ docker run -d --name apache e121d5f99e1e

北京凝思软件股份有限公司 第 12 页

Page 15: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

$ docker run -d --name ubuntu 9b9cb95443b5 bash

$ docker run -it ubuntu 9b9cb95443b5 bash

9)查看容器列表语法: docker ps [OPTIONS]

描述:List containers

参数:显示所有:-a

条件过滤:-f

美化输出:--format

最新几条:-n

最新一条:-l

不截输出:--no-trunc

仅显编号:-q

显示大小:-s

$ docker ps

$ docker ps -n 5

$ docker ps -l

$ docker ps --no-trunc

$ docker ps -q

$ docker ps -s

10)流的状态语法:docker stats [OPTIONS] [CONTAINER...]

描述:Display a live stream of container(s) resource usage statistics

参数:

11)端口列表语法:docker port CONTAINER [PRIVATE_PORT[/PROTO]]

北京凝思软件股份有限公司 第 13 页

Page 16: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

描述:List port mappings or a specific mapping for the container

$ docker port 0fb4a58d3982 #?

$ docker port apache #?

$ docker port ubuntu

12)容器日志语法: docker logs [OPTIONS] CONTAINER

描述:Fetch the logs of a container

参数:内容细节:--details

按照日志:-f

从某时起:--since string

最后几行:--tail string

显示时间:-t

在某时前:--until string

$ docker logs -f apache

$ docker logs 2d3c7afae625

$ docker logs ubuntu

13)底层信息语法: docker inspect [OPTIONS] NAME|ID [NAME|ID...]

描述:Return low-level information on Docker objects

参数:美化输出:-f string

显示大小:-c

指定格式:--type string

$ docker inspect apache

$ docker inspect ubuntu

14)列出进程北京凝思软件股份有限公司 第 14 页

Page 17: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

语法: docker top CONTAINER [ps OPTIONS]

描述:Display the running processes of a container

15)文件差异语法: docker diff CONTAINER

描述:Inspect changes to files or directories on a container's

16)修改重新命名语法:docker rename CONTAINER NEW_NAME

描述:Rename a container

17)重新启动语法: docker restart [OPTIONS] CONTAINER [CONTAINER...]

描述:Restart one or more containers

参数:延迟几秒:-t int

18)暂停进程语法: docker pause CONTAINER [CONTAINER...]

描述:Pause all processes within one or more containers

19)启动进程语法:docker unpause CONTAINER [CONTAINER...]

描述:Unpause all processes within one or more containers

20)更新配置

北京凝思软件股份有限公司 第 15 页

Page 18: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

语法:docker update [OPTIONS] CONTAINER [CONTAINER...]

描述:Update configuration of one or more containers

参数: --blkio-weight uint16 Block IO (relative weight), between 10 and

1000, or 0 to disable

(default 0)

--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period

--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota

--cpu-rt-period int Limit the CPU real-time period in microseconds

--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds

-c, --cpu-shares int CPU shares (relative weight)

--cpus decimal Number of CPUs

--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)

--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)

--kernel-memory bytes Kernel memory limit

-m, --memory bytes Memory limit

--memory-reservation bytes Memory soft limit

--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap

--restart string Restart policy to apply when a container exits

21)使用容器制作镜像steps-01:进入容器交互终端$ docker run –ti e121d5f99e1e /bin/bash

后台运行+指定名字

北京凝思软件股份有限公司 第 16 页

Page 19: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

$ docker run -d --name apache e121d5f99e1e

正在运行$ docker exec -ti apache /bin/bash

steps-02:修改容器steps-03:创建镜像将容器做成镜像语法:docker commit containerID/containerName newImageName:tag

描述:Create a new image from a container's changes

参数:镜像作者: -a, --author string Author (e.g., "John Hannibal Smith

<[email protected]>")

配置文件: -c, --change list Apply Dockerfile instruction to the created image

注释备注: -m, --message string Commit message

暂停进程:-p, --pause Pause container during commit (default true)

示例:$ docker commit 4c8066cd8c01 apachephp:v1

3.2 仓库管理官方仓库(公共仓库)

注册语法:docker login [OPTIONS] [SERVER]

参数:指定密码:-p string

指定账户:-u string

输入输密:--password-stdin

注册成功后,本地用户目录的 .dockercfg 中将保存用户的认证信息。$ docker login

登录$ docker login

北京凝思软件股份有限公司 第 17 页

Page 20: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

注销语法:docker logout [SERVER]

$ docker logout

基本操作搜索$ sudo docker search centos

下载$ sudo docker pull centos

上传$ docker push

自动创建基础镜像:由 Docker 公司创建、验证、支持、提供。这样的镜像往往使用

单个单词作为名字。用户镜像:由 Docker 的用户创建并维护的,往往带有用户名称前缀。比如

tianon/centos 镜像3.3 数据卷管理

用途1. 绕过“拷贝写”系统,以达到本地磁盘 IO 的性能,(比如运行一个容器,

在容器中对数据卷修改内容,会直接改变宿主机上的数据卷中的内容,所以是本地磁盘 IO 的性能,而不是先在容器中写一份,最后还要将容器中的修改的内容拷贝出来进行同步。)

2. 绕过“拷贝写”系统,有些文件不需要在 docker commit打包进镜像文件。

3. 在多个容器之间共享目录 :使用数据卷容器4. 在宿主和容器间共享目录 5. 在宿主和容器间共享文件

创建语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

北京凝思软件股份有限公司 第 18 页

Page 21: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

方式 1:使用 -v 标记docker run -d -P --name web -v /webapp training/webapp python app.py

create a container

$ docker images

$ docker run --name data -v /opt/data -t -i docker.io/ubuntu /bin/bash

do in container

$ cd /opt/data

$ ls

$ echo "123" > 123

$ echo "123123" > 123123

$ ls

do in the vm

$ exit #退出时关闭容器$ Ctrl+P+Q #退出时不关容器$ docker ps

$ docker inspect data|grep /var/lib/docker/volumes

$ sudo ls /mnt/sda1/var/lib/docker/volumes/5887b7a4de662e531b6bdc32aa4a69a90fc841199133b3dd00315cb9b87f08b3/_data

$ sudo su

$ echo "asdhfjashdfjk" >> /mnt/sda1/var/lib/docker/volumes/5887b7a4de662e531b6bdc32aa4a69a90fc841199133b3dd00315cb9b87f08b3/_data/123

do in container

$ docker start -i data #exit 时用$ docker attach data

$ ls /opt/data

$ cat /opt/data/123

方式 2:也可在 Dockerfile 中使用 VOLUME

北京凝思软件股份有限公司 第 19 页

Page 22: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

添加一个或者多个新的卷到由该镜像创建的任意容器

挂载挂载目录:-v

docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py

指定权限::ro

只读::ro

docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py

读写:

挂载文件:-v

docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash

后台运行:-d

端口映射:-p

环境变量:-v

缓存大小:-m

交互终端:-it

主机名字:-h

退出关闭:-rm

工作目录:-w

容器名字:--name

备份docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf

/backup/backup.tar /dbdata

tar /backup/backup.tar (待查看命令)

恢复

北京凝思软件股份有限公司 第 20 页

Page 23: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

docker run -v /dbdata --name dbdata2 ubuntu /bin/bash

docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf

/backup/backup.tar

查看语法: docker inspect [OPTIONS] NAME|ID [NAME|ID...]

语法:grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]..

docker inspect data|grep /var/lib/docker/volumes

`

4 访问控制容器访问外部网络:'端口映射'

前提条件需要本地系统的转发支持查防火墙$ sudo sysctl net.ipv4.ip_forward

开防火墙$ sudo sysctl -w net.ipv4.ip_forward=1

简单示例$ sudo docker run -d -P training/webapp python app.py

$ sudo docker ps -l

$ sudo docker logs -f nostalgic_boyd

默认协议所有地址随机端口sudo docker run -d -P training/webapp python app.py

默认协议所有地址指定端口$ sudo docker run -d -p 5000:5000 training/webapp python app.py

默认协议指定地址随机端口$ sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py

北京凝思软件股份有限公司 第 21 页

Page 24: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

默认协议指定地址指定端口$ sudo docker run -d -p 127.0.0.1:5000:5000 training/webapp python

app.py

指定协议指定地址指定端口$ sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python

app.py

标记多个地址或者多个端口$ sudo docker run -d -p 5000:5000 -p 3000:80 training/webapp python

app.py

查看本地与容器的端口映射$ docker port nostalgic_boyd 5000

备注:容器有自己的内部网络和 ip 地址

各个容器之间访问:容器互联前提条件容器的网络拓扑是否已经互联。默认情况下,所有容器都会被连接到

docker0 网桥上需要本地系统的转发支持简单示例建数据库容器$ sudo docker run -d --name db training/postgres

删除网页容器$ docker rm -f web

创建网页容器+建立容器链接$ sudo docker run -d -P --name web --link db:db training/webapp python

app.py

查看容器连接$ docker ps

查看容器环境变量:通过 env命令$ sudo docker run --rm --name web2 --link db:db training/webapp env

北京凝思软件股份有限公司 第 22 页

Page 25: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

查看容器环境变量:通过 hosts 文件 cat /etc/hosts

访问所有端口切换--icc=true

或在 /etc/default/docker 文件中配置 DOCKER_OPTS=--icc=true

设置访问指定端口

5 端口映射容器访问外部实现注释:容器所有到外部网络的连接,源地址都会被NAT 成本地系统的 IP 地

址。注释:这是使用 iptables 的源地址伪装操作实现的。查看主机的 NAT 规则$ sudo iptables -t nat -nL

外部访问容器实现在 docker run 时候通过 -p 或 -P 参数来启用查看主机的 NAT 规则$ sudo iptables -t nat -nL

指定端口指定地址:-p IP:host_port:container_port

6 域名管理语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

主机名字-h HOSTNAME or --hostname=HOSTNAME

注释:设定容器的主机名字,它会被写到容器内的 /etc/hostname 和

/etc/hosts。

北京凝思软件股份有限公司 第 23 页

Page 26: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

主机别名--link=CONTAINER_NAME:ALIAS

情景:在创建容器的时候,添加一个其他容器的主机别名到 /etc/hosts 文件中

让新容器的进程可以使用主机别名 ALIAS 就可以连接它。

指定容器 dns 服务器地址--dns=IP_ADDRESS

情景:添加 DNS 服务器地址到容器的 /etc/resolv.conf 中,让容器用这个服务器来解析所有不在 /etc/hosts 中的主机名。

设定某容器的 dns搜索域--dns-search=DOMAIN

容器默认的 dsn 配置文件如果没有指定容器的 dns 配置,Docker 会默认用主机上的 /etc/resolv.conf

来配置容器。情景:宿主主机 DNS 信息发生更新后,所有 Docker 容器的 dns 配置通过

/etc/resolv.conf 文件立刻得到更新。7 网桥管理

创建Docker 服务默认会创建一个 docker0 网桥(其上有一个 docker0 内部接

口),它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络。

--bip=CIDR -- IP 地址加掩码格式--mtu=BYTES -- 接口允许接收的最大传输单元

北京凝思软件股份有限公司 第 24 页

Page 27: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

查看sudo brctl show

删除$ sudo service docker stop

$ sudo ip link set dev docker0 down

$ sudo brctl delbr docker0

创建$ sudo brctl addbr bridge0

$ sudo ip addr add 192.168.5.1/24 dev bridge0

$ sudo ip link set dev bridge0 up

$ ip addr show bridge0 查看确认

配置:docker 服务echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker

启动:docker 服务sudo service docker star

知识储备:查看桥接信息brctl show

查看 ip 地址配置ip addr

查看 ip路由信息ip route

8 平台基础命令

北京凝思软件股份有限公司 第 25 页

Page 28: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

8.1 自动补全$ source <(kubectl completion bash) setup autocomplete in bash, bash-completion package should be installed first.$ source <(kubectl completion zsh) setup autocomplete in zsh8.2 kubectl上下文和配置$ kubectl config view 显示合并后的 kubeconfig 配置同时使用多个 kubeconfig 文件并查看合并后的配置$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view获取 e2e 用户的密码$ kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'$ kubectl config current-context 显示当前的上下文$ kubectl config use-context my-cluster-name 设置默认上下文为 my-cluster-name向 kubeconf 中增加支持基本认证的新集群$ kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword使用指定的用户名和 namespace 设置上下文$ kubectl config set-context gce --user=cluster-admin --namespace=foo \&& kubectl config use-context gce

9 容器管理9.1 创建对象

Kubernetes 的清单文件可以使用 json 或 yaml 格式定义。可以以

.yaml、.yml、或者 .json 为扩展名。

北京凝思软件股份有限公司 第 26 页

Page 29: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

$ kubectl create -f ./my-manifest.yaml 创建资源$ kubectl create -f ./my1.yaml -f ./my2.yaml 使用多个文件创建资源$ kubectl create -f ./dir 使用目录下的所有清单文件来创建资

源$ kubectl create -f https://git.io/vPieo 使用 url 来创建资源$ kubectl run nginx --image=nginx 启动一个 nginx 实例$ kubectl explain pods,svc 获取 pod 和 svc 的文档从 stdin 输入中创建多个 YAML 对象$ cat <<EOF | kubectl create -f -

apiVersion: v1

kind: Pod

metadata:

name: busybox-sleep

spec:

containers:

- name: busybox

image: busybox

args:

- sleep

- "1000000"

---

apiVersion: v1

kind: Pod

metadata:

name: busybox-sleep-less

spec:

containers:

- name: busybox

image: busybox

args:

- sleep

- "1000"

北京凝思软件股份有限公司 第 27 页

Page 30: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

EOF

创建包含几个 key 的 Secret

$ cat <<EOF | kubectl create -f -

apiVersion: v1

kind: Secret

metadata:

name: mysecret

type: Opaque

data:

password: $(echo "s33msi4" | base64)

username: $(echo "jane" | base64)

EOF

9.2 查找和显示资源$ kubectl get services 列出所有 namespace 中的所有 service

$ kubectl get pods --all-namespaces 列出所有 namespace 中的所有 pod

$ kubectl get pods -o wide 列出所有 pod 并显示详细信息$ kubectl get deployment my-dep 列出指定 deployment

$ kubectl get pods --include-uninitialized 列出该 namespace 中的所有 pod 包括未初始化的使用详细输出来描述命令$ kubectl describe nodes my-node

$ kubectl describe pods my-pod

$ kubectl get services --sort-by=.metadata.name List Services Sorted by Name

根据重启次数排序列出 pod

$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

获取所有具有 app=cassandra 的 pod 中的 version 标签$ kubectl get pods --selector=app=cassandra rc -o \

jsonpath='{.items[*].metadata.labels.version}'

获取所有节点的 ExternalIP

$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?

北京凝思软件股份有限公司 第 28 页

Page 31: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

(@.type=="ExternalIP")].address}'

列出属于某个 PC 的 Pod 的名字“jq”命令用于转换复杂的 jsonpath,参考 https://stedolan.github.io/jq/

$ sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[]| "\(.key)=\(.value),"')%?}

$ echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})

查看哪些节点已就绪$ JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \

&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

列出当前 Pod 中使用的 Secret

$ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

9.3 更新资源$ kubectl rolling-update frontend-v1 -f frontend-v2.json 滚动更新 pod frontend-v1

$ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 更新资源名称并更新镜像$ kubectl rolling-update frontend --image=image:v2 更新 frontend pod

中的镜像$ kubectl rolling-update frontend-v1 frontend-v2 --rollback 退出已存在的进行中的滚动更新$ cat pod.json | kubectl replace -f - 基于 stdin 输入的 JSON

替换 pod

强制替换,删除后重新创建资源。会导致服务中断。$ kubectl replace --force -f ./pod.json

为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口$ kubectl expose rc nginx --port=80 --target-port=8000

更新单容器 pod 的镜像版本(tag)到 v4

$ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl

北京凝思软件股份有限公司 第 29 页

Page 32: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

replace -f -

$ kubectl label pods my-pod new-label=awesome 添加标签$ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq 添加注解$ kubectl autoscale deployment foo --min=2 --max=10 自动扩展 deployment “foo”

9.4 修补资源$ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' 部分更新节点更新容器镜像; spec.containers[*].name 是必须的,因为这是合并的关键字$ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'

使用具有位置数组的 json 补丁更新容器镜像$ kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

使用具有位置数组的 json 补丁禁用 deployment 的 livenessProbe

$ kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'

9.5 编辑资源$ kubectl edit svc/docker-registry 编辑名为 docker-registry 的 service

$ KUBE_EDITOR="nano" kubectl edit svc/docker-registry 使用其它编辑器9.6 缩放资源$ kubectl scale --replicas=3 rs/foo Scale a replicaset named'foo' to 3

$ kubectl scale --replicas=3 -f foo.yaml Scale a resource specified in "foo.yaml" to 3

$ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql If the deployment named mysql's current size is 2, scale mysql to 3

$ kubectl scale --replicas=5 rc/foo rc/bar rc/baz Scale multiple replication controllers

北京凝思软件股份有限公司 第 30 页

Page 33: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

9.7 删除资源$ kubectl delete -f ./pod.json 删除 pod.json 文件中定义的类型和名称的 pod

$ kubectl delete pod,service baz foo 删除名为“baz”的

pod 和名为“foo”的 service

$ kubectl delete pods,services -l name=myLabel 删除具有

name=myLabel 标签的 pod 和 serivce

$ kubectl delete pods,services -l name=myLabel --include-uninitialized 删除具有 name=myLabel 标签的 pod 和 service,包括尚未初始化的$ kubectl -n my-ns delete po,svc --all 删除 my-ns

namespace 下的所有 pod 和 serivce,包括尚未初始化的9.8 与运行中的 Pod交互$ kubectl logs my-pod dump 输出 pod 的日志(stdout)$ kubectl logs my-pod -c my-container dump 输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)$ kubectl logs -f my-pod 流式输出 pod 的日志(stdout)$ kubectl logs -f my-pod -c my-container 流式输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)$ kubectl run -i --tty busybox --image=busybox -- sh 交互式 shell 的方式运行 pod

$ kubectl attach my-pod -i 连接到运行中的容器$ kubectl port-forward my-pod 5000:6000 转发 pod 中的 6000 端口到本地的 5000 端口$ kubectl exec my-pod -- ls / 在已存在的容器中执行命令(只有一个容器的情况下)$ kubectl exec my-pod -c my-container -- ls / 在已存在的容器中执行命令(pod 中有多个容器的情况下)$ kubectl top pod POD_NAME --containers 显示指定 pod 和容器的指标度量

北京凝思软件股份有限公司 第 31 页

Page 34: 凝思容器系统使用手册 - linx-info.com‡思容器系统(适用于凝思... · 编号文件:--cidfile string Write the container ID to the file 添加主机:--device

凝思容器系统使用手册-V1.0.0

9.9 与节点和集群交互$ kubectl cordon my-node 标记 my-node 不可调度$ kubectl drain my-node 清空 my-node 以待维护$ kubectl uncordon my-node 标记 my-node 可调度$ kubectl top node my-node 显示 my-node 的指标度量$ kubectl cluster-info 显示 master 和服务的地址$ kubectl cluster-info dump 将当前集群状态输出到 stdout

$ kubectl cluster-info dump --output-directory=/path/to/cluster-state 将当前集群状态输出到 /path/to/cluster-state

如果该键和影响的污点(taint)已存在,则使用指定的值替换$ kubectl taint nodes foo dedicated=special-user:NoSchedule

插图

北京凝思软件股份有限公司 第 32 页