Skip to content
Snippets Groups Projects
Commit 39976ab1 authored by Xueshan Feng's avatar Xueshan Feng
Browse files

Initial release.

parents
No related branches found
No related tags found
No related merge requests found
README.md 0 → 100644
# Getting Started with Docker-for-Mac with Kubernetes
## Install docker for mac
Both Stable and Edge installers come with experimental features in Docker Engine enabled by default and configurable on Docker Daemon preferences for experimental mode.
Go [docker-for-mac](https://docs.docker.com/docker-for-mac/install/) to install.
## Enable the local Kubernetes cluster
Click the `Docker` icon in the status bar, go to `Preferences`, and on the `Kubernetes`-tab, check *Enable Kubernetes*. This will start a single node Kubernetes cluster for you and install the kubectl command line utility. This might take a while, but the dialog will let you know once the Kubernetes cluster is ready.
## Install latest kubectl and kubectx
```console
$ brew install kubectl kubectx
```
## Switch Kubernetes context
If you have been working with Kubernetes, you may have multiple cluster configurations in your $HOME/.kube directory. These kubeconfig files contain cluster API server URL, user credentials etc. You can use *kubectx* to list, show, and switch context. Run `kubectx -h` for details.
* List kube contexts
```console
$ kubectx
docker-for-desktop
* eks-preview
kube-lab
kubev1
```
The one with the "*" (or highlighted) is the current context. To swtich to docker-for-desktop:
* Switch context
```console
$ kubectx docker-for-desktop
Switched to context "docker-for-desktop".
```
* Get kubernetes docker-for-descktop context details:
```console
$ kubectl config view --minify
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://localhost:6443
name: docker-for-desktop-cluster
contexts:
- context:
cluster: docker-for-desktop-cluster
user: docker-for-desktop
name: docker-for-desktop
current-context: docker-for-desktop
kind: Config
```
## Getting to know the cluster
* To verify local kubernetes cluster is running
```console
$ kubectl cluster-info
Kubernetes master is running at https://localhost:6443
KubeDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
```
* List what's running by default installastion
```console
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
docker compose-5d4f4d67b6-wkfkm 1/1 Running 0 28m
docker compose-api-7bb7b5968f-j88m4 1/1 Running 0 28m
kube-system etcd-docker-for-desktop 1/1 Running 0 29m
kube-system kube-apiserver-docker-for-desktop 1/1 Running 0 29m
kube-system kube-controller-manager-docker-for-desktop 1/1 Running 0 29m
kube-system kube-dns-6f4fd4bdf-mh6ng 3/3 Running 0 29m
kube-system kube-proxy-2bz9q 1/1 Running 0 29m
kube-system kube-scheduler-docker-for-desktop 1/1 Running 0 29m
```
## Install kubernetes-dashboard applications
The first application we are going to install is "kubernetes-dashboard". This allows you to use UI to manage the cluster and see workload status etc.
```console
kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
secret "kubernetes-dashboard-certs" created
serviceaccount "kubernetes-dashboard" created
role "kubernetes-dashboard-minimal" created
rolebinding "kubernetes-dashboard-minimal" created
deployment "kubernetes-dashboard" created
service "kubernetes-dashboard" created
```
Now run `kubectl proxy` so we can connect to UI from our laptop. In case you have left-over proxy running, kill it first, then start a new one, as shown below:
```console
$ pkill kubectl
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
```
Then go to the [kubernetes-dashboard](http://127.0.0.1:8001/ui/). Click "SKIP" in the login page, you should see the dashboard:
![Kubernetes-dashboard](./images/dashboard.png)
Now explore key concepts of Kubernetes: Namespaces, Pods, Secrets etc. Note that you are in "default" namesapces when you sign-in. Switch to "kube-system", you will see a lot more workloads running in that namespace. Click on "Nodes", you can see a graphical overview of "docker-for-desktop" one-node cluster - its resource usage etc.
![docker-for-desktop node](./images/node.png)
## Install a guestbook 3 tier application
We are going to install a native Kubernetes application [guestbook]([https://kubernetes.io/docs/tutorials/stateless-application/guestbook/). The link gives step by step deployment to install redis-master, redis-slave, guestbook application and service load-balancer. You can go ahead to follow that documentation or just do the installation with all-in-one yaml file:
```console
$ kubectl create -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/guestbook/all-in-one/guestbook-all-in-one.yaml
service "redis-master" created
deployment "redis-master" created
service "redis-slave" created
deployment "redis-slave" created
service "frontend" created
deployment "frontend" created
```
The guestbook application is created in "Default" namespace, you can see it on UI or run the following command to slice-and-dice the application view, with their "label" defined in their kubernetes manifest.
* Frontend: loadbalancer svc/frontend, and 3 replicas to serve the application
```console
$ kubectl get deploy,svc,pods -n default -l tier=frontend
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/frontend 3 3 3 3 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/frontend ClusterIP 10.105.182.163 <none> 80/TCP 5m
NAME READY STATUS RESTARTS AGE
po/frontend-67f65745c-dmkzq 1/1 Running 0 5m
po/frontend-67f65745c-n24fq 1/1 Running 0 5m
po/frontend-67f65745c-nzmr4 1/1 Running 0 5m
```
* Backend: One replica of redis-master, 3 redis-slave
```console
$ kubectl get deploy,svc,pods -n default -l tier=backend
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/redis-master 1 1 1 1 8m
deploy/redis-slave 2 2 2 2 8m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/redis-master ClusterIP 10.103.215.140 <none> 6379/TCP 8m
svc/redis-slave ClusterIP 10.105.74.181 <none> 6379/TCP 8m
NAME READY STATUS RESTARTS AGE
po/redis-master-585798d8ff-tqcpn 1/1 Running 0 8m
po/redis-slave-865486c9df-7kd75 1/1 Running 0 8m
po/redis-slave-865486c9df-b45sq 1/1 Running 0 8m
* Port-forwarding
Here we demonstrate `kubectl port-forward` feature. We willconnect to the Redis master server we just deployed for guestbook app. Even the port 6379 is not open to the internet, we can connect to the server by port-forwarding.
```console
$ kubectl port-forward redis-master-585798d8ff-tqcpn 6379:6379
Forwarding from 127.0.0.1:6379 -> 6379
Handling connection for 6379
```
On another terminal on your local machine, try:
```console
$ redis-cli ping
PONG
```
```console
$ redis-cli info |grep redis_
redis_version:2.8.19
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a74551038794e13f
redis_mode:standalone
```
```console
$ redis-cli info |grep db
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1523552802
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
```
Or just get inside of the redis master:
```console
$ redis-cli
127.0.0.1:6379>
quit
```
You can Control-C to terminate the port-forward proxy in the other terminal.
* Port-forward the service so we can access guestbook from our laptop users can use it
The guestbook deployment use "LoadBalancer" type for guestbook service, which means internal pods can use the service endpoint to access guestbook without knowing pods address at cluster IP 10.105.182.163:80. To access from outside of the kubernetes cluster network, we can use port-forwarding again without expose it to the internet:
```console
$ kc get svc -l app=guestbook
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend ClusterIP 10.105.182.163 <none> 80/TCP 54m
$ kubectl port-forward svc/frontend 8000:80
Forwarding from 127.0.0.1:8000 -> 80
Forwarding from [::1]:8000 -> 80
Handling connection for 8000
Handling connection for 8000
```
Now you can connect to the guestbook application on http://127.0.0.1:8000 and sign-up as a guest, as shown blow:
![Guestbook](./images/guestbook.png)
You can terminate the proxy by Control-C to in the proxy window.
## Teardown
* Delete dashboard application
```console
$ kubeclt delete -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/guestbook/all-in-one/guestbook-all-in-one.yaml
service "redis-master" deleted
deployment "redis-master" deleted
service "redis-slave" deleted
deployment "redis-slave" deleted
service "frontend" deleted
deployment "frontend" deleted
```
* Delete the dashboard
```console
$ kubectl delete -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
secret "kubernetes-dashboard-certs" deleted
serviceaccount "kubernetes-dashboard" deleted
role "kubernetes-dashboard-minimal" deleted
rolebinding "kubernetes-dashboard-minimal" deleted
deployment "kubernetes-dashboard" deleted
service "kubernetes-dashboard" deleted
```
## What's next
You now have a Kubernetes on your laptop you can play, learn, develope your kubernetes applications before you put the load on a production kubernetes system.
You can use the cluster you just enable to complete [Kubernetes tutorial](https://kubernetes.io/docs/tutorials/).
images/dashboard.png

229 KiB

images/guestbook.png

47.8 KiB

images/node.png

313 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment