Figure 5-2. Using a Kubernetes cluster
The calling code can find the IP for a service in two ways: environment variables and
DNS. The environment variables created for services are similar to Docker link vari‐
ables. For example, suppose you have a service called
redis
that
exposes port
6379
:
REDIS_MASTER_SERVICE_HOST=10.0.0.11
REDIS_MASTER_SERVICE_PORT=6379
REDIS_MASTER_PORT=tcp://10.0.0.11:6379
REDIS_MASTER_PORT_6379_TCP=tcp://10.0.0.11:6379
REDIS_MASTER_PORT_6379_TCP_PROTO=tcp
REDIS_MASTER_PORT_6379_TCP_PORT=6379
REDIS_MASTER_PORT_6379_TCP_ADDR=10.0.0.11
Much preferred, however, is to use DNS to find your services. When DNS support is
configured on a Kubernetes cluster, each service will also be given a resolvable name.
In this example, assuming
the default namespace of
default
and a DNS domain root
of
cluster.local
, the service will be exposed as
redis.default.cluster.local
.
However, when working within a single namespace, users
can simply use the name of
the service:
redis
.
See Also
•
WordPress example in the Kubernetes documentation
• Documentation on Kubernetes
Services
150 | Chapter 5: Kubernetes
5.9 Creating a Single-Node Kubernetes Cluster Using
Docker Compose
Problem
You know how to create a Kubernetes cluster by running the various cluster compo‐
nents (e.g., API server, scheduler, kubelet) as
systemd
units. But why not take advan‐
tage of Docker itself to run these components? It would simplify deployment of the
cluster. To
test this deployment scenario, you want to run a one-node Kubernetes
cluster locally using only Docker containers.
Solution
The Kubernetes documentation has a good
resource
about this scenario. This recipe
goes one step further and takes advantage of Docker Compose (see
Recipe 7.1
). To get
started, you will need a Docker host and Docker Compose installed.
You can clone
the repository that comes with this book and use the Vagrantfile provided:
$ git clone https://github.com/how2dock/docbook.git
$ cd docbook/ch05/docker
$ tree
.
├── Vagrantfile
├── k8s.yml
└── kubectl
The Vagrantfile contains a small bootstrap script that will install Docker in the virtual
machine as well as Docker Compose. The
k8s.yml
is the
docker-compose
definition
to start all the components of Kubernetes as containers.
Bring up the machine and
run
docker-compose
; all the required images will be downloaded and the containers
will start:
$ vagrant up
$ vagrant ssh
$ cd /vagrant
$ docker-compose -f k8s.yml up -d
$
docker ps
CONTAINER ID IMAGE COMMAND
64e0073615c5 gcr.io/google_containers/... "/hyperkube controll ...
9603f3b5b186 gcr.io/google_containers/... "/hyperkube schedule ...
3ce44e77989f gcr.io/google_containers/... "/hyperkube apiserve ...
1b0bcbb56d59 kubernetes/pause:go "/pause" ...
0b0c3e2735a9 kubernetes/etcd:2.0.5.1 "/usr/local/bin/etcd ...
459c45ef9389 gcr.io/google_containers/... "/hyperkube proxy -- ...
005c5ac1de0e gcr.io/google_containers/... "/hyperkube kubelet ...
Do'stlaringiz bilan baham: