Kubernetes 安裝筆記

-- Pageviews

Kubernetes 安裝筆記

用 CentOS 練習安裝 Kubernetes 的筆記。

Install Docker

1
2
3
4
5
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl enable docker
systemctl start docker

Install kubeadm & kubelet & kubectl

1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubeadm kubelet kubectl

Before Start Service

1
2
3
# 允許 containers 連到 host
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

關閉 swap

1
2
3
swapoff -a
# 如果 /etc/fstab 有掛載 swap,必須要註解掉,不然重開機時又會重新掛載 swap
sed -i 's/.*swap.*/#&/' /etc/fstab

Start Service

1
2
3
4
5
systemctl enable kubelet
systemctl start kubelet

# 初始化 Cluster
kubeadm init --pod-network-cidr=10.244.0.0/16

修改權限,讓 root 以外的權限也可以使用 kubernetes

1
2
3
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Check Service

1
2
kubectl version
kubectl get cs

Apply Master has Node

1
kubectl taint nodes --all node-role.kubernetes.io/master-

Install Pod network

1
2
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
systemctl restart kubelet

Create Pod

1
2
3
4
5
6
7
8
9
10
11
12
13
# vi lab1-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: lab1-pod
labels:
app: webserver
spec:
containers:
- name: lab1-container
image: httpd
ports:
- containerPort: 80
1
2
3
kubectl create -f lab1-pod.yml
kubectl get pods
# kubectl delete pods lab1-pod

Bind Service

1
2
3
kubectl expose pod lab1-pod --name=lab1-pod-service --type=NodePort --port=80
kubectl get services
# kubectl delete services lab1-pod-service

Bind Replication Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# vi lab2-replication-controller.yml
apiVersion: v1
kind: ReplicationController
metadata:
name: lab2-replication-controller
labels:
app: webserver
spec:
replicas: 3
selector:
app: lab2-pod
template:
metadata:
labels:
app: lab2-pod
spec:
containers:
- name: lab2-container
image: httpd
ports:
- containerPort: 80
1
2
3
4
5
6
kubectl create -f lab2-replication-controller.yml
kubectl get rc

kubectl scale --replicas=4 -f lab2-replication-controller.yml

# kubectl delete rc lab2-replication-controller

Ref