nginx webserver container plus one log-shipper sidecar container plus shared volume for log data.nodes by the kube-scheduler. graph TD
subgraph Node1["Node (VM)"]
Kubelet1["kubelet + runtime"]
subgraph Pod1["Pod"]
Nginx["Container: Nginx"]
end
end
subgraph Node2["Node (VM)"]
Kubelet2["kubelet + runtime"]
subgraph Pod2["Pod"]
Redis["Container: Redis"]
end
end
Kubelet1 --> Pod1
Kubelet2 --> Pod2
1
kubectl get nodes -o wide
nginx-pod.yaml with the following content
1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest # pulls Docker/OCI image
ports:
- containerPort: 80
kubectl and provide path to your nginx-pod.yaml. In the example below, I am in the same directory as my file.
1
2
kubectl apply -f nginx-pod.yaml
kubectl get pods -o wide
At this point, if we try to access the above pod using the containerPort 80, it will fail.
ephemeral nginx if its internal IP changes?-P and -p is not adequate for this.Service provides Pods with a stable virtual IP and DNS name.Service load-balances traffic to all matching Pods via label.ClusterIP service gives access only inside the cluster.NodePort service opens a port on every node’s IP.LoadBalancer (on cloud) provisions an external IP (if available).
1
kubectl get nodes
nginx-svc.yaml with the following content
1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007
kubectl and Pods-only YAML files is a manual process. Deployment (or StatefulSets, DaemonSets) to manage PodsService to maintain stable networking access.nginx pod and one nginx service running. Use the following commmands to check the existence of the pod and service, then to delete the pod and service. After deletion, check again to confirm that the pod and service are gone.
1
2
3
4
5
6
kubectl get pods -o wide
kubectl get svc -o wide
kubectl delete pod nginx
kubectl delete svc nginx
kubectl get pods -o wide
kubectl get svc -o wide
1
<details class="details details--default" data-variant="default"><summary>Step 2: Create deployment</summary>
Deployment and Service are distinct objects, usually defined in separate YAML files. Deployment: workload management (replicas, rolling updates, Pod templates).Service: network exposure (ClusterIP, NodePort, LoadBalancer).--- as a separator.nginx-deployment.yaml with the following content
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
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007
</details>
nginx Pods always runapp: nginx.nginx-service)30007 on every node.
1
2
3
4
kubectl apply -f nginx-deployment.yaml
kubectl get deployments
kubectl get pods -o wide
kubectl get svc
</details>
kubectl get pods -o wide, delete one pod.nginx-deployment immediately create a replacement pod.
</details>