Skip to content

Commit

Permalink
chore: Add Kubernetes resources for deployment, pod, job, replicaset,…
Browse files Browse the repository at this point in the history
… service, and namespace
  • Loading branch information
Pradumnasaraf committed Aug 30, 2024
1 parent 43c23b0 commit 96456f0
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/argocd/manifests/declarative.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ spec:
selfHeal: true
syncOptions:
- CreateNamespace=true # This will create namespace if not present


154 changes: 130 additions & 24 deletions docs/kubernetes/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ kubectl create job --from=cronjob/<cronjob-name> <job-name>

DemonSet ensures that all nodes run a copy of a pod. It is used to run a copy of a pod on all or a subset of nodes in a cluster. It can be used for monitoring, logging, etc.

NOTE: It will run on all worker nodes, except the master node.

```yaml
apiVersion: apps/v1
kind: DaemonSet
Expand All @@ -423,57 +425,161 @@ spec:
image: fluentd:v1.16-1
```

## StatefulSet

StatefulSet is a controller that manages the deployment and scaling of a set of pods. It is used to run stateful applications. It is used to run applications that require stable, unique network identifiers, stable storage, and ordered deployment and scaling.

<p align="center" >
`serviceName` is used to create a headless service. It is used to create a service that does not load balance the traffic. It is used to create a service that does not have a cluster IP. It is used of headless services that are used to discover the pods.

![Service](https://user-images.githubusercontent.com/51878265/204233963-c7bde7da-f631-49f3-b9db-1750ed55a37f.png)
```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-with-init-containers
spec:
serviceName: nginxs # Headless service
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
initContainers:
- name: populate-default-html
image: nginx:1.26.0
command:
- bash
- "-c"
- |
set -ex
[[ $HOSTNAME =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo "<h1>Hello from pod $ordinal</h1>" > /usr/share/nginx/html/index.html
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
containers:
- name: nginx
image: nginx:1.26.0
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
</p>
volumeClaimTemplates: # PersistentVolumeClaim templates for each replica
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "standard"
resources:
requests:
storage: 100Mi
```

- Port forwarding
## ConfigMap

We can forward a port from a pod to our local machine
ConfigMap enables environment specific configuration to be decoupled from the container image. It is used to store non-sensitive data in key-value pairs.

```bash
kubectl port-forward <pod-name> <localhost-port>:<pod-port>
Thee are two ways to primary style to create a ConfigMap:

- Property like Keys (MYAPP_COLOR=blue) - This is useful when we want to use the ConfigMap as environment variables.
- File like Keys (conf.yml = <multi line string>) - This is useful when we want to use the ConfigMap as a file.


File like style:

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: file-like-keys
data:
conf.yml: |
name: YourAppName
version: 1.0.0
author: YourName
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-example-file
spec:
containers:
- name: nginx
image: nginx:1.26.0
volumeMounts:
- name: configmap-file-like-keys
mountPath: /etc/config
volumes:
- name: configmap-file-like-keys
configMap:
name: file-like-keys
```

or
Property like style:

Note: In this case pod port is same as localhost port
```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: property-like-keys
data:
NAME: YourAppName
VERSION: 1.0.0
AUTHOR: YourName
```bash
kubectl port-forward <pod-name> <localhost-port>
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-example-key
spec:
containers:
- name: nginx
image: nginx:1.26.0
envFrom:
- configMapRef:
name: property-like-keys
```


### Ingress

It is use for an external traffic/request, which can be accessed by an URL instead of `IP-PORT - 17.28.55.44.5:7800`. For that we need an ingress controller to make work of ingress.
Ingress enables routing traffic to services based on the request host or path. It is an API object that manages external access to services in a cluster, typically HTTP. It provides HTTP and HTTPS routing to services in a cluster.

The way it works it that the traffic from the client comes to the Ingress Controller, then the Ingress Controller routes the traffic to the respective service and then the service routes the traffic to the respective pod. `Traffic -> Ingress Controller -> Service -> Pod`

![Ingress](https://user-images.githubusercontent.com/51878265/201585224-eca055af-eeb6-473c-bd96-33af9b5f6c55.png)
Some common ingress controllers are Nginx (below example), Traefik, etc. Some support annotations to configure the routing and some have their Ingress Class name.

NOTE: One things to note here is it only supports layer 7 routing that is HTTP/HTTPS.

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: kubernetes-ingress
namespace: kubernetes-dashboard
name: nginx-ingress-ingress
spec:
ingressClassName: nginx # Ingress controller name
rules:
- host: example.com
- host: mydomain.com # Domain name
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: kubernetes-dashboard
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: nginx-ingress-service # Service name
port:
number: 80 # Service port
```


![Image](https://user-images.githubusercontent.com/51878265/201604299-264768c3-e5b1-48fa-9bc1-3762a3052006.png)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: file-like-keys
data:
conf.yml: |
name: YourAppName
version: 1.0.0
author: YourName
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: property-like-keys
data:
NAME: YourAppName
VERSION: 1.0.0
AUTHOR: YourName
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: configmap-example-file
spec:
containers:
- name: nginx
image: nginx:1.26.0
volumeMounts:
- name: configmap-file-like-keys
mountPath: /etc/config
volumes:
- name: configmap-file-like-keys
configMap:
name: file-like-keys
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
name: configmap-example-key
spec:
containers:
- name: nginx
image: nginx:1.26.0
envFrom:
- configMapRef:
name: property-like-keys
16 changes: 16 additions & 0 deletions docs/kubernetes/k8s-resources-type/daemonset/daemonset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-daemonset
spec:
selector:
matchLabels:
app: fluentd-app
template:
metadata:
labels:
app: fluentd-app
spec:
containers:
- name: fluentd
image: fluentd:v1.16-1
17 changes: 17 additions & 0 deletions docs/kubernetes/k8s-resources-type/ingress/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx
image: nginx:1.26.0
17 changes: 17 additions & 0 deletions docs/kubernetes/k8s-resources-type/ingress/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress-ingress
spec:
ingressClassName: nginx
rules:
- host: fba5362f-28ca-4c8f-bf56-482fdeaeb331.lb.civo.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-ingress-service
port:
number: 80
12 changes: 12 additions & 0 deletions docs/kubernetes/k8s-resources-type/ingress/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-service
spec:
type: ClusterIP
selector:
app: nginx-app
ports:
- protocol: TCP
port: 80 # Port the service is listening on
targetPort: 80 # Port the container is listening on (if unset, defaults to equal port value)
7 changes: 7 additions & 0 deletions docs/kubernetes/k8s-resources-type/secret/secret-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: base64-data
type: Opaque # This is the default type
data:
foo: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: dockerconfigjson
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: |
eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MSI6eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCIsImVtYWlsIjoiZm9vQGJhci5jb20iLCJhdXRoIjoiZFhObGNtNWhiV1U2Y0dGemMzZHZjbVE9In19fQ==
7 changes: 7 additions & 0 deletions docs/kubernetes/k8s-resources-type/secret/secret-string.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: string-data
type: Opaque # This is the default type
stringData:
foo: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: nginxs # Plural because it sets up DNS for each replica in the StatefulSet (e.g. nginx-0.nginxs.default.svc.cluster.local)
spec:
type: ClusterIP
clusterIP: None # This makes it a "headless" service
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
13 changes: 13 additions & 0 deletions docs/kubernetes/k8s-resources-type/statefulset/service-nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: nginx # singular since it points to a single cluster IP
spec:
type: ClusterIP
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80

Loading

0 comments on commit 96456f0

Please sign in to comment.