Kubernetes Deploy Example - Part 2

This is part 2 of Kubernetes Deploy Example, in this example, we will use the yaml file to deploy a nginx.

In this example, we have combined the Deployment part and the Service part together. We could deploy them at the same time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
# https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service
kind: Service
apiVersion: v1
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 32000
type: NodePort

Step 1: Apply applications with yaml file

1
2
3
kubectl apply -f ./deployment.yaml
deployment.apps "nginx-deployment" created
service "nginx-service" created

Step 2: Get kubernetes ip

1
2
3
4
5
kubectl get endpoints kubernetes

NAME ENDPOINTS AGE
kubernetes 192.168.99.100:8443 143d

Step 3: Access to nginx

Open your navigator and type the url of kubernetes with the port of nginx(we defined 32000 in the yaml file), you will access to welcome page of nginx

1
2
http://192.168.99.100:32000