代替docker_docker为什么要用k8s

代替docker_docker为什么要用k8s今天,来说一个在service中非常高级的知识点,configs. 然后,通过一些示例,来一步一步的演示,如何在service中使用,有什么关键的注意事项。 什么是configs? configs的准确说法应该是,docker swar service configs. 通过configs这个名字可

今天,来说一个在service中非常高级的知识点,configs.

 

然后,通过一些示例,来一步一步的演示,如何在service中使用,有什么关键的注意事项。

什么是configs?

 

configs的准确说法应该是,docker swar service configs.

 

通过configs这个名字可以看出,一般是存配置数据的。是的,没错,在configs中可以存储非敏感的信息,比如配置文件。

 

这个配置文件可以是独立于镜像和容器之外的。

 

也就是说,在容器运行的时候,将配置挂载到容器中。这样,同样的镜像,就可以在不同的环境中运行,仅仅是更新下不同的配置文件就可以了。

 

同时呢,configs以配置文件挂载容器中,容器不需要以bind挂载的方式,将宿主机中的配置挂载到容器了。

 

简单来说,configs的好处

 

  • configs以文件挂载到容器,容器无需bind挂载
  • 镜像通用即可,不受环境限制
  • configs是独立镜像之外的独立资源对象

 

注意:docker configs只适用于集群service,不适用于独立的容器。

 

示例

 

创建configs

 

创建配置文件 index.html.tmpl

<html lang="en">
  <head><title>Hello Docker</title></head>
  <body>
    <p>Hello Docker! You have deployed a HTML page.</p>
  </body>
</html>

 

 

创建configs

docker config create homepage index.html.tmpl

 

 

查看创建configs

[root@nccztsjb-node-01 ~]# docker config ls
ID                          NAME       CREATED         UPDATED
pj31j03jkm31l6azeen39ibgv   homepage   3 seconds ago   3 seconds ago
[root@nccztsjb-node-01 ~]# 

 

# 查看详细的config信息
# 发现数据都是加密的
[root@nccztsjb-node-01 ~]# docker config inspect homepage
[
    {
        "ID": "pj31j03jkm31l6azeen39ibgv",
        "Version": {
            "Index": 3333
        },
        "CreatedAt": "2022-09-28T02:49:35.559023328Z",
        "UpdatedAt": "2022-09-28T02:49:35.559023328Z",
        "Spec": {
            "Name": "homepage",
            "Labels": {},
            "Data": "PGh0bWwgbGFuZz0iZW4iPgogIDxoZWFkPjx0aXRsZT5IZWxsbyBEb2NrZXI8L3RpdGxlPjwvaGVhZD4KICA8Ym9keT4KICAgIDxwPkhlbGxvIERvY2tlciEgWW91IGhhdmUgZGVwbG95ZWQgYSBIVE1MIHBhZ2UuPC9wPgogIDwvYm9keT4KPC9odG1sPgo="
        }
    }
]
[root@nccztsjb-node-01 ~]# 

 

 

使用config

 

有了config之后,接下来就是要将这个config挂载到容器的文件系统中

 

创建service使用这个config,使用–config参数

 

source : configs的名字

target : 挂载到容器中的位置及名字,注意:名字可以是自定义的

docker service create \
  --with-registry-auth \
  --name=nginx \
  --replicas=4 \
  --config source=homepage,target=/usr/share/nginx/html/index.html \
  --publish published=8080,target=80 \
  172.20.58.152/middleware/nginx:1.21.4

 

 

查看service状态

[root@nccztsjb-node-01 ~]# docker service ps nginx
ID             NAME      IMAGE                                   NODE               DESIRED STATE   CURRENT STATE            ERROR     PORTS
tkntzl1iqppt   nginx.1   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-05   Running         Running 14 seconds ago             
t40w6uia7u18   nginx.2   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-04   Running         Running 14 seconds ago             
psk6hhwuktvy   nginx.3   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-02   Running         Running 14 seconds ago             
hlvggbg4ym24   nginx.4   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-01   Running         Running 14 seconds ago             
[root@nccztsjb-node-01 ~]# 

 

 

查看容器中的配置文件

 

文件以及经挂载到/usr/share/nginx/html/index.html (把镜像中的文件给覆盖掉了)

