How to Run Kubernetes Cluster inside Docker Container
How to Run Kubernetes inside Docker Container using Rancher
What is Rancher
Rancher is a powerful open-source platform for managing Kubernetes clusters. Installing Rancher on a single node is an efficient way to evaluate its capabilities or manage a small-scale deployment. In this guide, we will walk through the steps to set up Rancher using Docker on a single-node setup, leveraging Docker Compose for simplicity.
Prerequisites
Before we begin, ensure you meet the following prerequisites:
- Operating System: A Linux-based system (e.g., Ubuntu or Debian) with Docker installed.
- Docker: Installed and running. Use the command
docker --version
to verify. - Sufficient Resources: At least 4 GB of RAM and 2 CPU cores are recommended for running Rancher.
Step 1: Create a Docker Compose File
The following Docker Compose file will set up Rancher in a single-node configuration. Save the file as docker-compose.yml
in a directory of your choice:
1
2
3
4
5
6
7
8
9
10
11
12
13
version: '3.8'
services:
rancher:
image: rancher/rancher:latest # Use the latest Rancher image from Docker Hub
container_name: rancher # Assign a custom container name
restart: unless-stopped # Automatically restart the container unless stopped manually
privileged: true # Required for Rancher to manage Kubernetes clusters
ports:
- "800:80" # Map HTTP traffic on port 80 to the host's port 800
- "4430:443" # Map HTTPS traffic on port 443 to the host's port 4430
volumes:
- /opt/rancher:/var/lib/rancher # Persist Rancher data to the host to retain state
Explanation of Key Settings
image
: Specifies the Rancher Docker image.privileged
: Allows Rancher to perform administrative tasks on the host system.ports
: Maps container ports to the host system. You can adjust these as needed.volumes
: Ensures Rancher data is persisted between container restarts.
Step 2: Deploy Rancher
Navigate to the directory containing your docker-compose.yml
file and run the following command:
1
docker-compose up -d
This command starts Rancher in detached mode.
Step 3: Verify the Installation
Run the following command to check the status of your Rancher container:
1
docker ps
You should see the rancher
container running. If you encounter any issues, check the logs with:
1
docker logs rancher
Step 4: Access Rancher
Open a web browser and navigate to https://<your-host-ip>:4430
.
Replace <your-host-ip>
with your serverās IP address. Follow the on-screen instructions to set up an admin account and complete the initial configuration.
Step 5: Configure Kubernetes
Once Rancher is up and running, you can start adding or creating Kubernetes clusters. Rancher simplifies this process with an intuitive user interface and robust automation tools.
Configuring Kubectl to Use a Rancher-Managed Kubernetes Cluster
After installing Kubectl, configure it to interact with a Kubernetes cluster managed by Rancher. Follow these steps:
Step 1: Download the KubeConfig File
Log in to the Rancher UI and download the kubeconfig.yaml
file for your cluster. This file contains the necessary credentials and cluster information for Kubectl.
Step 2: Place the KubeConfig File in the Default Directory
Move the downloaded kubeconfig.yaml
file to the Kubernetes configuration directory:
1
2
mkdir -p ~/.kube
mv kubeconfig.yaml ~/.kube/config
Step 3: Set the KUBECONFIG Environment Variable
Export the KUBECONFIG
environment variable to point to your configuration file:
1
export KUBECONFIG=/home/user/.kube/config/kubeconfig.yaml
Replace /home/user
with your actual home directory path.
Step 4: Make the Configuration Persistent
Add the export
line to your ~/.bashrc
file to ensure it persists across sessions:
1
2
echo 'export KUBECONFIG=/home/user/.kube/config/kubeconfig.yaml' >> ~/.bashrc
source ~/.bashrc
Step 5: Test Connectivity to the Cluster
Run the following commands to verify connectivity and functionality:
- List namespaces:
1
kubectl get ns
- Get cluster information:
1
kubectl cluster-info
- List nodes in the cluster:
1
kubectl get nodes
If the commands return expected results, your Kubectl is successfully configured.
Conclusion
Installing Rancher on a single node using Docker is a straightforward process that provides a powerful platform for managing Kubernetes clusters. By leveraging the Docker Compose file provided above, you can quickly spin up a Rancher instance and start exploring its capabilities.