This documentation provides a step-by-step guide on how Docker is utilized in the development environment for kubernetes/client-go
. The instructions focus on building and running applications using Docker without involving production configurations.
Prerequisites
Ensure that you have the following prerequisites fulfilled:
- A Kubernetes cluster is set up and properly configured with
kubectl
. - Docker installed and running on your development machine.
Step 1: Setting Up Your Dockerfile
Create a Dockerfile
to define how your application will be built and run inside a Docker container. Below is a basic example of a Dockerfile.
# Dockerfile
FROM golang:1.17-alpine as builder
WORKDIR /app
# Copy source code
COPY . .
# Build the application
RUN go build -o app .
FROM alpine:latest
# Copy the built application from the builder stage
COPY --from=builder /app/app .
ENTRYPOINT ["/app"]
Explanation:
- The first stage uses the
golang:1.17-alpine
base image to compile the Go application. - The second stage uses a smaller
alpine
image to run the application. This minimizes the image size. - The
ENTRYPOINT
command sets the default command to run when the container starts.
Step 2: Compiling the Application
To compile the application, follow the steps below:
cd create-update-delete-deployment
go build -o ./app
This command compiles the main.go
file in the specified directory, outputting the compiled binary as app
.
Step 3: Building the Docker Image
If using a Minikube cluster, you can build the Docker image directly on the Minikube Docker environment. Run the following command:
eval $(minikube docker-env)
docker build -t in-cluster .
Without Minikube:
If you are developing outside of Minikube or other Docker-compatible Kubernetes setups, build the image and push it to a Docker registry.
docker build -t my-registry/in-cluster:latest .
docker push my-registry/in-cluster:latest
Replace my-registry
with the actual registry path.
Step 4: Running the Application in Your Local Environment
Once you have built the Docker image, it’s time to run it locally, using your kubeconfig:
./app -kubeconfig=$HOME/.kube/config
Application Operations
The application will execute various operations on your Kubernetes cluster. It will:
Create Deployment: Create a Deployment with 2 replicas. Verify by running:
kubectl get pods
Update Deployment: Modify the Deployment to change the replica count and update the container image. Use:
kubectl describe deployment demo
List Deployments: Retrieve and list all Deployments in the
default
namespace.Delete Deployment: Remove the created Deployment. Check via:
kubectl get deployments
Step 5: Verify Your Setup
After running the application, you should see prompts guiding you through each operation, allowing you meaningful interactions with your Kubernetes cluster.
Important Notes
- Ensure the
Dockerfile
and the Go application source code are in the same directory. - Modify the
Dockerfile
and application code as needed to fit your specific requirements and dependencies. - If running on Minikube, ensure that Docker is configured to use the Minikube Docker daemon to avoid escalated permissions.
Conclusion
This guide describes the essential steps to configure Docker for the kubernetes/client-go
development environment. Properly utilizing Docker during development simplifies integration with Kubernetes and provides a streamlined workflow for application testing and deployment.
Source: Code examples and configurations are referenced from sample application folders in the Kubernetes/client-go repository.