I used to create deployments quickly using imperative commands
kubectl run nginx --image=nginx --restart=Always --port=80 --replicas=3 .
Now run command with deployment seems to have been deprecated. Is there any other way to do the same with kubectl create... with replicas and ports?
Since the Kubernetes v1.18 the kubectl run will no longer create deployments but pods.
What might be used instead is the imperative option of kubectl create deployment.
So the following command:
k create deploy nginx --image nginx
will do the trick for you.
It will create Deployment object in imperative way. (No need for intermediate yaml files)
# Run:
kubectl create deploy nginx --image nginx && kubectl scale deploy nginx --replicas 3
# Check:
kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 14s
Note there is no --replicas flag of kubectl create deployemnt so the scaling is controlled separately.
Try this-
kubectl create deploy nginx --image=nginx --dry-run -o yaml > webapp.yaml
change the replicas to 5 in the yaml and create it
kubectl create -f webapp.yaml
Okay, the generators were deprecated because of the pain it was to maintain that code. For easy deployment generator via CLI the best recommendation its helm3, it now doesn't need tillier and its very straightforward to use:
https://helm.sh/docs/intro/install/
Then, after installing running an Nginx deployment via CLI:
Add the repo
helm repo add bitnami https://charts.bitnami.com/bitnami
Also, you can first check what is going to get installed by adding --dry-run
helm install Nginx bitnami/nginx --dry-run
Then run without --dry-run if you are satisfied with what is going to get deployed.
I am using kubernetes : v1.22.5
Usage of imeperative command:
kubectl create deploy mydep --image=nginx --replicas=3 --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mydep
name: mydep
spec:
replicas: 3
selector:
matchLabels:
app: mydep
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mydep
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
The imperative way to do this, including creating the replicas on the commandline without first saving the yaml and then editing the yaml, would be by running the following command:
kubectl create deploy nginx --image nginx --replicas 3 --port 80
you can add the --restart=Always switch if you need it to the above command.
And, if you still want to save the yaml, for any reason, like pushing it to git, you should be able to redirect the above command as usual. No change in the way shell redirection works.
kubectl create deploy nginx --image nginx --replicas 3 --port 80 --output yaml --dry-run=client > file.yaml
Create nginx-deployment.yaml file with below content.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
and run kubectl create -f nginx-deployment.yaml
Related
I'm running Kubernetes with docker desktop on windows. DD is up-to-date, and the kubectl version command returns 1.22 as both the client and server version.
I executed kubectl run my-apache --image httpd, then kubectl get all, which only shows
There is no deployment or replicaset as I expect. This means some commands, such as kubectl scale doesn't work. Any idea what's wrong? Thanks.
The kubectl run command create pod not deployment, I mean it used to create in the past before kubernetes version 1.18 I guess.
For deployment you have run this command
kubectl create deployment my-apache --image httpd
You basically spin up a pod in the default namespace, if you want to deploy your app using deployment you should have a deployment.yml file and use :
kubectl apply -f <depoyment_file>
Example to spin-up 3 Nginx pods:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Side note: There are 2 approaches when creating basic k8s objects:
IMPERATIVE way:
kubectl create deployment <DEPLOYMENT_NAME> --image=<IMAGE_NAME:TAG>
DECLARATIVE way:
kubectl create -f deployment.yaml
I'm working through Kubernetes in Action (copyright 2018), and at least one of the examples is out-of-date with respect to current versions of kubectl.
Currently I'm stuck in section 2.3 on just trying to demo a simple web-server docker container ("kubia"):
kubectl run kubia --image=Dave/kubia --port=8080 --generator=run/v1
the --generator option has been removed from current versions of kubectl. What command(s) achieve the same end in the current version of kubectl?
Note: I'm literally just 2 chapters into learning about Kubernetes, so I don't really know what a deployment or anything else (so the official kubernetes docuementation doesn't help), I just need the simplest way to verify that that I can, in fact, run this container in my minikube "cluster".
in short , you can use following commands to create pods and deployments (imperative way) using following commands which are similar to the commands mentioned in that book :
To create a pod named kubia with image Dave/kubia
kubectl run kubia --image=Dave/kubia --port=8080
To create a deployment named kubia with image Dave/kubia
kubectl create deployment kubia --image=Dave/kubia --port=8080
You can just instantiated the pod, since --generator has been deprecated.
apiVersion: v1
kind: Pod
metadata:
name: kubia
spec:
containers:
- name: kubia
image: Dave/kubia
ports:
- containerPort: 8080
Alternatively, you can use a deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubia-deployment
labels:
app: kubia
spec:
replicas: 1
selector:
matchLabels:
app: kubia
template:
metadata:
labels:
app: kubia
spec:
containers:
- name: kubia
image: Dave/kubia
ports:
- containerPort: 8080
Save either one to a something.yaml file and run
kubectl create -f something.yaml
And to clean up
kubectl delete -f something.yaml
✌️
If someone who read same book (Kubernetes in Action, copyright 2018) have same issue in the future, just run pod instead of the replication controller and expose pod instead of rc in following chapter.
I've just started learning Kubernetes and I have created the deployment using the command kubectl run demo1-k8s --image=demo1-k8s:1.0 --port 8080 --image-pull-policy=Never. I got the message that deployment get created. But when I listed the deployment (kubectl get deployments), deployments not listed instead and I got the message No resources found in default namespace.
Any idea guys?
From the docs kubectl run creates a pod and not deployment. So you can use kubectl get pods command to check if the pod is created or not. For creating deployment use kubectl create deployment as documented here
for deployment creation you need to use kubectl create deployment. with kubectl run a pod will be created.
kubectl create deployment demo1-k8s --image=demo1-k8s:1.0
the template is like kubectl create deployment <deployment-name> --<flags>
but it's always better if you use yaml to create deployment or other k8s resource. just create a .yaml file.
deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo1-k8s
spec:
selector:
matchLabels:
app: demo1-k8s
replicas: 4 # Update the replicas from 2 to 4
template:
metadata:
labels:
app: demo1-k8s
spec:
containers:
- name: demo1-k8s
image: demo1-k8s:1.0
imagePullPolicy: Never
ports:
- containerPort: 8080
run this command kubectl apply -f deploy.yaml
We can create a deployment with:
kubectl create deployment nginx-deployment --image=nginx
How can we pass an environment variable, say, key=value, for container while creating a deployment using kubectl?
Additionally, can we also use configmap or secret values as environment variables?
kubectl run nginx-pod --generator=run-pod/v1 --image=nginx --env="key1=value1" --env="key2=value2"...
Reference - run.
kubectl create deployment command does not have option to pass environment variable as a flag on the imperative command .. possible available flags on create deployment command are as below (listed by autocomplete on on kubectl cli)
$ kubectl create deployment nginx --image=nginx --
--add-dir-header --client-certificate= --insecure-skip-tls-verify --log-flush-frequency= --profile-output= --token
--allow-missing-template-keys --client-key --kubeconfig --logtostderr --request-timeout --token=
--alsologtostderr --client-key= --kubeconfig= --match-server-version --request-timeout= --user
--as --cluster --log-backtrace-at --namespace --save-config --user=
--as= --cluster= --log-backtrace-at= --namespace= --server --username
--as-group --context --log-dir --output --server= --username=
--as-group= --context= --log-dir= --output= --skip-headers --v
--cache-dir --dry-run --log-file --password --skip-log-headers --v=
--cache-dir= --generator --log-file= --password= --stderrthreshold --validate
--certificate-authority --generator= --log-file-max-size --profile --stderrthreshold= --vmodule
--certificate-authority= --image --log-file-max-size= --profile= --template --vmodule=
--client-certificate --image= --log-flush-frequency --profile-output --template=
Alternately kubectl run command can be used to create a deployment which will allow you to pass env flag on the imperative command , refer below example
$ kubectl run nginx --image=nginx --env="TEST"="/var/tmp"
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
Now to check the env variable has been correctly set you can connect to the POD and display the env variables to verify it
Connect
$ kubectl exec -it nginx /bin/bash
List env variables on the pod
root#nginx:/# env | grep -i test
TEST=/var/tmp
Refer official doc example for second part of your question link
When creating a pod you can specify the environment variables using the --env option eg.
kubectl run nginx-pod --restart Never --image=nginx --env=key1=value1,key2=value2
Checkout kubectl run documentation
However, you cannot do this with kubectl create deployment. I'll recommend you use a declarative manifest instead.
There is a possibility as other community members pointed to pass a variable to a pod but I would advise you to use a declarative approach to creating objects in Kubernetes. Why would I?
There is a nice comic explaining differences between imperative and declarative approach.
Below are examples to:
Create a basic NGINX deployment
Create a Configmap and Secret in a declarative approach
Apply above Configmap and Secret to already created NGINX deployment
Create a basic NGINX deployment
Create a YAML definition of NGINX similar to this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Apply it by running $ kubectl apply -f FILE_NAME.yaml
Create a basic ConfigMap
Create a YAML definition of ConfigMap similar to this:
apiVersion: v1
kind: ConfigMap
metadata:
name: config-for-nginx
data:
port: "12345"
Apply it by running $ kubectl apply -f FILE_NAME.yaml
Create a basic Secret
Create a YAML definition of Secret similar to this:
apiVersion: v1
kind: Secret
metadata:
name: password-for-nginx
type: Opaque
data:
password: c3VwZXJoYXJkcGFzc3dvcmQK
Take a specific look at:
password: c3VwZXJoYXJkcGFzc3dvcmQK
This password is base64 encoded.
To create this password invoke command from your terminal:
$ echo "YOUR_PASSWORD" | base64
Paste the output to the YAML definition and apply it with:
$ kubectl apply -f FILE_NAME.
Apply above Configmap and Secret to already created NGINX deployment
You can edit your previously created NGINX deployment and add the part responsible for adding the ConfigMap and Secret to be available as environmental variables:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
env:
- name: NGINX_PASSWORD
valueFrom:
secretKeyRef:
name: password-for-nginx
key: password
- name: NGINX_PORT
valueFrom:
configMapKeyRef:
name: config-for-nginx
key: port
ports:
- containerPort: 80
Please take a specific look on below part which will add the environmental variables to created pods as NGINX_PASSWORD and NGINX_PORT:
env:
- name: NGINX_PASSWORD
valueFrom:
secretKeyRef:
name: password-for-nginx
key: password
- name: NGINX_PORT
valueFrom:
configMapKeyRef:
name: config-for-nginx
key: port
The secretKeyRef is reference to created Secret and the configMapKeyRef is the reference for created ConfigMap.
Apply it by running $ kubectl apply -f FILE_NAME.yaml once more.
It will terminate old pods and create new one with new configuration.
To check if environmental variables are configured correctly invoke below commands:
$ kubectl get pods
$ kubectl exec -it NAME_OF_THE_POD -- /bin/bash
$ echo $NGINX_PORT
$ echo $NGINX_PASSWORD
You should see the variables from ConfigMap and Secret accordingly.
Trying to figure out how do I create multicontainer pod from terminal with kubectl without yaml config of any resource
tried kubectl run --image=redis --image=nginx but second --image just overrides the first one .. :)
You can't do this in a single kubectl command, but you could do it in two: using a kubectl run command followed by a kubectl patch command:
kubectl run mypod --image redis && kubectl patch deploy mypod --patch '{"spec": {"template": {"spec": {"containers": [{"name": "patch-demo", "image": "nginx"}]}}}}'
kubectl run is for running 1 or more instances of a container image on your cluster
see https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
Go with yaml config file
follow the below steps
create patch-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: patch-demo
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: nginx
image: nginx
----
deploy patch-demo.yaml
create patch-containers.yaml as below
---
spec:
template:
spec:
containers:
- name: redis
image: redis
---
patch the above yaml to include redis container
kubectl patch deployment patch-demo --patch "$(cat patch-containers.yaml)"