如何在k8s的任意节点使用用kubectl
# 正常在node节点上是无法执行kubectl命令
[root@k8s-node-01 ~]# kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
1、将master节点中/etc/kubernetes/,admin.conf拷贝到需要运行的node服务器的/etc/kubernetes目录中。
- scp /etc/kubernetes/admin.conf root@k8s-node-01:/etc/kubernetes/admin.conf
- scp /etc/kubernetes/admin.conf root@k8s-node-02:/etc/kubernetes/admin.conf
2、在对应的服务器上配置环境变量
- echo " export KUBECONFIG=/etc/kubernetes/admin.conf " >> ~/.bash_profile
- source ~/.bash_profile
# 执行上面的master第一步后,在node节点执行以下,就可以正常是用kubectl了
[root@k8s-node-01 ~]# echo " export KUBECONFIG=/etc/kubernetes/admin.conf " >> ~/.bash_profile
[root@k8s-node-01 ~]#
[root@k8s-node-01 ~]#
[root@k8s-node-01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6768c68f7b-rsxb4 1/1 Running 1 (47m ago) 105m
nginx-test 1/1 Running 1 (47m ago) 70m
查看pod信息
[root@k8s-node-02 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6768c68f7b-rsxb4 1/1 Running 1 (62m ago) 120m
nginx-test 1/1 Running 1 (62m ago) 85m
下面这种执行方式是错误的,操作的是pod控制器,而非pod本身
[root@k8s-node-02 ~]# kubectl scale --replicas 3 nginx-6768c68f7b-rsxb4
error: the server doesn't have a resource type "nginx-6768c68f7b-rsxb4"
获取pod控制器信息
# 在创建nginx的时候指定了pod控制器版本是:deployment
#[root@k8s-master ~]# kubectl create deployment nginx --image=nginx:1.20
# deployment.apps/nginx created# 在此处获取nginx这个pode的deploy控制器的版本时候就没有另外一个ningx-test这个信息
[root@k8s-node-02 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 120m
此处设置了deploy控制器是nginx的pod扩容为3个pod
[root@k8s-node-02 ~]# kubectl scale deployment --replicas 3 nginx
deployment.apps/nginx scaled# 此处可以查看到nginx这个deploy的数量是3
[root@k8s-node-02 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 10s# 同时可以到pod的信息,默认访问namespace是default
[root@k8s-node-02 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6768c68f7b-5m2zp 1/1 Running 0 10s 10.2.1.12 k8s-node-02 <none> <none>
nginx-6768c68f7b-krdfm 1/1 Running 0 10s 10.2.2.8 k8s-node-01 <none> <none>
nginx-6768c68f7b-rsxb4 1/1 Running 1 (64m ago) 123m 10.2.1.10 k8s-node-02 <none> <none>
nginx-test 1/1 Running 1 (64m ago) 87m 10.2.1.11 k8s-node-02 <none> <none>
获取到 deploy 的yaml信息,可以看到Replicas是3
[root@k8s-node-02 ~]# kubectl get deploy -o yaml
apiVersion: v1
items:
- apiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2024-02-22T09:30:58Z"generation: 2labels:app: nginxname: nginxnamespace: defaultresourceVersion: "187458"uid: 1515f10f-5072-4c8b-a9a5-1d04378a4b62spec:progressDeadlineSeconds: 600replicas: 3revisionHistoryLimit: 10selector:matchLabels:app: nginxstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:app: nginxspec:containers:- image: nginx:1.20imagePullPolicy: IfNotPresentname: nginxresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30status:availableReplicas: 3conditions:- lastTransitionTime: "2024-02-22T09:30:58Z"lastUpdateTime: "2024-02-22T09:31:30Z"message: ReplicaSet "nginx-6768c68f7b" has successfully progressed.reason: NewReplicaSetAvailablestatus: "True"type: Progressing- lastTransitionTime: "2024-02-22T11:34:04Z"lastUpdateTime: "2024-02-22T11:34:04Z"message: Deployment has minimum availability.reason: MinimumReplicasAvailablestatus: "True"type: AvailableobservedGeneration: 2readyReplicas: 3replicas: 3updatedReplicas: 3
kind: List
metadata:resourceVersion: ""
修改这个deploy控制器是nginx的可以减少为1个
[root@k8s-node-02 ~]# kubectl scale deployment --replicas 1 nginx
deployment.apps/nginx scaled[root@k8s-node-02 ~]# kubectl get deploy -o yaml
apiVersion: v1
items:
- apiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2024-02-22T09:30:58Z"generation: 3labels:app: nginxname: nginxnamespace: defaultresourceVersion: "188540"uid: 1515f10f-5072-4c8b-a9a5-1d04378a4b62spec:progressDeadlineSeconds: 600replicas: 1revisionHistoryLimit: 10selector:matchLabels:app: nginxstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:app: nginxspec:containers:- image: nginx:1.20imagePullPolicy: IfNotPresentname: nginxresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30status:availableReplicas: 1conditions:- lastTransitionTime: "2024-02-22T09:30:58Z"lastUpdateTime: "2024-02-22T09:31:30Z"message: ReplicaSet "nginx-6768c68f7b" has successfully progressed.reason: NewReplicaSetAvailablestatus: "True"type: Progressing- lastTransitionTime: "2024-02-22T11:34:04Z"lastUpdateTime: "2024-02-22T11:34:04Z"message: Deployment has minimum availability.reason: MinimumReplicasAvailablestatus: "True"type: AvailableobservedGeneration: 3readyReplicas: 1replicas: 1updatedReplicas: 1
kind: List
metadata:resourceVersion: ""