Skip to content

Instantly share code, notes, and snippets.

@LawAbidingNinja
Last active April 24, 2018 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LawAbidingNinja/57093d4cb9346ef61feb230ef3bcc871 to your computer and use it in GitHub Desktop.
Save LawAbidingNinja/57093d4cb9346ef61feb230ef3bcc871 to your computer and use it in GitHub Desktop.
Managing Kubernetes

Create Deployment

We're going to deploy a basic nginx docker container to the cluster.

  • Create a new directory called K8s
  • Copy the following into a file called my-nginx-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: my-nginx-deployment
  labels:
    app: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
  • Open terminal/powershell and navigate to the directory of the file (on windows you can shift+right click to open powershell in the current directory).

  • Run kubectl create -f my-nginx-deployment.yaml

  • In order to check if the deployment was created successfully run kubectl get deployments. There should be a deployment called my-nginx-deployment

  • To get more information about the deployment run: kubectl describe deploy my-nginx-deployment

Create Load Balancing Service

Nginx is now running on your Kubernetes Cluster, however it's not yet accessible to the internet! We're going to create a service that automatically sets up a load balancer for your deployment. Create a file called my-nginx-service.yaml and copy in the following:

apiVersion: v1
kind: Service
metadata:
  name: my-nginx-service
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: my-nginx
  sessionAffinity: None
  type: LoadBalancer
  • Once the service has been created run kubectl get svc to see a list of services. The 'External IP' of the new service will be pending whilst Kubernetes is setting up the Google Cloud Load balancer.

  • To get more information about the service run kubectl describe svc my-nginx-service

  • Wait a few minutes, once the external IP becomes available navigate to it in your browser, if you hit a 'Welcome to nginx' page, that's the site you just deployed!

Scale Deployment

Need to scale up the instances of your app/site? This is super straightforward.

  • First let's check how many pods are running as part of your deployment, run: kubectl get pods

  • Now let's scale it up: kubectl scale deploy my-nginx-deployment --replicas=3

  • Run kubectl get pods again to check the number of active pods.

  • Note if you want to watch changes as they happen add the watch (-w) flag to a get command, e.g. kubectl get pods -w (use Ctrl+C to stop)

Update Deployment

The 'Welcome to nginx' page isn't particularly great as far as websites go. Considering you already created a website earlier, let's deploy that!

Make sure you have your Docker Hub user name on hand: Run the following to update: kubectl set image deployment my-nginx-deployment my-nginx=MY_DOCKERHUB_USERNAME/my-nginx:latest

This performs something called a rolling update, one by one the pods are terminated and replaced with pods running the new docker container. You can watch this process with the kubectl get pods -w command. Refresh your website (the one at the external IP address from earlier), the updated site should come up soon!

Extras

Kubernetes Self Healing

To try out the self-healing features of kubernetes do the following:

  • Run kubectl get po, note one of the pod names.
  • Run kubectl delete po POD_NAME
  • Run kubectl get po again and check the new pod coming online.

Useful Reading

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