In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article shares with you the content of the sample analysis of run in docker. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Docker source code correlation
By abstracting the initialization operations of various commands in / components/cli/command/commands.go.
Use the third-party library "github.com/spf13/cobra"
Docker run initializes the parsing parameters of the command line terminal, and finally generates APIclient to issue a REQUEST request to docker daemon.
Initialize docker daemon, set the listening address of server, initialize routerSwapper, registryService and various storage such as layStore, imageStore, volumeStore, etc.
The command of docker run is parsed into two requests of docker container create and container start:
Container create does not involve the call of the underlying containerd. First, host.config, networkingConfig and AdjustCPUShares are assembled into a client request and sent to docker daemon to register the container. The request finishes pulling the image and initializing the RWlayer, config files of the baseContainer, etc., after which daemon can use the container through containerid.
The core of the container start command is the call to containerStart () of daemon, which completes the
Call containerd for create container, call the initialization of libcontainerd module clnt * client
Set the container file system, mount point: / var/lib/docker/overlay/ {container.RWLayer.mountID} / merged
Set the network mode of the container, call libnetwork, CNM model (sandbox, endpoint,network)
Create spec files such as / proc / dev to set container-specific properties
Call containerd for create container
Container.create1) get containers2 in the libcontainerd module) get gid and uid3) create a state directory and configure the file path. 4) create a containercommon object, create the container directory, and the configuration file path, and create the configuration file according to the spec. Container.start1) read the configuration file of the spec object 2) create a pipe3 of fifo) define the request object of the containerd, and grpc calls the containerd module. After ctr.client.remote.apiClient.CreateContainer (context.Background (), r) 4) starts successfully, update the container status. Daemon starts libcontainerd as the server of grpc.
There is a process for initializing libcontainerd in cmd/dockerd/daemon.go.
This includes starting the grpc server and listening to the socket.
Establish a connection conn with grpc server through grpc.dail, establish an apiclient object according to the link, and send a json request.
RunContainerdDaemon
Communicate with grpc server through docker-containerd binary
Docker-containerd- l unix:///var/run/docker/libcontainerd/docker-containerd.sock-metrics-interval=0-- start-timeout 2m-- state-dir / var/run/docker/libcontainerd/containerd-- shim docker-containerd-shim-- runtime docker-runc
The input and output stream of the execution result is redirected to docker daemon.
Runc saves the state information of the state.json file in the container runtime, which is stored in / run/runc/ {containerID} / state.json by default.
Containerd source code related type Supervisor struct {/ / stateDir is the directory on the system to store container runtime state information. StateDir string / / name of the OCI compatible runtime used to execute containers runtime string runtimeArgs [] string shim string containers map [string] * containerInfo startTasks chan * startTask / / this is the bridge from containerd to runc Consumed by func (w * worker) Start () / / we need a lock around the subscribers map only because additions and deletions from / / the map are via the API so we cannot really control the concurrency subscriberLock sync.RWMutex subscribers map [chan Event] struct {} machine Machine tasks chan Task / / all request from docker-daemon will be converted to event and stored here Consumption of monitor * Monitor eventLog [] Event eventLock sync.Mutex timeout time.Duration} type startTask struct {Container runtime.Container CheckpointPath string Stdin string Stdout string Stderr string Err chan error StartResponse chan StartResponse} by func (s * Supervisor) Start ()
We know that containerd, as the grpc server end of docker daemon, converts apiclient request into corresponding events, and transfers data in different subsystems distribution, bundles, and runtime, including image upload and download, image packaging and decompression, runtime creation and destruction, and so on.
The core components of containerd include supervisor and executor, and the data flow is as follows:
Docker-daemon--- > tasks chan Task-> func (s * Supervisor) Start () consumption-> Storage to startTasks chan * startTask-> func (w * worker) Start () initialization of consumption containerd
Docker-containerd initialization includes creating a new Supervisor object:
This object launches 10 worker, which handles the task of creating a new container (task).
Initialization of supervisor, including startTask chan initialization, monitor that starts monitoring container processes
A worker contains a supervisor and sync.waitgroup,wg for container startup.
The start of supervisor consumes tasks, assembles the container data in task into runtime.container, encapsulates it to type startTask struct, and sends it to startTask chan queue.
Start grpc server (startServer) to receive request requests from dockerd.
Func daemon (context * cli.Context) error {s: = make (chan os.Signal, 2048) signal.Notify (s, syscall.SIGTERM, syscall.SIGINT) / * create a new supervisor This is the core component of containerd = > / supervisor/supervisor.go = > func New * / sv, err: = supervisor.New (context.String ("state-dir"), context.String ("runtime"), context.String ("shim") Context.StringSlice ("runtime-args"), context.Duration ("start-timeout") Context.Int ("retain-count") if err! = nil {return err} wg: & sync.WaitGroup {} / * supervisor launch 10 worker = = > / supervisor/worker.go * / for I: = 0 I < 10; iTunes + {wg.Add (1) w: = supervisor.NewWorker (sv, wg) go w.Start ()} / / start supervisor if err: = sv.Start () Err! = nil {return err} / / Split the listen string of the form proto://addr / * the value of the listener listenSpec obtained according to the parameter is unix:///var/run/docker/libcontainerd/docker-containerd.sock * / listenSpec: = context.String ("listen") listenParts: = strings.SplitN (listenSpec, ": / /" 2) if len (listenParts)! = 2 {return fmt.Errorf ("bad listen address format% s, expected proto://address", listenSpec)} / * start grpc server * / server, err: = startServer (listenParts [0], listenParts [1], sv) if err! = nil {return err}
StartServer is responsible for starting grpc server, listening to docker-containerd.sock, and declaring to register routing handler.
When CreateContainer handler receives a Request, it converts it to type startTask struct and into a StartTask event that holds the request information that created the container.
The event is sent to the supervosior main loop through s.sv.SendTask (e).
/ / SendTask sends the provided event to the the supervisors main event loop/* SendTask sends evt Task to the supervisors main event loop all request from docker-daemon will be converted to event and stored here, producer * / func (s * Supervisor) SendTask (evt Task) {TasksCounter.Inc (1) / / number of tasks + 1 s.tasks
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.