尊龙凯时人生就是博

LinuxºÍDocker£ºÔõÑùʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí£¿

linuxºÍdocker: ÔõÑùʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí£¿

ͻ񻣼

Ëæ×ÅÈÝÆ÷ÊÖÒÕµÄѸËÙÉú³¤ºÍÆÕ±éÓ¦Óà £¬ÔõÑù¸üºÃµØʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí³ÉΪÁËÒ»¸öÖ÷ÒªµÄ»°Ìâ¡£±¾ÎĽ«ÖصãÏÈÈÝLinuxºÍDockerÖеÄһЩ³£ÓûúÖƺÍÒªÁì £¬ÒÔ¼°Ê¾Àý´úÂë £¬×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí¡£

СÐò£º

ÈÝÆ÷ÊÖÒÕµÄÐËÆðΪӦÓóÌÐòµÄ°²ÅźÍÔËÐдøÀ´Á˸ïÃüÐÔµÄת±ä¡£¹Å°åµÄÐéÄ⻯ÊÖÒÕÐèÒªÐéÄâ»úÖÎÀí³ÌÐò £¬¶øÈÝÆ÷ÊÖÒÕÔò¿ÉÒÔÖ±½ÓÔÚËÞÖ÷»úÉÏÔËÐÐÓ¦ÓóÌÐò £¬²»ÐèÒªÌØÁíÍâÖÎÀí³ÌÐò¡£

ÈÝÆ÷ÊÖÒյĽ¹µãÊÇÈÝÆ÷ÒýÇæ £¬ÆäÖÐ×îÖøÃûµÄ¾ÍÊÇDocker¡£DockerÌṩÁËÒ»ÖÖÇáÁ¿¼¶ÇÒÒ×ÓÚʹÓõÄÈÝÆ÷½â¾ö¼Æ»® £¬³ÉΪÁËÈÝÆ÷ÊÖÒյĴú±í¡£±¾ÎĽ«ÒÔDockerΪÀý £¬ÍŽáLinuxϵͳµÄ»úÖÆ £¬ÏÈÈÝÔõÑùʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí¡£

Ò»¡¢LinuxÖеÄÈÝÆ÷ÊÖÒÕ

ÔÚLinuxϵͳÖÐ £¬ÈÝÆ÷ÊÖÒÕÖ÷ÒªÒÀÀµÓÚÁ½¸öÖ÷ÒªµÄ¹¦Ð§ £¬¼´cgroupºÍnamespace¡£cgroup£¨control groups£©¿ÉÒÔʵÏÖ¶Ôϵͳ×ÊÔ´µÄÏÞÖƺÍÖÎÀí £¬¶ønamespace¿ÉÒÔʵÏÖ¶ÔÀú³ÌºÍÎļþϵͳµÄ¸ôÀë¡£

cgroup

cgroupÊÇÒ»ÖÖ¹¦Ð§Ç¿Ê¢µÄ×ÊÔ´ÉèÖúÍÏÞÖÆ»úÖÆ £¬¿ÉÒÔΪ²î±ðµÄÀú³Ì×éÉèÖÃ×ÊÔ´ÏÞÖÆ¡£Í¨¹ýʹÓÃcgroup £¬ÎÒÃÇ¿ÉÒÔΪÈÝÆ÷ÖеÄÀú³ÌºÍÍⲿÀú³ÌÖ¸¶¨²î±ðµÄ×ÊÔ´ÏÞÖÆ £¬ÈçCPUÅä¶î¡¢ÄÚ´æÅä¶îµÈ¡£ÔÚLinuxϵͳÖÐ £¬cgroup±»ÆÕ±éÓ¦ÓÃÓÚʵÏÖÈÝÆ÷µÄ×ÊÔ´ÖÎÀí¡£

ÏÂÃæÊÇÒ»¸öʹÓÃcgroupÏÞÖÆÈÝÆ÷ÄÚÀú³ÌCPUʹÓÃÂʵÄʾÀý´úÂ룺

