|
| 1 | +# Google Cloud Platform with Kubernetes |
| 2 | + |
| 3 | +## Kubernetes on Google Cloud Platform |
| 4 | + |
| 5 | +When you run a Kubernetes Engine cluster, you also gain the benefit of advanced cluster management features |
| 6 | +that Google Cloud Platform provides. These include: |
| 7 | + |
| 8 | +1. Load-balancing for Compute Engine instances. |
| 9 | +2. Node Pools to designate subsets of nodes within a cluster for additional flexibility. |
| 10 | +3. Automatic scaling of your cluster's node instance count. |
| 11 | +4. Automatic upgrades for your cluster's node software. |
| 12 | +5. Node auto-repair to maintain node health and availability. |
| 13 | +6. Logging and Monitoring with Stackdriver for visibility into your cluster. |
| 14 | + |
| 15 | + |
| 16 | +**gcloud** is the command-line tool for Google Cloud Platform. |
| 17 | + |
| 18 | +List the active account name with this command: |
| 19 | + |
| 20 | + gcloud auth list |
| 21 | + |
| 22 | +List the project ID with this command: |
| 23 | + |
| 24 | + gcloud config list project |
| 25 | + |
| 26 | +## Google Kubernetes Engine |
| 27 | + |
| 28 | +In the cloud shell environment, to set the zone: |
| 29 | + |
| 30 | + gcloud config set compute/zone us-central1-b |
| 31 | + |
| 32 | +Now start up a cluster for use: |
| 33 | + |
| 34 | + gcloud container clusters create io |
| 35 | + |
| 36 | + |
| 37 | +Following as per GCP Kubernates tutorial: |
| 38 | + |
| 39 | + git clone https://github.com/googlecodelabs/orchestrate-with-kubernetes.git |
| 40 | + cd orchestrate-with-kubernetes/kubernetes |
| 41 | + |
| 42 | +#### The easiest way to get started with Kubernetes is to use the `kubectl create` command. |
| 43 | + |
| 44 | +In Kubernetes, all containers run in a pod. Use the `kubectl` get pods command to view the running container |
| 45 | + |
| 46 | +Expose running container using the `kubectl expose`. |
| 47 | +You may list services using the `kubectl get services` command |
| 48 | + |
| 49 | +Kubernetes supports an easy to use workflow out of the box using the `kubectl` run and expose commands. |
| 50 | + |
| 51 | +------- |
| 52 | + |
| 53 | +### Kubernates Components - |
| 54 | + |
| 55 | +**Pods** represent and hold a collection of one or more containers. |
| 56 | +Generally, if you have multiple containers with a hard dependency on each other, you package the containers inside a single pod. |
| 57 | + |
| 58 | +Pods also have Volumes. Volumes are data disks that live as long as the pods live, and can be used by the containers in that pod. |
| 59 | +Pods provide a shared namespace for their contents. Pods also share a network namespace. This means that there is one IP Address per pod. |
| 60 | + |
| 61 | +#### Creating Pods |
| 62 | +Pods can be created using pod configuration files. |
| 63 | + |
| 64 | +Sample config file: |
| 65 | + |
| 66 | +``` |
| 67 | + name: monolith |
| 68 | + labels: |
| 69 | + app: monolith |
| 70 | +spec: |
| 71 | + containers: |
| 72 | + - name: monolith |
| 73 | + image: kelseyhightower/monolith:1.0.0 |
| 74 | + args: |
| 75 | + - "-http=0.0.0.0:80" |
| 76 | + - "-health=0.0.0.0:81" |
| 77 | + - "-secret=secret" |
| 78 | + ports: |
| 79 | + - name: http |
| 80 | + containerPort: 80 |
| 81 | + - name: health |
| 82 | + containerPort: 81 |
| 83 | + resources: |
| 84 | + limits: |
| 85 | + cpu: 0.2 |
| 86 | + memory: "10Mi" |
| 87 | +``` |
| 88 | + |
| 89 | +Create the monolith pod using kubectl (above sample file is named `monolith.yaml` in pods diretory): |
| 90 | + |
| 91 | + kubectl create -f pods/monolith.yaml |
| 92 | + |
| 93 | +Use the `kubectl get pods` command to list all pods running in the default namespace. |
| 94 | +Use `kubectl describe pods [<pod_name>]` command to get more information. |
| 95 | + |
| 96 | +#### Interacting with Pods |
| 97 | + |
| 98 | +By default, pods are allocated a private IP address and cannot be reached outside of the cluster. |
| 99 | +Use the `kubectl port-forward command` to map a local port to a port inside the monolith pod(created above). |
| 100 | +To test this, ona cli use: `kubectl port-forward monolith 10080:80` |
| 101 | +On another cli use: `curl http://127.0.0.1:10080` |
| 102 | + |
| 103 | +Use the `kubectl logs` command to view the logs for the monolith Pod. |
| 104 | + |
| 105 | + kubectl logs monolith |
| 106 | + |
| 107 | +To get a stream of the logs happening in real-time: |
| 108 | + |
| 109 | + kubectl logs -f monolith |
| 110 | + |
| 111 | +### Services |
| 112 | +Pods aren't meant to be persistent. They can be stopped or started for many reasons - like failed liveness or readiness checks. |
| 113 | +Services use labels to determine what Pods they operate on. |
| 114 | + |
| 115 | +The level of access a service provides to a set of pods depends on the Service's type. Currently there are three types: |
| 116 | + |
| 117 | +**ClusterIP (internal)** -- the default type means that this Service is only visible inside of the cluster, |
| 118 | +**NodePort** gives each node in the cluster an externally accessible IP and |
| 119 | +**LoadBalancer** adds a load balancer from the cloud provider which forwards traffic from the service to Nodes within it. |
| 120 | + |
| 121 | +Creating a Service |
| 122 | + |
| 123 | +Create the secure-monolith pods and their configuration data: |
| 124 | + |
| 125 | + kubectl create secret generic tls-certs --from-file tls/ |
| 126 | + kubectl create configmap nginx-proxy-conf --from-file nginx/proxy.conf |
| 127 | + kubectl create -f pods/secure-monolith.yaml |
| 128 | + |
| 129 | +Things to note: |
| 130 | + |
| 131 | +1. There's a selector which is used to automatically find and expose any pods with the labels "app=monolith" and "secure=enabled" |
| 132 | +2. Now you have to expose the nodeport here because this is how we'll forward external traffic from port 31000 to nginx (on port 443). |
| 133 | +Use the kubectl create command to create the monolith service from the monolith service configuration file (same as earlier): |
| 134 | + |
| 135 | + kubectl create -f services/monolith.yaml |
| 136 | + |
| 137 | +You're using a port to expose the service. This means that it's possible to have port collisions if another app tries to bind to port 31000 on one of your servers. |
| 138 | + |
| 139 | +Normally, Kubernetes would handle this port assignment. |
| 140 | + |
| 141 | +Use the gcloud compute firewall-rules command to allow traffic to the monolith service on the exposed nodeport: |
| 142 | + |
| 143 | + gcloud compute firewall-rules create allow-monolith-nodeport --allow=tcp:31000 |
| 144 | + |
| 145 | + |
| 146 | +List all compute instances: |
| 147 | + |
| 148 | + gcloud compute instances list |
| 149 | + |
| 150 | +#### Adding Labels to Pods |
| 151 | + |
| 152 | +Currently the monolith service does not have endpoints. Use the kubectl get pods command with a label query: |
| 153 | + |
| 154 | + kubectl get pods -l "app=monolith" |
| 155 | + |
| 156 | +Check if labels have been updated: |
| 157 | + |
| 158 | + kubectl label pods secure-monolith 'secure=enabled' |
| 159 | + kubectl get pods secure-monolith --show-labels |
| 160 | + |
| 161 | +To view the list of endpoints on the monolith service: |
| 162 | + |
| 163 | + kubectl describe services monolith | grep Endpoints |
| 164 | + |
| 165 | +#### Deploying Applications with Kubernetes |
| 166 | + |
| 167 | +Deployments are a declarative way to ensure that the number of Pods running is equal to the desired number of Pods, specified by the user. |
| 168 | +The main benefit of Deployments is in abstracting away the low level details of managing Pods. Behind the scenes Deployments use Replica Sets to manage starting and stopping the Pods. |
| 169 | +If Pods need to be updated or scaled, the Deployment will handle that. Deployment also handles restarting Pods if they happen to go down for some reason. |
| 170 | + |
| 171 | +**Creating Deployments** |
| 172 | + |
| 173 | +We're going to break the monolith app into three separate pieces: |
| 174 | + |
| 175 | +`auth` - Generates JWT tokens for authenticated users. |
| 176 | +`hello` - Greet authenticated users. |
| 177 | +`frontend` - Routes traffic to the auth and hello services. |
| 178 | + |
| 179 | +Examine the auth deployment configuration file: |
| 180 | + |
| 181 | + cat deployments/auth.yaml |
| 182 | + |
| 183 | +Create your deployment object: |
| 184 | + |
| 185 | + kubectl create -f deployments/auth.yaml |
| 186 | + |
| 187 | + |
| 188 | +create the auth service: |
| 189 | + |
| 190 | + kubectl create -f services/auth.yaml |
| 191 | + |
| 192 | +Create and expose the hello deployment: |
| 193 | + |
| 194 | + kubectl create -f deployments/hello.yaml |
| 195 | + kubectl create -f services/hello.yaml |
| 196 | + |
| 197 | +Create and expose the frontend Deployment: |
| 198 | + |
| 199 | + kubectl create configmap nginx-frontend-conf --from-file=nginx/frontend.conf |
| 200 | + kubectl create -f deployments/frontend.yaml |
| 201 | + kubectl create -f services/frontend.yaml |
| 202 | + |
| 203 | +Interact with the frontend by grabbing it's External IP and then curling to it: |
| 204 | + |
| 205 | + kubectl get services frontend |
| 206 | + curl -k https://<EXTERNAL-IP> |
| 207 | + |
0 commit comments