You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.6 KiB

  1. # Introduction to kubectl
  2. ## Prerequisites
  3. For using this you must install the following tools:
  4. - Python
  5. - Pip
  6. - Kubectl
  7. - Openstack-Client
  8. ### Python
  9. Install python and pip for your OS, if it is not installed. This is already done at the NetLab PCs.
  10. ### Installing kubectl and arkade
  11. Run this in a **bash** terminal to download arkade and install kubectl:
  12. For windows you can use e.g. the **Git Bash** terminal.
  13. ```bash
  14. curl -sLS https://dl.get-arkade.dev | sh
  15. arkade get kubectl
  16. # To load the path to the binary
  17. export PATH=$PATH:$HOME/.arkade/bin/
  18. ```
  19. With arkade you can also install other tools: ```arkade get``` to see all possibilities.
  20. ### Openstack Client
  21. To install the Openstack client and the magnum package:
  22. If you are using Windows, you must start the bash terminal with **Administrator** privileges!
  23. ```bash
  24. pip3 install openstackclient python-magnumclient
  25. # Download the Openstack RC File from your Openstack account
  26. # Load the file
  27. source {USERNAME}-openrc.sh
  28. ```
  29. ### Download the kubeconfig
  30. Download the kubeconfig file for your cluster from OpenStack.
  31. ```bash
  32. # Before using this, check if Openstack RC File is loaded!
  33. openstack coe cluster config {CLUSTER_NAME}
  34. # Then run the given command in terminal
  35. export ...
  36. # Now you are able to run kubectl commands at your Kubernetes cluster
  37. ```
  38. ## Deployment of the needed description in kubernetes
  39. These are two approaches to create the description in Kubernetes:
  40. ```bash
  41. # create a namespace
  42. kubectl create namespace web-test
  43. # Then choose one of the following ways:
  44. # (1) Create the description
  45. kubectl create -f nginx.yml -f service.yml
  46. # (2) Create or update descriptions if existing
  47. kubectl apply -f nginx.yml -f service.yml
  48. ```
  49. ## To show it is working
  50. Here are some useful commands:
  51. ```bash
  52. # show nodes
  53. kubectl get nodes
  54. # show pods for our namespace
  55. kubectl get -n web-test pods
  56. # show deployments for our namespace
  57. kubectl get -n web-test deployment
  58. # show services for our namespace
  59. kubectl get -n web-test service
  60. # show logs of deployed nginx instances
  61. kubectl logs -n web-test deployment/nginx-deployment
  62. ```
  63. ## Scaling up and down
  64. ```bash
  65. # scale up instances to 5
  66. kubectl scale -n web-test deployment/nginx-deployment --replicas=5
  67. # scale down instances again to 3
  68. kubectl scale -n web-test deployment/nginx-deployment --replicas=3
  69. ```
  70. ## To see it is working in the browser ;)
  71. The nginx instances are reachable at the floating ips of the **node**-instances at port **30007** in our case.
  72. ## Many other things can be done ;)
  73. - Autoscaling
  74. - Detailled Service functions (LoadBalancer, ...)
  75. - Dashboard
  76. - FaaS Setup ;)
  77. - ...
  78. ## Appendix
  79. ### Nginx description
  80. *nginx.yml:*
  81. ```yml
  82. apiVersion: apps/v1
  83. kind: Deployment
  84. metadata:
  85. name: nginx-deployment
  86. namespace: web-test
  87. spec:
  88. selector:
  89. matchLabels:
  90. app: nginx-deployment
  91. replicas: 3 # tells deployment to run 3 pods matching the template
  92. template:
  93. metadata:
  94. labels:
  95. app: nginx-deployment
  96. spec:
  97. containers:
  98. - name: nginx
  99. image: nginx:latest
  100. ports:
  101. - containerPort: 80
  102. ```
  103. ### Nginx service description
  104. *service.yml:*
  105. ```yml
  106. apiVersion: v1
  107. kind: Service
  108. metadata:
  109. name: nginx-service
  110. namespace: web-test
  111. spec:
  112. type: NodePort
  113. selector:
  114. app: nginx-deployment
  115. ports:
  116. # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
  117. - port: 80
  118. targetPort: 80
  119. # Optional field
  120. # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
  121. nodePort: 30007
  122. ```