#!/bin/bash

# ½¨Éècgroup
cgcreate -g cpu:/docker_container

# ÉèÖÃCPUÅä¶îΪ50%
cgset -r cpu.cfs_quota_us=50000 /docker_container

# Æô¶¯ÈÝÆ÷
docker run -d -it --name=my_container --cgroup-parent=/docker_container ubuntu /bin/bash

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓÃcgcreateÏÂÁÉèÁËÒ»¸öÃûΪdocker_containerµÄcgroup £¬²¢Ê¹ÓÃcgsetÉèÖÃÁ˸ÃcgroupµÄCPUÅä¶îΪ50%¡£È»ºóÎÒÃÇʹÓÃdockerÏÂÁîÆô¶¯ÁËÒ»¸öÃûΪmy_containerµÄÈÝÆ÷ £¬²¢½«Æä¹éÊôÓÚdocker_containerÕâ¸öcgroup¡£ÕâÑù £¬¸ÃÈÝÆ÷ÄÚµÄÀú³ÌµÄCPUʹÓÃÂʽ«±»ÏÞÖÆÔÚ50%ÒÔÄÚ¡£

namespace

namespace¿ÉÒÔΪÀú³ÌÌṩһ¸ö×ÔÁ¦µÄÔËÐÐÇéÐÎ £¬°üÀ¨ÎļþϵͳºÍÀú³Ì¿Õ¼ä¡£Í¨¹ýʹÓÃnamespace £¬ÎÒÃÇ¿ÉÒÔʵÏÖÈÝÆ÷ÓëËÞÖ÷»úÖ®¼äµÄ¸ôÀë¡£

ÏÂÃæÊÇÒ»¸öʹÓÃnamespace¸ôÀëÈÝÆ÷ÖÐÎļþϵͳµÄʾÀý´úÂ룺

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    // ½¨ÉèÒ»¸öеÄnamespace
    int ret = unshare(CLONE_NEWNS);
    if (ret) {
        perror("unshare");
        exit(EXIT_FAILURE);
    }

    // ÔÚеÄnamespaceÖйÒÔØÒ»¸öÎļþϵͳ
    ret = mount("rootfs", "/mnt", "ext4", MS_MGC_VAL, NULL);
    if (ret) {
        perror("mount");
        exit(EXIT_FAILURE);
    }

    // Ö´ÐÐÈÝÆ÷ÐèÒªµÄÏÂÁî
    system("/bin/bash");

    return 0;
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓÃunshareº¯Êý½¨ÉèÁËÒ»¸öеÄnamespace £¬²¢ÔÚÆäÖйÒÔØÁËÒ»¸örootfsÎļþϵͳ¡£½Ó×Å £¬ÎÒÃÇÖ´ÐÐÁËÒ»¸ö/bin/bashÏÂÁî £¬Õâ¸ö±»Ö´ÐеÄÏÂÁÔÚеÄnamespaceÖÐÔËÐÐ £¬´Ó¶øʵÏÖÁ˶ÔÎļþϵͳµÄ¸ôÀë¡£

¶þ¡¢DockerÖеÄÈÝÆ÷µ÷ÀíºÍ×ÊÔ´ÖÎÀí

DockerÔÚÌṩÈÝÆ÷µÄ»ù´¡ÉÏ £¬»¹ÌṩÁËһЩ¸ß¼¶¹¦Ð§ £¬ÈçÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí¡£ÕâЩ¹¦Ð§Ê¹µÃDocker³ÉΪһ¸öÇ¿Ê¢µÄÈÝÆ÷ÖÎÀíƽ̨¡£

ÈÝÆ÷µÄ¶¯Ì¬µ÷Àí

