Creating a simple application¶
To create a simple application, we will need 2 things:
A deployment definition
A service definition
Normally, you would create the service definition FIRST.
Example Deployment¶
nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
This can be deployed to a cluster by running kubectl apply -f nginx-deployment.yaml
Example service¶
nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
This can be deployed to a cluster by running kubectl apply -f nginx-service.yaml
Viewing the service outside the cluster¶
The general method for exposing a service to the outside world is to install an Ingress Controller and define an ingress rule for the service.
Alternatively, you can add a type: <foo> property to the spec: where <foo>= :
For testing purposes, you can use: kubectl port-forward
Examples
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward pod/mypod 5000 6000
# Listen on port 8888 forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
Order of creation¶
You should create service definitions before you create the deployment it references and before any resources that use it. This is because kubernetes will expose the service properties to the environment of any containers created after the service is created. For more information, see Discovering services in the Kubernetes documentation.