K8S配置和常用命令
kubectl默认没有配置自动完成,可以执行以下命令配置。
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion
package
should be installed first.
echo
"source <(kubectl completion bash)"
>> ~/.bashrc # add autocomplete permanently to your bash shell.
查看集群信息
[root
@host229
tools]# kubectl cluster-info
Kubernetes master is running at https:
//192.168.243.229:6443
dnsmasq is running at https:
//192.168.243.229:6443/api/v1/namespaces/kube-system/services/dnsmasq:dns-tcp/proxy
KubeDNS is running at https:
//192.168.243.229:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
查看k8s组件运行状态
[root
@host229
tools]# kubectl -s http:
//localhost:8080 get componentstatuses
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-
0
Healthy {
"health"
:
"true"
}
etcd-
2
Healthy {
"health"
:
"true"
}
etcd-
1
Healthy {
"health"
:
"true"
}
查看namespace
[root
@host216
~]# kubectl get ns
NAME STATUS AGE
aaaa Active 3d
bbbb Active 3d
...
可以为K8S配置context,切换到响应的context之后,不需要指定命名空间即可访问本context(namespace)中的服务。方便测试。
$ kubectl config set-context aa --namespace=aaaa \
--cluster=cluster.local \
--user=admin-cluster.local
$ kubectl config set-context bb --namespace=bbbb\
--cluster=cluster.local \
--user=admin-cluster.local
kubectl config view
kubectl config use-context aa
kubectl config current-context
如果不配置context,可以使用 -n <namespace> 查看指定命名空间的service、pod和deployment。也可以单独查看pod、service或者deployment。
也可以使用 --all-namespaces 查看所有命名空间的组件信息。
[root
@host216
~]# kubectl -n aaaa get pods,svc,deploy
NAME READY STATUS RESTARTS AGE
po/aaaa-xxxx-844cb4794f-gr8g7
1
/
1
Running
1
3d
...
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/aaaa-xxxx NodePort
10.233
.
1.172
<none>
9090
:
32700
/TCP,
4242
:
32701
/TCP 3d
...
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/aaaa
1
1
1
1
3d
...
exec可以在容器内部执行命令,常用来执行 /bin/bash 进入具体的容器。
[root
@host216
~]# kubectl exec -ti aaaa-xxxx-844cb4794f-gr8g7 /bin/bash
[root@aaaa-xxxx-844cb4794f-gr8g7 /]#
[root@aaaa-xxxx-844cb4794f-gr8g7 /]#
[root@aaaa-xxxx-844cb4794f-gr8g7 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root
1
0
0
8
月
13
?
00
:
00
:
00
/bin/sh -c /usr/local/bin/entrypoint;
while
true
;
do
echo
...
描述资源的详细信息,示例是查看pod信息,包括所在节点、关键的events等。
[root
@host216
~]# kubectl describe pod <podname>
Name: <podname>
Namespace: aaaa
Node: host215/
192.168
.
231.215
Start Time: Sat,
11
Aug
2018
18
:
19
:
26
+
0800
Labels: app=aaaa
...
以指定的输出格式查看资源的信息。
[root
@host216
~]# kubectl get deployment aaaa -o yaml/json
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision:
"1"
creationTimestamp:
2018
-
08
-11T10:
19
:26Z
generation:
1
labels:
app: aaaa
name: aaaa
namespace: aaaa
resourceVersion:
"189254"
...
查看pod运行在哪个节点上
[root
@host229
tools]# kubectl -n aaaa get pods aaaa-xxxx
-o wide
NAME READY STATUS RESTARTS AGE IP NODE
aaaa-xxxx
1
/
1
Running
2
3d
10.233
.
110.13
host229