核心概念
安装
配置apt源:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF
安装docker和kubernate包:
sudo apt-get update sudo apt-get install -y docker-ce=18.06.1~ce~3-0~ubuntu kubelet=1.13.5-00 kubeadm=1.13.5-00 kubectl=1.13.5-00 #sudo apt-mark hold docker-ce kubelet kubeadm kubectl
请确保kubelet, kubeadm, kubectl这三个组件的版本一致!
允许iptables bridge call:
echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee -a /etc/sysctl.conf sudo sysctl -p
master节点
初始化kubernates集群:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
初始化本地配置:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装 Flannel 网络插件:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
node节点
把本机加入集群:
sudo kubeadm join $controller_private_ip:6443 --token $token --discovery-token-ca-cert-hash $hash
controller_private_ip, token, hash 是值在初始化时候获得
测试
验证所有节点已经加入集群,在master节点执行:
kubectl get nodes NAME STATUS ROLES AGE VERSION wboyd1c.mylabserver.com Ready master 54m v1.13.4 wboyd2c.mylabserver.com Ready <none> 49m v1.13.4 wboyd3c.mylabserver.com Ready <none> 49m v1.13.4
kubernate apis
获取K8s所有的组件名称:
kubectl api-resources -o name
获取 kube-system 这个 namespace 下的所有pod:
kubectl get pods -n kube-system
查询特定 node_name 的节点信息
kubectl get nodes $node_name
把节点信息导出成yaml格式:
kubectl get nodes $node_name -o yaml
展示某个节点的详细信息:
kubectl describe node $node_name
Pod
表示一组容器
创建
my-pod.yml:
apiVersion: v1 kind: Pod metadata: name: my-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
从定义文件创建
kubectl create -f my-pod.yml
修改
编辑完定义文件,后执行:
kubectl apply -f my-pod.yml
也可以执行:
kubectl edit pod my-pod
注意:不是所有的配置修改都可以通过edit命令完成
删除
kubectl delete pod my-pod
namespace
通过namespace可以隔离不同的pod
查询所有的namespace:
kubectl get namespaces
增加一个namespace:
kubectl create ns my-ns
为pod指定namesapce,在定义文件中设置 metadata.namespace 属性:
apiVersion: v1 kind: Pod metadata: name: my-ns-pod namespace: my-ns labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
查询某个namesapce下的pod:
kubectl get pod -n my-ns
查询某个pod的详细信息也必须指定namespace:
kubectl describe pod my-ns-pod -n my-ns
基本容器配置
添加启动执行命令 command :
apiVersion: v1 kind: Pod metadata: name: my-command-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['echo'] restartPolicy: Never
添加命令的参数 args :
apiVersion: v1 kind: Pod metadata: name: my-args-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['echo'] args: ['This is my custom argument'] restartPolicy: Never
容器暴露的端口 containerPort :
apiVersion: v1 kind: Pod metadata: name: my-containerport-pod labels: app: myapp spec: containers: - name: myapp-container image: nginx ports: - containerPort: 80
多容器的Pod
一个pod可以创建多个容器:
apiVersion: v1 kind: Pod metadata: name: multi-container-pod spec: containers: - name: nginx image: nginx:1.15.8 ports: - containerPort: 80 - name: busybox-sidecar image: busybox command: ['sh', '-c', 'while true; do sleep 30; done;']