6. Quelques tests
Formation suivie via le blog de Stéphane Robert : parcours Kubernetes.
Test 1 : multi-container dans 1 pod
Section titled “Test 1 : multi-container dans 1 pod”Je crée un pod contenant 2 containers et je tente de voir s’ils communiquent entre eux.
Manifest
Section titled “Manifest”apiVersion: v1kind: Podmetadata: name: nginxspec: containers: - name: nginx image: nginx ports: - containerPort: 80 - name: alpine image: alpine:latest command: ["/bin/sh", "-c", "echo 'Hello from Alpine!' && sleep 3600"]kubectl apply -f manifest.ymlkubectl get podskubectl logs nginx -c alpineOn ouvre un shell dans le conteneur alpine, puis on appelle nginx via localhost :
$ kubectl exec -it nginx -c alpine -- /bin/sh/ # wget -qO- http://localhost:80<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, nginx is successfully installed and working.Further configuration is required for the web server, reverse proxy,API gateway, load balancer, content cache, or other features.</p>
<p>For online documentation and support please refer to<a href="https://nginx.org/">nginx.org</a>.<br/>To engage with the community please visit<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>For enterprise grade support, professional services, additionalsecurity features and capabilities please refer to<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>Test 2 : namespace
Section titled “Test 2 : namespace”# Pour changer le namespace courantkubectl config set-context --current --namespace=<mon_namespace>
# Pour vérifier le namespace courantkubectl config view --minify | grep namespacePour changer rapidement de contexte, possibilité d’utiliser kubectx et kubens (à installer) :
| Outil | Fonction | Commande classique équivalente |
|---|---|---|
kubectx |
Change de contexte | kubectl config use-context ... |
kubens |
Change de namespace | kubectl config set-context --current --namespace=... |
Test 3 : ReplicaSet
Section titled “Test 3 : ReplicaSet”Manifest
Section titled “Manifest”apiVersion: apps/v1kind: ReplicaSetmetadata: name: nginx-replicaset namespace: devspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80# Modification dynamique du nombre de réplicaskubectl scale rs nginx-replicaset --replicas=5
# Modification dynamique de l'imagekubectl set image rs nginx-replicaset nginx=nginx:1.23
# Check les pods existantskubectl describe pod <pod_name> | grep Image