Creating a Kubernetes Cluster to Run Docker Formatted Container Images
Request a new translation or translation update
A translation for your language preference does not exist. Click below to request a translation for your selected language.
Using Kubernetes, you can run and manage docker containers from one system (a Kubernetes master) and deploy them to run on other systems (Kubernetes nodes). This procedure will help you:
- Set up three Red Hat Enterprise Linux 7 or RHEL Atomic systems to use as a Kubernetes master and two Kubernetes nodes (previously called minions).
- Configure networking between containers using Flannel.
- Create data files (in yaml format) that define services, pods and replication controllers.
The systemd services you start to run a Kubernetes master include etcd, kube-apiserver, kube-controller-manager, and kube-scheduler. For a Kubernetes node, you need to run docker, kube-proxy and kubelet systemd services. To launch containers from yaml files and otherwise manage your Kubernetes environment, you run the kubectl command.
IMPORTANT: If you are starting from this procedure from scratch, the master and nodes in this cluster should be running RHEL Atomic host or RHEL 7 Server using at least the following versions:
- RHEL Atomic: Version 7.1.4 . (If you are not at this version, run atomic host upgrade and reboot your RHEL Atomic host.)
-
RHEL 7 Server: Your RHEL servers must be running at least these versions (if you are not, run yum upgrade kubernetes etcd docker flannel) :
- kubernetes-1.0
- etcd-2.0.13
- docker-1.7.1
- flannel-0.2.0-10
If you have used an earlier version of this article, refer to the Upgrading a Kubernetes Cluster article for information on upgrading that configuration to this latest software.
The result of this procedure is a Kubernetes environment that you can use as a foundation for deploying Docker containers across multiple host systems. Here are a few other documents you might find useful:
-
Get Started Orchestrating Docker Containers with Kubernetes: If you prefer a simpler, sandbox approach to trying out Kubernetes, refer to this article. This article sets up a Kubernetes master and node on a single system, to begin trying out Kubernetes.
-
Get Started with Docker Containers in RHEL 7 and RHEL Atomic: If you are new to Docker or RHEL Atomic, refer to this article for information on working with containers.
Table of Contents
- Preparing to Deploy Containers with Kubernetes
- Starting Kubernetes Services
- Setting up Flannel networking for Kubernetes
- Launching Services, Replication Controllers, and Container Pods with Kubernetes
- Checking Kubernetes
- Cleaning up Kubernetes
Preparing to Deploy Containers with Kubernetes
To set up a Kubernetes environment between three systems, do the following:
-
Install a RHEL 7.1 Server or RHEL Atomic system: For this Kubernetes environment, install three RHEL 7 or RHEL Atomic systems and subscribe the systems. Refer here for information on setting up a basic RHEL or RHEL Atomic system to use with Kubernetes:
Get Started with Docker Formatted Containers Images on Red Hat Systems
Run yum upgrade (on RHEL 7 systems) or atomic upgrade (on RHEL Atomic systems) to make sure you get the latest versions of the operating system software.
-
Configure Network Time Protocol: All systems in the cluster (master and nodes) need to have their time synced together. Refer to the RHEL 7 System Administrator's Guide for instruction on setting up Network Time Protocol daemon (ntpd) or Chrony Daemon (chronyd) on those systems.
-
Install Kubernetes: If you are on a RHEL 7 system, install the kubernetes-master and etcd packages on the master and just docker and kubernetes-node on nodes. On the nodes, start and enable the docker service. All Docker and Kubernetes software is already installed on RHEL Atomic:
- On master:
# yum install kubernetes-master etcd
- On nodes:
# yum install docker kubernetes-node # systemctl start docker # systemctl enable docker
-
Assign hostnames and addresses: You can use hostnames or IP addresses to identify the master and nodes to each other. For this example, I assigned the following hostnames for the three Kubernetes systems (add the hostnames and IP addresses to the etc/hosts file if you don't have a handy way of setting up addresses for these sysstems in DNS):
master.example.com
node1.example.com
node2.example.com -
Disable firewalld: If you are using a RHEL 7 host, be sure that the firewalld service is disabled (the firewalld service is not installed on an Atomic host). On RHEL 7, type the following to disable and stop the firewalld service:
# systemctl disable firewalld # systemctl stop firewalld
-
Get Docker Containers: Build the following two containers and make images of those containers available on both nodes (in other words, make sure you can see the two images when you type docker images on either node):
- Building a Simple Apache Web Server in a Docker Container
- Building a Simple Database Server in a Docker Container
After you build and test the containers, stop them (docker stop mydbforweb and docker stop mywebwithdb).
Starting Kubernetes Services
Different systemd services are required to run a Kubernetes master and node. Follow the steps below on your three host systems (one Kubernetes master and two Kubernetes nodes) to configure and start the systemd services needed for your Kubernetes cluster:
IMPORTANT: Be sure to configure and start the master before you start the nodes. If the nodes come up and the master is not yet up, the nodes may not be properly registered with Kubernetes.
-
Configure the Kubernetes Master: On the master, you need to configure the locations of the nodes, the etcd server, and API server. To do that, edit these configuration files on the master:
- /etc/etcd/etcd.conf: The etcd service needs to be configured to listen on all interfaces to ports 2380 (ETCD_LISTEN_PEER_URLS) and port 2379 (ETCD_LISTEN_CLIENT_URLS), and listen on 2380 on localhost (ETCD_LISTEN_PEER_URLS). The resulting uncommented lines in this file should appear as follows:
ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_LISTEN_PEER_URLS="http://localhost:2380" ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
- /etc/kubernetes/config: Change the KUBE_MASTER line to identify the location of your master server (it points to 127.0.0.1, by default). Leave other settings as they are. The changed line appears as follows:
KUBE_MASTER="--master=http://master.example.com:8080"
- /etc/kubernetes/apiserver: Add a new KUBE_ETCD_SERVERS line (as shown below), then review and change other lines in the apiserver configuration file. Change KUBE_API_ADDRESS to listen on all network addresses(0.0.0.0), instead of just localhost. Set an address range for the KUBE_SERVICE_ADDRESS that Kubernetes can use to assign to services (see a description of this address below). Finally, remove the term "ServiceAccount" from the KUBE_ADMISSION_CONTROL instruction (below is the new line, with the original line above it commented out). Here are examples:
KUBE_API_ADDRESS="--address=0.0.0.0" KUBE_ETCD_SERVERS="--etcd_servers=http://master.example.com:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota" KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
Here are a few things you should know about the KUBE_SERVICE_ADDRESSES address range you use:
- The address range is used by Kubernetes to assign to Kubernetes services.
- In the example just shown, the address range of 10.254.0.0/16 consumes a set of 10.254 subnets that can be assigned by Kubernetes as needed. For example, 10.254.1.X, 10.254.2.X and so on.
- Make sure this address range isn't used anywhere else in your environment.
- Each address range that is assigned is only used within a node and is not routable outside of that node.
- This address range must be different than the range used by flannel. (Flannel address ranges are assigned to pods.)
-
Configure the Kubernetes systemd services on master: You need to start several services associated with a Kubernetes master. From the master, run the following for loop to start and enable Kubernetes systemd services on the master:
# for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done -
Configure the Kubernetes Nodes: On each of the two Kubernetes nodes (node1.example.com and node2.example.com in this example), you need to edit the following configuration files:
- /etc/kubernetes/config: Edit the KUBE_MASTER line to identify the location of your master (change it from the default 127.0.0.1). Leave other settings as they are.
KUBE_MASTER="--master=http://master.example.com:8080"
- /etc/kubernetes/kubelet: In this file on each node, modify KUBELET_ADDRESS (0.0.0.0 to listen on network interfaces), KUBELET_HOSTNAME (comment it out or replace hostname_override with the hostname or IP address of the local system: node1.example.com or node2.example.com), and KUBELET_API_SERVER (set the location of the master), as shown below:
KUBELET_ADDRESS="--address=0.0.0.0" # KUBELET_HOSTNAME="--hostname_override=127.0.0.1" KUBELET_API_SERVER="--api_servers=http://master.example.com:8080" KUBELET_ARGS="--register-node=true"
- /etc/kubernetes/proxy: No settings are required in this file.
-
Start the Kubernetes nodes systemd services: On each node, you need to start several services associated with a Kubernetes node:
# for SERVICES in docker kube-proxy.service kubelet.service; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done -
Check the services: Run the netstat command on each of the three systems to check which ports the services are running on. The etcd service should only
- On master:
# netstat -tulnp | grep -E "(kube)|(etcd)"
- On nodes:
# netstat -tulnp | grep kube
NOTE: Although the firewalld service is not installed in Atomic (and should be disabled on RHEL) for this procedure, it is possible that you might be using iptables rules directly to block access to ports on your system. If you have done that, the netstat command just shown displays the ports you need to have accessible on each system for the Kubernetes cluster to work. So you can create individual firewall rules to open access to those ports.
-
Test the etcd service: Use the curl command from any of the three systems to check that the etcd service is running and accessible:
# curl -s -L http://master.example.com:2379/version etcd 2.0.11
-
Check the nodes: From the master, type the following command to make sure the master is communicating with the two nodes:
# kubectl get nodes NAME LABELS STATUS node1.example.com kubernetes.io/hostname=node1.example.com Ready node2.example.com kubernetes.io/hostname=node2.example.com Ready
At this point, you have a configured Kubernetes cluster. Next, you can begin using this setup to deploy containers by configuring networking, then deploying Kubernetes services, replication controllers, and pods.
Setting up Flannel networking for Kubernetes
The flannel package contains features that allow you to configure networking between the master and nodes in your Kubernetes cluster. You configure the flanneld service by creating and uploading a json configuration file with your network configuration to your etcd server (on the master). You then configure the flanneld systemd service on the master and each node to point to that etcd server and start the flanneld service. This procedure describes how to do that.
For this example, flannel assigns an address range of 10.20.0.0/16 to be used by all nodes in the Kubernetes environment. This allow separate /24 subnets within that range to be assigned to network interfaces on the master and both nodes. For the three systems in this example, the steps below resulted in the following address ranges being assigned to flannel.1 and docker0 interfaces (yours may differ):
- master:
- flannel.1: 10.20.67.0/16
- node1:
- flannel.1: 10.20.92.0/16
- docker0: 10.20.92.1/24
- node2:
- flannel.1: 10.20.13.0/16
- docker0: 10.20.13.1/24
IMPORTANT: Because the docker0 interface is probably already in place when you run this procedure, the IP address range assigned by flanneld to docker0 will not immediately take effect. To get the flanneld address ranges to take effect, you need to stop docker (systemctl stop docker), delete the docker0 interface, and restart the docker interface (systemctl start docker). When you get to that step, the procedure simply asks you to reboot.
-
Install flannel: The flannel package is preinstalled for RHEL Atomic. If you are using RHEL 7, however, you can install the flannel package on the master and each node as follows:
# yum install flannel
-
Create Flannel configuration file: On the master, identify a set of IP addresses and network type in a Flannel configuration file (in json format). For this example, we created a file named flannel-config.json that container the following content:
{ "Network": "10.20.0.0/16", "SubnetLen": 24, "Backend": { "Type": "vxlan", "VNI": 1 } } -
Upload Flannel configuration to etcd service: On the master, to upload the flannel configuration file to the etcd service, type the following:
# etcdctl set coreos.com/network/config flannel-config.json
Then check that the upload completed properly:
# etcdctl get coreos.com/network/config { "Network": "10.20.0.0/16", "SubnetLen": 24, "Backend": { "Type": "vxlan", "VNI": 1 } } -
Configure flanneld overlay network: When the flanneld systemd service starts up, it reads the /etc/sysconfig/flanneld file for options to pass to the flanneld daemon. On the master and both nodes, edit /etc/sysconfig/flanneld to insert the name or IP address of the system containing the etcd service (master). Leave the FLANNEL_ETCD_KEY line as it is:
FLANNEL_ETCD="http://master.example.com:2379" FLANNEL_ETCD_KEY="/coreos.com/network"
-
Start flanneld on master and nodes: First on the master, then on the two nodes, start and enable the flanneld service as follows:
# systemctl restart flanneld # systemctl enable flanneld # systemctl status flanneld
-
Check flannel.1 network interfaces: Run the following commands from any system to check that the flannel.1 network interface is configured properly on each system. The first command runs the ip a command on each node to check for the flannel.1 interface. The second command shows how the subnet is configured for flannel on each system.
# for i in 1 2; do ssh root@node$i ip a l flannel.1; done | grep 'inet ' root@node1's password: password inet 10.20.92.0/16 scope global flannel.1 root@node2's password: password inet 10.20.13.0/16 scope global flannel.1 # for i in master node1 node2; \ do echo --- $i ---; ssh root@$i cat /run/flannel/subnet.env; done root@master's password: password --- master --- FLANNEL_SUBNET=10.20.67.1/24 FLANNEL_MTU=1450 root@node1's password: password --- node1 --- FLANNEL_SUBNET=10.20.92.1/24 FLANNEL_MTU=1450 root@node2's password: password --- node2 --- FLANNEL_SUBNET=10.20.13.1/24 FLANNEL_MTU=1450 -
Reboot nodes: For the docker systemd service to pick up the flannel changes, and to make sure the network interfaces all come up properly, run the following on each node to reboot it:
# systemctl reboot
-
Start a container to get flannel addresses: On each node, check the address range of the flannel network, then run a container image to make sure it picks up addresses from that network:
# ip a | grep flannel 3: flannel.1: mtu 1450 qdisc noqueue state UNKNOWN inet 10.20.26.0/16 scope global flannel.1 # docker run -d --name=mydbforweb dbforweb # docker inspect --format='{{.NetworkSettings.IPAddress}}' mydbforweb 10.20.92.2 # docker stop mydbforweb # docker rm mydbforweb
Launching Services, Replication Controllers, and Container Pods with Kubernetes
With the Kubernetes cluster in place, you can now create the yaml files needed to set up Kubernetes services, define replication controllers and launch pods of containers. Using the two containers described earlier in this document (Web and DB), you will create the following types of Kubernetes objects:
- Services: Creating a Kubernetes service lets you assign a specific IP address and port number to a label and associate it with a container. Because pods and IP addresses can come and go with Kubernetes, that label can be used within a pod to find the location of the services it needs.
- Replication Controllers: By defining a replication controller, you can set not only which pods to start, but how many replicas of each pod should start. If a pod stops, the replication controller starts another to replace it.
- Pods: A pod loads one or more containers, along with options associated with running each container.
In this example, we create the service objects (yaml files) needed to allow the two pods to communicate with each other. Then we create replication controllers that identify the pods that launch and maintain the Web server and database server containers described earlier. For copies of all the yaml and json configuration files used in this article, download the following tarball:
-
Deploy Kubernetes Service for Database server: On the master, create a Kubernetes service yaml file that identifies a label to tie the database server container to a particular IP address and port. Here is an example using a file called db-service.yaml:
apiVersion: v1 kind: Service metadata: labels: name: db name: db-service namespace: default spec: ports: - port: 3306 selector: name: dbThis service in not directly accessed from the outside world. The webserver container will access the db service by adding a uses instruction to the webserver container's definition. Kubernetes will manage the mapping of the service name to its actual location. To start that service, type the following on the master:
# kubectl create -f db-service.yaml services/db-service
-
Deploy Kubernetes ReplicationController for Database server: Create a replication controller yaml file that identifies then number of database pods to have running (two in this case). In this example, the file is named db-rc.yaml:
kind: ReplicationController apiVersion: v1 metadata: name: db-controller spec: replicas: 2 selector: name: db template: spec: containers: - name: db image: dbforweb ports: - containerPort: 3306 metadata: labels: name: "db" selectorname: "db" labels: name: "db"To start the replication controller for the webserver pod, type the following:
# kubectl create -f db-rc.yaml replicationcontrollers/db-controller
-
Create Kubernetes Service yaml file for Web server and start it: On the master, create a Kubernetes service yaml file that identifies a label to tie the Web server container to a particular IP address and port. Here is an example using a file called webserver-service.yaml:
apiVersion: v1 kind: Service metadata: labels: name: webserver name: webserver-service namespace: default spec: ports: - port: 80 selector: name: webserverTo start that service, type the following on the master:
# kubectl create -f webserver-service.yaml services/webserver-service
With the with the apiserver and the two services we just added running, run the following command to see those services:
# kubectl get services NAME LABELS SELECTOR IP(S) PORT(S) db-service name=db name=db 10.254.244.87 3306/TCP kubernetes component=apiserver... none 10.254.0.1 443/TCP webserver-service name=webserver name=webserver 10.254.255.168 80/TCP
-
Deploy Kubernetes ReplicationController for Web server: Create a replication controller yaml file that identifies the number of webserver pods to have running. Based on the following yaml file, the replication controller will try to keep two pods labeled "webserver" running at all times. The pod definition is inside the replications controller yaml file, so no separate pod yaml file is needed. Any running pod with the webserver id will be taken by the replication controller as fulfilling the requirement (regardless of how the pod was started). Here's an example of a file called webserver-rc.yaml:
kind: ReplicationController apiVersion: v1 metadata: name: webserver-controller spec: replicas: 2 selector: selectorname: webserver template: spec: containers: - name: apache-frontend image: webwithdb ports: - containerPort: 80 metadata: labels: name: webserver uses: db labels: name: "webserver"To start the replication controller for the webserver pod, type the following:
# kubectl create -f webserver-rc.yaml webserver-service
Checking Kubernetes
To check that the application is working, you can run a couple of curl commands to get data from the Web server. To check that your cluster is working properly, there are some kubectl commands you can run.
-
Get the IP address of the Web server: Run the following commands on the master to get the IP address for the webserver-service Kubernetes service:
# kubectl get service webserver-service NAME LABELS SELECTOR IP(S) PORT(S) webserver-service name=webserver name=webserver 10.254.255.168 80/TCP
-
Get data from webserver (and database server): On either node, run these curl commands to talk to the web server. The first curl command simply displays the contents of the index.html file from the web server. The second curl command runs a script named action on the web server, which queries the database for information ("Red Hat Rocks" and "Success").
# curl http://10.254.255.168/index.html The Web Server is Running # curl http://10.254.255.168/cgi-bin/action <html> <head> <title>My Application</title> </head> <body> <h2>Red Hat Rocks</h2> <h2>Success</h2> </body> </html>
-
Check the Kubernetes Cluster: Here are some commands you can run from the master to check the cluster:
# kubectl get pods NAME READY STATUS RESTARTS AGE db-controller-6kvpl 1/1 Running 0 52m db-controller-h0ue0 1/1 Running 0 52m webserver-controller-vmw01 1/1 Running 0 25m webserver-controller-yg6ip 1/1 Running 0 25m # kubectl get replicationControllers CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS db-controller db dbforweb name=db 2 webserver-controller webserver webwithdb name=webserver 2 # kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) db-service name=db name=db 10.254.244.87 3306/TCP kubernetes component=apiserver,provider=kubernetes 10.254.0.1 443/TCP webserver-service name=webserver name=webserver 10.254.255.168 80/TCP # kubectl get node NAME LABELS STATUS 127.0.0.1 kubernetes.io/hostname=127.0.0.1 NotReady node1-rhel71 kubernetes.io/hostname=node1-rhel71 Ready node2-rhel71 kubernetes.io/hostname=node2-rhel71 Ready
Cleaning up Kubernetes
If you are done using your cluster, you should take it down in a particular way, to make sure it is done neatly. The order is important. One reason is that, if you delete a pod before deleting the replication controller, the pod is restarted. To take down the Kubernetes cluster and related replication controllers and services, type the following from the master:
# kubectl delete replicationControllers webserver-controller replicationcontrollers/webserver-controller # kubectl delete replicationControllers db-controller replicationcontrollers/db-controller # kubectl delete services webserver-service services/webserver-service # kubectl delete services db-service services/db-service # kubectl get pods NAME READY STATUS RESTARTS AGE # kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) kubernetes component=apiserver,provider=kubernetes 10.254.0.1 443/TCP
If the commands just shown worked properly, the pods should be gone (as shown above) and only the kubernetes service should be running.
If you are done with the nodes, you can remove them from the master as follows:
# kubectl delete node node1.example.com # kubectl delete node node2.example.com
Attachments
This solution is part of Red Hat's fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.