Skip to content

Commit 01ede64

Browse files
David BritchDavid Britch
authored andcommitted
Merge branch 'master' into xamarin
2 parents 482f8d4 + 27fd97b commit 01ede64

142 files changed

Lines changed: 64356 additions & 821 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

KUBERNETES.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Kubernetes 101
2+
## Docker vs. Kubernetes
3+
Docker helps you package applications into images, and execute them in containers. Kubernetes is a robust platform for containerized applications. It abstracts away the underlying network infrastructure and hardware required to run them, simplifying their deployment, scaling, and management.
4+
5+
## Kubernetes from the container up
6+
### Pods
7+
The basic unit of a Kubernetes deployment is the **Pod**. A Pod encapsulates one or more containers. For example, the `basket` Pod specifies two containers:
8+
>`deployments.yaml`
9+
>
10+
>The first container runs the `eshop/basket.api` image:
11+
>```yaml
12+
>spec:
13+
> containers:
14+
> - name: basket
15+
> image: eshop/basket.api
16+
> env:
17+
> - name: ConnectionString
18+
> value: 127.0.0.1
19+
>```
20+
>Note the `ConnectionString` environment variable: containers within a Pod are networked via `localhost`. The second container runs the `redis` image:
21+
>```yaml
22+
>- name: basket-data
23+
> image: redis:3.2-alpine
24+
> ports:
25+
> - containerPort: 6379
26+
>```
27+
Placing `basket` and `basket-data` in the same Pod is reasonable here because the former requires the latter, and owns all its data. If we wanted to scale the service, however, it would be better to place the containers in separate Pods because the basket API and redis scale at different rates.
28+
29+
If the containers were in separate Pods, they would no longer be able to communicate via `localhost`; a **Service** would be required.
30+
31+
### Services
32+
Services expose Pods to external networks. For example, the `basket` Service exposes Pods with labels `app=eshop` and `component=basket` to the cluster at large:
33+
>`services.yaml`
34+
>```yaml
35+
>kind: Service
36+
>metadata:
37+
> ...
38+
> name: basket
39+
>spec:
40+
> ports:
41+
> - port: 80
42+
> selector:
43+
> app: eshop
44+
> component: basket
45+
>```
46+
Kubernetes's built-in DNS service resolves Service names to cluster-internal IP addresses. This allows the nginx frontend to proxy connections to the app's microservices by name:
47+
>`nginx.conf`
48+
>```
49+
>location /basket-api {
50+
> proxy_pass http://basket;
51+
>```
52+
The frontend Pod is different in that it needs to be exposed outside the cluster. This is accomplished with another Service:
53+
>`frontend.yaml`
54+
>```yaml
55+
>spec:
56+
> ports:
57+
> - port: 80
58+
> targetPort: 8080
59+
> selector:
60+
> app: eshop
61+
> component: frontend
62+
> type: LoadBalancer
63+
>```
64+
`type: LoadBalancer` tells Kubernetes to expose the Service behind a load balancer appropriate for the cluster's platform. For Azure Container Service, this creates an Azure load balancer rule with a public IP.
65+
66+
### Deployments
67+
Kubernetes uses Pods to organize containers, and Services to network them. It uses **Deployments** to organize creating, and modifying, Pods. A Deployment describes a state of one or more Pods. When a Deployment is created or modified, Kubernetes attempts to realize that state.
68+
69+
The Deployments in this project are basic. Still, `deploy.ps1` shows some more advanced Deployment capabilities. For example, Deployments can be paused. Each Deployment of this app is paused at creation:
70+
>`deployments.yaml`
71+
>```yaml
72+
>kind: Deployment
73+
>spec:
74+
> paused: true
75+
>```
76+
This allows the deployment script to change images before Kubernetes creates the Pods:
77+
>`deploy.ps1`
78+
>```powershell
79+
>kubectl set image -f deployments.yaml basket=$registry/basket.api ...
80+
>kubectl rollout resume -f deployments.yaml
81+
>```
82+
83+
### ConfigMaps
84+
A **ConfigMap** is a collection of key/value pairs commonly used to provide configuration information to Pods. The deployment script uses one to store the frontend's configuration:
85+
>`deploy.ps1`
86+
>```
87+
>kubectl create configmap config-files from-file=nginx-conf=nginx.conf
88+
>```
89+
This creates a ConfigMap named `config-files` with key `nginx-conf` whose value is the content of nginx.conf. The frontend Pod mounts that value as `/etc/nginx/nginx.conf`:
90+
>`frontend.yaml`
91+
>```yaml
92+
>spec:
93+
> containers:
94+
> - name: nginx
95+
> ...
96+
> volumeMounts:
97+
> - name: config
98+
> mountPath: /etc/nginx
99+
> volumes:
100+
> - name: config
101+
> configMap:
102+
> name: config-files
103+
> items:
104+
> - key: nginx-conf
105+
> path: nginx.conf
106+
>```
107+
This facilitates rapid iteration better than other techniques, e.g. building an image to bake in configuration.
108+
109+
The script also stores public URLs for the app's components in a ConfigMap:
110+
>`deploy.ps1`
111+
>```powershell
112+
>kubectl create configmap urls --from-literal=BasketUrl=http://$($frontendUrl)/basket-api ...
113+
>```
114+
>Here's how the `webspa` Deployment uses it:
115+
>
116+
>`deployments.yaml`
117+
>```yaml
118+
>spec:
119+
> containers:
120+
> - name: webspa
121+
> ...
122+
> env:
123+
> ...
124+
> - name: BasketUrl
125+
> valueFrom:
126+
> configMapKeyRef:
127+
> name: urls
128+
> key: BasketUrl
129+
>```
130+
131+
### Further reading
132+
* [Kubernetes Concepts](https://kubernetes.io/docs/concepts/)
133+
* [kubectl for Docker Users](https://kubernetes.io/docs/user-guide/docker-cli-to-kubectl/)
134+
* [Kubernetes API reference](https://kubernetes.io/docs/api-reference/v1.5/)

README.CICD.k8s.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Kubernetes CI/CD VSTS
2+
For k8s CI/CD pipeline delivery a series of tasks must be created in VSTS to deploy k8s in Azure
3+
4+
## Prerequisites
5+
* A Kubernetes cluster. Follow Azure Container Service's [walkthrough](https://docs.microsoft.com/en-us/azure/container-service/container-service-kubernetes-walkthrough) to create one.
6+
* A private Docker registry. Follow Azure Container Registry's [guide](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal) to create one.
7+
* Optionally, previous steps can be skipped if you run gen-k8s-env.ps1 script to automatically create the azure environment needed for kubernetes deployment. Azure cli 2.0 must be previously installed [installation guide](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). For example:
8+
>```
9+
>./gen-k8s-env -resourceGroupName k8sGroup -location westeurope -registryName k8sregistry -orchestratorName k8s-cluster -dnsName k8s-dns
10+
>```
11+
* An `Azure Blob storage`. It is needed for storing the kubernetes config file used by the hosted agent to access to Kubernetes cluster. Example:
12+
13+
<img src="img/k8s/blob_creation.png">
14+
15+
* Upload the `kubernetes config file` to the blob storage previously created. Execute the following command which will download the config file into the directory `c:\Users\<User>\.kube\` and then, upload it to your blob storage:
16+
>```
17+
>https://eshopk8s.blob.core.windows.net/k8s-config/config
18+
>```
19+
## Create the VSTS tasks
20+
1. Create a `Download File` task to download the kubernetes binary `kubectl` to the hosted agent. For example:
21+
>```
22+
>https://storage.googleapis.com/kubernetes-release/release/v0.0.1.7.0-alpha.0/bin/windows/386/kubectl.exe
23+
>```
24+
<img src="img/k8s/get_kubectlbin_task.png">
25+
26+
2. Create a Download File task to download the kubernetes config file to the hosted agent. For example:
27+
>```
28+
>https://eshopk8s.blob.core.windows.net/k8s-config/config
29+
>```
30+
<img src="img/k8s/get_kubectlconfig_task.png">
31+
32+
3. Create a powershell task to execute the k8s deployment script. For example:
33+
34+
* Deployment script path
35+
>```
36+
>$(System.DefaultWorkingDirectory)/All Microservices/docker-compose/deploy.ps1
37+
>```
38+
39+
* Deployment script path arguments. Where:
40+
- userDockerHub: indicates if Docker Hub is used instead of ACR
41+
- deployCI: indicates that it is a CI/CD deployment
42+
- execPath: path where the k8s binary is stored
43+
- kubeconfigPath: path where the k8s config file is stored
44+
>```
45+
>-deployCI $true -useDockerHub $true -execPath '$(System.DefaultWorkingDirectory)/' -kubeconfigPath '$(System.DefaultWorkingDirectory)/'
46+
>```
47+
48+
<img src="img/k8s/deploy_script_task.png">

README.k8s.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# eShopOnContainers on Kubernetes
2+
The k8s directory contains Kubernetes configuration for the eShopOnContainers app and a PowerShell script to deploy it to a cluster. Each eShopOnContainers microservice has a deployment configuration in `deployments.yaml`, and is exposed to the cluster by a service in `services.yaml`. The microservices are exposed externally on individual routes (`/basket-api`, `/webmvc`, etc.) by an nginx reverse proxy specified in `frontend.yaml` and `nginx.conf`.
3+
4+
## Prerequisites
5+
* A Kubernetes cluster. Follow Azure Container Service's [walkthrough](https://docs.microsoft.com/en-us/azure/container-service/container-service-kubernetes-walkthrough) to create one.
6+
* A private Docker registry. Follow Azure Container Registry's [guide](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal) to create one.
7+
* Optionally, previous steps can be skipped if you run gen-k8s-env.ps1 script to automatically create the azure environment needed for kubernetes deployment. Azure cli 2.0 must be previously installed [installation guide](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). For example:
8+
>```
9+
>./gen-k8s-env -resourceGroupName k8sGroup -location westeurope -registryName k8sregistry -orchestratorName k8s-cluster -dnsName k8s-dns
10+
>```
11+
* A Docker development environment with `docker` and `docker-compose`.
12+
* Visit [docker.com](https://docker.com) to download the tools and set up the environment. Docker's [installation guide](https://docs.docker.com/engine/getstarted/step_one/#step-3-verify-your-installation) covers verifying your Docker installation.
13+
* The Kubernetes command line client, `kubectl`.
14+
* This can be installed with the `az` tool as described in the Azure Container Service [walkthrough](https://docs.microsoft.com/en-us/azure/container-service/container-service-kubernetes-walkthrough). `az` is also helpful for getting the credentials `kubectl` needs to access your cluster. For other installation options, and information about configuring `kubectl` yourself, see the [Kubernetes documentation](https://kubernetes.io/docs/tasks/kubectl/install/).
15+
16+
## Deploy the application with the deployment script
17+
1. Open a PowerShell command line at the `k8s` directory of your local eShopOnContainers repository.
18+
1. Ensure `docker`, `docker-compose`, and `kubectl` are on the path, and configured for your Docker machine and Kubernetes cluster.
19+
1. Run `deploy.ps1` with your registry information. The Docker username and password are provided by Azure Container Registry, and can be retrieved from the Azure portal. Optionally, ACR credentials can be obtained by running the following command:
20+
>```
21+
>az acr credential show -n eshopregistry
22+
>```
23+
24+
Once the user and password are retrieved, run the following script for deployment. For example:
25+
>```
26+
>./deploy.ps1 -registry myregistry.azurecr.io -dockerUser User -dockerPassword SecretPassword
27+
>```
28+
The script will build the code and corresponding Docker images, push the latter to your registry, and deploy the application to your cluster. You can watch the deployment unfold from the Kubernetes web interface: run `kubectl proxy` and open a browser to [http://localhost:8001/ui](http://localhost:8001/ui)

0 commit comments

Comments
 (0)