You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -731,13 +731,12 @@ Data in host dir volume will survive to any crash and restart of both container
731
731
This works only when kubernetes schedules the nginx pod on the same worker node as before.
732
732
733
733
## Config Maps
734
-
Kubernetes allows separating configuration options into a separate object called **ConfigMap**, which is a map containing key/value pairs with the values ranging from short literals to full config files.
734
+
Kubernetes allows separating configuration options into a separate object called **ConfigMap**, which is a map containing key/value pairs with the values ranging from short literals to full config files. An application doesn’t need to read the ConfigMap directly or even know that it exists. The contents of the map are instead passed to containers as either environment variables or as files in a volume.
735
735
736
-
An application doesn’t need to read the ConfigMap directly or even know that it exists. The contents of the map are instead passed to containers as either environment variables or as files in a volume.
736
+
### Mount Config Map as volume
737
+
The content of config Map is mounted as a volume into the pod. For example, let's to create a Config Map from the ``nginx.conf`` configuration file
737
738
738
-
For example, let's to create a ConfigMap from the ``nginx.conf`` configuration file
Having the configuration stored in a standalone object, allows to keep multiple ConfigMaps with the same name, each for a different environment (development, test, pre-stage, production, and so on). Because pods reference the ConfigMap by name, we can use a different config in each environment while using the same pod specification across all of them.
776
-
777
-
As alternative, a ConfigMap can be created as in the ``nginx-conf.yaml`` manifest file
774
+
As alternative, a Config Map can be created as in the ``nginx-conf.yaml`` manifest file
778
775
779
776
```yaml
780
777
apiVersion: v1
778
+
kind: ConfigMap
779
+
metadata:
780
+
name: nginx
781
+
namespace:
781
782
data:
782
783
nginx.conf: |
783
784
server {
784
785
listen 80;
785
786
server_name www.noverit.com;
786
-
charset koi8-r;
787
-
access_log /var/log/nginx/host.access.log main;
788
787
location / {
789
788
root /usr/share/nginx/html;
790
789
index index.html index.htm;
791
790
}
792
-
error_page 404 /404.html;
793
-
error_page 500 502 503 504 /50x.html;
794
-
location = /50x.html {
791
+
}
792
+
```
793
+
794
+
Using a Config Map and exposing it through a volume brings the ability to update the configuration without having to recreate the pod or even restart the container. When updating a Config Map, the files in all the volumes referencing it are updated. It’s then up to the process to detect that they’ve been changed and reload them.
795
+
796
+
For example, to update the configuration above, edit the Config Map
797
+
798
+
kubectl edit cm nginx
799
+
800
+
Then check the nginx application running inside the pod reloaded the configuration
In the previous example, we mounted the volume as a directory, which means any file that is stored in the ``/etc/nginx/conf.d`` directory in the container image is hidden. This is generally what happens in Linux when mounting a filesystem into a directory and the directory then only contains the files from the mounted filesystem, whereas the original files are inaccessible.
805
+
806
+
To avoid this pitfall, in kubernetes, it is possible to mount only individual files from a Config Map into an existing directory without hiding existing files stored on that directory. For example, as reference to the previous example, the ``/etc/nginx/conf.d`` directory of the container file system, already contains a default configuration file called ``default.conf``. We do not want to hide this file but we only want to add an additional configuration file called ``custom.conf`` for our customized nginx container.
807
+
808
+
Create a Config Map as in the following ``nginx-custom.conf`` file descriptor
809
+
810
+
```yaml
811
+
apiVersion: v1
812
+
kind: ConfigMap
813
+
metadata:
814
+
name: nginx
815
+
namespace:
816
+
data:
817
+
custom.conf: |
818
+
server {
819
+
listen 8080;
820
+
server_name www.noverit.com;
821
+
location / {
795
822
root /usr/share/nginx/html;
823
+
index index.html index.htm;
796
824
}
797
825
}
826
+
```
827
+
828
+
and then create the nginx pod from the following ``nginx-pod-cm-custom.yaml`` file descriptor
829
+
830
+
```
831
+
apiVersion: v1
832
+
kind: Pod
833
+
metadata:
834
+
name: nginx
835
+
namespace:
836
+
labels:
837
+
spec:
838
+
containers:
839
+
- name: nginx
840
+
image: nginx:latest
841
+
ports:
842
+
- containerPort: 8080
843
+
volumeMounts:
844
+
- name: config
845
+
mountPath: /etc/nginx/conf.d/custom.conf
846
+
subPath: custom.conf
847
+
readOnly: true
848
+
volumes:
849
+
- name: config
850
+
configMap:
851
+
name: nginx
852
+
```
853
+
854
+
The resulting pod will mount the both the default and custom configuration files
In addition to mounting a volume, configuration values contained into a Config Map can be passed to a container directly into environment variables. For example, the following ``mysql-cm.yaml`` file defines a Config Map containing configuration paramenters for a MySQL application
863
+
864
+
```yaml
865
+
apiVersion: v1
798
866
kind: ConfigMap
799
867
metadata:
800
-
name: nginxconfig
868
+
name: mysql
869
+
namespace:
870
+
data:
871
+
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
872
+
MYSQL_DATABASE: "employees"
873
+
MYSQL_USER: "admin"
874
+
MYSQL_PASSWORD: "password"
875
+
```
876
+
877
+
The following ``mysql-cm.yaml`` file defines the MySQL pod using the values from the map above
878
+
879
+
```yaml
880
+
apiVersion: v1
881
+
kind: Pod
882
+
metadata:
883
+
name: mysql
801
884
namespace:
885
+
labels:
886
+
run: mysql
887
+
spec:
888
+
containers:
889
+
- name: mysql
890
+
image: mysql:5.6
891
+
env:
892
+
- name: MYSQL_RANDOM_ROOT_PASSWORD
893
+
# The generated root password will be printed to stdout
894
+
# kubectl logs mysql | grep GENERATED
895
+
valueFrom:
896
+
configMapKeyRef:
897
+
name: mysql
898
+
key: random_root_password
899
+
- name: MYSQL_DATABASE
900
+
valueFrom:
901
+
configMapKeyRef:
902
+
name: mysql
903
+
key: database
904
+
- name: MYSQL_USER
905
+
valueFrom:
906
+
configMapKeyRef:
907
+
name: mysql
908
+
key: user
909
+
- name: MYSQL_PASSWORD
910
+
valueFrom:
911
+
configMapKeyRef:
912
+
name: mysql
913
+
key: password
914
+
ports:
915
+
- name: mysql
916
+
protocol: TCP
917
+
containerPort: 3306
802
918
```
803
919
804
-
Using a ConfigMap and exposing it through a volume brings the ability to update the configuration without having to recreate the pod or even restart the container.
920
+
Create the config map
805
921
806
-
When updating a ConfigMap, the files in all the volumes referencing it are updated. It’s then up to the process to detect that they’ve been changed and reload them.
922
+
kubectl apply -f mysql-cm.yaml
807
923
808
-
For example, to update the configuration above, edit the ConfigMap
924
+
and create the MySQL pod
809
925
810
-
kubectl edit cm nginxconfig
926
+
kubectl apply -f mysql-pod-cm.yaml
811
927
812
-
Then check the nginx application running inside the pod reloaded the configuration
928
+
To check the configurations are correctly loaded from the map, try to connect the MySQL with the defined user and password.
When the Config Map contains more than just a few entries, it becomes tedious to create environment variables from each entry individually. It is also possible to expose all entries of a Config Map as environment variables without specifying them in the pod descriptor. For example the following ``mysql-pod-cmx.yaml`` is a more compact form of the descriptor above
815
931
932
+
```yaml
933
+
apiVersion: v1
934
+
kind: Pod
935
+
metadata:
936
+
name: mysql
937
+
namespace:
938
+
labels:
939
+
run: mysql
940
+
spec:
941
+
containers:
942
+
- name: mysql
943
+
image: mysql:5.6
944
+
envFrom:
945
+
- configMapRef:
946
+
name: mysql
947
+
ports:
948
+
- name: mysql
949
+
protocol: TCP
950
+
containerPort: 3306
951
+
```
816
952
817
953
## Daemons
818
954
A Daemon Set is a controller type ensuring each node in the cluster runs a pod. As new node is added to the cluster, a new pod is added to the node. As the node is removed from the cluster, the pod running on it is removed and not scheduled on another node. Deleting a Daemon Set will clean up all the pods it created.
0 commit comments