Skip to content

Instantly share code, notes, and snippets.

@leonimurilo
Created January 19, 2020 21:56
Show Gist options
  • Save leonimurilo/b6a0bff8ea3d79ebfd8bdf385fd93d70 to your computer and use it in GitHub Desktop.
Save leonimurilo/b6a0bff8ea3d79ebfd8bdf385fd93d70 to your computer and use it in GitHub Desktop.
Declarative Kubernetes with yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment-name # The name of your Deployment
spec:
replicas: 3 # The amount of replicated pods
selector:
matchLabels: # specifies which pods the Deployment will target ("defines how the Deployment finds which Pods to manage")
app: my-app
environment: production
template: # template defines how the pods will be created as part of the Deployment
metadata:
labels: # label the Pods
app: my-app
environment: production
spec:
containers: # array defining the containers within the Pod
- name: my-app
image: leonimurilo/node-app:1.0.11 # docker hub image
ports:
- containerPort: 3000 # you can tell kubernetes which port the container is listening to
resources: # here you set the hardware resources your container will be able to use
requests:
memory: '256Mi'
cpu: '0.5'
limits:
memory: '1024Mi'
cpu: '2'
env: # the environment variables set for the container
- name: NODE_ENV
value: "production" # this could even be dinamically inserted if you need
# envs can also come from Kubernetes Secrets or reference Pod fields via fieldRef -> fieldPath
# The --- separates objects but you can also have separate files and use kubectl apply in an entire folder
---
apiVersion: v1
kind: Service
metadata:
name: my-service-name
spec:
type: NodePort # exposes the service on a static port on the node IP address
ports: # defines which requests to a service port get forwarded to which ports on its Pods
- NodePort: 32000 # access service via external port number (optional. if not set, the port will random be in the 30000-32767 range)
port: 8080 # port exposed internally in cluster
targetPort: 3000 # the port on which the app is running inside the container
protocol: TCP
name: http
selector:
run: my-app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment