Found at: Dev.to
- Docker installed on your machine
- Basic knowledge of Docker and Kubernetes
- A terminal or command prompt
- Internet connection
- Sufficient system resources (CPU, RAM, Disk Space)
- Optional: Docker Compose installed for easier management of multi-container applications
-
Pull the K3S Docker Image: Open your terminal and run the following command to pull the K3S Docker image:
docker pull rancher/k3s:latest
-
Run the K3S Container: Start a K3S container with the following command:
docker run -d --name k3s-server --privileged -p 6443:6443 rancher/k3s:latest server --docker
This command runs the K3S server in a detached mode, exposing the Kubernetes API server on port 6443.
-
Setup Volumes: To persist data, you can mount volumes to the container. Modify the
docker runcommand to include volume mounts:docker run -d --name k3s-server \ --privileged \ -p 6443:6443 \ -v /var/lib/rancher/k3s:/var/lib/rancher/k3s \ -v /etc/rancher/k3s:/etc/rancher/k3s \ rancher/k3s:latest server --docker
-
Mounting Volumes Using Docker Named Volumes: Alternatively, you can use Docker named volumes to manage persistent storage:
docker volume create k3s-data docker volume create k3s-etc docker run -d --name k3s-server \ --privileged \ -p 6443:6443 \ -v k3s-data:/var/lib/rancher/k3s \ -v k3s-etc:/etc/rancher/k3s \ rancher/k3s:latest server --docker
-
Setup Networking: Ensure that your Docker network settings allow the K3S server to communicate with other containers and services. You might need to configure firewall rules or Docker network settings depending on your environment.
docker network create k3s-net
docker network connect k3s-net k3sRestart the server on this network:
docker run -d --name k3s-server \
--privileged \
-p 6443:6443 \
-v /var/lib/rancher/k3s:/var/lib/rancher/k3s \
-v /etc/rancher/k3s:/etc/rancher/k3s \
--network k3s-net \
--hostname k3s-server \
rancher/k3s:latest server \
--node-name k3s-server
--docker- Retrieve the Node Token: If you plan to add worker nodes to your K3S cluster, you'll need the node token. You can retrieve it from the running container:
docker exec -it k3s-server cat /var/lib/rancher/k3s/server/node-token
The output is a lengthy string (e.g., K10abc123...::node:xyz789...), generated when the server starts. This token ensures the secure registration of worker nodes. Copy it for the next step.
7. Add Worker Nodes (Optional): If you want to add worker nodes to your K3S cluster, run the following command on each worker node, replacing <K3S_SERVER_IP> with the IP address of your K3S server and <NODE_TOKEN> with the token you retrieved in the previous step:
docker run -d --name k3s-agent-1 \
--privileged \
--network k3s-net \
-e K3S_URL=https://k3s-server:6443 \
-e K3S_TOKEN=<NODE_TOKEN> \
rancher/k3s:latest agent --dockerFull example:
docker stop k3s-agent-1
docker rm k3s-agent-1
docker run -d --name k3s-agent-1 \
--privileged \
--network k3s-net \
--hostname k3s-agent-1 \
rancher/k3s agent \
--server https://k3s-server:6443 \
--token <NODE_TOKEN> \
--node-name k3s-agent-1- Check the K3S Server Logs: To ensure that the K3S server is running correctly, check the logs with:
Look for messages indicating that the server has started successfully.
docker logs -f k3s-server
- Set Up Kubeconfig: To interact with your K3S cluster, you need to set up the kubeconfig file. Copy the kubeconfig from the container to your local machine:
Then, set the
docker cp k3s-server:/etc/rancher/k3s/k3s.yaml ./k3s.yaml
KUBECONFIGenvironment variable to point to this file:export KUBECONFIG=$(pwd)/k3s.yaml
- Verify K3S Installation: After the container is running, you can verify the installation by checking the status of the K3S server:
docker exec -it k3s-server k3s kubectl get nodes
kubectl run nginx --image=nginx --restart=Never
kubectl get pods -o wide
kubectl get pods
kubectl expose pod nginx --port=80 --type=NodePort
kubectl get svcThis command should show the K3S server node in a Ready state.
Launch a BusyBox pod to test the service:
kubectl run busybox --image=busybox --restart=Never --rm -it -- sh
wget -qO- http://<NodeIP>:<NodePort>
wget -qO- http://10.43.4.246:80Port Forwarding
kubectl port-forward svc/nginx 8080:80
kubectl port-forward service/nginx 8080:80
curl http://localhost:8080NodePort Access - Expose the NodePort on the server container:
docker run -d --name k3s-server \
--privileged \
-p 6443:6443 \
-p 30000-32767:30000-32767 \
-v /var/lib/rancher/k3s:/var/lib/rancher/k3s \
-v /etc/rancher/k3s:/etc/rancher/k3s \
--network k3s-net \
--hostname k3s-server \
rancher/k3s:latest server \
--node-name k3s-server
--dockeror
docker stop k3s-server
docker rm k3s-server
docker run -d --name k3s-server \
--privileged \
-p 6443:6443 -p 32116:32116 \
-v k3s-data:/var/lib/rancher/k3s \
--network k3s-net \
--hostname k3s-server \
rancher/k3s server \
--node-name k3s-server
--dockerTest NodePort access:
curl http://127.0.0.1:32116kubectl get nodes
kubectl get pods --all-namespaces
kubectl cluster-infokubectl get componentstatuses
kubectl get pods -n kube-systemkubectl get deployments --all-namespaces
kubectl get replicasets --all-namespaces
kubectl get daemonsets --all-namespaces
kubectl logs nginx
kubectl describe pod nginx- Delete and Recreate: Remove the node and restart the container:
Then recreate it with the same command used earlier.
kubectl delete node e36b344d4930 docker stop k3s-agent-1 docker rm k3s-agent-1 docker run -d --name k3s-agent-1 \ --privileged \ --network k3s-net \ --hostname k3s-agent-1 \ rancher/k3s agent \ --server https://k3s-server:6443 \ --token <NODE_TOKEN> \ --node-name k3s-agent-1
To delete the K3S setup, stop and remove the containers:
docker stop k3s-server
docker stop k3s-agent-1
docker rm k3s-server
docker rm k3s-agent-1
docker network rm k3s-net
docker volume rm k3s-data
docker volume rm k3s-etcRemoving a Node To remove k3s-agent-1:
kubectl delete node k3s-agent-1
docker stop k3s-agent-1
docker rm k3s-agent-1Wipe everything:
docker stop k3s-server
docker rm k3s-server
docker volume rm k3s-data
docker volume rm k3s-etcThen recreate the server with the same command used earlier.
# K3s Network Policy Controller Fix - Multiple Solutions
# SOLUTION 1: Disable Network Policy (Simplest)
# This disables the network policy controller entirely
Write-Host "=== SOLUTION 1: Disable Network Policy ===" -ForegroundColor Cyan
docker stop k3s-server 2>$null
docker rm k3s-server 2>$null
docker run -d --name k3s-server `
--privileged `
-p 6443:6443 `
-v k3s-data:/var/lib/rancher/k3s `
--network k3s-net `
--hostname k3s-server `
rancher/k3s server `
--node-name k3s-server `
--flannel-backend=none `
--disable-network-policy `
--cluster-cidr=10.42.0.0/16 `
--service-cidr=10.43.0.0/16
Write-Host "Started K3s with network policy disabled" -ForegroundColor Green
# SOLUTION 2: Specify Node IP (If you need network policies)
Write-Host "`n=== SOLUTION 2: With Specific Node IP ===" -ForegroundColor Cyan
# First, get the container IP from the k3s-net network
$networkInfo = docker network inspect k3s-net | ConvertFrom-Json
$subnet = $networkInfo.IPAM.Config[0].Subnet
Write-Host "K3s Network Subnet: $subnet" -ForegroundColor Yellow$token = docker exec k3s-server cat /var/lib/rancher/k3s/server/node-token 2>$nulldocker run -d --name k3s-agent-1 `
--privileged `
--network k3s-net `
--hostname k3s-agent-1 `
rancher/k3s agent `
--server https://k3s-server:6443 `
--token $token `
--node-name k3s-agent-1Goto the k3s-server container and check nodes from command line
kubectl get nodesdocker volume create registry-data
docker run -d --name local-registry `
-p 5000:5000 `
-v registry-data:/var/lib/registry `
--network k3s-net `
--hostname local-registry `
--restart unless-stopped `
registry:2 # Start local container registry
docker run -d --name local-registry `
-p 5000:5000 `
-v registry-data:/var/lib/registry `
--network host `
--hostname local-registry `
--restart unless-stopped `
registry:2