Skip to content

Instantly share code, notes, and snippets.

@dims
Last active May 10, 2022 21:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dims/aafc4b23c6653402f056d5c185fd6cf9 to your computer and use it in GitHub Desktop.
Save dims/aafc4b23c6653402f056d5c185fd6cf9 to your computer and use it in GitHub Desktop.
Experiment with Kubernetes with a custom Docker Image
  1. Build a custom docker image using Dockerfile:
   docker build -t python/server .
  1. Test the image:
   docker run -i -t -p 8000:8000 python/server
   curl http://localhost:8000
  1. Deploy on Kubernetes
kubectl create -f myapp.yaml
  1. Look around:
[dims@ubuntu-brix 17:30] ~/Downloads ⟩ kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
myapp-jxd9f   1/1       Running   0          47s
[dims@ubuntu-brix 17:30] ~/Downloads ⟩ kubectl get services
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.0.0.1     <none>        443/TCP    3h
myappsvc     10.0.0.74    nodes         8000/TCP   52s
[dims@ubuntu-brix 17:30] ~/Downloads ⟩ kubectl get replicationcontrollers
NAME      DESIRED   CURRENT   AGE
myapp     1         1         56s
  1. Test the app:
curl -kvs 10.0.0.74:8000
  1. Alternate to step #3:
kubectl run myapp-node --image=python/server --port=8000
kubectl expose deployment myapp-node --port=8000 --name=myapp-node-http
kubectl describe pods
FROM python:3
EXPOSE 8000
CMD ["python", "-m", "http.server"]
apiVersion: v1
kind: Service
metadata:
name: myappsvc
labels:
app: myapp
spec:
type: NodePort
ports:
- port: 8000
protocol: TCP
name: http
selector:
app: myapp
---
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp
spec:
replicas: 1
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: python/server
ports:
- containerPort: 8000
@yuya-kanai
Copy link

yuya-kanai commented Sep 4, 2019

Thanks for the gist. This helped a lot.

I encountered errimagepull since I wanted to load from worker node but the image was in my master node.
I got it working by running a local image register.

docker run -d -p 5000:5000 --restart=always --name registry registry:2
docker tag python/server localhost:5000/server
docker push localhost:5000/server

and I loaded it with image: localhost:5000/server
based on this post

@bendavis78
Copy link

Where do you actually store your custom images for use with Kubernetes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment