KubernetesUpAndRunning-Containers

来源:互联网 发布:ping值测试软件 编辑:程序博客网 时间:2024/05/16 18:49

Container images are typically combined with a container configuration file, which provides instructions on how to setup the container environment and execute an application entrypoint.

System Containers

System containers seek to mimic virtual machines and often run a full boot process.

Application Containers

Application containers differ from system containers in that they commonly run a single application.

Building Application Images with Docker

A Dockerfile can be used to automate the creation of a Docker container image.

FROM scratch # The scratch base image does not take any space.MAINTAINER Kelsey Hightower <kelsey.hightower@kuar.io>COPY influxd /influxdENTRYPOINT ["/influxd"]

Run the following command to create the influxdb Docker image:

$ docker build -t influxdb:0.9.4.2 .

Storing Images in a Remote Registry

First I authenticated to the Google Container Registry using gcloud:

$ gcloud docker --authorize-only

Next I tagged the influxdb image by prepending the target Docker registry:

$ docker tag influxdb:0.9.4.2 b.gcr.io/kuar/influxdb:0.9.4.2

Then I pushed the influxdb image:

$ docker push b.gcr.io/kuar/influxdb:0.9.4.2

To make all images in this bucket public I ran the following command:

gsutil -m acl ch -R -u AllUsers:R gs://kuar

Running Containers with Docker

To deploy a container from the b.gcr.io/kuar/influxdb:0.9.4.2 image run the following command:

$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  b.gcr.io/kuar/influxdb:0.9.4.2

The above command starts the influxdb database and maps ports 38083 to 8083 and 38086 to 8086. This will allow us to access influxdb on the host IP address. The ability to map high level ports to containers helps avoid port conflicts. Each container gets its own IP address assigned by Docker.

1

Exporing the influxdb API

$ curl -G 'http://localhost:38086/query' \  --data-urlencode "q=CREATE DATABASE kubernetes"

Output:

{"results":[{}]}

Now lets add some test data to the kubernetes database:

$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node0 count=10'$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node1 count=9'$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node2 count=12'

Run the following command to get the sum of all pod counts stored in the kuberentes test database:

$ curl -G http://localhost:38086/query?pretty=true \  --data-urlencode "db=kubernetes" \  --data-urlencode "q=SELECT SUM(count) FROM pod"

Output:

{    "results": [        {            "series": [                {                    "name": "pod",                    "columns": [                        "time",                        "sum"                    ],                    "values": [                        [                            "1970-01-01T00:00:00Z",                            31                        ]                    ]                }            ]        }    ]}

Limiting Resource Usage

LIMITING MEMORY RESOURCES

Stop and remove the current influxdb container:

$ docker stop influxdb$ docker rm influxdb

Then start another influxdb container using the appropriate flags to limit memory usage:

$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  --memory 200m \  --memory-swap 1G \  b.gcr.io/kuar/influxdb:0.9.4.2

LIMITING CPU RESOURCES

$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  --memory 200m \  --memory-swap 1G \  --cpu-shares 1024 \  b.gcr.io/kuar/influxdb:0.9.4.2

Persisting Data with Volumes

Stop and remove the current influxdb container:

$ docker stop influxdb$ docker rm influxdb

Start a new instance of the influxdb database container:

$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  --memory 200m \  --memory-swap 1G \  --cpu-shares 1024 \  b.gcr.io/kuar/influxdb:0.9.4.2

Run the query to sum the pod count in the kubernetes test database again:

$ curl -G http://localhost:38086/query?pretty=true \  --data-urlencode "db=kubernetes" \  --data-urlencode "q=SELECT SUM(count) FROM pod"

Output:

{    "results": [        {            "error": "database not found: kubernetes"        }    ]}

Let’s create a directory on our container host to house that volume.

$ mkdir /var/lib/influxdb/$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  --memory 200m \  --memory-swap 1G \  --cpu-shares 1024 \  --volume /var/lib/influxdb:/.influxdb \  b.gcr.io/kuar/influxdb:0.9.4.2

Create the kubernetes database:

$ curl -G 'http://localhost:38086/query' \  --data-urlencode "q=CREATE DATABASE kubernetes"

Add some data:

$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node0 count=10'$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node1 count=9'$ curl -X POST 'http://localhost:38086/write?db=kubernetes' \  -d 'pod,host=node2 count=12'

Run the pod sum query:

$ curl -G http://localhost:38086/query?pretty=true \  --data-urlencode "db=kubernetes" \  --data-urlencode "q=SELECT SUM(count) FROM pod"

Output:

{    "results": [        {            "series": [                {                    "name": "pod",                    "columns": [                        "time",                        "sum"                    ],                    "values": [                        [                            "1970-01-01T00:00:00Z",                            31                        ]                    ]                }            ]        }    ]}

First stop and remove the current influxdb container:

$ docker stop influxdb$ docker rm influxdb

Create the influxdb container again attaching the same host volume:

$ docker run -d --name influxdb \  --publish 38083:8083 \  --publish 38086:8086 \  --memory 200m \  --memory-swap 1G \  --cpu-shares 1024 \  --volume /var/lib/influxdb:/.influxdb \  b.gcr.io/kuar/influxdb:0.9.4.2

Run the pod sum query again:

$ curl -G http://localhost:38086/query?pretty=true \  --data-urlencode "db=kubernetes" \  --data-urlencode "q=SELECT SUM(count) FROM pod"

Output:

{    "results": [        {            "series": [                {                    "name": "pod",                    "columns": [                        "time",                        "sum"                    ],                    "values": [                        [                            "1970-01-01T00:00:00Z",                            31                        ]                    ]                }            ]        }    ]}
0 0
原创粉丝点击