Dockerͨ¹ýʹÓõ÷ÀíÆ÷£¨scheduler£©ÊµÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷Àí¡£µ÷ÀíÆ÷¿ÉÒÔƾ֤²î±ðµÄµ÷ÀíÕ½ÂÔ×Ô¶¯½«ÈÝÆ÷·ÖÅɸøËÞÖ÷»ú¡£³£Óõĵ÷ÀíÕ½ÂÔÓУº

Ëæ»úµ÷Àí£ºÆ¾Ö¤Ëæ»úËã·¨½«ÈÝÆ÷·ÖÅɸøËÞÖ÷»ú£»

¸ºÔØƽºâµ÷Àí£ºÆ¾Ö¤ËÞÖ÷»úµÄ¸ºÔØÇéÐν«ÈÝÆ÷·ÖÅɸø×î¿ÕÏеÄËÞÖ÷»ú£»

Э×÷µ÷Àí£º

ÏÂÃæÊÇÒ»¸öʹÓÃDockerµÄµ÷ÀíÆ÷¾ÙÐÐÈÝÆ÷µ÷ÀíµÄʾÀý´úÂ룺

#!/bin/bash

# ʹÓøºÔØƽºâµ÷ÀíÆ÷
docker run -d -p 8080:80 --name=mynginx --scheduler=random nginx

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓÃÁ˸ºÔØƽºâµ÷ÀíÆ÷½«Ò»¸öÃûΪmynginxµÄÈÝÆ÷·ÖÅɸøÒ»¸öËÞÖ÷»ú¡£¸ÃÈÝÆ÷½«¼àÌýËÞÖ÷»úµÄ8080¶Ë¿Ú £¬²¢½«ÇëÇóת·¢µ½ÈÝÆ÷ÄÚµÄ80¶Ë¿Ú¡£

×ÊÔ´ÖÎÀí

DockerÌṩÁËһϵÁÐÏÂÁîºÍAPIÒÔʵÏÖ¶ÔÈÝÆ÷µÄ×ÊÔ´ÖÎÀí¡£ÎÒÃÇ¿ÉÒÔʹÓÃÕâЩ¹¦Ð§À´¼à¿ØºÍÖÎÀíÈÝÆ÷µÄ×ÊԴʹÓÃÇéÐΡ£

ÏÂÃæÊÇÒ»¸öʹÓÃDockerÏÂÁîÉó²éÈÝÆ÷×ÊԴʹÓÃÇéÐεÄʾÀý´úÂ룺

#!/bin/bash

# Éó²éÈÝÆ÷µÄCPUʹÓÃÇéÐÎ
docker stats --format "table {{.Container}}    {{.CPUPerc}}    {{.MemUsage}}" my_container

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖÐ £¬ÎÒÃÇʹÓÃdocker statsÏÂÁîÉó²éÁËÒ»¸öÃûΪmy_containerµÄÈÝÆ÷µÄCPUʹÓÃÂʺÍÄÚ´æʹÓÃÇéÐΡ£

½áÂÛ£º

±¾ÎÄÏÈÈÝÁËLinuxºÍDockerÖÐʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀíµÄһЩ³£ÓÃÒªÁìºÍʾÀý´úÂ롣ͨ¹ýʹÓÃcgroupʵÏÖ×ÊÔ´ÏÞÖƺÍʹÓÃnamespaceʵÏÖÇéÐθôÀë £¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØÖÎÀíºÍÔËάÈÝÆ÷¡£Docker½øÒ»²½ÌṩÁ˸߼¶¹¦Ð§ £¬È綯̬µ÷ÀíºÍ×ÊÔ´ÖÎÀí £¬×ÊÖúÎÒÃǸüºÃµØʹÓÃÈÝÆ÷ÊÖÒÕ¡£Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈÝ £¬¶ÁÕßÄܶÔÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀíÓиüÉîÈëµÄÃ÷È·¡£

ÒÔÉϾÍÊÇLinuxºÍDocker£ºÔõÑùʵÏÖÈÝÆ÷µÄ¶¯Ì¬µ÷ÀíºÍ×ÊÔ´ÖÎÀí£¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