[root@nccztsjb-node-01 ~]# docker ps
CONTAINER ID   IMAGE                                   COMMAND                  CREATED              STATUS              PORTS     NAMES
48deef4bbd6f   172.20.58.152/middleware/nginx:1.21.4   "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    nginx.4.hlvggbg4ym241lewhtk8q2h40
[root@nccztsjb-node-01 ~]# docker exec -it 48deef4bbd6f bash
root@48deef4bbd6f:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          49G   20G   27G  43% /
tmpfs            64M     0   64M   0% /dev
tmpfs           7.6G     0  7.6G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda2        49G   20G   27G  43% /etc/hosts
tmpfs           7.6G  4.0K  7.6G   1% /usr/share/nginx/html/index.html
tmpfs           7.6G     0  7.6G   0% /proc/acpi
tmpfs           7.6G     0  7.6G   0% /proc/scsi
tmpfs           7.6G     0  7.6G   0% /sys/firmware
root@48deef4bbd6f:/# cat /usr/share/nginx/html/index.html
<html lang="en">
  <head><title>Hello Docker</title></head>
  <body>
    <p>Hello Docker! You have deployed a HTML page.</p>
  </body>
</html>
root@48deef4bbd6f:/# 

 

 

浏览器访问

代替docker_docker为什么要用k8s

 

管理config

 

删除config

 

通过下面的命令删除config

[root@nccztsjb-node-01 ~]# docker config rm homepage
Error response from daemon: rpc error: code = InvalidArgument desc = config 'homepage' is in use by the following service: nginx
[root@nccztsjb-node-01 ~]# 

 

 

如果这个config正在被service使用是不能进行删除的

 

将config从service删除后,进行config的删除

[root@nccztsjb-node-01 ~]# docker config rm homepage
homepage
[root@nccztsjb-node-01 ~]# 

 

 

将config从service移除

 

[root@nccztsjb-node-01 ~]# docker service update --config-rm homepage nginx
nginx
overall progress: 4 out of 4 tasks 
1/4: running   [==================================================>] 
2/4: running   [==================================================>] 
3/4: running   [==================================================>] 
4/4: running   [==================================================>] 
verify: Service converged 
[root@nccztsjb-node-01 ~]# docker service ps nginx
ID             NAME          IMAGE                                   NODE               DESIRED STATE   CURRENT STATE             ERROR     PORTS
98yv6zykhi0n   nginx.1       172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-05   Running         Running 26 seconds ago              
tkntzl1iqppt    \_ nginx.1   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-05   Shutdown        Shutdown 27 seconds ago             
shk5gyh2fgu0   nginx.2       172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-04   Running         Running 11 seconds ago              
t40w6uia7u18    \_ nginx.2   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-04   Shutdown        Shutdown 13 seconds ago             
5r5uxqzc7zzy   nginx.3       172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-02   Running         Running 16 seconds ago              
psk6hhwuktvy    \_ nginx.3   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-02   Shutdown        Shutdown 17 seconds ago             
ttzbp0q8z9ty   nginx.4       172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-01   Running         Running 21 seconds ago              
hlvggbg4ym24    \_ nginx.4   172.20.58.152/middleware/nginx:1.21.4   nccztsjb-node-01   Shutdown        Shutdown 23 seconds ago             
[root@nccztsjb-node-01 ~]# 

 

 

移除就相当于是从新发布了service

 

现有service中增加config

 docker service update --config-add source=homepage,target=/usr/share/nginx/html/index.html nginx

 

 
[root@nccztsjb-node-01 ~]# docker service update --config-add source=homepage,target=/usr/share/nginx/html/index.html nginx

nginx

overall progress: 4 out of 4 tasks

1/4: running [==================================================>]

2/4: running [==================================================>]

3/4: running [==================================================>]

4/4: running [==================================================>]

verify: Service converged

[root@nccztsjb-node-01 ~]#

 

 

多个service共享一个config

 

再创建一个service用这个config

docker service create \
  --with-registry-auth \
  --name=nginx-new \
  --replicas=4 \
  --config source=homepage,target=/usr/share/nginx/html/index.html \
  --publish published=8082,target=80 \
  172.20.58.152/middleware/nginx:1.21.4  

 

 

[root@nccztsjb-node-01 ~]# docker service inspect nginx-new --pretty

ID:        7ilcep8nbxuujbfyaft9gbp4e
Name:        nginx-new
Service Mode:    Replicated
 Replicas:    4
Placement:
UpdateConfig:
 Parallelism:    1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:    1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:        172.20.58.152/middleware/nginx:1.21.4@sha256:2f14a471f2c2819a3faf88b72f56a0372ff5af4cb42ec45aab00c03ca5c9989f
 Init:        false
