Skip to content

Commit f3044e0

Browse files
committed
Initial commit
0 parents  commit f3044e0

13 files changed

+1067
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
.DS_Store

Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Just for demo purposes obviously
2+
FROM httpd:2.4-alpine
3+
4+
COPY ./index.html /usr/local/apache2/htdocs/

LICENSE

+674
Large diffs are not rendered by default.

README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Kubernetes Tutorial
2+
My kubernetes examples for Developers. A collection of how to work with Kubernetes and Docker.
3+
4+
 
5+
6+
## **Contents**
7+
8+
01. **[Prerequisites](#prerequisites)**
9+
02. **[Installation](#minikube)**
10+
- [Setup Kubernetes on Local Machine](#minikube)
11+
- [Setup Kubernetes with Vagrant and Virtualbox](https://github.com/sayems/kubernetes.examples/blob/master/vagrant.md#kubernetes-with-vagrant-and-virtualbox)
12+
- [Setup Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine/)
13+
- [Setup Kubernetes on AWS](https://kubernetes.io/docs/getting-started-guides/aws/)
14+
03. **[Dashboard](#kubernetes-dashboard)**
15+
04. **[Useful Commands](#useful-commands)**
16+
05. **[Resources](#resources)**
17+
- [Courses](#courses)
18+
- [Books](#books)
19+
- [Cheat Sheet](https://github.com/sayems/kubernetes.examples/blob/master/cheat-sheet.md#kubernetes-cheat-sheet)
20+
21+
 
22+
23+
![](https://github.com/sayems/kubernetes.examples/blob/master/images/kubernetes.png)
24+
25+
 
26+
27+
## Prerequisites
28+
- Basic Linux command line skills
29+
- Experience with using Docker
30+
- Familiarity with YAML is also a plus
31+
32+
 
33+
34+
## Minikube
35+
The simplest and quickest way to get started with Kubernetes is to install [Minikube](https://kubernetes.io/docs/getting-started-guides/minikube/) on your local machine. Minikube is a tool that sets up a single-node cluster that's great for both testing Kubernetes and developing apps locally.
36+
37+
- [macOS](https://github.com/sayems/kubernetes.examples/blob/master/mac.md#requirements)
38+
- [Linux](https://github.com/sayems/kubernetes.examples/blob/master/linux.md#requirements)
39+
- [Windows](https://github.com/sayems/kubernetes.examples/blob/master/windows.md#requirements)
40+
41+
Since Minikube is running locally instead of on a cloud provider, certain provider-specific features like LoadBalancers and PersistentVolumes will not work out-of-the-box. However, you can use NodePort LoadBalancers and HostPath PersistentVolumes.
42+
43+
44+
**NOTE** Minikube doesn't support ```LoadBalancer``` services, so the service will never get an external IP. But you get the IP and port through which you can access service by running ```minikube service kubia-http```.
45+
46+
 
47+
48+
### Kubernetes Dashboard
49+
50+
![](https://github.com/sayems/kubernetes.examples/blob/master/images/dashboard.png)
51+
52+
To open the dashboard in your browser when using Minikube to run your Kubernetes cluster, run the following command:
53+
54+
```
55+
$ minikube dashboard
56+
```
57+
The dashboard will open in your default browser. Unlike with GKE, you won’t need to enter any credentials to access it.
58+
59+
 
60+
61+
## Useful Commands
62+
63+
| Command | Description |
64+
| --- | --- |
65+
| `kubectl get pods` | List all pods in ps output format. |
66+
| `kubectl get pods -o wide` | List all pods in ps output format with more information (such as node name) |
67+
| `kubectl get replicationcontroller web` | List a single replication controller with specified NAME in ps output format. |
68+
| `kubectl get -o json pod web-pod-13je7` | List a single pod in JSON output format. |
69+
| `kubectl get -f pod.ya ml -o json` | List a pod identified by type and name specified in “pod.ya ml” in JSON output format. |
70+
| `kubectl get -o template pod/web-pod-13je7 --template=` | Return only the phase value of the specified pod. |
71+
| `kubectl get rc,services` | List all replication controllers and services together in ps output format. |
72+
| `kubectl get rc/web service/frontend pods/web-pod-13je7` | List one or more resources by their type and names. |
73+
74+
 
75+
76+
## Resources
77+
There are a lot of helpful Kubernetes resources on the internet. This is a short list of my favorites courses and books!
78+
79+
### Courses
80+
81+
- [Scalable Microservices with Kubernetes](https://www.udacity.com/course/scalable-microservices-with-kubernetes--ud615)
82+
83+
84+
### Books
85+
86+
- [Kubernetes in Action](https://www.manning.com/books/kubernetes-in-action)

cheat-sheet.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Kubernetes Cheat Sheet
2+
3+
This is a small [Kubernetes Cheat Sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/) with the list of commands and settings I use, almost on a daily basis, when working with kubernetes.
4+
5+
 
6+
7+
### Cluster Introspection
8+
9+
| Command | Description |
10+
| --- | --- |
11+
| `kubectl get services` | List all services |
12+
| `kubectl get pods` | List all pods |
13+
| `kubectl get nodes -w` | Watch nodes continuously |
14+
| `kubectl version` | Get version information |
15+
| `kubectl cluster-info` | Get cluster information |
16+
| `kubectl config view` | Get the configuration |
17+
| `kubectl describe node <node>` | Output information about a node |
18+
19+
20+
### Pod and Container Introspection
21+
22+
| Command | Description |
23+
| --- | --- |
24+
| `kubectl get pods` | List the current pods |
25+
| `kubectl describe pod <name>` | Describe pod <name> |
26+
| `kubectl get rc` | List the replication controllers |
27+
| `kubectl get rc --namespace="<namespace>"` | List the replication controllers in <namespace> |
28+
| `ubectl describe rc <name>` | Describe replication controller <name> |
29+
| `kubectl get svc` | List the services |
30+
| `kubectl describe svc <name>` | Describe service <name> |
31+
32+
33+
### Interacting with Pods
34+
35+
| Command | Description |
36+
| --- | --- |
37+
| `kubectl run <name> --image=<image-name>` | `Launch a pod called <name> using image <image-name>` |
38+
| `kubectl create -f <manifest.yaml>` | `Create a service described in <manifest.yaml>` |
39+
| `kubectl scale --replicas=<count> rc <name>` | `Scale replication controller <name> to <count> instances` |
40+
| `kubectl expose rc <name> --port=<external> --target-port=<internal>` | `Map port <external> to port <internal> on replication controller <name>` |
41+
42+
### Stopping Kubernetes
43+
44+
| Command | Description |
45+
| --- | --- |
46+
| `kubectl delete pod <name>` | Delete pod <name> |
47+
| `kubectl delete rc <name>` | Delete replication controller <name> |
48+
| `kubectl delete svc <name>` | Delete service <name> |
49+
| `kubectl drain <n> --delete-local-data --force --ignore-daemonsets ` | Stop all pods on <n> |
50+
| `kubectl delete node <name>` | Remove <node> from the cluster |
51+
52+
### Debugging
53+
54+
| Command | Description |
55+
| --- | --- |
56+
| `kubectl exec <service> <command> [-c <$container>]` | execute <command> on <service>, optionally selecting container <$container> |
57+
| `kubectl logs -f <name> [-c <$container>]` | Get logs from service <name>, optionally selecting container <$container> |
58+
| `watch -n 2 cat /var/log/kublet.log` | Watch the Kublet logs |
59+
| `kubectl top node` | Show metrics for nodes |
60+
| `kubectl top pod` | Show metrics for pods |
61+
62+
### Administration
63+
64+
| Command | Description |
65+
| --- | --- |
66+
| `kubeadm init` | Initialize your master node |
67+
| `kubeadm join --token <token> <master-ip>:<master-port>` | Join a node to your Kubernetes cluster |
68+
| `kubectl create namespace <namespace>` | Create namespace <name> |
69+
| `kubectl taint nodes --all node-role.kubernetes.io/master-` | Allow Kubernetes master nodes to run pods |
70+
| `kubeadm reset` | Reset current state |
71+
| `kubectl get secrets` | List all secrets |

images/dashboard.png

126 KB
Loading

images/kubernetes.png

120 KB
Loading

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!

linux.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Requirement

mac.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Requirements
2+
3+
Minikube requires that VT-x/AMD-v virtualization is enabled in BIOS. To check that this is enabled on OSX / macOS run:
4+
5+
sysctl -a | grep machdep.cpu.features | grep VMX
6+
7+
If there's output, you're good!
8+
9+
# Prerequisites
10+
11+
- kubectl
12+
- docker (for Mac)
13+
- minikube
14+
- virtualbox
15+
16+
```
17+
brew update && brew install kubectl && brew cask install docker minikube virtualbox
18+
```
19+
20+
# Verify
21+
22+
docker --version # Docker version 17.09.0-ce, build afdb6d4
23+
docker-compose --version # docker-compose version 1.16.1, build 6d1ac21
24+
docker-machine --version # docker-machine version 0.12.2, build 9371605
25+
minikube version # minikube version: v0.22.3
26+
kubectl version --client # Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.1", GitCommit:"f38e43b221d08850172a9a4ea785a86a3ffa3b3a", GitTreeState:"clean", BuildDate:"2017-10-12T00:45:05Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"darwin/amd64"}
27+
28+
# Start
29+
30+
minikube start
31+
32+
This can take a while, expected output:
33+
34+
Starting local Kubernetes cluster...
35+
Kubectl is now configured to use the cluster.
36+
37+
Great! You now have a running Kubernetes cluster locally. Minikube started a virtual machine for you, and a Kubernetes cluster is now running in that VM.
38+
39+
# Check k8s
40+
41+
kubectl get nodes
42+
43+
Should output something like:
44+
45+
NAME STATUS ROLES AGE VERSION
46+
minikube Ready <none> 40s v1.7.5
47+
48+
# Use minikube's built-in docker daemon:
49+
50+
eval $(minikube docker-env)
51+
52+
Running `docker ps` should now output something like:
53+
54+
```
55+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56+
e97128790bf9 gcr.io/google-containers/kube-addon-manager "/opt/kube-addons.sh" 22 seconds ago Up 22 seconds k8s_kube-addon-manager_kube-addon-manager-minikube_kube-system_c654b2f084cf26941c334a2c3d6db53d_0
57+
69707e54d1d0 gcr.io/google_containers/pause-amd64:3.0 "/pause" 33 seconds ago Up 33 seconds k8s_POD_kube-addon-manager-minikube_kube-system_c654b2f084cf26941c334a2c3d6db53d_0
58+
```
59+
60+
# Build, deploy and run an image on your local k8s setup
61+
62+
First setup a local registry, so Kubernetes can pull the image(s) from there:
63+
64+
docker run -d -p 5000:5000 --restart=always --name registry registry:2
65+
66+
## Build
67+
68+
First of, store all files (Dockerfile, my-app.yml, index.html) in this gist locally in some new (empty) directory.
69+
70+
You can build the Dockerfile below locally if you want to follow this guide to the letter. Store the Dockerfile locally, preferably in an empty directory and run:
71+
72+
docker build . --tag my-app
73+
74+
You should now have an image named 'my-app' locally, check by using `docker images` (or your own image of course). You can then publish it to your local docker registry:
75+
76+
docker tag my-app localhost:5000/my-app:0.1.0
77+
78+
Running `docker images` should now output the following:
79+
80+
```
81+
REPOSITORY TAG IMAGE ID CREATED SIZE
82+
my-app latest cc949ad8c8d3 44 seconds ago 89.3MB
83+
localhost:5000/my-app 0.1.0 cc949ad8c8d3 44 seconds ago 89.3MB
84+
httpd 2.4-alpine fe26194c0b94 7 days ago 89.3MB
85+
```
86+
87+
## Deploy and run
88+
89+
Store the file below `my-app.yml` on your system and run the following:
90+
91+
kubectl create -f my-app.yml
92+
93+
You should now see your pod and your service:
94+
95+
kubectl get all
96+
97+
The configuration exposes `my-app` outside of the cluster, you can get the address to access it by running:
98+
99+
minikube service my-app --url
100+
101+
This should give an output like `http://192.168.99.100:30304` (the port will most likely differ). Go there with your favorite browser, you should see "Hello world!". You just accessed your application from outside of your local Kubernetes cluster!
102+
103+
# Kubernetes GUI
104+
105+
minikube dashboard
106+
107+
# Delete deployment of my-app
108+
109+
kubectl delete deploy my-app
110+
kubectl delete service my-app
111+
112+
You're now good to go and deploy other images!
113+
114+
# Reset everything
115+
116+
minikube stop;
117+
minikube delete;
118+
rm -rf ~/.minikube .kube;
119+
brew uninstall kubectl;
120+
brew cask uninstall docker virtualbox minikube;
121+
122+
# Version
123+
124+
Last tested on 2017 October 20th
125+
macOS Sierra 10.12.6

my-app.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# APP DEPLOYMENT
2+
3+
apiVersion: extensions/v1beta1
4+
kind: Deployment
5+
metadata:
6+
labels:
7+
run: my-app
8+
name: my-app
9+
spec:
10+
replicas: 1
11+
selector:
12+
matchLabels:
13+
run: my-app-exposed
14+
template:
15+
metadata:
16+
labels:
17+
run: my-app-exposed
18+
spec:
19+
containers:
20+
- image: localhost:5000/my-app:0.1.0
21+
name: my-app
22+
ports:
23+
- containerPort: 80
24+
protocol: TCP
25+
26+
---
27+
28+
# APP SERVICE
29+
30+
apiVersion: v1
31+
kind: Service
32+
metadata:
33+
labels:
34+
run: my-app
35+
name: my-app
36+
spec:
37+
ports:
38+
- port: 80
39+
protocol: TCP
40+
targetPort: 80
41+
selector:
42+
run: my-app-exposed
43+
type: NodePort

0 commit comments

Comments
 (0)