k8s维护

kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true#干跑模式,不会创建pod
kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
进入容器
kubectl exec -it nginx-deploy-7689897d8d-ldgxj -- /bin/sh
暴露服务访问地址,只能通过节点访问
kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80
查看pod信息
kubectl get pods -n kube-system -o wide
查看服务信息
kubectl get svc -n kube-system
查看DNS信息
cat /etc/resolv.conf
创建客户端
[root@master ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never
地址解析
[root@master ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10
查看服务信息
kubectl get svc
查看服务详细信息
kubectl describe svc nginx
服务扩容
kubectl scale --replicas=5 deployment nginx-deploy
服务缩容
kubectl scale --replicas=3 deployment nginx-deploy
服务升级
kubectl set image deployment nginx-deploy nginx-deploy=nginx:1.15
服务回滚
kubectl rollout undo deployment nginx-deploy
将服务中的type改为NodePort,即可从外部访问
kubectl edit svc nginx-deploy
type:NodePort

通过外部浏览器访问:http://192.168.2.123:30719/
kube命令补全
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc

以yaml格式查看pod详细信息
kubectl get pod nginx-deploy-7689897d8d-ldgxj -o yml
查看pod访问日志信息
kubectl logs pod-demo nginx

通过yamal创建pod
vi pod-demo

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: nginx
    tier: frontend
  annotations:
    nginx/create-by: "trent hu"
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
  livenessProbe:#第一种探针
      httpGet:
        port: http
        path: /index.html    
      initialDelaySeconds: 1
      periodSeconds: 3      
  - name: busybox
    image: busybox:latest
    command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/helpthy; sleep 3600"]
    livenessProbe:#第二种探针
      exec:
        command: ["test","-e","/tmp/healthy"]
      initialDelaySeconds: 1
      periodSeconds: 3    
  nodeSelector:
    disktype: ssd

创建pod

kubectl create -f pod-demo.yml
删除pod
kubectl delete -f pod-demo.yml
查看pod标签
kubectl get pods --show-labels
kubectl get pods -l app --show-labels
给pod打新标签
kubectl label pods pod-demo release=canary
kubectl label pods pod-demo release=stable --overwrite #修改标签值
通过标签查找pod
kubectl get pods -l release
kubectl get pods -l "release in (alpha,stable)"
给node打标签
kubectl label nodes node1 disktype=ssd
查看pod详细信息
kubectl describe pod pod-demo

制作静像
cd /home/hello/
--------------------------------------------
vi hello.js
var http = require('http');

var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);
------------------------------------------------
vi Dockerfile
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
生成静像
docker build /home/hello/ --tag=hello-node


官网教程
查看kubernetes版本
kubectl version
查看集群信息
kubectl cluster-info
查看节点状态
kubectl get nodes
部署应用
kubectl run kubernetes-bootcamp --image=fernandox/kubernetes-bootcamp:v1 --port=8080
查看部署应用状态
kubectl get deployments
查看副本状态
kubectl get rs
查点部署应用POD状态
kubectl get pods
查看pod详细信息
kubectl describe pods kubernetes-bootcamp-8644c875c7-jds9m
启动代理服务
kubectl proxy
查点代理信息
curl http://localhost:8001/version
获取pod名称
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .item}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
访问部署应用
curl http://localhost:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-8644c875c7-jds9m/proxy/
查看pod访问日志信息
kubectl logs kubernetes-bootcamp-8644c875c7-jds9m
查看pod环境变量
kubectl exec kubernetes-bootcamp-8644c875c7-jds9m env
进入pod容器
kubectl exec -it kubernetes-bootcamp-8644c875c7-jds9m bash
查看应用代码
root@kubernetes-bootcamp-8644c875c7-jds9m:/# cat server.js
在pod容器内访问应用
root@kubernetes-bootcamp-8644c875c7-jds9m:/# curl localhost:8080
退出容器
root@kubernetes-bootcamp-8644c875c7-jds9m:/# exit
查看服务信息
kubectl get services
对外公开服务地址
kubectl expose deployment kubernetes-bootcamp --type="NodePort" --port 8080
通过外部或任意节点访问部署应用
http://192.168.2.123:31751/
查看服务详细信息
kubectl describe services kubernetes-bootcamp
查看部署详细信息
kubectl describe deployments kubernetes-bootcamp
通过标签查询资源、部署应用、服务
kubectl get pods[deployments/services] -l run=kubernetes-bootcamp
给资料增加标签
kubectl label pod kubernetes-bootcamp-8644c875c7-jds9m app=v1
查看资源标签
kubectl get pods kubernetes-bootcamp-8644c875c7-jds9m --show-labels
删除服务
kubectl delete service -l run=kubernetes-bootcamp#通过标签删除
kubectl delete service kubernetes-bootcamp#通过名称删除
修改应用部署数量
kubectl scale deployments kubernetes-bootcamp --replicas=4

查看资源扩展信息
kubectl get pods kubernetes-bootcamp -o wide
更新应用
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=mricheng/kubernetes-bootcamp:v2
应用版本回滚
kubectl rollout status deployments/kubernetes-bootcamp

posted @ 2019-09-16 22:30  BicycleBoy  阅读(449)  评论(0编辑  收藏  举报