Configs:
 Target:    /usr/share/nginx/html/index.html
  Source:    homepage
Resources:
Endpoint Mode:    vip
Ports:
 PublishedPort = 8082
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress 

[root@nccztsjb-node-01 ~]# 

 

 

代替docker_docker为什么要用k8s

 

那么,这个service也使用了这个config了。

 

–config未加target参数默认的挂载位置

 

如果指定target,默认的位置/config_name

代替docker_docker为什么要用k8s

 

使用模板config

 

在config中可以使用go 模板,放置一些占位符,然后在创建service,启动容器的时候会自动的获取对应的变量值

 

使用下面的文件来创建config

<html lang="en">
  <head><title>Hello Docker</title></head>
  <body>
    <p>Hello {{ env "HELLO" }}! I'm on node {{ env "NODENAME" }}.</p>
  </body>
</html>

 

 

其中下面2个,以变量获取的方式,获取对应的值

{{ env "HELLO" }}

{{ env "NODENAME" }}

 

创建config,使用模板驱动–template-driver golang

docker config create --template-driver golang homepage index.html.tmpl

 

 

变量在哪里呢?

 

创建service的时候,通过–env来设置

 

  --env HELLO="Docker" 
  --env NODENAME={{.Node.Hostname}}

 

docker service create \
  --with-registry-auth \
  --name=nginx \
  --replicas=4 \
  --env HELLO="Docker" \
  --env NODENAME={{.Node.Hostname}} \
  --config source=homepage,target=/usr/share/nginx/html/index.html \
  --publish published=8080,target=80 \
  172.20.58.152/middleware/nginx:1.21.4  

 

 

[root@nccztsjb-node-01 ~]# docker service create \

> --with-registry-auth \

> --name=nginx \

> --replicas=4 \

> --env HELLO="Docker" \

> --env NODENAME={{.Node.Hostname}} \

> --config source=homepage,target=/usr/share/nginx/html/index.html \

> --publish published=8080,target=80 \

> 172.20.58.152/middleware/nginx:1.21.4

sywtvyc8ipg6zxxfegzlfmy1i

overall progress: 4 out of 4 tasks

1/4: running [==================================================>]

2/4: running [==================================================>]

3/4: running [==================================================>]

4/4: running [==================================================>]

verify: Service converged

[root@nccztsjb-node-01 ~]#

 

 

查看容器中的挂载

 

文件已经在挂载到容器的时候,将变量给替换了

[root@nccztsjb-node-01 ~]# docker ps
CONTAINER ID   IMAGE                                   COMMAND                  CREATED          STATUS          PORTS     NAMES
1c3358559e56   172.20.58.152/middleware/nginx:1.21.4   "/docker-entrypoint.…"   19 seconds ago   Up 18 seconds   80/tcp    nginx.3.txrhegcjr0dskys9h19eydgax
[root@nccztsjb-node-01 ~]# docker exec -it 1c3358559e56 bash
root@1c3358559e56:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          49G   20G   27G  43% /
tmpfs            64M     0   64M   0% /dev
tmpfs           7.6G     0  7.6G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vda2        49G   20G   27G  43% /etc/hosts
tmpfs           7.6G  4.0K  7.6G   1% /usr/share/nginx/html/index.html
tmpfs           7.6G     0  7.6G   0% /proc/acpi
tmpfs           7.6G     0  7.6G   0% /proc/scsi
tmpfs           7.6G     0  7.6G   0% /sys/firmware
root@1c3358559e56:/# cat /usr/share/nginx/html/index.html
<html lang="en">
  <head><title>Hello Docker</title></head>
  <body>
    <p>Hello Docker! I'm on node nccztsjb-node-01.</p>
  </body>
</html>
root@1c3358559e56:/# 

 

 

浏览器访问nginx服务

代替docker_docker为什么要用k8s

 

清浏览器缓存之后,再访问

 

代替docker_docker为什么要用k8s

 

代替docker_docker为什么要用k8s

 

多个实例,是可以通过路由网格,也就是ingress网络,访问到不同的节点上的task的。

 

注意:是同一个入口!

 

 

这样,就实现了将一些信息,动态的加载容器里面!

 

OK,关于configs就介绍到这里~

今天的文章代替docker_docker为什么要用k8s分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/45830.html

(0)
编程小号编程小号
上一篇 2023-09-06 14:30
下一篇 2023-09-06 15:06

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注